Skip to content

Commit 127f913

Browse files
authored
284 series methods on nest (#342)
* Nests should fallback to default behavior when dot does not indicate a field * Verify issue #284 is resolved
1 parent 16dc69e commit 127f913

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

.setup_dev.sh

100644100755
File mode changed.

src/nested_pandas/nestedframe/expr.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,7 @@ def __contains__(self, item):
109109

110110
def __getitem__(self, item):
111111
top_nest = item if "." not in item else item.split(".")[0].strip()
112-
if not super().__contains__(top_nest):
113-
if top_nest not in self._outer.nested_columns:
114-
raise KeyError(f"Unknown nest {top_nest}")
112+
if not super().__contains__(top_nest) and top_nest in self._outer.nested_columns:
115113
self._initialize_column_resolver(top_nest, self._outer)
116114
return super().__getitem__(top_nest)
117115

@@ -165,7 +163,8 @@ def __getattr__(self, item_name: str):
165163
result.nest_name = self._nest_name
166164
result.flat_nest = self._flat_nest
167165
return result
168-
raise AttributeError(f"No attribute {item_name}")
166+
# Outside of this special case, delegate to the underlying Pandas object.
167+
return getattr(self._outer[self._nest_name], item_name)
169168

170169

171170
def _subexprs_by_nest(parents: list, node) -> dict[str, list]:

tests/nested_pandas/nestedframe/test_nestedframe.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,30 @@ def test_is_known_column():
152152
assert base._is_known_column("nested.d")
153153

154154

155+
def test_series_methods_on_nest_in_query_eval():
156+
"""Test that a nest can also be treated as a Series.
157+
.isna() and .notna() should be available and work, for example.
158+
"""
159+
160+
base = NestedFrame(data={"a": [1, 2, 3], "b": [2, 4, 6]}, index=[0, 1, 2])
161+
162+
nested = pd.DataFrame(
163+
data={"c": [0, 2, 4, 1, 4, 3, 1, 4, 1], "d": [5, 4, 7, 5, 3, 1, 9, 3, 4]},
164+
index=[0, 0, 0, 1, 1, 1, 2, 2, 2],
165+
)
166+
167+
base = base.add_nested(nested, "nested")
168+
169+
# Prepare to test isna, notna
170+
base.loc[1, "nested"] = None
171+
172+
assert (base["nested"].isna() == base.eval("nested.isna()")).all()
173+
assert (base["nested"].notna() == base.eval("nested.notna()")).all()
174+
175+
assert len(base[base["nested"].isna()]) == 1
176+
assert len(base.query("nested.isna()")) == 1
177+
178+
155179
def test_get_nested_column():
156180
"""Test that __getitem__ can retrieve a nested column"""
157181

0 commit comments

Comments
 (0)