python - 在日期时间索引中查找缺失时段的开始和结束日期
问题描述
我有一个巨大datetime index
的,应该有 1 分钟的频率。我知道有一段时间的数据丢失。我想检测所有丢失的数据周期并找到每个数据周期的开始和结束日期。到目前为止,我想出了如何找到丢失的时间戳:
fullrange = pd.date_range(start = obs.index.min(), end = obs.index.max(), freq = "1T")
missing_dates = obs.index.difference(fullrange)
现在我不知道如何missing_dates
分成几个时期并找到它们的开始和结束日期。
obs.index
看起来像这样:
DatetimeIndex(['2020-05-10 09:08:00', '2020-05-10 09:09:00',
'2020-05-10 09:10:00', '2020-05-10 09:11:00',
'2020-05-10 09:12:00', '2020-05-10 09:13:00',
'2020-05-10 09:14:00', '2020-05-10 09:15:00',
'2020-05-10 09:16:00', '2020-05-10 12:24:00', # missing data
...
'2020-07-09 12:35:00', '2020-07-09 12:36:00',
'2020-07-09 12:37:00', '2020-07-09 12:38:00',
'2020-07-09 12:39:00', '2020-07-09 12:40:00',
'2020-07-09 12:41:00', '2020-07-09 12:42:00',
'2020-07-09 12:43:00', '2020-08-09 13:14:00'], # missing data
dtype='datetime64[ns]', name='timestamp', length=86617)
预期结果是缺失数据周期的列表,每个周期是一个带有 [start, end] 的列表:
[['2020-05-10 09:16:00', '2020-05-10 12:24:00'], ['2020-07-09 12:43:00', '2020-08-09 13:14:00']]
解决方案
利用:
rng = pd.date_range('2017-04-03', periods=10, freq='T')
df = pd.DataFrame({'a': range(10)}, index=rng)
obs = df.iloc[[0,1,2,4,5,7,8,9]]
print (obs)
a
2017-04-03 00:00:00 0
2017-04-03 00:01:00 1
2017-04-03 00:02:00 2
2017-04-03 00:04:00 4
2017-04-03 00:05:00 5
2017-04-03 00:07:00 7
2017-04-03 00:08:00 8
2017-04-03 00:09:00 9
首先比较所有索引值与省略的第一个值的差异:
a = obs.index[obs.index.to_series().diff().ne(pd.Timedelta(1, 'T'))][1:]
然后通过以下方式获取此值的位置Index.get_indexer
:
pos = obs.index.get_indexer(a)
将输出转换为字符串:
idx = obs.index.astype(str)
最后使用zip
通过减去1
实际值来索引先前的值list comprehension
:
out = [list(x) for x in zip(idx[pos-1], idx[pos])]
print (out)
[['2017-04-03 00:02:00', '2017-04-03 00:04:00'],
['2017-04-03 00:05:00', '2017-04-03 00:07:00']]
推荐阅读
- git - 为什么 git push all 不需要远程上游分支
- sql-server - Powershell:检索数据库属性,以百分比和格式转换大小以发送邮件报告
- keras - 为什么这个简单的 keras 3 类分类器只预测一个类而不是其他类?
- javascript - Vue & Vuex 没有在 html 中检索到正确的值
- c++ - 用 cairo 绘制贝塞尔路径
- firebase - Firebase REST-API 使用 iOS 最低版本创建短动态链接
- javascript - 每 X 秒循环一个新的 json 对象
- r - R:如何编写一个函数来从一个数据帧中提取特定值以便输入另一个数据帧
- python - 未找到 PYTHONPATH
- itext - 将添加图像到 pdf 从 iText 迁移到 PDFBox