Python Pandas:year/month的数据框groupby,返回新的DatetimeIndex
我需要一些指导,将一个Python Pandas:year/month的数据框groupby,返回新的DatetimeIndex,python,pandas,datetimeindex,Python,Pandas,Datetimeindex,我需要一些指导,将一个日期框对象按年份或月份分组,并返回一个新的日期框对象和一个新索引。 这是到目前为止我的代码分组方式按预期工作 从.csv文件加载数据,解析“日期”到日期格式(来自finance.yahoo.com的历史股票报价) 按“日期”升序排序文件,并将索引设置为Date In [24]: daily = file.sort_values(by='Date').set_index('Date') daily.head() Out[24]: O
日期框
对象按年份或月份分组,并返回一个新的日期框
对象和一个新索引。
这是到目前为止我的代码<代码>分组方式按预期工作
从.csv文件加载数据,解析“日期”到日期格式(来自finance.yahoo.com的历史股票报价)
按“日期”升序排序文件,并将索引设置为Date
In [24]: daily = file.sort_values(by='Date').set_index('Date')
daily.head()
Out[24]:
Open High Low Close Volume Adj Close
Date
2000-01-03 14.20 14.50 14.15 14.40 277400 2.7916
2000-01-04 14.29 14.30 13.90 14.15 109200 2.7431
月份分组
我将对这些组执行另一个apply()
,这将压缩特定组的数据,例如,查找年/月的最高High
值或sum()
量值。对于该示例,省略该步骤
In [39]: monthly = daily.groupby(lambda x: (x.year, x.month))
monthly.first()
Out[39]:
Open High Low Close Volume Adj Close
(2000, 1) 14.200 14.500 14.150 14.400 277400 2.7916
(2000, 2) 13.900 14.390 13.900 14.250 287200 2.7625
... ... ... ... ... ... ...
(2016, 1) 23.620 23.620 23.620 23.620 0 22.1893
(2016, 2) 19.575 19.630 19.140 19.450 1783000 18.2719
这是可行的,但它给了我一个带有元组作为索引的日期框
对象
在本例中,对于月份分组,所需的结果将是一个全新的DataFrame
对象,但是Date
索引应该是一个新的DatetimeIndex
,形式为%Y-%m
,如果按年份分组,则只需%Y
Out[39]:
Open High Low Close Volume Adj Close
Date
2000-01 14.200 14.500 14.150 14.400 277400 2.7916
2000-02 13.900 14.390 13.900 14.250 287200 2.7625
... ... ... ... ... ... ...
2016-01 23.620 23.620 23.620 23.620 0 22.1893
2016-02 19.575 19.630 19.140 19.450 1783000 18.2719
我感谢你给我指路 您可以使用列表理解从时间戳访问年份和月份访问器变量,然后对这些变量进行分组
>>> df.groupby([[d.year for d in df.Date], [d.month for d in df.Date]]).first()
Date Open High Low Close Volume Adj_Close
2000 1 2000-01-01 14.200 14.50 14.15 14.40 277400 2.7916
2 2000-02-01 13.900 14.39 13.90 14.25 287200 2.7625
2016 1 2016-01-01 23.620 23.62 23.62 23.62 0 22.1893
2 2016-02-01 19.575 19.63 19.14 19.45 1783000 18.2719
您可以使用daily.index.year、daily.index.month
或更改index
,然后通过index
:
print daily
Open High Low Close Volume Adj Close
Date
2000-01-01 14.200 14.50 14.15 14.40 277400 2.7916
2000-02-01 13.900 14.39 13.90 14.25 287200 2.7625
2016-01-01 23.620 23.62 23.62 23.62 0 22.1893
2016-02-01 19.575 19.63 19.14 19.45 1783000 18.2719
print daily.groupby([daily.index.year, daily.index.month]).first()
Open High Low Close Volume Adj Close
2000 1 14.200 14.50 14.15 14.40 277400 2.7916
2 13.900 14.39 13.90 14.25 287200 2.7625
2016 1 23.620 23.62 23.62 23.62 0 22.1893
2 19.575 19.63 19.14 19.45 1783000 18.2719
daily.index = daily.index.to_period('M')
print daily.groupby(daily.index).first()
Open High Low Close Volume Adj Close
Date
2000-01 14.200 14.50 14.15 14.40 277400 2.7916
2000-02 13.900 14.39 13.90 14.25 287200 2.7625
2016-01 23.620 23.62 23.62 23.62 0 22.1893
2016-02 19.575 19.63 19.14 19.45 1783000 18.2719
欢迎来到堆栈溢出。你可以查一下,谢谢,但为什么?我的问题有问题吗?不,问题很好。没问题。啊,好的。可以,但尚未测试建议的解决方案。谢谢,最后一个示例非常有效。但奇怪的是,如果我重新运行这个单元格,我会得到一个属性错误
--->4 daily.index=daily.index.to_period('M')5 monthly=daily.groupby(daily.index).first()6 print(monthly)AttributeError:'PeriodIndex'对象没有属性'to_period'
我必须重新运行所有单元格才能使此工作。您只需将PeriodIndex
设置为daily.index=daily.index。to_period('M')
一次,然后使用其他代码。尝试测试:print daily
,daily.index=daily.index.to_period('M')
和print daily
,但它会永久更改daily
的period index
。如果我想将它改回daily.index=daily.index.to_period('D')
几天,或者进一步使用daily.index=daily.index.to_period('A')
年,我会得到attributeError,那么最好使用列而不是像这样的索引:daily['m']=daily.index.to_period('m')
和monthly=daily.groupby('m').first()
,然后daily['d']=daily.index.to_period('d')
和daily1=daily.groupby('d').first()
,或者您可以记住索引到类似df['i']=df.index的列中,然后使用groupby
print daily
Open High Low Close Volume Adj Close
Date
2000-01-01 14.200 14.50 14.15 14.40 277400 2.7916
2000-02-01 13.900 14.39 13.90 14.25 287200 2.7625
2016-01-01 23.620 23.62 23.62 23.62 0 22.1893
2016-02-01 19.575 19.63 19.14 19.45 1783000 18.2719
print daily.groupby([daily.index.year, daily.index.month]).first()
Open High Low Close Volume Adj Close
2000 1 14.200 14.50 14.15 14.40 277400 2.7916
2 13.900 14.39 13.90 14.25 287200 2.7625
2016 1 23.620 23.62 23.62 23.62 0 22.1893
2 19.575 19.63 19.14 19.45 1783000 18.2719
daily.index = daily.index.to_period('M')
print daily.groupby(daily.index).first()
Open High Low Close Volume Adj Close
Date
2000-01 14.200 14.50 14.15 14.40 277400 2.7916
2000-02 13.900 14.39 13.90 14.25 287200 2.7625
2016-01 23.620 23.62 23.62 23.62 0 22.1893
2016-02 19.575 19.63 19.14 19.45 1783000 18.2719