9 practice
Let’s try to answer the following questions:
- We need to filter our data to contain only night times.
- We need to divide rain data by seasons (3 months), and then take the mean for each season.
a possible solution
- We need to divide our data into two:
rainy_season_1
andrainy_season_2
. - We need to find the time of the last rain in
rainy_season_1
. - We need to find the time of the first rain in
rainy_season_2
. - We need to compute the time difference between the two dates.
a possible solution
split_date = '2019-08-01'
rainy_season_1 = df[:split_date] # everything before split date
rainy_season_2 = df[split_date:] # everything after split date
malkosh = rainy_season_1['rain'].loc[rainy_season_1['rain'] > 0].last_valid_index()
yoreh = rainy_season_2['rain'].loc[rainy_season_2['rain'] > 0].first_valid_index()
dry_period = yoreh - malkosh
# extracting days, hours, and minutes
days = dry_period.days
hours = dry_period.components.hours
minutes = dry_period.components.minutes
print(f'The dry period of 2019 was {days} days, {hours} hours and {minutes} minutes.')
- We need to filter our data to contain only morning times.
- We need to sum rain by day.
- We need to find the day with the maximum value.
a possible solution
bonus solution
# filter to only night data
df_night = df.loc[((df.index.hour < 6) | (df.index.hour >= 18))]
# resampling night for each day is tricky because the date changes at 12:00. We can do this trick:
# we shift the time back by 6 hours so all the data for the same night will have the same date.
df_shifted = df_night.tshift(-6, freq='H')
night_rain = df_shifted['rain'].resample('D').sum()
rainiest_night = night_rain.idxmax()
# plot
night_rain.plot()
plt.axvline(rainiest_night, c='r', alpha=0.5, linestyle='--')
Note: this whole webpage is actually a Jupyter Notebook rendered as html. If you want to know how to make interactive graphs, go to the top of the page and click on “