简体   繁体   English

在熊猫中按日期重新采样-弄乱索引中的日期

[英]Resample by Date in Pandas — messes up a date in index

I have a multi-index dataFrame in Pandas, with data indexed by building, and then by date. 我在Pandas中有一个多索引的dataFrame,其中数据先按构建索引,然后按日期索引。 The different columns represent different kinds of energy, and the values represent how much energy was used for a given month. 不同的列代表不同种类的能量,而值代表给定月份使用了多少能量。 Image of the dataframe's head is here. 数据帧头部的图像在这里。 I'd like to turn this into yearly data. 我想将其转换为年度数据。 I currently have the line 我现在有线

df.unstack(level=0).resample('BAS-JUL').sum()

and this works almost perfectly. 几乎可以完美地工作。 Here is the issue: all the dates are given as the 1st of the month, but for some reason, as it does resample , it picks July 2nd as the cut-off for 2012. So the number for July 1, 2012 ends up being counted in the 2011 data. 问题是:所有日期都是该月的1号,但是由于某种原因,由于resample ,因此选择7月2日作为2012年的截止日期。因此,2012年7月1日的数字最终为计入2011年数据。 It ends up looking like this. 最终看起来像这样。 You can see that the second value in the Usage Month column is July 2. Other than that, the resample appears to work perfectly. 您可以看到“使用月份”列中的第二个值是7月2日。除此之外, resample似乎可以正常工作。

If I run df.index.get_level_values(1)[:20] , the output is: 如果我运行df.index.get_level_values(1)[:20] ,则输出为:

DatetimeIndex(['2011-07-01', '2011-08-01', '2011-09-01', '2011-10-01',
           '2011-11-01', '2011-12-01', '2012-01-01', '2012-02-01',
           '2012-03-01', '2012-04-01', '2012-05-01', '2012-06-01',
           '2012-07-01', '2012-08-01', '2012-09-01', '2012-10-01',
           '2012-11-01', '2012-12-01', '2013-01-01', '2013-02-01'],
          dtype='datetime64[ns]', name='Usage Month', freq=None)

So the index is July 1 2012 in the original dataframe. 因此,索引为原始数据帧中的2012年7月1日。

Any ideas on how to fix this mini-bug would be appreciated! 任何有关如何解决此小错误的想法将不胜感激!

Use 'AS-JUL': 使用“ AS-JUL”:

df.unstack(level=0).resample('AS-JUL').sum()

The B is for Business Annual Start. B用于企业年度启动。

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

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