Reputation: 763
I would like to convert data with the following column of elapsed time in milliseconds into a time series with pandas:
I assume that I have to convert this column somehow into timedeltas, but I don't know how.
One extra thing: Although it looks like this, I would not assume that the time spans are always equally spaced.
Looking forward to some help, please.
Upvotes: 2
Views: 294
Reputation: 862671
I think you need to_timedelta
:
df = pd.DataFrame({'MILLISEC':[0,33,67,100]})
td = pd.to_timedelta(df['MILLISEC'], unit='ms')
print (td)
0 00:00:00
1 00:00:00.033000
2 00:00:00.067000
3 00:00:00.100000
And if need timeseries add datetime to timedelatas:
ts = pd.datetime.now() + td
print (ts)
0 2018-01-17 13:27:42.104580
1 2018-01-17 13:27:42.137580
2 2018-01-17 13:27:42.171580
3 2018-01-17 13:27:42.204580
Name: MILLISEC, dtype: datetime64[ns]
ts = pd.datetime(2018,1,1) + td
print (ts)
0 2018-01-01 00:00:00.000
1 2018-01-01 00:00:00.033
2 2018-01-01 00:00:00.067
3 2018-01-01 00:00:00.100
Name: MILLISEC, dtype: datetime64[ns]
Upvotes: 1