我想从几个小时重新采样到半个小时。在示例中,我使用了.ffill()
,但我也测试了.asfreq()
作为中间步骤。
目标是获得半小时的时间间隔,其中每小时的值分布在上采样的时间间隔中,我正在尝试为存在相同问题的任何范围找到一个通用的解决方案。
import pandas as pd
index = pd.date_range('2018-10-10 00:00', '2018-10-10 02:00', freq='H')
hourly = pd.Series(range(10, len(index)+10), index=index)
half_hourly = hourly.resample('30min').ffill() / 2
hourly
系列看起来像:
2018-10-10 00:00:00 10
2018-10-10 01:00:00 11
2018-10-10 02:00:00 12
Freq: H, dtype: int64
还有half_hourly
:
2018-10-10 00:00:00 5.0
2018-10-10 00:30:00 5.0
2018-10-10 01:00:00 5.5
2018-10-10 01:30:00 5.5
2018-10-10 02:00:00 6.0
Freq: 30T, dtype: float64
最后一个问题是没有行代表02:30:00
我想实现的目标是:
2018-10-10 00:00:00 5.0
2018-10-10 00:30:00 5.0
2018-10-10 01:00:00 5.5
2018-10-10 01:30:00 5.5
2018-10-10 02:00:00 6.0
2018-10-10 02:30:00 6.0
Freq: 30T, dtype: float64
我知道hourly
系列会在02:00结束,因此没有理由期望大熊猫默认插入最后一个半小时。但是,在阅读了许多已弃用/过时的帖子,一些较新的帖子documentation和cookbook之后,我仍然找不到直截了当的解决方案。
最后,我还测试了.mean()
的用法,但没有满足NaN . And interpolate()
的要求,没有按小时平均。
在这种情况下,我的.ffill() / 2
几乎可以用作将小时传播到半小时的一种方式,但是对于这个问题,我希望熊猫已经提供了更好的解决方案。
谢谢。
答案 0 :(得分:0)
您的精确问题可以像这样解决
<li class='uk-active' uk-filter-control><a href='#'>Reset</a></li>";
我怀疑这是一个最低限度的示例,因此我也会尝试一般性地解决。可以说您每天要填写多个点
>>> import pandas as pd
>>> index = pd.date_range('2018-10-10 00:00', '2018-10-10 02:00', freq='H')
>>> hourly = pd.Series(range(10, len(index)+10), index=index)
>>> hourly.reindex(index.union(index.shift(freq='30min'))).ffill() / 2
2018-10-10 00:00:00 5.0
2018-10-10 00:30:00 5.0
2018-10-10 01:00:00 5.5
2018-10-10 01:30:00 5.5
2018-10-10 02:00:00 6.0
2018-10-10 02:30:00 6.0
Freq: 30T, dtype: float64
>>> import pandas as pd
>>> index = pd.date_range('2018-10-10 00:00', '2018-10-10 02:00', freq='H')
>>> hourly = pd.Series(range(10, len(index)+10), index=index)
>>> hourly.reindex(index.union(index.shift(freq='30min'))).ffill() / 2
采用类似的技巧,在2018-09-22的凌晨6点,中午12点,下午6点也包括在内。
重新索引,其偏移量等于您要包含为包含端点的偏移量。在这种情况下,我们的上班时间会增加
>>> import pandas as pd
>>> x = pd.Series([1.5, 2.5], pd.DatetimeIndex(['2018-09-21', '2018-09-22']))
>>> x.resample('6h').ffill()
2018-09-21 00:00:00 1.5
2018-09-21 06:00:00 1.5
2018-09-21 12:00:00 1.5
2018-09-21 18:00:00 1.5
2018-09-22 00:00:00 2.5
Freq: 6H, dtype: float64