My Dataframe looks like:
Time,User,value
2018-03-30T14:18:49.600,U101,23
2018-03-30T14:18:49.800,U102,22
2018-03-30T14:18:50.000,U101,24
I would like to change String timestamp to epoch in milliseconds in column Time
in DataFrame. I am able to do that by saving values in list of lists and iterate over each row. But I need an efficient way such that, I can replace String time to epoch time in Dataframe itself. Thanks in advance.
df['Time'] = (df['Time'] - pd.Timestamp("1970-01-01")) // pd.Timedelta('1s')
is the recommended solution from pandas, reference.
example:
import pandas as pd
df = pd.DataFrame()
df['Time'] = pd.date_range('2012-10-08 18:15:05', periods=4, freq='D')
df['Epoch'] = (df['Time'] - pd.Timestamp("1970-01-01")) // pd.Timedelta('1s')
print(df)
output:
Time Epoch
0 2012-10-08 18:15:05 1349720105
1 2012-10-09 18:15:05 1349806505
2 2012-10-10 18:15:05 1349892905
3 2012-10-11 18:15:05 1349979305
I think need convert to_datetime
, then to numpy array
with cast to int64
and last divide by 10**6
because native format is in nanosecond
s:
df['Time'] = pd.to_datetime(df['Time']).values.astype(np.int64) // 10**6
print (df)
Time User value
0 1522419529600 U101 23
1 1522419529800 U102 22
2 1522419530000 U101 24
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With