-1

I am working on time series data and want to arrange data belonging to same month in a contiguous fashion. Please look at the code.

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)]

The last line accomplishes what I want, I am wondering if there is an efficient way of doing this as I have to repeat the same line for all the months. Thanks in advance

2 Answers2

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

This for loop will generate all twelve months in different dataframe's.

Sayali Sonawane
  • 12,289
  • 5
  • 46
  • 47
0

If need each DataFrame separately, the best is use 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

Or groupby by strings created by strftime if need month and year keys of 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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252