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
Changes from 2 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
4 changes: 2 additions & 2 deletions filefisher/_filefinder.py
Original file line number Diff line number Diff line change
@@ -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])
9 changes: 5 additions & 4 deletions filefisher/tests/test_filecontainer.py
Original file line number Diff line number Diff line change
@@ -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)