熊猫间隔 - 如果完全覆盖了较大的间隔,则掉落
假设我有一个包含DateTime间隔的多指数PANDAS DATAFAME,
arrays = [[pd.datetime(2022,1,1), pd.datetime(2022,2,1), pd.datetime(2022,1,1), pd.datetime(2022,4,1)], [pd.datetime(2022,1,31), pd.datetime(2022,2,28), pd.datetime(2022,3,30), pd.datetime(2022,4,30)]]
idx = pd.MultiIndex.from_arrays(arrays)
df = pd.DataFrame(np.zeros(4), index = idx)
df
0
2022-01-01 2022-01-31 0.0
2022-02-01 2022-02-28 0.0
2022-01-01 2022-03-30 0.0
2022-04-01 2022-04-30 0.0
我要做的就是放弃另一个间隔完全覆盖的间隔。在示例中,我们观察到间隔01.01.2022-30.03.2022,因此我想删除前两个观察结果。
我尝试了pandas 重叠()
,其中我在重叠间隔中变得正确:
iix = pd.IntervalIndex.from_arrays(df.index.get_level_values(0), df.index.get_level_values(1), closed='both')
[iix.overlaps(x)for x in iix]
[array([ True, False, True, False]),
array([False, True, True, False]),
array([ True, True, True, False]),
array([False, False, False, True])]
基于此,我可以掉落,但后来我也会放下最大的重叠间隔。所以不是一个好的解决方案。有人有更好的人吗?我会非常感激!
Suppose I have a multi-index pandas dataframe containing datetime intervals
arrays = [[pd.datetime(2022,1,1), pd.datetime(2022,2,1), pd.datetime(2022,1,1), pd.datetime(2022,4,1)], [pd.datetime(2022,1,31), pd.datetime(2022,2,28), pd.datetime(2022,3,30), pd.datetime(2022,4,30)]]
idx = pd.MultiIndex.from_arrays(arrays)
df = pd.DataFrame(np.zeros(4), index = idx)
df
0
2022-01-01 2022-01-31 0.0
2022-02-01 2022-02-28 0.0
2022-01-01 2022-03-30 0.0
2022-04-01 2022-04-30 0.0
What I want to do is to drop the intervals that are completely covered by another interval. In the example, we observe the interval 01.01.2022-30.03.2022, so I want to drop the first two observations.
I tried pandas overlaps()
, where I am getting True for overlapping intervals:
iix = pd.IntervalIndex.from_arrays(df.index.get_level_values(0), df.index.get_level_values(1), closed='both')
[iix.overlaps(x)for x in iix]
[array([ True, False, True, False]),
array([False, True, True, False]),
array([ True, True, True, False]),
array([False, False, False, True])]
Based on this, I could drop, but then I would also drop the largest overlapping interval. So not a good solution. Does anybody have a better one? I'd be very grateful!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论