Skip to content

Commit 9d7a282

Browse files
jbrockmendeljreback
authored andcommitted
CLN: simplify comparison method, docstring cleanups (#27923)
1 parent 48dd753 commit 9d7a282

File tree

4 files changed

+47
-68
lines changed

4 files changed

+47
-68
lines changed

pandas/core/computation/common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import numpy as np
44

5-
import pandas as pd
5+
from pandas._config import get_option
66

77
# A token value Python's tokenizer probably will never use.
88
_BACKTICK_QUOTED_STRING = 100
@@ -11,7 +11,7 @@
1111
def _ensure_decoded(s):
1212
""" if we have bytes, decode them to unicode """
1313
if isinstance(s, (np.bytes_, bytes)):
14-
s = s.decode(pd.get_option("display.encoding"))
14+
s = s.decode(get_option("display.encoding"))
1515
return s
1616

1717

pandas/core/computation/expressions.py

+23-21
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,18 @@ def _bool_arith_check(
203203

204204

205205
def evaluate(op, op_str, a, b, use_numexpr=True, **eval_kwargs):
206-
""" evaluate and return the expression of the op on a and b
207-
208-
Parameters
209-
----------
210-
211-
op : the actual operand
212-
op_str: the string version of the op
213-
a : left operand
214-
b : right operand
215-
use_numexpr : whether to try to use numexpr (default True)
216-
"""
206+
"""
207+
Evaluate and return the expression of the op on a and b.
208+
209+
Parameters
210+
----------
211+
op : the actual operand
212+
op_str : the string version of the op
213+
a : left operand
214+
b : right operand
215+
use_numexpr : bool, default True
216+
Whether to try to use numexpr.
217+
"""
217218

218219
use_numexpr = use_numexpr and _bool_arith_check(op_str, a, b)
219220
if use_numexpr:
@@ -222,16 +223,17 @@ def evaluate(op, op_str, a, b, use_numexpr=True, **eval_kwargs):
222223

223224

224225
def where(cond, a, b, use_numexpr=True):
225-
""" evaluate the where condition cond on a and b
226-
227-
Parameters
228-
----------
229-
230-
cond : a boolean array
231-
a : return if cond is True
232-
b : return if cond is False
233-
use_numexpr : whether to try to use numexpr (default True)
234-
"""
226+
"""
227+
Evaluate the where condition cond on a and b
228+
229+
Parameters
230+
----------
231+
cond : ndarray[bool]
232+
a : return if cond is True
233+
b : return if cond is False
234+
use_numexpr : bool, default True
235+
Whether to try to use numexpr.
236+
"""
235237

236238
if use_numexpr:
237239
return _where(cond, a, b)

pandas/core/computation/ops.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@
5151

5252

5353
class UndefinedVariableError(NameError):
54-
55-
"""NameError subclass for local variables."""
54+
"""
55+
NameError subclass for local variables.
56+
"""
5657

5758
def __init__(self, name, is_local):
5859
if is_local:
@@ -191,8 +192,8 @@ def __repr__(self):
191192

192193

193194
class Op:
194-
195-
"""Hold an operator of arbitrary arity
195+
"""
196+
Hold an operator of arbitrary arity.
196197
"""
197198

198199
def __init__(self, op, operands, *args, **kwargs):
@@ -204,8 +205,9 @@ def __iter__(self):
204205
return iter(self.operands)
205206

206207
def __repr__(self):
207-
"""Print a generic n-ary operator and its operands using infix
208-
notation"""
208+
"""
209+
Print a generic n-ary operator and its operands using infix notation.
210+
"""
209211
# recurse over the operands
210212
parened = ("({0})".format(pprint_thing(opr)) for opr in self.operands)
211213
return pprint_thing(" {0} ".format(self.op).join(parened))
@@ -296,15 +298,15 @@ def _not_in(x, y):
296298

297299

298300
def _cast_inplace(terms, acceptable_dtypes, dtype):
299-
"""Cast an expression inplace.
301+
"""
302+
Cast an expression inplace.
300303
301304
Parameters
302305
----------
303306
terms : Op
304307
The expression that should cast.
305308
acceptable_dtypes : list of acceptable numpy.dtype
306309
Will not cast if term's dtype in this list.
307-
308310
dtype : str or numpy.dtype
309311
The dtype to cast to.
310312
"""
@@ -325,8 +327,8 @@ def is_term(obj):
325327

326328

327329
class BinOp(Op):
328-
329-
"""Hold a binary operator and its operands
330+
"""
331+
Hold a binary operator and its operands.
330332
331333
Parameters
332334
----------
@@ -355,7 +357,8 @@ def __init__(self, op, lhs, rhs, **kwargs):
355357
)
356358

357359
def __call__(self, env):
358-
"""Recursively evaluate an expression in Python space.
360+
"""
361+
Recursively evaluate an expression in Python space.
359362
360363
Parameters
361364
----------
@@ -377,7 +380,8 @@ def __call__(self, env):
377380
return self.func(left, right)
378381

379382
def evaluate(self, env, engine, parser, term_type, eval_in_python):
380-
"""Evaluate a binary operation *before* being passed to the engine.
383+
"""
384+
Evaluate a binary operation *before* being passed to the engine.
381385
382386
Parameters
383387
----------

pandas/core/ops/__init__.py

+6-33
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
is_period_dtype,
3333
is_scalar,
3434
is_timedelta64_dtype,
35-
needs_i8_conversion,
3635
)
3736
from pandas.core.dtypes.generic import (
3837
ABCDataFrame,
@@ -758,50 +757,24 @@ def _comp_method_SERIES(cls, op, special):
758757
code duplication.
759758
"""
760759
op_name = _get_op_name(op, special)
761-
masker = _gen_eval_kwargs(op_name).get("masker", False)
762760

763761
def na_op(x, y):
764762
# TODO:
765-
# should have guarantess on what x, y can be type-wise
763+
# should have guarantees on what x, y can be type-wise
766764
# Extension Dtypes are not called here
767765

768-
# Checking that cases that were once handled here are no longer
769-
# reachable.
770-
assert not (is_categorical_dtype(y) and not is_scalar(y))
771-
772766
if is_object_dtype(x.dtype):
773767
result = _comp_method_OBJECT_ARRAY(op, x, y)
774768

775769
elif is_datetimelike_v_numeric(x, y):
776770
return invalid_comparison(x, y, op)
777771

778772
else:
779-
780-
# we want to compare like types
781-
# we only want to convert to integer like if
782-
# we are not NotImplemented, otherwise
783-
# we would allow datetime64 (but viewed as i8) against
784-
# integer comparisons
785-
786-
# we have a datetime/timedelta and may need to convert
787-
assert not needs_i8_conversion(x)
788-
mask = None
789-
if not is_scalar(y) and needs_i8_conversion(y):
790-
mask = isna(x) | isna(y)
791-
y = y.view("i8")
792-
x = x.view("i8")
793-
794-
method = getattr(x, op_name, None)
795-
if method is not None:
796-
with np.errstate(all="ignore"):
797-
result = method(y)
798-
if result is NotImplemented:
799-
return invalid_comparison(x, y, op)
800-
else:
801-
result = op(x, y)
802-
803-
if mask is not None and mask.any():
804-
result[mask] = masker
773+
method = getattr(x, op_name)
774+
with np.errstate(all="ignore"):
775+
result = method(y)
776+
if result is NotImplemented:
777+
return invalid_comparison(x, y, op)
805778

806779
return result
807780

0 commit comments

Comments
 (0)