简体   繁体   English

从一年的第一天开始对Pandas Dataframe进行每周重新采样

[英]Pandas Dataframe resample week, starting first day of the year

I have a dataframe containing hourly data, i want to get the max for each week of the year, so i used resample to group data by week 我有一个包含每小时数据的数据框,我想获取一年中每个星期的最大值,所以我使用重采样按周对数据进行分组

weeks = data.resample("W").max()

the problem is that week max is calculated starting the first monday of the year, while i want it to be calculated starting the first day of the year. 问题是,周最大值是从一年的第一天开始计算的,而我希望它是从一年的第一天开始计算的。

I obtain the following result, where you can notice that there is 53 weeks, and the last week is calculated on the next year while 2017 doesn't exist in the data 我得到以下结果,您会发现有53周,而上周是在下一年计算的,而数据中不存在2017年

Date        dots       
2016-01-03  0.647786
2016-01-10  0.917071
2016-01-17  0.667857
2016-01-24  0.669286
2016-01-31  0.645357


Date        dots                
2016-12-04  0.646786
2016-12-11  0.857714
2016-12-18  0.670000
2016-12-25  0.674571
2017-01-01  0.654571

is there a way to calculate week for pandas dataframe starting first day of the year? 有没有一种方法可以从一年的第一天开始计算熊猫数据框的周数?

One quick remedy is, given you data in one year, you can group it by day first, then take group of 7 days: 一种快速的补救方法是,给定一年中的数据,您可以先按天分组,然后再按7天一组:

new_df = (df.resample("D", on='Date').dots
            .max().reset_index()
         )

new_df.groupby(new_df.index//7).agg({'Date': 'min', 'dots': 'max'})

new_df.head()

Output: 输出:

    Date        dots
0   2016-01-01  0.996387
1   2016-01-08  0.999775
2   2016-01-15  0.997612
3   2016-01-22  0.979376
4   2016-01-29  0.998240
5   2016-02-05  0.995030
6   2016-02-12  0.987500

and tail: 和尾巴:

    Date        dots
48  2016-12-02  0.999910
49  2016-12-09  0.992910
50  2016-12-16  0.996877
51  2016-12-23  0.992986
52  2016-12-30  0.960348

找到一年的开始日期,例如,假设是星期五,然后您可以指定要重新采样的锚定后缀 ,以便计算从一年的第一天开始的weeks = data.resample("W-FRI").max()weeks = data.resample("W-FRI").max()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM