python - 重新采样每日数据以获得每月数据框?

标签 python pandas

我有一个每日数据框,我正在尝试对其进行重新采样以获得每月的Open High Low Close

daily_df

            Open   High    Low   Last  Close

Date                                         

2010-01-04  55.15  57.55  54.55  57.50  57.30

2010-01-05  59.70  59.70  57.45  57.90  58.00

2010-01-06  60.30  60.30  57.10  57.55  57.50

2010-01-07  60.25  60.25  57.35  58.85  58.90

2010-01-08  59.40  59.95  56.90  57.30  57.65

2010-01-11  57.30  57.95  56.00  56.25  56.25

2010-01-12  56.25  56.80  53.80  54.25  54.10

2010-01-13  54.00  55.00  52.15  54.90  54.85

2010-01-14  55.45  55.70  54.15  54.30  54.35

2010-01-15  54.60  55.30  54.00  54.30  54.30

2010-01-18  53.90  55.20  53.85  54.35  54.40

2010-01-19  54.60  55.20  53.55  53.65  53.75

2010-01-20  54.40  54.40  53.45  53.60  53.70

2010-01-21  53.85  53.85  51.95  52.10  52.25

2010-01-22  51.80  52.85  50.30  51.85  52.00

2010-01-25  52.50  52.50  50.50  50.70  50.85

2010-01-27  51.25  51.25  47.80  47.90  48.20

2010-01-28  48.55  50.50  47.10  47.45  47.35

2010-01-29  47.45  52.15  45.60  51.80  51.70

2010-02-01  51.80  52.40  50.50  51.50  51.45

2010-02-02  53.25  54.10  51.40  51.80  51.80

2010-02-03  51.60  52.90  51.50  51.85  51.95

我试过:

df2 = df_daily.resample('M',convention='end').asfreq()

这给了我一个只有收盘价的数据框,即如果日期恰好是月末,则为第 30 个开盘价高低收盘价,否则 NaN

df2=df_daily.resample('M').mean()

这导致我假设的值是特定月份开盘高低收盘值的平均值。

我希望从有价格的月份的第一天开始获得该月的开盘价,高点是该月的最高值,低点是该月的最低值,接近实际收盘价。

我相信我可以使用 min max 以不同的方式在 pandas 中做到这一点,但我只是想知道是否可以使用重采样来做到这一点。

预期df

         Open   High    Low   Close

Date                                         

2010-01-29  55.15  60.3  45.6  51.7

谢谢

最佳答案

resample 按月考虑月份的最后一天,而不考虑列日期。

df2 = df_daily.resample('M').agg({'Open':'first', 'High':'max', 
                                      'Low': 'min', 'Close':'last'})

输出:

            Open    High    Low    Close
Date                
2010-01-31  55.15   60.3    45.6    51.70
2010-02-28  51.80   54.1    50.5    51.95

您可以将索引更改为列中显示的最后一天:

df2 = df_daily.resample('M').agg({'Open':'first', 'High':'max', 
                                      'Low': 'min', 'Close':'last'})

idx = df_daily.reset_index().groupby(df_daily.index.to_period('M'))['Date'].idxmax()
df2.index = df_daily.iloc[idx].index
print(df2)

Output:

            Open    High    Low    Close
Date                
2010-01-29  55.15   60.3    45.6    51.70
2010-02-03  51.80   54.1    50.5    51.95

如果你只想groupby年月使用:

df3 = df_daily.groupby([df_daily.index.year,df_daily.index.month]).agg({'Open':'first',
                         'High':'max', 'Low': 'min', 'Close':'last'})

df3.index.names= ['Year', 'Month']
print(df3)

Output:

                Open    High    Low     Close
Year    Month               
2010      1     55.15   60.3    45.6    51.70
          2     51.80   54.1    50.5    51.95

关于python - 重新采样每日数据以获得每月数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52184181/

相关文章:

python - 根据另一个数据帧的多个列条件创建列

python - 如何删除第一列和第三列中都包含 NaN 的行?

python - Django 没有序列化自然键值

python - Pandas :如何将列表转换为按列分组的矩阵?

Python - matplotlib - 加权图

python - 将 Pandas 列名连接到列值

python - Pandas df 操作 : new column with list of values if other column rows repeated

python - pandas 聚合的条件总和

Python for循环json.loads错误字符串索引必须是整数

python - sklearn : Get Distance from Point to Nearest Cluster