diff --git a/metcalcpy/util/utils.py b/metcalcpy/util/utils.py index 492d4584..e6030601 100644 --- a/metcalcpy/util/utils.py +++ b/metcalcpy/util/utils.py @@ -469,6 +469,9 @@ def aggregate_field_values(series_var_val, input_data_frame, line_type): Returns: Pandas DataFrame with aggregates statistics """ + + warnings.filterwarnings('error') + # get unique values for valid date/time and lead time unique_valid = input_data_frame.fcst_valid_beg.unique() unique_lead = input_data_frame.fcst_lead.unique() @@ -507,16 +510,27 @@ def aggregate_field_values(series_var_val, input_data_frame, line_type): rows_for_agg.at[0, series_var] = series_val # add it to the result - input_data_frame = input_data_frame.append(rows_for_agg.iloc[:1]) + input_data_frame = pd.concat([input_data_frame, rows_for_agg.iloc[:1]]) + else: # if the aggregated field is 'fcst_lead' + # pandas treats the fcst_lead values as dtype int64, therefore convert + # the single_values to int type so the pd.isin() properly matches + # the input_data_frame[series_var]. + single_values_int = [] + for val in single_values: + if val != '': + single_values_int.append((int(val))) + # find rows for the aggregation and their indexes rows_for_agg = input_data_frame[ (input_data_frame.fcst_valid_beg == valid) - & (input_data_frame[series_var].isin(single_values)) + & (input_data_frame[series_var].isin(single_values_int)) ] + rows_indexes = rows_for_agg.index.values + # reset indexes rows_for_agg.reset_index(inplace=True, drop=True) @@ -531,10 +545,21 @@ def aggregate_field_values(series_var_val, input_data_frame, line_type): rows_for_agg.at[0, field] = aggregated_result[field][0] # replace the aggregated field name - rows_for_agg.at[0, series_var] = series_val + + # some versions of pandas will generate an error when assigning a numerical value with a ';' + # strip off the ';' to make this "version-independent" + if ';' in series_val: + sep_val = series_val.split(';') + + # different versions of pandas may generate a SettingWithCopy Warning + # For pandas 1.2, 1.3, and 1.5, use the df.at, but for pandas 1.4, use the df.loc. + try: + rows_for_agg.at[0, series_var] = sep_val[0] + except: + rows_for_agg.loc[0, series_var] = sep_val[0] # add it to the result - input_data_frame = input_data_frame.append(rows_for_agg.iloc[:1]) + input_data_frame = pd.concat([input_data_frame,(rows_for_agg.iloc[:1])]) return input_data_frame