蟒蛇在统一的半年周期重新采样(相当于大熊猫重新采样的'BQ')



python中是否存在相当于半年一次的'BQ'抽样?我没有在这里找到它

http://pandas.pydata.org/pandas-docs/dev/timeseries.html up-and-downsampling

我有一组记录,其中一些遵循六月-十二月,一些jan-jul,一些feb-auh等,我如何将它们重新采样到六月-dec(对于六月-dec是并发的,对于其他记录是遵循六月/dec的?

谢谢。

'2BQ'呢?

In [57]: ts = pd.Series(range(1000), index=pd.date_range('2000-4-15', periods=1000))
In [58]: ts.resample('2BQ', how='sum')
Out[58]: 
2000-06-30      2926
2000-12-29     30485
2001-06-29     63609
2001-12-31     98605
2002-06-28    127985
2002-12-31    166935
2003-06-30      8955
Freq: 2BQ-DEC, dtype: int64

2季度偏移量将基于序列中的第一个时间戳,因此如果您的数据恰好开始于1月至3月或6月至9月,则锚定将是错误的。解决这个问题的一种方法是在系列的开头填充一个虚拟日期,以便锚点是正确的。

ts = pd.Series(range(1000), index=pd.date_range('2000-3-15', periods=1000))
from datetime import datetime
if ts.index[0].month in [1,2,3]:
    ts.loc[datetime(ts.index[0].year - 1, 12, 1)] = np.nan
elif ts.index[0].month in [7,8,9]:
    ts.loc[datetime(ts.index[0].year, 6, 1)] = np.nan

应该给出正确的答案(并且可以删除第一个条目)。

In [85]: ts.resample('2BQ', how='sum')
Out[85]: 
1999-12-31       NaN
2000-06-30      5778
2000-12-29     36127
2001-06-29     69251
2001-12-31    104340
2002-06-28    133534
2002-12-31    150470
Freq: 2BQ-DEC, dtype: float64

相关内容

最新更新