Solved – Convert Daily Data to Monthly Data in Python : Time Series Analysis

python

I am new to data analysis with python. I have daily data of flu cases for a five year period which I want to do Time Series Analysis on. Am using the Pandas library. It is easy to plot this data and see the trend over time, however now I want to see seasonality. As it is, the daily data when plotted is too dense (because it's daily) to see seasonality well and I would like to transform/convert the data (pandas DataFrame) into monthly data so I can better see seasonality. Is there an easy way to do this with pandas (or any other python data munging library)?

Best Answer

There are examples of doing what you want in the pandas documentation. In pandas the method is called resample.

monthly_x = x.resample('M')

Or this is an example of a monthly seasonal plot for daily data in statsmodels may be of interest.

import statsmodels.api as sm
import pandas as pd
dta = sm.datasets.elnino.load_pandas().data
dta['YEAR'] = dta.YEAR.astype(int).astype(str)
dta = dta.set_index('YEAR').T.unstack()
dates = map(lambda x : pd.datetools.parse('1 '+' '.join(x)),
                                       dta.index.values)
dta.index = pd.DatetimeIndex(dates, freq='M')
fig = sm.graphics.tsa.month_plot(dta)

enter image description here