Skip to content

Commit

Permalink
25 better error message when providing multiple locations to ddlpymea…
Browse files Browse the repository at this point in the history
…surements (#78)

* added typerror for df instead of series locations in measurements

* added test

* speed up testbank by retrieving locations only once
  • Loading branch information
veenstrajelmer authored Mar 12, 2024
1 parent aad270f commit 83ecb7f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
5 changes: 5 additions & 0 deletions ddlpy/ddlpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
23 changes: 17 additions & 6 deletions tests/test_ddlpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 83ecb7f

Please sign in to comment.