Skip to content

Commit 75e3a17

Browse files
committed
Better names for utility functions.
1 parent 3d42de5 commit 75e3a17

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

src/nested_pandas/nestedframe/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from nested_pandas.series.dtype import NestedDtype
1717

1818
from ..series.packer import pack_sorted_df_into_struct
19-
from .utils import check_expr_nesting
19+
from .utils import extract_nest_names
2020

2121

2222
class NestedPandasExprVisitor(PandasExprVisitor):
@@ -509,7 +509,7 @@ def query(self, expr: str, *, inplace: bool = False, **kwargs) -> NestedFrame |
509509
# At present, the query expression must be either entirely within a
510510
# single nest, or have nothing but base columns. Mixed structures are not
511511
# supported, so preflight the expression.
512-
nest_names = check_expr_nesting(expr)
512+
nest_names = extract_nest_names(expr)
513513
if len(nest_names) > 1:
514514
raise ValueError("Queries cannot target multiple structs/layers, write a separate query for each")
515515
result = self.eval(expr, **kwargs)

src/nested_pandas/nestedframe/utils.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
import ast
55

66

7-
def _actionable_splits(parents: list[ast.expr], node: ast.expr | None) -> dict[str, list]:
7+
def _subexprs_by_nest(parents: list[ast.expr], node: ast.expr | None) -> dict[str, list]:
88
"""
9-
Given an expression which contains references to both base and nested columns,
10-
return a dictionary of the sub-expressions that should be evaluated independently.
11-
The key of the dictionary is the name of the nested column, and will be a blank
12-
string in the case of base columns. The value is a list of the parent nodes
13-
that lead to sub-expressions that can be evaluated successfully.
9+
Given an expression which contains references to both base and nested
10+
columns, return a dictionary of the sub-expressions that should be
11+
evaluated independently, keyed by nesting context.
1412
15-
While this is not in use today for automatically splitting expressions, it can
16-
be used to detect whether an expression is suitably structured for evaluation:
17-
the returned dictionary should have a single key.
13+
The key of the dictionary is the name of the nested column, and will
14+
be a blank string in the case of base columns. The value is a list
15+
of the parent nodes that lead to sub-expressions that can be evaluated
16+
successfully.
17+
18+
While this is not in use today for automatically splitting expressions,
19+
it can be used to detect whether an expression is suitably structured
20+
for evaluation: the returned dictionary should have a single key.
1821
"""
1922
if not isinstance(node, ast.expr):
2023
return {}
@@ -28,8 +31,8 @@ def _actionable_splits(parents: list[ast.expr], node: ast.expr | None) -> dict[s
2831
+ getattr(node, "comparators", [])
2932
)
3033
result: dict[str, list] = {}
31-
for s in sources:
32-
child = _actionable_splits(parents, s)
34+
for source in sources:
35+
child = _subexprs_by_nest(parents, source)
3336
for k, v in child.items():
3437
result.setdefault(k, []).append(v)
3538
# After a complete traversal across sources, check for any necessary splits.
@@ -47,12 +50,12 @@ def _actionable_splits(parents: list[ast.expr], node: ast.expr | None) -> dict[s
4750
return result
4851

4952

50-
def check_expr_nesting(expr: str) -> set[str]:
53+
def extract_nest_names(expr: str) -> set[str]:
5154
"""
5255
Given a string expression, parse it and visit the resulting AST, surfacing
5356
the nesting types. The purpose is to identify expressions that attempt
5457
to mix base and nested columns, or columns from two different nests.
5558
"""
5659
expr_tree = ast.parse(expr, mode="eval").body
57-
separable = _actionable_splits([], expr_tree)
60+
separable = _subexprs_by_nest([], expr_tree)
5861
return set(separable.keys())

tests/nested_pandas/utils/test_utils.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pandas as pd
33
import pytest
44
from nested_pandas import NestedFrame
5-
from nested_pandas.nestedframe.utils import check_expr_nesting
5+
from nested_pandas.nestedframe.utils import extract_nest_names
66
from nested_pandas.utils import count_nested
77

88

@@ -52,16 +52,16 @@ def test_check_expr_nesting():
5252
used to ensure that an expression-based query does not try to combine base and nested
5353
sub-expressions.
5454
"""
55-
assert check_expr_nesting("a > 2 & nested.c > 1") == {"", "nested"}
56-
assert check_expr_nesting("(nested.c > 1) and (nested.d>2)") == {"nested"}
57-
assert check_expr_nesting("-1.52e-5 < abc < 35.2e2") == {""}
58-
assert check_expr_nesting("(n.a > 1) and ((b + c) > (d - 1e-8)) or n.q > c") == {"n", ""}
55+
assert extract_nest_names("a > 2 & nested.c > 1") == {"", "nested"}
56+
assert extract_nest_names("(nested.c > 1) and (nested.d>2)") == {"nested"}
57+
assert extract_nest_names("-1.52e-5 < abc < 35.2e2") == {""}
58+
assert extract_nest_names("(n.a > 1) and ((b + c) > (d - 1e-8)) or n.q > c") == {"n", ""}
5959

60-
assert check_expr_nesting("a.b > 2 & c.d < 5") == {"a", "c"}
60+
assert extract_nest_names("a.b > 2 & c.d < 5") == {"a", "c"}
6161

62-
assert check_expr_nesting("a>3") == {""}
63-
assert check_expr_nesting("a > 3") == {""}
64-
assert check_expr_nesting("test.a>5&b==2") == {"test", ""}
65-
assert check_expr_nesting("test.a > 5 & b == 2") == {"test", ""}
66-
assert check_expr_nesting("(a.b > 3)&(a.c == 'f')") == {"a"}
67-
assert check_expr_nesting("(a.b > 3) & (a.c == 'f')") == {"a"}
62+
assert extract_nest_names("a>3") == {""}
63+
assert extract_nest_names("a > 3") == {""}
64+
assert extract_nest_names("test.a>5&b==2") == {"test", ""}
65+
assert extract_nest_names("test.a > 5 & b == 2") == {"test", ""}
66+
assert extract_nest_names("(a.b > 3)&(a.c == 'f')") == {"a"}
67+
assert extract_nest_names("(a.b > 3) & (a.c == 'f')") == {"a"}

0 commit comments

Comments
 (0)