Skip to content
Advertisement

How to count pandas datetime months by continuous season

I have a large time-series dataframe. The column has already been formatted as datetime. Such as

JavaScript

I want to plot the sample numbers for each season. Such as the following. The values are the count number of samples in that season.

JavaScript

I do make a little search and realize I can create a dictionary to convert the months into seasons. However, The most tricky part since the ‘real wintertime’ contains two years’ data. For example, the 1997 winter actually should contain 1997 December 1998 January, and 1998 February.

Please note, I want the ‘1997 January, 1997 February’ to be excluded from 1997 winter since they are ‘1996 winter’.

I am wondering what is the most efficient way to do that? It does not have to be named such as ‘1997 winter’, any index should work for me as long as the counting numbers are successive from the beginning to the end.

Many thanks!

Advertisement

Answer

There is a fast way to solve it, but it’s not very orthodox… You create a column ‘season’, and with a np.where(), you assign the season. At the beginning, you say winter for the first 3 month, spring, for the 3 next, and so. And then, you apply a shift(-1) on the column to shift it by one row back. Then, you’ve got your seasons (just ffill the las nan). You can then solve your problem in a lazy way. If you’re not confortable with the code, tell me, I will edit it.

EDIT:

I assume that the dates are in the index. If not, you should apply a dt.month instead of .month. I decompose it to make it clear

JavaScript

EDIT 2:

Here a complete example :

JavaScript

EDIT 3:

Here the complete example if you have several rows for same month:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement