pandas 在我的数据中按第一天重新采样 [英] Pandas resample by first day in my data

查看:61
本文介绍了 pandas 在我的数据中按第一天重新采样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在熊猫数据框中输入了Yahoo财经的每日股票价格.我想使用.resample()通过获取每月第一个QUOTED每日价格的价格将其转换为每月的股票价格.

I have a Yahoo finance daily stock price imported in a pandas dataframe. I want to use .resample() to convert it to the monthly stock price by taking the price of the first QUOTED daily price of each month.

.resample('MS', how='first')

返回每个月的正确价格,但它会将索引更改为该月的第一天,而通常一个月的第一天的报价可能是该月的2号或3号,因为假期和周末.

returns the correct price of each month but it changes the index to the first day of the month while in general the first day of a month for a quoted price maybe 2nd or 3rd of the month because of holidays and weekends.

如何仅通过对现有日期重新采样而不更改它们来使用resample()?

How can I use resample() by only resampling the existing dates and not changing them?

推荐答案

我认为您想要

I think what you want is BMS (business month start):

.resample('BMS').first()

注意:在熊猫0.18之前,这是使用已弃用的how kwarg完成的:

Note: Prior to pandas 0.18 this was done using the deprecated how kwarg:

.resample('BMS', how='first')


另一种方法是按月份分组,然后以普通的分组方式进行分组(例如,使用


An alternative would be to groupby month and take the first with a plain ol' groupby (and e.g. use nth to get the first entry in each group):

.groupby(pd.Grouper(freq='M')).nth(0)

注意:在熊猫0.21之前,此操作已使用已弃用的TimeGrouper:

Note: Prior to pandas 0.21 this was done using the deprecated TimeGrouper:

.groupby(pd.TimeGrouper('M')).nth(0)

这篇关于 pandas 在我的数据中按第一天重新采样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆