Skip to content

Commit 402ab66

Browse files
authored
Merge pull request #173 from lincc-frameworks/setitem-missed-column
Check column exists in NestedFrame.__setitem__
2 parents 96503cd + 1f86e14 commit 402ab66

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/nested_pandas/nestedframe/core.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ def __getitem__(self, item):
202202
nested = item.split(".")[0]
203203
col = ".".join(item.split(".")[1:])
204204
return self[nested].nest.get_flat_series(col)
205+
else:
206+
raise KeyError(f"Column '{item}' not found in nested columns or base columns")
205207
else:
206208
return super().__getitem__(item)
207209

tests/nested_pandas/nestedframe/test_nestedframe.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,3 +1014,10 @@ def test_eval_assignment():
10141014
assert set(nf.p2.nest.fields) == {"e", "f"}
10151015
assert (nf["p2.e"] == nf["packed.d"] * 2 + nf.c).all()
10161016
assert (nf["p2.f"] == nf["p2.e"] + nf.b).all()
1017+
1018+
1019+
def test_access_non_existing_column():
1020+
"""Test that accessing a non-existing column raises a KeyError"""
1021+
nf = NestedFrame()
1022+
with pytest.raises(KeyError):
1023+
_ = nf["non_existing_column"]

0 commit comments

Comments
 (0)