Skip to content

Commit

Permalink
start on from_dataframe()
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbjoernl committed Oct 9, 2024
1 parent 501f033 commit 23809dc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
32 changes: 28 additions & 4 deletions pyaerocom/colocation/colocated_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1330,17 +1330,41 @@ def to_dataframe(self):

return df

def from_dataframe(self, df):
@staticmethod
def _validate_dataframe_for_import(df: pd.DataFrame):
"""Validates a pandas dataframe and checks that it will likely
work with ColocatedData.from_dataframe()
:param df: The pandas dataframe to be validated.
"""
if not isinstance(df, pd.DataFrame):
raise TypeError(f"Expected pandas DataFrame, got {type(df)}")

Check warning on line 1341 in pyaerocom/colocation/colocated_data.py

View check run for this annotation

Codecov / codecov/patch

pyaerocom/colocation/colocated_data.py#L1341

Added line #L1341 was not covered by tests

if (tmp := df.shape[1]) != 9:
raise ValueError(f"Expected DataFrame with 9 columns, got {tmp}")

Check warning on line 1344 in pyaerocom/colocation/colocated_data.py

View check run for this annotation

Codecov / codecov/patch

pyaerocom/colocation/colocated_data.py#L1344

Added line #L1344 was not covered by tests

if (tmp := len(df["data_source_obs"].unique())) != 1:
raise ValueError(f"Expected dataframe with 1 unique data_source_obs, got {tmp}.")

Check warning on line 1347 in pyaerocom/colocation/colocated_data.py

View check run for this annotation

Codecov / codecov/patch

pyaerocom/colocation/colocated_data.py#L1347

Added line #L1347 was not covered by tests

if (tmp := len(df["data_source_mod"].unique())) != 1:
raise ValueError(f"Expected dataframe with 1 unique data_source_mod, got {tmp}.")

Check warning on line 1350 in pyaerocom/colocation/colocated_data.py

View check run for this annotation

Codecov / codecov/patch

pyaerocom/colocation/colocated_data.py#L1350

Added line #L1350 was not covered by tests

# TODO: Check that required columns exist.
if "time" not in set(df.columns):
raise ValueError("Missing column '{time}'")

Check warning on line 1354 in pyaerocom/colocation/colocated_data.py

View check run for this annotation

Codecov / codecov/patch

pyaerocom/colocation/colocated_data.py#L1354

Added line #L1354 was not covered by tests

# ...

@staticmethod
def from_dataframe(df: pd.DataFrame) -> ColocatedData:
"""Create colocated Data object from dataframe
Note
----
This is intended to be used as back-conversion from :func:`to_dataframe`
and methods that use the latter (e.g. :func:`to_csv`).
"""
raise NotImplementedError("Coming soon...")
data = df.to_xarray()
self.data = data
ColocatedData._validate_dataframe_for_import(df)

def to_csv(self, out_dir, savename=None):
"""Save data object as .csv file
Expand Down
20 changes: 20 additions & 0 deletions tests/colocation/test_colocated_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,3 +549,23 @@ def test_ColocatedData_to_dataframe(coldata: ColocatedData):
def test_ColocatedData_to_dataframe_exception(coldata: ColocatedData):
with pytest.raises(NotImplementedError):
coldata.to_dataframe()


@pytest.mark.parametrize(
"coldataset",
(
pytest.param(
"tm5_aeronet",
),
pytest.param(
"fake_3d_hr",
),
pytest.param(
"fake_3d",
),
),
)
def test_ColocatedData_from_dataframe(coldata: ColocatedData):
df = coldata.to_dataframe()

ColocatedData.from_dataframe(df)

0 comments on commit 23809dc

Please sign in to comment.