Skip to content

Commit

Permalink
Patch bug in FileContainer.concat: switch to pd.Index.equals() inst…
Browse files Browse the repository at this point in the history
…ead of `is` comparison for equal keys (#145)

* FileContainer.concat: compare keys with `pd.Index.equals` instead of `is`

* test FileContainer.concat with two different FileContainer objects

* add CHANGELOG entry
  • Loading branch information
veni-vidi-vici-dormivi authored Jan 14, 2025
1 parent f896d68 commit 5e06c4b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## v1.0.1 - unreleased

- compare keys of two `FileContainer`s with `pandas.Index.equals()` instead of `is` in `FileContainer.concat()` ([#145](https://github.com/mpytools/filefisher/pull/145)) - this fixes a bug where no two FileContainers that are not the same object could be concatenated together, see [Github issue](https://github.com/mpytools/filefisher/issues/143).

## v1.0.0 - 07.01.2025

Expand Down
4 changes: 2 additions & 2 deletions filefisher/_filefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,13 +714,13 @@ def concat(self, other, drop_duplicates=True):
ValueError
If the other object is not a FileContainer.
ValueError
If the two FileContainers do not have the same keys.
If the two FileContainers do not have the same keys (in the same order).
"""

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

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

df = pd.concat([self.df, other.df])
Expand Down
9 changes: 5 additions & 4 deletions filefisher/tests/test_filecontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,18 @@ def test_filecontainer_concat(example_fc):
with pytest.raises(ValueError, match="Can only concatenate two FileContainers."):
example_fc.concat("not a FileContainer")

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

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

pd.testing.assert_frame_equal(result.df, expected)
assert len(result) == 10

result = example_fc.concat(example_fc, drop_duplicates=True)
result = example_fc.concat(example_fc_2, drop_duplicates=True)
expected = example_fc

pd.testing.assert_frame_equal(result.df, expected.df)
Expand Down

0 comments on commit 5e06c4b

Please sign in to comment.