示例:
作为一个极简例子,可以参考以下内容:
>>> df
GWA_BTC GWA_ETH GWA_LTC GWA_XLM GWA_XRP
Date
2013-01-19 11,826.36 1,068.45 195.00 0.51 1.82
2013-01-20 13,062.68 1,158.71 207.58 0.52 1.75
2013-01-28 12,326.23 1,108.90 197.36 0.48 1.55
2013-01-29 11,397.52 1,038.21 184.92 0.47 1.43
我们可以找到 2013-01-19
和 2013-01-29
之间缺失的日期。
方法一:
请查看 @Vaishali 的回答
使用 .difference
查找您的日期时间索引与范围内所有日期的差异:
pd.date_range('2013-01-19', '2013-01-29').difference(df.index)
这将返回:
DatetimeIndex(['2013-01-21', '2013-01-22', '2013-01-23', '2013-01-24',
'2013-01-25', '2013-01-26', '2013-01-27'],
dtype='datetime64[ns]', freq=None)
方法二:
您可以使用所需日期范围内的所有日期重新索引您的数据框,并查找reindex
插入NaN
的位置。
要查找2013-01-19
和2013-01-29
之间缺失的日期:
>>> df.reindex(pd.date_range('2013-01-19', '2013-01-29')).isnull().all(1)
2013-01-19 False
2013-01-20 False
2013-01-21 True
2013-01-22 True
2013-01-23 True
2013-01-24 True
2013-01-25 True
2013-01-26 True
2013-01-27 True
2013-01-28 False
2013-01-29 False
Freq: D, dtype: bool
那些值为
True
的是你原始数据中缺失的日期。