Skip to content

Commit b8707a9

Browse files
committed
Invalidate nest-resolver caches during assignment.
Resolves GH#159.
1 parent a96a508 commit b8707a9

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/nested_pandas/nestedframe/core.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,19 @@ def __getitem__(self, item):
9494
super().__setitem__(top_nest, _NestedFieldResolver(top_nest, self._outer))
9595
return super().__getitem__(top_nest)
9696

97-
def __setitem__(self, key, value):
97+
def __setitem__(self, item, _):
9898
# Called to update the resolver with intermediate values.
9999
# The important point is to intercept the call so that the evaluator
100-
# does not create any new resolvers on the fly. Storing the value
101-
# is not important, since that will have been done already in
102-
# the NestedFrame.
103-
pass
100+
# does not create any new resolvers on the fly. We do NOT want to
101+
# store the given value, since the resolver does lazy-loading.
102+
# What we DO want to do, however, is to invalidate the cache for
103+
# any field resolver for a given nest that is receiving an assignment.
104+
# Since the resolvers are created as-needed in __getitem__, all we need
105+
# to do is delete them from the local cache when this pattern is detected.
106+
if "." in item:
107+
top_nest = item.split(".")[0].strip()
108+
if top_nest in self._outer.nested_columns and super().__contains__(top_nest):
109+
del self[top_nest] # force re-creation in __setitem__
104110

105111

106112
class _NestedFieldResolver:

tests/nested_pandas/nestedframe/test_nestedframe.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -884,14 +884,20 @@ def test_eval_assignment():
884884
assert (nf_n3["p2.d"] == nf_n2["p2.c2"] + nf["packed.d"] * 2 + nf["b"]).all()
885885

886886
# Now test multiline and inplace=True
887+
# Verify the resolution of GH#159, where a nested column created in
888+
# an existing nest during a multi-line eval was not being recognized
889+
# in a subsequent line.
887890
nf.eval(
888891
"""
889892
c = a + b
890-
p2.e = packed.d * 2 + c
893+
packed.e = packed.d * 2
894+
p2.e = packed.e + c
891895
p2.f = p2.e + b
892896
""",
893897
inplace=True,
894898
)
895-
assert len(nf.p2.nest.fields) == 2
899+
assert set(nf.nested_columns) == {"packed", "p2"}
900+
assert set(nf.packed.nest.fields) == {"c", "d", "e", "time"}
901+
assert set(nf.p2.nest.fields) == {"e", "f"}
896902
assert (nf["p2.e"] == nf["packed.d"] * 2 + nf.c).all()
897903
assert (nf["p2.f"] == nf["p2.e"] + nf.b).all()

0 commit comments

Comments
 (0)