Skip to content

Commit

Permalink
fix for #223
Browse files Browse the repository at this point in the history
  • Loading branch information
OnnoEbbens committed Jul 12, 2024
1 parent cd4d492 commit cc779f5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
27 changes: 26 additions & 1 deletion hydropandas/obs_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,31 @@ def read_pastastore(
)


def _obscollection_constructor_with_fallback(*args, **kwargs):
"""
A flexible constructor for ObsCollection._constructor, which falls back
to returning a DataFrame (if a certain operation does not preserve the
obs column). Copied from geopandas.
"""
oc = ObsCollection(*args, **kwargs)
if 'obs' not in oc.columns:
oc = pd.DataFrame(oc)

return oc


def is_observation_type(data):
"""
Check if the data is of geometry dtype.
Does not include object array of shapely scalars.
"""
if isinstance(getattr(data, "dtype", None), GeometryDtype):
# GeometryArray, GeoSeries and Series[GeometryArray]
return True
else:
return False

class ObsCollection(pd.DataFrame):
"""Class for a collection of point observations.
Expand Down Expand Up @@ -871,7 +896,7 @@ def __init__(self, *args, **kwargs):

@property
def _constructor(self):
return ObsCollection
return _obscollection_constructor_with_fallback

def _infer_otype(self):
"""Infer observation type from the obs column.
Expand Down
11 changes: 11 additions & 0 deletions tests/test_002_obs_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ def test_copy_obs():
assert "answer" in o3.meta.keys(), "copy method failed"


def test_returns():
# check if a DataFrame is returned when an ObsCollection is sliced without the
# 'obs' column
oc = _obscollection_from_list()

assert isinstance(oc.loc[:, ["x", "y"]], pd.DataFrame)
assert not isinstance(oc.loc[:, ["x", "y"]], hpd.ObsCollection)

assert isinstance(oc.loc[:, ["x", "y", "obs"]], hpd.ObsCollection)


def test_convert_waterlvl_groundwater_obs():
# create WaterlvlObs
df = pd.DataFrame(
Expand Down

0 comments on commit cc779f5

Please sign in to comment.