Reputation: 1033
I am trying to extract the seasons from a large dataframe with a date time column. This is the code I have used:
def season_of_date(date_UTC):
year = str(date_UTC.year)
seasons = {'spring': pd.date_range(start= year +'-03-21 00:00:00', end=year + '-06-20 00:00:00'),
'summer': pd.date_range(start= year + '-06-21 00:00:00', end= year + '-09-22 00:00:00'),
'autumn': pd.date_range(start= year + '-09-23 00:00:00', end= year + '-12-20 00:00:00')}
if date_UTC in seasons['spring']:
return 'spring'
if date_UTC in seasons['summer']:
return 'summer'
if date_UTC in seasons['autumn']:
return 'autumn'
else:
return 'winter'
df['season'] = df.date_UTC.map(season_of_date)
The issue lies in the fact that I dont know how to handle the hours minutes and seconds in my datetime column, so I end up with a result that is mostly winter, apart from datetime entries when the time is 00:00:00:
date_UTC season
616602 2019-11-24 17:00:00 winter
792460 2019-06-18 13:00:00 winter
230088 2019-11-30 07:00:00 winter
560826 2019-05-20 08:00:00 winter
718547 2019-03-23 04:00:00 winter
241890 2020-01-11 03:00:00 winter
513845 2018-12-23 22:00:00 winter
665954 2019-03-18 00:00:00 winter
474988 2019-05-20 08:00:00 winter
120281 2019-04-22 12:00:00 winter
697519 2018-10-12 05:00:00 winter
669144 2019-09-10 11:00:00 winter
310637 2019-11-03 04:00:00 winter
127973 2018-12-01 10:00:00 winter
325177 2019-03-16 11:00:00 winter
785162 2019-05-07 21:00:00 winter
840131 2018-11-24 00:00:00 autumn
580472 2020-01-10 19:00:00 winter
635219 2019-12-16 23:00:00 winter
799642 2019-11-11 18:00:00 winter
Can I have some advice on how to modify my code so that the seasons map correctly?
UPDATE:
I modified the code to create a string for the timestamp element and thought this would fix the issue but it didnt.. After making the modification like so I end up with this error:
def season_of_date(date_UTC):
year = str(date_UTC.year)
time = str(date_UTC.time)
seasons = {'spring': pd.date_range(start= year +'-03-21' + time, end=year + '-06-20' + time),
'summer': pd.date_range(start= year + '-06-21' + time, end= year + '-09-22' + time),
'autumn': pd.date_range(start= year + '-09-23' + time, end= year + '-12-20' + time)}
if date_UTC in seasons['spring']:
return 'spring'
if date_UTC in seasons['summer']:
return 'summer'
if date_UTC in seasons['autumn']:
return 'autumn'
else:
return 'winter'
df['season'] = df.date_UTC.map(season_of_date)
ValueError: could not convert string to Timestamp
SECOND UPDATE:
What I ended up doing was the following, it is fast but I don't like the solution since it wrongly groups whole months into seasons, when actually for a given year a season may start mid way through a month.
df['season'] = (df['date_UTC'].dt.month%12 + 3)//3
seasons = {
1: 'Winter',
2: 'Spring',
3: 'Summer',
4: 'Autumn'
}
df['season_name'] = df['season'].map(seasons)
Upvotes: 2
Views: 6576
Reputation: 150765
first you want your date_UTC
in datetime
format, second, you can use pd.cut
:
date = df.date_UTC.dt.month*100 + df.date_UTC.dt.day
df['season'] = (pd.cut(date,[0,321,620,922,1220,1300],
labels=['winter','spring','summer','autumn','winter '])
.str.strip()
)
With a little numeric trick, you can get rid of the slow str.strip()
:
df['date_offset'] = (df.date_UTC.dt.month*100 + df.date_UTC.dt.day - 320)%1300
df['season'] = pd.cut(df['date_offset'], [0, 300, 602, 900, 1300],
labels=['spring', 'summer', 'autumn', 'winter'])
Upvotes: 7