From 83ecb7f0865b7bc896e150367791566cd0844d3a Mon Sep 17 00:00:00 2001 From: veenstrajelmer <60435591+veenstrajelmer@users.noreply.github.com> Date: Tue, 12 Mar 2024 16:15:42 +0100 Subject: [PATCH] 25 better error message when providing multiple locations to ddlpymeasurements (#78) * added typerror for df instead of series locations in measurements * added test * speed up testbank by retrieving locations only once --- ddlpy/ddlpy.py | 5 +++++ tests/test_ddlpy.py | 23 +++++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/ddlpy/ddlpy.py b/ddlpy/ddlpy.py index bc50e60..b9b9f03 100644 --- a/ddlpy/ddlpy.py +++ b/ddlpy/ddlpy.py @@ -301,6 +301,11 @@ def _measurements_slice(location, start_date, end_date): def measurements(location, start_date, end_date, clean_df=True): """return measurements for the given location and time window (start_date, end_date)""" + + if isinstance(location, pd.DataFrame): + raise TypeError("The provided location is a pandas.DataFrame, but should be a pandas.Series, " + "supply only one location/row instead, for instance by doing 'location.iloc[0]'") + start_date, end_date = _check_convert_dates(start_date, end_date, return_str=False) measurements = [] diff --git a/tests/test_ddlpy.py b/tests/test_ddlpy.py index 07978b7..f42e3d5 100755 --- a/tests/test_ddlpy.py +++ b/tests/test_ddlpy.py @@ -10,21 +10,26 @@ from ddlpy import cli -def test_locations(): +@pytest.fixture +def locations(): + """return all locations""" locations = ddlpy.locations() - assert locations.shape[0] > 1 + return locations @pytest.fixture -def location(): +def location(locations): """return sample location""" - locations = ddlpy.locations() bool_grootheid = locations['Grootheid.Code'] == 'WATHTE' bool_groepering = locations['Groepering.Code'] == 'NVT' location = locations[bool_grootheid & bool_groepering].loc['DENHDR'] return location +def test_locations(locations): + assert locations.shape[0] > 1 + + def test_measurements_available(location): start_date = dt.datetime(1953, 1, 1) end_date = dt.datetime(1953, 4, 1) @@ -54,6 +59,13 @@ def test_measurements_empty(location): assert measurements.empty +def test_measurements_typerror(locations): + start_date = dt.datetime(1953, 1, 1) + end_date = dt.datetime(1953, 4, 1) + with pytest.raises(TypeError): + _ = ddlpy.measurements(locations, start_date=start_date, end_date=end_date) + + def test_measurements(location): """measurements for a location """ start_date = dt.datetime(1953, 1, 1) @@ -107,13 +119,12 @@ def test_measurements_sorted(location): assert isinstance(meas_wathte_raw.index, pd.DatetimeIndex) -def test_measurements_duplicated(location): +def test_measurements_duplicated(locations): """ WALSODN 2010 contains all values three times, ddlpy drops duplicates https://github.com/deltares/ddlpy/issues/24 if the data is cleaned in ddl, this test will fail and can be removed or adjusted """ - locations = ddlpy.locations() location = locations[locations['Grootheid.Code'] == 'WATHTE'].loc['WALSODN'] start_date = dt.datetime(2010, 1, 1) end_date = dt.datetime(2010, 1, 1, 0, 20)