Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Datetime to season

I want to convert a date time series to season, for example for months 3, 4, 5 I want to replace them with 2 (spring); for months 6, 7, 8 I want to replace them with 3 (summer) etc.

So, I have this series

id
1       2011-08-20
2       2011-08-23
3       2011-08-27
4       2011-09-01
5       2011-09-05
6       2011-09-06
7       2011-09-08
8       2011-09-09
Name: timestamp, dtype: datetime64[ns]

and this is the code I have been trying to use, but to no avail.

# Get seasons
spring = range(3, 5)
summer = range(6, 8)
fall = range(9, 11)
# winter = everything else

month = temp2.dt.month
season=[]

for _ in range(len(month)):
    if any(x == spring for x in month):
       season.append(2) # spring 
    elif any(x == summer for x in month):
        season.append(3) # summer
    elif any(x == fall for x in month):
        season.append(4) # fall
    else:
        season.append(1) # winter

and

for _ in range(len(month)):
    if month[_] == 3 or month[_] == 4 or month[_] == 5:
        season.append(2) # spring 
    elif month[_] == 6 or month[_] == 7 or month[_] == 8:
        season.append(3) # summer
    elif month[_] == 9 or month[_] == 10 or month[_] == 11:
        season.append(4) # fall
    else:
        season.append(1) # winter

Neither solution works, specifically in the first implementation I receive an error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

While in the second is a large list with errors. Any ideas please? Thanks

like image 477
Jespar Avatar asked May 23 '17 01:05

Jespar


4 Answers

You can use a simple mathematical formula to compress a month to a season, e.g.:

>>> [month%12 // 3 + 1 for month in range(1, 13)]
[1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 1]

So for your use-case using vector operations (credit @DSM):

>>> temp2.dt.month%12 // 3 + 1
1    3
2    3
3    3
4    4
5    4
6    4
7    4
8    4
Name: id, dtype: int64
like image 162
AChampion Avatar answered Oct 25 '22 19:10

AChampion


It's, also, possible to use dictionary mapping.

  1. Create a dictionary that maps a month to a season:

    In [27]: seasons = [1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 1]
    
    In [28]: month_to_season = dict(zip(range(1,13), seasons))
    
    In [29]: month_to_season 
    Out[29]: {1: 1, 2: 1, 3: 2, 4: 2, 5: 2, 6: 3, 7: 3, 8: 3, 9: 4, 10: 4, 11: 4, 12: 1}
    
  2. Use it to convert the months to seasons

    In [30]: df.id.dt.month.map(month_to_season) 
    Out[30]: 
    1    3
    2    3
    3    3
    4    4
    5    4
    6    4
    7    4
    8    4
    Name: id, dtype: int64
    

Performance: This is fairly fast

In [35]: %timeit df.id.dt.month.map(month_to_season) 
1000 loops, best of 3: 422 µs per loop
like image 22
Mohamed Ali JAMAOUI Avatar answered Oct 25 '22 19:10

Mohamed Ali JAMAOUI


I think a more precise solution may be useful. If we have a month (1, ..., 12), we can convert it to season decreasing one and dividing by 3,

df = pd.Series(["2011-06-07", 
                "2011-08-23", 
                "2011-08-27", 
                "2011-09-01", 
                "2011-09-05", 
                "2011-09-06", 
                "2011-09-08", 
                "2011-12-25"])
 df = pd.to_datetime(df)

 season = (df.dt.month - 1) // 3

Therefore we will be mapping 1,2,3 to 0 (winter), 4,5,6 to 1 (spring), 7,8,9 to 2 (summer), and 10,11,12 to 3 (fall). However, we know the months 3,6,9, and 12 divide two seasons each. I propose the following approach:

If the month is 3 and the day is greater or equal 20, the season is spring, and we need to sum 1. If the month is 6 and the day is greater or equal 21, the season is summer, and we need to sum 1. If the month is 9 and the day is greater or equal 23, the season is fall, and we need to sum 1. If the month is 3 and the day is greater or equal 20, the season is winter, and we need to decrease 3 (or sum +1 in modulus 4). Then we have

season += (df.dt.month == 3)&(df.dt.day>=20)
season += (df.dt.month == 6)&(df.dt.day>=21)
season += (df.dt.month == 9)&(df.dt.day>=23)
season -= 3*((df.dt.month == 12)&(df.dt.day>=21)).astype(int)

The solution for this series will be [1,2,2,2,2,2,2,0].

like image 3
Lucas Machado Moschen Avatar answered Oct 25 '22 17:10

Lucas Machado Moschen


I think this would work.

while True:
date=int(input("Date?"))
season=""
if date<4:
    season=1
elif date<7:
    season=2
elif date<10:
    season=3
elif date<13:
    season=4
else:
    print("This would not work.")
print(season)
like image 1
John Hao Avatar answered Oct 25 '22 17:10

John Hao