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

Patch bug in FileContainer.concat: switch to pd.Index.equals() instead of is comparison for equal keys #145

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading