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

Check column exists in NestedFrame.__setitem__ #173

Merged
merged 1 commit into from
Nov 6, 2024
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
2 changes: 2 additions & 0 deletions src/nested_pandas/nestedframe/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ def __getitem__(self, item):
nested = item.split(".")[0]
col = ".".join(item.split(".")[1:])
return self[nested].nest.get_flat_series(col)
else:
raise KeyError(f"Column '{item}' not found in nested columns or base columns")
else:
return super().__getitem__(item)

Expand Down
7 changes: 7 additions & 0 deletions tests/nested_pandas/nestedframe/test_nestedframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,3 +1014,10 @@ def test_eval_assignment():
assert set(nf.p2.nest.fields) == {"e", "f"}
assert (nf["p2.e"] == nf["packed.d"] * 2 + nf.c).all()
assert (nf["p2.f"] == nf["p2.e"] + nf.b).all()


def test_access_non_existing_column():
"""Test that accessing a non-existing column raises a KeyError"""
nf = NestedFrame()
with pytest.raises(KeyError):
_ = nf["non_existing_column"]