Skip to content

Commit eadaebc

Browse files
DataFrame.eval error fixed
1 parent dcb5494 commit eadaebc

File tree

1 file changed

+18
-32
lines changed

1 file changed

+18
-32
lines changed

pandas/core/computation/align.py

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
"""
22
Core eval alignment algorithms.
33
"""
4-
54
from __future__ import annotations
65

76
from functools import (
87
partial,
98
wraps,
109
)
11-
from typing import TYPE_CHECKING
10+
from typing import (
11+
TYPE_CHECKING,
12+
Callable,
13+
)
1214
import warnings
1315

1416
import numpy as np
1517

16-
from pandas._config.config import get_option
17-
1818
from pandas.errors import PerformanceWarning
1919
from pandas.util._exceptions import find_stack_level
2020

@@ -28,10 +28,7 @@
2828
from pandas.core.computation.common import result_type_many
2929

3030
if TYPE_CHECKING:
31-
from collections.abc import (
32-
Callable,
33-
Sequence,
34-
)
31+
from collections.abc import Sequence
3532

3633
from pandas._typing import F
3734

@@ -113,7 +110,7 @@ def _align_core(terms):
113110
ax, itm = axis, items
114111

115112
if not axes[ax].is_(itm):
116-
axes[ax] = axes[ax].union(itm)
113+
axes[ax] = axes[ax].join(itm, how="outer")
117114

118115
for i, ndim in ndims.items():
119116
for axis, items in zip(range(ndim), axes):
@@ -127,24 +124,20 @@ def _align_core(terms):
127124
reindexer_size = len(reindexer)
128125

129126
ordm = np.log10(max(1, abs(reindexer_size - term_axis_size)))
130-
if (
131-
get_option("performance_warnings")
132-
and ordm >= 1
133-
and reindexer_size >= 10000
134-
):
127+
if ordm >= 1 and reindexer_size >= 10000:
135128
w = (
136129
f"Alignment difference on axis {axis} is larger "
137-
f"than an order of magnitude on term {terms[i].name!r}, "
130+
f"than an order of magnitude on term {repr(terms[i].name)}, "
138131
f"by more than {ordm:.4g}; performance may suffer."
139132
)
140133
warnings.warn(
141134
w, category=PerformanceWarning, stacklevel=find_stack_level()
142135
)
143136

144-
obj = ti.reindex(reindexer, axis=axis)
137+
obj = ti.reindex(reindexer, axis=axis, copy=False)
145138
terms[i].update(obj)
146139

147-
terms[i].update(terms[i].value.values)
140+
terms[i].update(terms[i].value)
148141

149142
return typ, _zip_axes_from_type(typ, axes)
150143

@@ -160,24 +153,19 @@ def align_terms(terms):
160153
# can't iterate so it must just be a constant or single variable
161154
if isinstance(terms.value, (ABCSeries, ABCDataFrame)):
162155
typ = type(terms.value)
163-
name = terms.value.name if isinstance(terms.value, ABCSeries) else None
164-
return typ, _zip_axes_from_type(typ, terms.value.axes), name
165-
return np.result_type(terms.type), None, None
156+
return typ, _zip_axes_from_type(typ, terms.value.axes)
157+
return np.result_type(terms.type), None
166158

167159
# if all resolved variables are numeric scalars
168160
if all(term.is_scalar for term in terms):
169-
return result_type_many(*(term.value for term in terms)).type, None, None
170-
171-
# if all input series have a common name, propagate it to the returned series
172-
names = {term.value.name for term in terms if isinstance(term.value, ABCSeries)}
173-
name = names.pop() if len(names) == 1 else None
161+
return result_type_many(*(term.value for term in terms)).type, None
174162

175163
# perform the main alignment
176164
typ, axes = _align_core(terms)
177-
return typ, axes, name
165+
return typ, axes
178166

179167

180-
def reconstruct_object(typ, obj, axes, dtype, name):
168+
def reconstruct_object(typ, obj, axes, dtype):
181169
"""
182170
Reconstruct an object given its type, raw value, and possibly empty
183171
(None) axes.
@@ -194,8 +182,8 @@ def reconstruct_object(typ, obj, axes, dtype, name):
194182
Returns
195183
-------
196184
ret : typ
197-
An object of type ``typ`` with the value `obj` and possible axes
198-
`axes`.
185+
An object of type typ with the value obj and possible axes
186+
axes.
199187
"""
200188
try:
201189
typ = typ.type
@@ -205,9 +193,7 @@ def reconstruct_object(typ, obj, axes, dtype, name):
205193
res_t = np.result_type(obj.dtype, dtype)
206194

207195
if not isinstance(typ, partial) and issubclass(typ, PandasObject):
208-
if name is None:
209-
return typ(obj, dtype=res_t, **axes)
210-
return typ(obj, dtype=res_t, name=name, **axes)
196+
return typ(obj, dtype=res_t, **axes)
211197

212198
# special case for pathological things like ~True/~False
213199
if hasattr(res_t, "type") and typ == np.bool_ and res_t != np.bool_:

0 commit comments

Comments
 (0)