[英]In python pandas, how can I re-sample and interpolate a DataFrame?
[英]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.