0

as input there is an array with numbers from 1 to 12. At the output, I want to get an array that will produce, depending on the number, the time of year

import pandas as pd

month = pd.Series([i for i in range(1,13)])
def mkseason(n):
    if 3<=n<=5: season = 'spring'
    elif 6<=n<=8: season = 'summer'
    elif 9<=n<=11: season = 'fall'
    elif n<=2 or n==12: season = 'winter'
    else: season = 'unknown'
    return(season)

As result I want to get array -

['winter','winter','spring','spring','spring','summer','summer','summer','fall','fall','fall','winter']

When I tried make something like this:

mkseason(month)

I have gor an error. How should I solve my problem? I need to use pandas without loops

2 Answers 2

1

Use modulo with 12 and integer division for groups and last map by dictionary:

month = (((month % 12) // 3).map({0:'winter',1:'spring',2:'summer',3:'fall'})
                            .fillna('unknown'))
print (month)
0     winter
1     winter
2     spring
3     spring
4     spring
5     summer
6     summer
7     summer
8       fall
9       fall
10      fall
11    winter
dtype: object

Details:

print ((month % 12) // 3)
0     0
1     0
2     1
3     1
4     1
5     2
6     2
7     2
8     3
9     3
10    3
11    0
dtype: int64

Performance:

#140k rows
#added 13 for test unknown
months = pd.Series([i for i in range(1,14)] * 10000)


In [199]: %timeit [season_for_month(m) for m in months]
58.3 ms ± 5.26 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [200]: %timeit (((months % 12) // 3).map({0:'winter',1:'spring',2:'summer',3:'fall'}).fillna('unknown'))
14.5 ms ± 286 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to use pandas, the following will work:

import pandas as pd

def season_for_month(month: int) -> str:
    """Returns the season as a string for a given month index.

    Args:
        month: The month index.
    Returns:
        The season for the given month index
    """
    if 3 <= month <= 5:
        return 'spring'
    elif 6 <= month <= 8:
        return 'summer'
    elif 9 <= month <= 11:
        return 'fall'
    elif month <= 2 or month == 12:
        return 'winter'
    else: 
        return 'unknown'

def main():
    months = pd.Series(range(1, 13))
    seasons = [season_for_month(m) for m in months]
    print(f'months = {months}')
    print(f'seasons = {seasons}')

if __name__ == '__main__':
    main()

In order to get the seasons as a list of strings, we need to use a list comprehension, i.e. seasons = [season_for_month(m) for m in months], using our function, season_for_month, that takes a monthly integer and returns the corresponding season.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.