Difference pandas.DateTimeIndex without a frequency
There is no implemented diff
function yet for index.
However, it is possible to convert the index to a Series
first by using Index.to_series
, if you need to preserve the original index. Use the Series
constructor with no index parameter if the default index is needed.
Code example:
rng = pd.to_datetime(['2015-01-10','2015-01-12','2015-01-13'])
data = pd.DataFrame({'a': range(3)}, index=rng)
print(data)
a
2015-01-10 0
2015-01-12 1
2015-01-13 2
a = data.index.to_series().diff()
print(a)
2015-01-10 NaT
2015-01-12 2 days
2015-01-13 1 days
dtype: timedelta64[ns]
a = pd.Series(data.index).diff()
print(a)
0 NaT
1 2 days
2 1 days
dtype: timedelta64[ns]
This question is a bit old but anyway...
I use numpy.diff(data.index)
to get the time deltas. Working fine.