2

I have time series with time stamps corresponding to timedeltas, but the actual time and date do not play a role. So I can assume some reference time point as a start for the time series.

ts = ref_time + pd.to_timedelta(dat["MILLISEC"], unit="ms")

dat.set_index(ts)

But I wonder if ref_time really needs to be of type datetime.datetime as I would prefer it to be of type datetime.time. But than I cannot use it as an index for Pandas as it seems. Why is that?

Best regards!

Aditi
  • 820
  • 11
  • 27
CodingButStillAlive
  • 733
  • 2
  • 8
  • 22

1 Answers1

1

I think yes, because datetime.time as python object is not implicitly converted to timestamp like datetime.datetime:

a = datetime.time(4) + pd.to_timedelta(['10:00:10','00:00:10'])
print (a)

TypeError: unsupported operand type(s) for +: 'datetime.time' and 'TimedeltaIndex'

a = datetime.datetime(2017,1,1,4) + pd.to_timedelta(['10:00:10','00:00:10'])
print (a)

DatetimeIndex(['2017-01-01 14:00:10', '2017-01-01 04:00:10'],
               dtype='datetime64[ns]', freq=None)

Or create TimedeltaIndex:

a = pd.Timedelta(4, unit='h') + pd.to_timedelta(['10:00:10','00:00:10'])
print (a)

TimedeltaIndex(['14:00:10', '04:00:10'], dtype='timedelta64[ns]', freq=None)

EDIT:

It is not supported in pure python also, check this.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252