Which month has the highest median for maximum_gust_speed out of all the available records

Viewed 1545

Which month has the highest median for maximum_gust_speed out of all the available records. Also find the respective value

The data set looks like below

Day Average temperature (°F)    Average humidity (%)    Average dewpoint (°F)   Average barometer (in)  Average windspeed (mph) Average gustspeed (mph) Average direction (°deg)    Rainfall for month (in) Rainfall for year (in)  Maximum rain per minute Maximum temperature (°F)    Minimum temperature (°F)    Maximum humidity (%)    Minimum humidity (%)    Maximum pressure    Minimum pressure    Maximum windspeed (mph) Maximum gust speed (mph)    Maximum heat index (°F)
0   1/01/2009   37.8    35  12.7    29.7    26.4    36.8    274 0.0 0.0 0.0 40.1    34.5    44  27  29.762  29.596  41.4    59.0    40.1
1   2/01/2009   43.2    32  14.7    29.5    12.8    18.0    240 0.0 0.0 0.0 52.8    37.5    43  16  29.669  29.268  35.7    51.0    52.8
2   3/01/2009   25.7    60  12.7    29.7    8.3 12.2    290 0.0 0.0 0.0 41.2    6.7 89  35  30.232  29.260  25.3    38.0    41.2
3   4/01/2009   9.3 67  0.1 30.4    2.9 4.5 47  0.0 0.0 0.0 19.4    -0.0    79  35  30.566  30.227  12.7    20.0    32.0
4   5/01/2009   23.5    30  -5.3    29.9    16.7    23.1    265 0.0 0.0 0.0 30.3    15.1    56  13  30.233  29.568  38.0    53.0    32.0

The code I have written is as below however the test case fails Code :

data1= data[data['Maximum gust speed (mph)']!= 0.0]
#print(data1.count())
#print(data.count())
#print(data.median())
#print(data1.median())
max_gust_value_median = data1.groupby(pd.DatetimeIndex(data1['Day']).month).agg({'Maximum gust speed (mph)':pd.Series.median})
#print(max_gust_value_median)
max_gust_month = "max_gust_month = " + str(max_gust_value_median.idxmax()[0])
max_gust_value = "max_gust_value = " + format((max_gust_value_median.max()[0]),'.2f')
print(max_gust_value)
print(max_gust_month)

Output : max_gust_value = 32.20 max_gust_month = 11

Error : =================================== FAILURES ===================================

_____________________________ test_max_gust_month ______________________________

def test_max_gust_month():
  assert hash_dict["max_gust_month"] == answer_dict["max_gust_month"]

E AssertionError: assert 'd1aecb72eff6...7412c2a651d81' == 'e6e3cedb0dc6...798711404a6c8'

E - e6e3cedb0dc67a96317798711404a6c8

E + d1aecb72eff64d1169f7412c2a651d81

test.py:52: AssertionError

_____________________________ test_max_gust_value ______________________________

def test_max_gust_value():
  assert hash_dict["max_gust_value"] == answer_dict["max_gust_value"]

E AssertionError: assert '6879064548a1...2361f91ecd7b0' == '5818ebe448c4...471e93c92d545'

E - 5818ebe448c43f2dfed471e93c92d545

E + 6879064548a136da2f22361f91ecd7b0

test.py:55: AssertionError

=========================== short test summary info ============================

FAILED test.py::test_max_gust_month - AssertionError: assert 'd1aecb72eff6......

FAILED test.py::test_max_gust_value - AssertionError: assert '6879064548a1......

========================= 2 failed, 9 passed in 0.13s ==========================

2 Answers

Below code

data['Month'] = pd.to_datetime(data['Day'], dayfirst=True).dt.strftime('%B')
month_list =['January', 'February','March','April', 'May', 'June','July','August','September','October','November','December']
month_grp = data.groupby(['Month'])
month_name_value_all = []
max_value=[]
for i in month_list:
    month_name_value =[]
    value = month_grp.get_group(i).median().loc['Maximum gust speed (mph)']
    month_name_value.append(i)
    max_value.append(value)
    month_name_value.append(value)
    month_name_value_all.append(month_name_value)

max_gust_value = format(max(max_value), '.2f')

for j in month_name_value_all:
    month_max_find =[]
    month_max_find.append(j)
    if max_gust_value in j:
        break

max_gust_month = month_max_find[0][0]

print("max_gust_value =   ", max_gust_value) 
print("max_gust_month =   ", max_gust_month)

You can try this way:

#Convert day column values to datetime
df['Date'] = pd.to_datetime(df['Day'],format = '%d/%m/%Y')

#Convert a new column month_index
df['month_index'] = df['Date'].dt.month

#Group the dataframe by month & then find the median for max gust speed
max_gust_month = df.groupby(['month_index'])
max_gust_month = max_gust_month['Maximum gust speed (mph)'].median()

#Find max value in the month
max_gust_value = max_gust_median_month.max()
max_gust_value

#Find the max value index in the month
max_gust_month = max_gust_median_month.idxmax()
max_gust_month
Related