在pandas-python中根据日期时间索引选择数据

时间:2017-07-03 11:49:13

标签: python pandas datetime

我正在研究时间序列数据,并希望以连续的方式安排属于同月的数据。请看代码。

from pandas_datareader import data as web

from datetime import datetime

from pandas.tseries.offsets import Day, MonthEnd



stock= web.DataReader('AAPL',data_source='google',start='1/1/2008', 

end='12/31/2009')

a1=stock['Close'].resample('M').apply(lambda x: x[-1])

a2=a1[(a1.index.month==1)]

最后一行完成了我想要的,我想知道是否有一种有效的方法可以做到这一点,因为我必须在所有月份重复相同的路线。提前致谢

2 个答案:

答案 0 :(得分:1)

for month in range(12):
    globals()['Month_%s' % str(month+1)]= a1[(a1.index.month==int(month+1))]

此for循环将在不同的数据框中生成所有12个月。

答案 1 :(得分:0)

如果需要单独使用DataFrame,最好使用dict of DataFrames

rng = pd.date_range('2017-04-03', periods=10, freq='20D')
df = pd.DataFrame({'a': range(10)}, index=rng)  
print (df)
            a
2017-04-03  0
2017-04-23  1
2017-05-13  2
2017-06-02  3
2017-06-22  4
2017-07-12  5
2017-08-01  6
2017-08-21  7
2017-09-10  8
2017-09-30  9

dfs = dict(tuple(df.groupby(df.index.month)))
print (dfs)
{4:             a
2017-04-03  0
2017-04-23  1, 5:             a
2017-05-13  2, 6:             a
2017-06-02  3
2017-06-22  4, 7:             a
2017-07-12  5, 8:             a
2017-08-01  6
2017-08-21  7, 9:             a
2017-09-10  8
2017-09-30  9}

print (dfs[4])
            a
2017-04-03  0
2017-04-23  1

groupby stringstrftime创建的dict of DataFrames如果需要dfs = dict(tuple(df.groupby(df.index.strftime('%Y-%m')))) print (dfs) {'2017-09': a 2017-09-10 8 2017-09-30 9, '2017-08': a 2017-08-01 6 2017-08-21 7, '2017-06': a 2017-06-02 3 2017-06-22 4, '2017-07': a 2017-07-12 5, '2017-05': a 2017-05-13 2, '2017-04': a 2017-04-03 0 2017-04-23 1} print (dfs['2017-06']) a 2017-06-02 3 2017-06-22 4 的月份和年份密钥:

{{1}}