Skip to content

Commit

Permalink
Added "pandas_offset_by_version" to address deprecated offset codes.
Browse files Browse the repository at this point in the history
* Some pandas offset codes changed in pandas 2.2.  If <2.2 offset
  codes are with >2.2 throws deprecation warnings and if >2.2 codes
  used with <2.2 throws value errors.  Using the new
  "pandas_offset_by_version" function will used the >2.2 codes and
  change to the older codes if run with pandas <2.2.
* Fixed to_numeric(..., errors="ignore", ...) deprecation warnings.
* Fixed resample(..., kind="timestamp", ...) deprecation warnings.
* Removed unused FILES block entry for WDM2 and fixed comments
  ("***" has to be in columns less than 80) in
  tests/ZRW_WestIndian/HSPF.uci and removed the unused
  tests/ZRW_WestIndian/HSPF.WDM.
* Added missing WDM file in tests/ipwater.
  • Loading branch information
timcera committed Dec 15, 2024
1 parent e99feb6 commit d019b19
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 16 deletions.
54 changes: 52 additions & 2 deletions src/hsp2/hsp2/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def transform(ts, name, how, siminfo):

if freq == tsfreq:
pass
elif tsfreq == None: # Sparse time base, frequency not defined
elif tsfreq is None: # Sparse time base, frequency not defined
ts = ts.reindex(siminfo["tbase"]).ffill().bfill()
elif how == "SAME":
ts = ts.resample(freq).ffill() # tsfreq >= freq assumed, or bad user choice
Expand Down Expand Up @@ -618,7 +618,7 @@ def get_gener_timeseries(
if link.SVOLNO in gener_instances:
gener = gener_instances[link.SVOLNO]
series = zeros(len(gener.ts_output)) + gener.ts_output
if type(link.MFACTOR) == float and link.MFACTOR != 1:
if isinstance(link.MFACTOR, float) and link.MFACTOR != 1:
series *= link.MFACTOR

key = f"{link.TMEMN}{link.TMEMSB1} {link.TMEMSB2}".rstrip()
Expand Down Expand Up @@ -700,3 +700,53 @@ def clean_name(TMEMN, TMEMSB):
tname = TMEMN + "1"

return tname


def pandas_offset_by_version(new_offset: str) -> str:
"""
Convert the time offset code to match the version of pandas.
Parameters
----------
offset
The offset to convert.
Returns
-------
offset_by_version
The offset converted to the correct version of pandas.
"""
new_to_old_freq = {}
major, minor = pd.__version__.split(".")[:2]
if (int(major) + int(minor) / 10) < 2.2:
new_to_old_freq = {
"Y": "A",
"ME": "M",
"BME": "BM",
"SME": "SM",
"CBME": "CBM",
"QE": "Q",
"BQE": "BQ",
"BYE": "BY",
"h": "H",
"bh": "BH",
"cbh": "CBH",
"min": "T",
"s": "S",
"ms": "L",
"us": "U",
"ns": "N",
"YE-JAN": "A-JAN",
"YE-FEB": "A-FEB",
"YE-MAR": "A-MAR",
"YE-APR": "A-APR",
"YE-MAY": "A-MAY",
"YE-JUN": "A-JUN",
"YE-JUL": "A-JUL",
"YE-AUG": "A-AUG",
"YE-SEP": "A-SEP",
"YE-OCT": "A-OCT",
"YE-NOV": "A-NOV",
"YE-DEC": "A-DEC",
}
return new_to_old_freq.get(new_offset, new_offset)
35 changes: 25 additions & 10 deletions src/hsp2/hsp2io/io.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import List, Union

import pandas as pd
from pandas.core.frame import DataFrame

from hsp2.hsp2.uci import UCI
from hsp2.hsp2.utilities import pandas_offset_by_version
from hsp2.hsp2io.protocols import (
Category,
SupportsReadTS,
Expand Down Expand Up @@ -86,11 +86,14 @@ def write_ts(
if drop_columns:
data_frame = data_frame.drop(columns=drop_columns)

if not isinstance(data_frame.index, pd.core.indexes.datetimes.DatetimeIndex):
data_frame = data_frame.to_timestamp()

if outstep == 3:
# change time step of output to daily
sumdf1 = data_frame.resample("D", kind="timestamp", origin="start").sum()
lastdf2 = data_frame.resample("D", kind="timestamp", origin="start").last()
meandf3 = data_frame.resample("D", kind="timestamp", origin="start").mean()
sumdf1 = data_frame.resample("D", origin="start").sum()
lastdf2 = data_frame.resample("D", origin="start").last()
meandf3 = data_frame.resample("D", origin="start").mean()
data_frame = pd.merge(
lastdf2.add_suffix("_last"),
sumdf1.add_suffix("_sum"),
Expand All @@ -105,9 +108,15 @@ def write_ts(
)
elif outstep == 4:
# change to monthly
sumdf1 = data_frame.resample("M", kind="timestamp", origin="start").sum()
lastdf2 = data_frame.resample("M", kind="timestamp", origin="start").last()
meandf3 = data_frame.resample("M", kind="timestamp", origin="start").mean()
sumdf1 = data_frame.resample(
pandas_offset_by_version("ME"), origin="start"
).sum()
lastdf2 = data_frame.resample(
pandas_offset_by_version("ME"), origin="start"
).last()
meandf3 = data_frame.resample(
pandas_offset_by_version("ME"), origin="start"
).mean()
data_frame = pd.merge(
lastdf2.add_suffix("_last"),
sumdf1.add_suffix("_sum"),
Expand All @@ -122,9 +131,15 @@ def write_ts(
)
elif outstep == 5:
# change to annual
sumdf1 = data_frame.resample("Y", kind="timestamp", origin="start").sum()
lastdf2 = data_frame.resample("Y", kind="timestamp", origin="start").last()
meandf3 = data_frame.resample("Y", kind="timestamp", origin="start").mean()
sumdf1 = data_frame.resample(
pandas_offset_by_version("YE"), origin="start"
).sum()
lastdf2 = data_frame.resample(
pandas_offset_by_version("YE"), origin="start"
).last()
meandf3 = data_frame.resample(
pandas_offset_by_version("YE"), origin="start"
).mean()
data_frame = pd.merge(
lastdf2.add_suffix("_last"),
sumdf1.add_suffix("_sum"),
Expand Down
6 changes: 5 additions & 1 deletion src/hsp2/hsp2tools/readUCI.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ def fix_df(df, op, save, ddfaults, valid):
pass
cols = [c.replace("(", "").replace(")", "") for c in df.columns]
df.columns = cols
df = df.apply(pd.to_numeric, errors="ignore") # todo: 'ignore' is deprecated.
for col in df.columns:
try:
df[col] = pd.to_numeric(df[col])
except ValueError:
pass
return df


Expand Down
Binary file removed tests/ZRW_WestIndian/HSPF.WDM
Binary file not shown.
5 changes: 2 additions & 3 deletions tests/ZRW_WestIndian/HSPF.uci
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ FILES
MESSU 24 HSPF.ECH
25 HSPF.OUT
WDM1 26 Met4.WDM
WDM2 27 HSPF.WDM
WDM3 28 ZUMBROSCEN.WDM
BINO 92 HSPF.HBN
END FILES
Expand Down Expand Up @@ -4378,8 +4377,8 @@ RCHRES
END PLNK-PARM1

PLNK-PARM2
RCHRES CMMLT CMMN CMMNP CMMP TALGRH TALGRL TALGRM ***
# - # y/min mg/l mg/l mg/l deg F deg F degF ***
*** RCHRES CMMLT CMMN CMMNP CMMP TALGRH TALGRL TALGRM
*** # - # y/min mg/l mg/l mg/l deg F deg F degF
1 109 .033 0.045 .01 .015 95. 20. 77
110 .033 0.045 .005 .025 95. 20. 67
111 506 .033 0.045 .01 .015 95. 20. 77
Expand Down
Binary file added tests/ipwater/data/HSPF_Test.wdm
Binary file not shown.

0 comments on commit d019b19

Please sign in to comment.