Python Forum

Full Version: selecting customized seasons from monthly time series
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have monthlly time seris data like this

data = [1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12,...]

where 1,2,3 represents values for those months respectively.

I want to create select all the data for [1,2,3,10,11,12,1,2,3,10,11,12,...] [4,5,6,7,8,9,4,5,6,7,8,9,...] and a bit stuck now about how to go about this.

Thanks in advance for help
Are you using Pandas to store the data? In any case, I would suggest to consider Series.isin method or, if you are working with Numpy, numpy.isin.

So, if your data is presented as a DataFrame df, you code may be the following:

import pandas as pd
df = pd.DataFrame({'month': range(1, 13), 'value': pd.np.random.rand(12)})
df[df.month.isin([3,4,5])]  # selects data for march, april and may
Hello once again

so after using df[df.month.isin([10,11,12,1,2,3])

I get the results to be 1,2,3,10,11,12 meaning the order is changed. Is there a way to keepp the same order after the selection?

thanks
Look at the sample data frame

df = pd.DataFrame({'month': range(12, 0,-1), 'value': pd.np.random.rand(12)})
Months countdown from 12 to 1. When we do selection
df[df.month.isin([3,4,5])] 
the order is retained:
Output:
month value 8 5 0.296964 9 4 0.780931 10 3 0.343411