Poka
Poka

Reputation: 387

Extract first 2 digits of a number using python

Hi I have an array of numbers which is in UTC format.The format is HH.MM.SS.MS i.e hours minutes seconds and milliseconds. I want to convert the entire number to milliseconds . So I am extracting first 2 digits like below

hh=int(str(x[1])[:2]) # returns 42 where  hours and minutes mixed

# My data also start with non zero for example x=142826.00
  # doing the same operation like above returns 14( It is perfect). So intention is when 04 extract only 4 and if it is 14 extract 14.


042826.00
042826.25
042826.50
042826.75
042827.00
042827.25
042827.50
042827.75
042828.00
042828.25
042828.50
042828.75
042829.00
042829.25
042829.50
042829.75
042830.00
042830.25
042830.50
042830.75

How to extract and convert to milliseconds .

Upvotes: 0

Views: 1359

Answers (2)

ALollz
ALollz

Reputation: 59519

I would save yourself a lot of trouble and work with the nice formatting options already supplied by pandas.to_datetime. Your column is clearly a string because of the leading zeros. From there, it's very easy to get a the time in any unit you want, in this case 'ms'

The format of your string is '%H%M%S.%f'. We'll convert this to a datetime object which gives you the starting date 1900-01-01. We just subtract that off and then convert the units to ms with np.timedelta64

import pandas as pd
import numpy as np
df
       number
0   042826.00
1   042826.25
2   042826.50
3   042826.75
4   042827.00
5   042827.25
6   042827.50
7   042827.75

(pd.to_datetime(df.number, format='%H%M%S.%f')
 -pd.to_datetime('1900-01-01'))/np.timedelta64(1, 'ms')
#0     16106000.0
#1     16106250.0
#2     16106500.0
#3     16106750.0
#4     16107000.0
#5     16107250.0
#6     16107500.0
#7     16107750.0
#Name: number, dtype: float64

The Date 1900-01-01 is used as the default most likely because of the default behavior of time.strptime

The default values used to fill in any missing data when more accurate values cannot be inferred are (1900, 1, 1, 0, 0, 0, 0, 1, -1). Both string and format must be strings.

Upvotes: 2

gaback
gaback

Reputation: 638

You probably don't want to change them back and forth in str and int. First use Decimal/float to get the millisecond then use int to convert the rest:

numbers = {your array}
milisecond = 0
for number in numbers:
    number = Decimal(number, '.3f')
    millisecond += (number * 1000) % 1000
    number = int(number)
    millisecond += (number % 100) * 1000
    number /= 100
    millisecond += (number % 100) * 60 * 1000
    number /= 100
    millisecond += number * 60 * 60 * 1000

The millisecond is milliseconds you get from the number

Upvotes: 1

Related Questions