差异pandas.DateTimeIndex没有频率 [英] Difference pandas.DateTimeIndex without a frequency
问题描述
不规则的时间序列data
存储在pandas.DataFrame
中.设置了DatetimeIndex
.我需要索引中连续条目之间的时间差.
An irregular time series data
is stored in a pandas.DataFrame
. A DatetimeIndex
has been set. I need the time difference between consecutive entries in the index.
我以为会很简单
data.index.diff()
但是得到
AttributeError: 'DatetimeIndex' object has no attribute 'diff'
我尝试过
data.index - data.index.shift(1)
但是得到
ValueError: Cannot shift with no freq
在执行此操作之前,我不想首先推断或强制执行频率.时间序列中有很大的差距,这些差距会扩大到nan
的大范围运行.关键是要首先找到这些差距.
I do not want to infer or enforce a frequency first before doing this operation. There are large gaps in the time series that would be expanded to large runs of nan
. The point is to find these gaps first.
那么,执行此看似简单的操作的一种干净方法是什么?
So, what is a clean way to do this seemingly simple operation?
推荐答案
索引尚未实现diff
函数.
但是,可以首先使用Index.to_series
,如果您需要保留原始索引.如果需要默认索引,请使用不带索引参数的Series
构造函数.
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.
代码示例:
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]
这篇关于差异pandas.DateTimeIndex没有频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!