2

I would like to convert data with the following column of elapsed time in milliseconds into a time series with pandas:

enter image description here

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.

CodingButStillAlive
  • 733
  • 2
  • 8
  • 22

1 Answers1

1

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]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thanks! That looks good and helps me :) However, I do wonder if it's definately necessary to introduce some reference time point in order to consider it a time series. I thought that pandas might be able to handle a time series of time deltas as well. But I see that probably most of the plotting functions require actual dates from a calendar. – CodingButStillAlive Jan 17 '18 at 12:47
  • 1
    In pandas timedelatas working very nice, but for ploting need convert it to numeric, check [this](https://stackoverflow.com/a/23544011/2901002) or using datetimes. – jezrael Jan 17 '18 at 12:48
  • Thanks for your help! – CodingButStillAlive Jan 18 '18 at 15:52