Skip to content

Commit d72dc24

Browse files
authored
CLN: avoid accessing private functions (#33427)
1 parent a142ad7 commit d72dc24

File tree

5 files changed

+49
-49
lines changed

5 files changed

+49
-49
lines changed

pandas/core/arrays/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ def _maybe_convert(arr):
12061206

12071207
return _maybe_convert(res)
12081208

1209-
op_name = ops._get_op_name(op, True)
1209+
op_name = f"__{op.__name__}__"
12101210
return set_function_name(_binop, op_name, cls)
12111211

12121212
@classmethod

pandas/core/indexes/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3286,7 +3286,7 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None):
32863286
preserve_names = not hasattr(target, "name")
32873287

32883288
# GH7774: preserve dtype/tz if target is empty and not an Index.
3289-
target = _ensure_has_len(target) # target may be an iterator
3289+
target = ensure_has_len(target) # target may be an iterator
32903290

32913291
if not isinstance(target, Index) and len(target) == 0:
32923292
if isinstance(self, ABCRangeIndex):
@@ -5576,7 +5576,7 @@ def ensure_index(index_like, copy: bool = False):
55765576
return Index(index_like)
55775577

55785578

5579-
def _ensure_has_len(seq):
5579+
def ensure_has_len(seq):
55805580
"""
55815581
If seq is an iterator, put its values into a list.
55825582
"""

pandas/core/indexes/multi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2288,7 +2288,7 @@ def reindex(self, target, method=None, level=None, limit=None, tolerance=None):
22882288

22892289
# GH7774: preserve dtype/tz if target is empty and not an Index.
22902290
# target may be an iterator
2291-
target = ibase._ensure_has_len(target)
2291+
target = ibase.ensure_has_len(target)
22922292
if len(target) == 0 and not isinstance(target, Index):
22932293
idx = self.levels[level]
22942294
attrs = idx._get_attributes_dict()

pandas/core/ops/__init__.py

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from pandas._libs import lib
1212
from pandas._libs.ops_dispatch import maybe_dispatch_ufunc_to_dunder_op # noqa:F401
13-
from pandas._typing import ArrayLike, Level
13+
from pandas._typing import Level
1414
from pandas.util._decorators import Appender
1515

1616
from pandas.core.dtypes.common import is_list_like
@@ -224,7 +224,7 @@ def _get_opstr(op):
224224
}[op]
225225

226226

227-
def _get_op_name(op, special):
227+
def _get_op_name(op, special: bool) -> str:
228228
"""
229229
Find the name to attach to this method according to conventions
230230
for special and non-special methods.
@@ -385,42 +385,6 @@ def _align_method_SERIES(left, right, align_asobject=False):
385385
return left, right
386386

387387

388-
def _construct_result(
389-
left: ABCSeries, result: ArrayLike, index: ABCIndexClass, name,
390-
):
391-
"""
392-
Construct an appropriately-labelled Series from the result of an op.
393-
394-
Parameters
395-
----------
396-
left : Series
397-
result : ndarray or ExtensionArray
398-
index : Index
399-
name : object
400-
401-
Returns
402-
-------
403-
Series
404-
In the case of __divmod__ or __rdivmod__, a 2-tuple of Series.
405-
"""
406-
if isinstance(result, tuple):
407-
# produced by divmod or rdivmod
408-
return (
409-
_construct_result(left, result[0], index=index, name=name),
410-
_construct_result(left, result[1], index=index, name=name),
411-
)
412-
413-
# We do not pass dtype to ensure that the Series constructor
414-
# does inference in the case where `result` has object-dtype.
415-
out = left._constructor(result, index=index)
416-
out = out.__finalize__(left)
417-
418-
# Set the result's name after __finalize__ is called because __finalize__
419-
# would set it back to self.name
420-
out.name = name
421-
return out
422-
423-
424388
def _arith_method_SERIES(cls, op, special):
425389
"""
426390
Wrapper function for Series arithmetic operations, to avoid
@@ -439,7 +403,7 @@ def wrapper(left, right):
439403
rvalues = extract_array(right, extract_numpy=True)
440404
result = arithmetic_op(lvalues, rvalues, op, str_rep)
441405

442-
return _construct_result(left, result, index=left.index, name=res_name)
406+
return left._construct_result(result, name=res_name)
443407

444408
wrapper.__name__ = op_name
445409
return wrapper
@@ -466,7 +430,7 @@ def wrapper(self, other):
466430

467431
res_values = comparison_op(lvalues, rvalues, op, str_rep)
468432

469-
return _construct_result(self, res_values, index=self.index, name=res_name)
433+
return self._construct_result(res_values, name=res_name)
470434

471435
wrapper.__name__ = op_name
472436
return wrapper
@@ -488,7 +452,7 @@ def wrapper(self, other):
488452
rvalues = extract_array(other, extract_numpy=True)
489453

490454
res_values = logical_op(lvalues, rvalues, op)
491-
return _construct_result(self, res_values, index=self.index, name=res_name)
455+
return self._construct_result(res_values, name=res_name)
492456

493457
wrapper.__name__ = op_name
494458
return wrapper

pandas/core/series.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
Optional,
1515
Tuple,
1616
Type,
17+
Union,
1718
)
1819
import warnings
1920

@@ -22,7 +23,7 @@
2223
from pandas._config import get_option
2324

2425
from pandas._libs import lib, properties, reshape, tslibs
25-
from pandas._typing import Axis, DtypeObj, Label
26+
from pandas._typing import ArrayLike, Axis, DtypeObj, Label
2627
from pandas.compat.numpy import function as nv
2728
from pandas.util._decorators import Appender, Substitution, doc
2829
from pandas.util._validators import validate_bool_kwarg, validate_percentile
@@ -2608,22 +2609,57 @@ def _binop(self, other, func, level=None, fill_value=None):
26082609
if not isinstance(other, Series):
26092610
raise AssertionError("Other operand must be Series")
26102611

2611-
new_index = self.index
26122612
this = self
26132613

26142614
if not self.index.equals(other.index):
26152615
this, other = self.align(other, level=level, join="outer", copy=False)
2616-
new_index = this.index
26172616

26182617
this_vals, other_vals = ops.fill_binop(this.values, other.values, fill_value)
26192618

26202619
with np.errstate(all="ignore"):
26212620
result = func(this_vals, other_vals)
26222621

26232622
name = ops.get_op_result_name(self, other)
2624-
ret = ops._construct_result(self, result, new_index, name)
2623+
ret = this._construct_result(result, name)
26252624
return ret
26262625

2626+
def _construct_result(
2627+
self, result: Union[ArrayLike, Tuple[ArrayLike, ArrayLike]], name: Label
2628+
) -> Union["Series", Tuple["Series", "Series"]]:
2629+
"""
2630+
Construct an appropriately-labelled Series from the result of an op.
2631+
2632+
Parameters
2633+
----------
2634+
result : ndarray or ExtensionArray
2635+
name : Label
2636+
2637+
Returns
2638+
-------
2639+
Series
2640+
In the case of __divmod__ or __rdivmod__, a 2-tuple of Series.
2641+
"""
2642+
if isinstance(result, tuple):
2643+
# produced by divmod or rdivmod
2644+
2645+
res1 = self._construct_result(result[0], name=name)
2646+
res2 = self._construct_result(result[1], name=name)
2647+
2648+
# GH#33427 assertions to keep mypy happy
2649+
assert isinstance(res1, Series)
2650+
assert isinstance(res2, Series)
2651+
return (res1, res2)
2652+
2653+
# We do not pass dtype to ensure that the Series constructor
2654+
# does inference in the case where `result` has object-dtype.
2655+
out = self._constructor(result, index=self.index)
2656+
out = out.__finalize__(self)
2657+
2658+
# Set the result's name after __finalize__ is called because __finalize__
2659+
# would set it back to self.name
2660+
out.name = name
2661+
return out
2662+
26272663
def combine(self, other, func, fill_value=None) -> "Series":
26282664
"""
26292665
Combine the Series with a Series or scalar according to `func`.

0 commit comments

Comments
 (0)