datetime groupby/重新采样不同年份中的月份和日期(删除年份) [英] datetime groupby/resample month and day across different years (drop the year)

查看:301
本文介绍了datetime groupby/重新采样不同年份中的月份和日期(删除年份)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我研究了Pandas中的重采样/时间分组器功能.但是,我试图弄清楚如何在特定情况下使用它.我想对金融资产进行季节性分析-假设是S& P500.我想知道该资产在多年中平均在任意两个自定义日期之间的表现如何.

I have looked at the resample/Timegrouper functionality in Pandas. However, I'm trying to figure out how to use it for this specific case. I want to do a seasonal analysis across a financial asset - let's say S&P 500. I want to know how the asset performs between any two custom dates on average across many years.

示例:如果我有10年的标准普尔500每日价格变化历史,并且选择了3月13日至3月23日之间的日期范围,那么我想知道我最近一次范围内每个日期的平均变化10年-即最近10年中每年3/13的平均变化,然后是3/14、3/15等,直到3/23.这意味着我需要按月和日分组,并对不同年份的值进行平均.

Example: If I have a 10 year history of daily changes of S&P 500 and I pick the date range between March 13th and March 23rd, then I want to know the average change for each date in my range across the last 10 years - i.e. average change on 3/13 each year for the last 10 years, and then for 3/14, 3/15 and so on until 3/23. This means I need to groupby month and day and do an average of values across different years.

我可以通过为年,月和日创建3个不同的列,然后按它们中的两个进行分组来做到这一点,但我想知道是否还有更优雅的方法.

I can probably do this by creating 3 different columns for year, month, and day and then grouping by two of them, but I wonder if there are more elegant ways of doing this.

推荐答案

我知道了.事实证明这很简单,我只是傻瓜.

I figured it out. It turned out to be pretty simple and I was just being dumb.

x.groupby([x.index.month, x.index.day], as_index=True).mean()

在我的情况下,其中x是熊猫系列(但我想也可能是数据框?).这将返回一个多索引系列,在我看来,这是可以的,但是如果在您的情况下,它不是可以的,那么您可以操纵它来降低某个级别或将索引变成新的列

where x is a pandas series in my case (but I suppose could also be a dataframe?). This will return a multi-index series which is ok in my case, but if it's not in your case then you can manipulate it to drop a level or turn the index into new columns

这篇关于datetime groupby/重新采样不同年份中的月份和日期(删除年份)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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