Skip to content

Commit 0674b2c

Browse files
jbrockmendelproost
authored andcommitted
CLN: de-privatize names in core.computation (pandas-dev#29665)
1 parent 9f015aa commit 0674b2c

File tree

6 files changed

+30
-27
lines changed

6 files changed

+30
-27
lines changed

pandas/core/computation/align.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
from pandas.errors import PerformanceWarning
1010

11-
import pandas as pd
11+
from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries
12+
1213
from pandas.core.base import PandasObject
1314
import pandas.core.common as com
14-
from pandas.core.computation.common import _result_type_many
15+
from pandas.core.computation.common import result_type_many
1516

1617

1718
def _align_core_single_unary_op(term):
@@ -49,7 +50,7 @@ def wrapper(terms):
4950

5051
# we don't have any pandas objects
5152
if not _any_pandas_objects(terms):
52-
return _result_type_many(*term_values), None
53+
return result_type_many(*term_values), None
5354

5455
return f(terms)
5556

@@ -60,7 +61,10 @@ def wrapper(terms):
6061
def _align_core(terms):
6162
term_index = [i for i, term in enumerate(terms) if hasattr(term.value, "axes")]
6263
term_dims = [terms[i].value.ndim for i in term_index]
63-
ndims = pd.Series(dict(zip(term_index, term_dims)))
64+
65+
from pandas import Series
66+
67+
ndims = Series(dict(zip(term_index, term_dims)))
6468

6569
# initial axes are the axes of the largest-axis'd term
6670
biggest = terms[ndims.idxmax()].value
@@ -70,7 +74,7 @@ def _align_core(terms):
7074
gt_than_one_axis = naxes > 1
7175

7276
for value in (terms[i].value for i in term_index):
73-
is_series = isinstance(value, pd.Series)
77+
is_series = isinstance(value, ABCSeries)
7478
is_series_and_gt_one_axis = is_series and gt_than_one_axis
7579

7680
for axis, items in enumerate(value.axes):
@@ -87,7 +91,7 @@ def _align_core(terms):
8791
ti = terms[i].value
8892

8993
if hasattr(ti, "reindex"):
90-
transpose = isinstance(ti, pd.Series) and naxes > 1
94+
transpose = isinstance(ti, ABCSeries) and naxes > 1
9195
reindexer = axes[naxes - 1] if transpose else items
9296

9397
term_axis_size = len(ti.axes[axis])
@@ -111,28 +115,28 @@ def _align_core(terms):
111115
return typ, _zip_axes_from_type(typ, axes)
112116

113117

114-
def _align(terms):
118+
def align_terms(terms):
115119
"""Align a set of terms"""
116120
try:
117121
# flatten the parse tree (a nested list, really)
118122
terms = list(com.flatten(terms))
119123
except TypeError:
120124
# can't iterate so it must just be a constant or single variable
121-
if isinstance(terms.value, pd.core.generic.NDFrame):
125+
if isinstance(terms.value, (ABCSeries, ABCDataFrame)):
122126
typ = type(terms.value)
123127
return typ, _zip_axes_from_type(typ, terms.value.axes)
124128
return np.result_type(terms.type), None
125129

126130
# if all resolved variables are numeric scalars
127131
if all(term.is_scalar for term in terms):
128-
return _result_type_many(*(term.value for term in terms)).type, None
132+
return result_type_many(*(term.value for term in terms)).type, None
129133

130134
# perform the main alignment
131135
typ, axes = _align_core(terms)
132136
return typ, axes
133137

134138

135-
def _reconstruct_object(typ, obj, axes, dtype):
139+
def reconstruct_object(typ, obj, axes, dtype):
136140
"""
137141
Reconstruct an object given its type, raw value, and possibly empty
138142
(None) axes.

pandas/core/computation/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def _ensure_decoded(s):
1515
return s
1616

1717

18-
def _result_type_many(*arrays_and_dtypes):
18+
def result_type_many(*arrays_and_dtypes):
1919
""" wrapper around numpy.result_type which overcomes the NPY_MAXARGS (32)
2020
argument limit """
2121
try:

pandas/core/computation/engines.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import abc
66

7-
from pandas.core.computation.align import _align, _reconstruct_object
7+
from pandas.core.computation.align import align_terms, reconstruct_object
88
from pandas.core.computation.ops import UndefinedVariableError, _mathops, _reductions
99

1010
import pandas.io.formats.printing as printing
@@ -67,11 +67,11 @@ def evaluate(self):
6767
The result of the passed expression.
6868
"""
6969
if not self._is_aligned:
70-
self.result_type, self.aligned_axes = _align(self.expr.terms)
70+
self.result_type, self.aligned_axes = align_terms(self.expr.terms)
7171

7272
# make sure no names in resolvers and locals/globals clash
7373
res = self._evaluate()
74-
return _reconstruct_object(
74+
return reconstruct_object(
7575
self.result_type, res, self.aligned_axes, self.expr.terms.return_type
7676
)
7777

pandas/core/computation/eval.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from pandas.util._validators import validate_bool_kwarg
1111

1212
from pandas.core.computation.engines import _engines
13+
from pandas.core.computation.expr import Expr, _parsers, tokenize_string
1314
from pandas.core.computation.scope import _ensure_scope
1415

1516
from pandas.io.formats.printing import pprint_thing
@@ -64,7 +65,7 @@ def _check_engine(engine):
6465
return engine
6566

6667

67-
def _check_parser(parser):
68+
def _check_parser(parser: str):
6869
"""
6970
Make sure a valid parser is passed.
7071
@@ -77,7 +78,6 @@ def _check_parser(parser):
7778
KeyError
7879
* If an invalid parser is passed
7980
"""
80-
from pandas.core.computation.expr import _parsers
8181

8282
if parser not in _parsers:
8383
raise KeyError(
@@ -115,7 +115,7 @@ def _check_expression(expr):
115115
raise ValueError("expr cannot be an empty string")
116116

117117

118-
def _convert_expression(expr):
118+
def _convert_expression(expr) -> str:
119119
"""
120120
Convert an object to an expression.
121121
@@ -131,7 +131,7 @@ def _convert_expression(expr):
131131
132132
Returns
133133
-------
134-
s : unicode
134+
str
135135
The string representation of an object.
136136
137137
Raises
@@ -144,8 +144,7 @@ def _convert_expression(expr):
144144
return s
145145

146146

147-
def _check_for_locals(expr, stack_level, parser):
148-
from pandas.core.computation.expr import tokenize_string
147+
def _check_for_locals(expr: str, stack_level: int, parser: str):
149148

150149
at_top_of_stack = stack_level == 0
151150
not_pandas_parser = parser != "pandas"
@@ -192,7 +191,7 @@ def eval(
192191
193192
Parameters
194193
----------
195-
expr : str or unicode
194+
expr : str
196195
The expression to evaluate. This string cannot contain any Python
197196
`statements
198197
<https://docs.python.org/3/reference/simple_stmts.html#simple-statements>`__,
@@ -282,7 +281,6 @@ def eval(
282281
See the :ref:`enhancing performance <enhancingperf.eval>` documentation for
283282
more details.
284283
"""
285-
from pandas.core.computation.expr import Expr
286284

287285
inplace = validate_bool_kwarg(inplace, "inplace")
288286

pandas/core/computation/expr.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import numpy as np
1313

14-
import pandas as pd
1514
import pandas.core.common as com
1615
from pandas.core.computation.common import (
1716
_BACKTICK_QUOTED_STRING,
@@ -40,7 +39,7 @@
4039
import pandas.io.formats.printing as printing
4140

4241

43-
def tokenize_string(source):
42+
def tokenize_string(source: str):
4443
"""
4544
Tokenize a Python source code string.
4645
@@ -171,7 +170,7 @@ def _compose(*funcs):
171170

172171

173172
def _preparse(
174-
source,
173+
source: str,
175174
f=_compose(
176175
_replace_locals,
177176
_replace_booleans,
@@ -600,6 +599,8 @@ def visit_Index(self, node, **kwargs):
600599
return self.visit(node.value)
601600

602601
def visit_Subscript(self, node, **kwargs):
602+
import pandas as pd
603+
603604
value = self.visit(node.value)
604605
slobj = self.visit(node.slice)
605606
result = pd.eval(

pandas/core/computation/ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pandas.core.dtypes.common import is_list_like, is_scalar
1414

1515
import pandas.core.common as com
16-
from pandas.core.computation.common import _ensure_decoded, _result_type_many
16+
from pandas.core.computation.common import _ensure_decoded, result_type_many
1717
from pandas.core.computation.scope import _DEFAULT_GLOBALS
1818

1919
from pandas.io.formats.printing import pprint_thing, pprint_thing_encoded
@@ -218,7 +218,7 @@ def return_type(self):
218218
# clobber types to bool if the op is a boolean operator
219219
if self.op in (_cmp_ops_syms + _bool_ops_syms):
220220
return np.bool_
221-
return _result_type_many(*(term.type for term in com.flatten(self)))
221+
return result_type_many(*(term.type for term in com.flatten(self)))
222222

223223
@property
224224
def has_invalid_return_type(self) -> bool:

0 commit comments

Comments
 (0)