[英]How to resample a timeseries of dataframe according to the actual timestamp?
我有一個像這樣的數據框“a”,
[In] a
[Out]
0
date
2014-01-28 10.1956
2014-01-29 10.6456
2014-01-30 10.4350
2014-02-07 10.8275
2014-02-10 10.9806
2014-02-11 10.9711
2014-02-12 10.8849
2014-02-13 10.3967
2014-02-14 10.5211
2014-02-17 10.7700
2014-02-18 10.5211
2014-02-19 10.9232
2014-02-20 10.5594
2014-02-21 10.3679
2014-02-24 10.5115
2014-02-25 10.2339
2014-02-26 10.1286
2014-02-27 9.9371
2014-02-28 10.2914
2014-03-03 10.4732
2014-03-04 10.3201
2014-03-05 10.3296
如果我使用 resample 來收集每個月的最后一天,結果是:
[In] a.resample('M').last()
[Out]
0
date
2014-01-31 10.4350
2014-02-28 10.2914
2014-03-31 10.3296
如您所見,原始索引中沒有“2014-01-31”和“2014-03-31”。 我只想從原始索引中保留每個月的最后日期。 結果應該是:
0
date
2014-01-30 10.4350
2014-02-28 10.2914
2014-03-05 10.3296
我怎樣才能得到這樣的結果? 順便說一句,我在重新采樣中使用了“BM”並得到了相同的結果。
將索引轉換為列,在resample
使用on
參數, last
和最后按date
列重新分配索引:
df = a.reset_index().resample('M', on='date').last().set_index('date')
print (df)
0
date
2014-01-30 10.4350
2014-02-28 10.2914
2014-03-05 10.3296
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.