Home > Software design >  converting the time stamp column into millisecond
converting the time stamp column into millisecond

Time:08-24

I've a column of time stamp below my code and would like to convert it to milisecond in python using for loop with this formula.

from datetime import datetime

dt_obj = datetime.strptime('2018-03-01 15:36:31.23834',
                           '%Y-%m-%d %H:%M:%S.%f')
millisec = dt_obj.timestamp() * 1000

Column:

TS

2018-03-01 15:34:51.347340
2018-03-01 15:34:51.447336
...

enter image description here

CodePudding user response:

pandas.to_datetime(df["TS"], format="%Y-%m-%d %H:%M:%S.%f")

CodePudding user response:

Say this is your dataframe:

import pandas as pd
import datetime

df = pd.DataFrame({'TS': pd.date_range(
    start='2018-03-01 15:34:51.347340', periods=5, freq='100ms').astype(str)})
print(df)
print(type(df['TS'][0]))

#
                           TS
0  2018-03-01 15:34:51.347340
1  2018-03-01 15:34:51.447340
2  2018-03-01 15:34:51.547340
3  2018-03-01 15:34:51.647340
4  2018-03-01 15:34:51.747340
<class 'str'>

Straightforward method would be just using apply:

df['TS'].apply(lambda x: datetime.datetime.strptime(x, '%Y-%m-%d %H:%M:%S.%f') \
            .timestamp() * 1000).astype('int64')

0    1519907691347
1    1519907691447
2    1519907691547
3    1519907691647
4    1519907691747
Name: TS, dtype: int64

Better approach, as described in this answer:

pd.to_datetime(df['TS']).astype('int64') // 10 ** 6

P.S. It's usually appropriate to provide your original data (column) created in code rather as a simple text, for responder's convenience.

  • Related