Skip to content

Commit

Permalink
Issue #249 address FutureWarnings and address compatibility issues di…
Browse files Browse the repository at this point in the history
…scovered with pandas versions 1.2, 1.3, and 1.5 vs 1.4
  • Loading branch information
bikegeek committed Jan 6, 2023
1 parent 817e2d3 commit 39580ab
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions metcalcpy/util/utils.py
Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand All @@ -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

Expand Down

0 comments on commit 39580ab

Please sign in to comment.