Gabriel Lucas
Gabriel Lucas

Reputation: 301

How can I group a time series according to the seasons using pandas?

I have a daily time series of sea surface temperature from MODIS/Aqua sensor, starting in 2002 until 2020:

data['Image_Datetime'] = pd.to_datetime(data['Image_Datetime'], format='%Y-%m-%d %H:%M:%S')
data = data.set_index('Image_Datetime')
data.head()

enter image description here

I want to group all data correspondent to the summer period (December, January and February months), so then I can analyse just the temperature variations over a single season. I know this should be more or less simple, but I've been searching for a solution and none of them seems quite intuitive using only pandas.

Upvotes: 1

Views: 601

Answers (1)

Code Different
Code Different

Reputation: 93181

You must live in the southern hemisphere for summer to take place in December.

Try this:

season = data['Image_Datetime'].dt.month.map(lambda x: 'Summer' if x in [12, 1, 2] else 'Winter')

# Mean temperature by season
data.group_by(season)['Temperature'].mean()

Upvotes: 1

Related Questions