Reputation: 8788
I am interesting in knowing if a time index contains holes. Say I have the following Series
ss = pd.Series( nr.randn(3), index=[ '2014-01-01', '2014-01-02', '2014-01-03' ] )
ss.index = pd.to_datetime( ss.index )
ss
Output
2014-01-01 0.976455
2014-01-02 -0.610322
2014-01-03 -0.631592
dtype: float64
I thought I could do what I would do with lists (l[1:]-l[:-1]
)
ss.index[1:] - ss.index[:-1]
But here is the output, which I don't understand
<class 'pandas.tseries.index.DatetimeIndex'>
[2014-01-03]
Length: 1, Freq: None, Timezone: None
I ended up doing this (which is kinda ugly)
tmp = pd.Series( ss.index[1:] ) - pd.Series( ss.index[:-1] )
(tmp[0] == tmp ).all()
So I have 2 questions:
ss.index[1:] - ss.index[:-1]
doing?Upvotes: 2
Views: 1923
Reputation: 129018
This is a slightly different method. A frequency will be returned if it can (e.g. its daily if the values are daily spaced with no holes). None
otherwise.
In [14]: pd.infer_freq(Series(np.random.randn(3),index=['20140101','20140102','20140103']).index)
Out[14]: 'D'
In [15]: pd.infer_freq(Series(np.random.randn(3),index=['20140101','20140102','20140104']).index)
In [31]: pd.infer_freq(Series(np.random.randn(3),index=['20140101','20140201','20140301']).index)
Out[31]: 'MS'
Upvotes: 1
Reputation: 11717
You can try
tDelta = ss.index.date[1:]-ss.index.date[:-1]
secondBetweenEachEntries = [t.total_seconds() for t in tDelta]
That gives
import pandas as pd
import numpy.random as nr
ss = pd.Series( nr.randn(3), index=[ '2014-01-01', '2014-01-02', '2014-01-03' ] )
ss.index = pd.to_datetime( ss.index )
tDelta = ss.index.date[1:]-ss.index.date[:-1]
Upvotes: 1
Reputation: 249394
You can do it with numpy.diff()
:
np.diff(np.array(ss.index))
There is probably some slightly slicker way to do this, but the above works. It gives you:
array([86400000000000, 86400000000000], dtype='timedelta64[ns]')
Upvotes: 1