diff --git a/src/hsp2/hsp2/utilities.py b/src/hsp2/hsp2/utilities.py index cde6cad3..0496798e 100644 --- a/src/hsp2/hsp2/utilities.py +++ b/src/hsp2/hsp2/utilities.py @@ -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 @@ -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() @@ -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) diff --git a/src/hsp2/hsp2io/io.py b/src/hsp2/hsp2io/io.py index 089069e9..5290a3d6 100644 --- a/src/hsp2/hsp2io/io.py +++ b/src/hsp2/hsp2io/io.py @@ -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, @@ -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"), @@ -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"), @@ -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"), diff --git a/src/hsp2/hsp2tools/readUCI.py b/src/hsp2/hsp2tools/readUCI.py index 05c9c00e..8d8be3cb 100644 --- a/src/hsp2/hsp2tools/readUCI.py +++ b/src/hsp2/hsp2tools/readUCI.py @@ -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 diff --git a/tests/ZRW_WestIndian/HSPF.WDM b/tests/ZRW_WestIndian/HSPF.WDM deleted file mode 100644 index d20499bd..00000000 Binary files a/tests/ZRW_WestIndian/HSPF.WDM and /dev/null differ diff --git a/tests/ZRW_WestIndian/HSPF.uci b/tests/ZRW_WestIndian/HSPF.uci index c8f03a79..cd6d85ef 100644 --- a/tests/ZRW_WestIndian/HSPF.uci +++ b/tests/ZRW_WestIndian/HSPF.uci @@ -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 @@ -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 diff --git a/tests/ipwater/data/HSPF_Test.wdm b/tests/ipwater/data/HSPF_Test.wdm new file mode 100644 index 00000000..dda4a0c5 Binary files /dev/null and b/tests/ipwater/data/HSPF_Test.wdm differ