繁体   English   中英

python在统一的半年期重新采样(在熊猫重新采样中等于“ BQ”)

[英]python re-sample at a uniform semiannual period (equivaent of 'BQ' in pandas resample)

python中是否有一个“ BQ”等效的半年重采样? 我在这里没找到

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

我有一组记录,其中一些遵循jun-dec,一些jan-jul,一些feb-auh等。我如何将它们全部重采样到jun-dec(jun-dec并发,并且遵循jun / 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM