Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add capability to concat two FileContainers #126

Merged
merged 12 commits into from
Nov 7, 2024
31 changes: 31 additions & 0 deletions filefinder/_filefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,37 @@ def search(self, **query):
df = self._get_subset(**query)
return type(self)(df)

def concat(self, other):
"""concatenate two FileContainers

Parameters
----------
other : FileContainer
The other FileContainer to concatenate.

Returns
-------
FileContainer
The concatenated FileContainer.

Raises
------
ValueError
If the other object is not a FileContainer.
ValueError
If the two FileContainers do not have the same keys.
"""

if not isinstance(other, FileContainer):
raise ValueError("Can only concatenate two FileContainers.")

if self.df.columns is not other.df.columns:
raise ValueError("FileContainers must have the same keys.")

ret = copy.copy(self)
ret.df = pd.concat([self.df, other.df])
return ret
veni-vidi-vici-dormivi marked this conversation as resolved.
Show resolved Hide resolved

def _get_subset(self, **query):
if not query:
return pd.DataFrame(
Expand Down
16 changes: 16 additions & 0 deletions filefinder/tests/test_filecontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,22 @@ def test_filecontainer_search(example_df, example_fc):
pd.testing.assert_frame_equal(result.df, expected)


def test_filecontainer_concat(example_fc):

with pytest.raises(ValueError, match="Can only concatenate two FileContainers."):
example_fc.concat("not a FileContainer")

with pytest.raises(ValueError, match="FileContainers must have the same keys"):
different_keys_fc = FileContainer(example_fc.df.loc[:, ["model", "scen"]])
example_fc.concat(different_keys_fc)

result = example_fc.concat(example_fc)
expected = pd.concat([example_fc.df, example_fc.df])

pd.testing.assert_frame_equal(result.df, expected)
assert result.__len__() == 10
veni-vidi-vici-dormivi marked this conversation as resolved.
Show resolved Hide resolved


def test_fc_combine_by_key_deprecated(example_fc):

with pytest.warns(FutureWarning, match="`combine_by_key` has been deprecated"):
Expand Down