From 7ece1804d7c25709b88b2b977c5b5b648f71ce42 Mon Sep 17 00:00:00 2001 From: rettigl Date: Fri, 22 Sep 2023 22:42:38 +0200 Subject: [PATCH] add type checking for dataframe setter --- sed/core/processor.py | 9 +++++++++ tests/test_processor.py | 2 ++ 2 files changed, 11 insertions(+) diff --git a/sed/core/processor.py b/sed/core/processor.py index 2075ae1d..d95a7e5d 100644 --- a/sed/core/processor.py +++ b/sed/core/processor.py @@ -173,6 +173,15 @@ def dataframe(self, dataframe: Union[pd.DataFrame, ddf.DataFrame]): Args: dataframe (Union[pd.DataFrame, ddf.DataFrame]): The dataframe object to set. """ + if not isinstance(dataframe, (pd.DataFrame, ddf.DataFrame)) or not isinstance( + dataframe, + self._dataframe.__class__, + ): + raise ValueError( + "'dataframe' has to be a Pandas or Dask dataframe and has to be of the same kind " + "as the dataframe loaded into the SedProcessor!.\n" + f"Loaded type: {self._dataframe.__class__}, provided type: {dataframe}.", + ) self._dataframe = dataframe @property diff --git a/tests/test_processor.py b/tests/test_processor.py index 2d75d865..d7609895 100644 --- a/tests/test_processor.py +++ b/tests/test_processor.py @@ -155,6 +155,8 @@ def test_attributes_setters(): processor.load(files=files, metadata={"test": {"key1": "value1"}}) dataframe = processor.dataframe assert isinstance(dataframe, ddf.DataFrame) + with pytest.raises(ValueError): + processor.dataframe = dataframe["X"] dataframe["X"] = dataframe["Y"] processor.dataframe = dataframe np.testing.assert_allclose(