[英]Pandas resample pulls weekends to Friday
我有一個在工作日可用的每日 OCHL 數據,我正在嘗試以 36 天為周期重新采樣,以使其與我的圖表保持一致。
數據格式如下:
date open close high low
0 2019-05-01 21:00:00 0.70147 0.70023 0.70292 0.69952
1 2019-04-30 21:00:00 0.70476 0.70140 0.70610 0.70074
2 2019-04-29 21:00:00 0.70554 0.70498 0.70692 0.70308
3 2019-04-28 21:00:00 0.70380 0.70564 0.70609 0.70377
4 2019-04-25 21:00:00 0.70149 0.70434 0.70613 0.70074
我正在做這樣的重采樣:
year_resampled = df_year.resample('36B').agg({'date':'first','open':'first','close':'last','high':'max','low':'min'})
當數據跨越周末時就會出現問題; 我有一個周四結束的時間間隔。 星期五有一個缺失值,resample 函數從星期日拉取數據以開始一個新的時間間隔。 它從星期日開始一個新的時間間隔,我需要將日期改回星期五。 例子:
[datetime.datetime(2018, 7, 23, 21, 0), 0.7379, 0.74176, 0.7434, 0.73596],
[datetime.datetime(2018, 7, 22, 21, 0), 0.74183, 0.73812, 0.74376, 0.73718], - Sunday, new interval starts. Here I want to change date to 2018-7-20
[datetime.datetime(2018, 7, 19, 21, 0), 0.73572, 0.74167, 0.74309, 0.73182], -- Thursday (interval ends)
默認情況下, 'W'
表示從星期日開始的幾周。 您可以通過指定'W-Fri'
來更改它
df.resample('W', on='date').first()
date open close high low
date
2019-04-28 2019-04-25 0.70149 0.70434 0.70613 0.70074
2019-05-05 2019-04-29 0.70554 0.70498 0.70692 0.70308
相對
df.resample('W-Fri', on='date').first()
date open close high low
date
2019-04-26 2019-04-25 0.70149 0.70434 0.70613 0.70074
2019-05-03 2019-04-28 0.70380 0.70564 0.70609 0.70377
我正在查看日期列,而我應該查看索引。 重新采樣后,索引按照我的需要按正確的順序排列日期。 我有一個日期聚合,因為我強迫數據框列出但有一個日期列是錯誤的,所以我刪除了它
year_resampled = df_year.resample('36B').agg({'open':'first','close':'last','high':'max','low':'min'})
並使用索引將日期拉入列表
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.