Skip to content

Commit 98df632

Browse files
jbrockmendelJulianWgs
authored andcommitted
REF: move get_op_result_name out of ops.__init__ (pandas-dev#37231)
1 parent 3f023b5 commit 98df632

File tree

3 files changed

+63
-64
lines changed

3 files changed

+63
-64
lines changed

pandas/core/ops/__init__.py

+5-63
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from pandas.util._decorators import Appender
1515

1616
from pandas.core.dtypes.common import is_array_like, is_list_like
17-
from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
17+
from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries
1818
from pandas.core.dtypes.missing import isna
1919

2020
from pandas.core import algorithms
@@ -25,7 +25,10 @@
2525
get_array_op,
2626
logical_op,
2727
)
28-
from pandas.core.ops.common import unpack_zerodim_and_defer # noqa:F401
28+
from pandas.core.ops.common import ( # noqa:F401
29+
get_op_result_name,
30+
unpack_zerodim_and_defer,
31+
)
2932
from pandas.core.ops.docstrings import (
3033
_flex_comp_doc_FRAME,
3134
_op_descriptions,
@@ -76,67 +79,6 @@
7679

7780
COMPARISON_BINOPS: Set[str] = {"eq", "ne", "lt", "gt", "le", "ge"}
7881

79-
# -----------------------------------------------------------------------------
80-
# Ops Wrapping Utilities
81-
82-
83-
def get_op_result_name(left, right):
84-
"""
85-
Find the appropriate name to pin to an operation result. This result
86-
should always be either an Index or a Series.
87-
88-
Parameters
89-
----------
90-
left : {Series, Index}
91-
right : object
92-
93-
Returns
94-
-------
95-
name : object
96-
Usually a string
97-
"""
98-
# `left` is always a Series when called from within ops
99-
if isinstance(right, (ABCSeries, ABCIndexClass)):
100-
name = _maybe_match_name(left, right)
101-
else:
102-
name = left.name
103-
return name
104-
105-
106-
def _maybe_match_name(a, b):
107-
"""
108-
Try to find a name to attach to the result of an operation between
109-
a and b. If only one of these has a `name` attribute, return that
110-
name. Otherwise return a consensus name if they match of None if
111-
they have different names.
112-
113-
Parameters
114-
----------
115-
a : object
116-
b : object
117-
118-
Returns
119-
-------
120-
name : str or None
121-
122-
See Also
123-
--------
124-
pandas.core.common.consensus_name_attr
125-
"""
126-
a_has = hasattr(a, "name")
127-
b_has = hasattr(b, "name")
128-
if a_has and b_has:
129-
if a.name == b.name:
130-
return a.name
131-
else:
132-
# TODO: what if they both have np.nan for their names?
133-
return None
134-
elif a_has:
135-
return a.name
136-
elif b_has:
137-
return b.name
138-
return None
139-
14082

14183
# -----------------------------------------------------------------------------
14284
# Masking NA values and fallbacks for operations numpy does not support

pandas/core/ops/common.py

+57
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,60 @@ def new_method(self, other):
6565
return method(self, other)
6666

6767
return new_method
68+
69+
70+
def get_op_result_name(left, right):
71+
"""
72+
Find the appropriate name to pin to an operation result. This result
73+
should always be either an Index or a Series.
74+
75+
Parameters
76+
----------
77+
left : {Series, Index}
78+
right : object
79+
80+
Returns
81+
-------
82+
name : object
83+
Usually a string
84+
"""
85+
if isinstance(right, (ABCSeries, ABCIndexClass)):
86+
name = _maybe_match_name(left, right)
87+
else:
88+
name = left.name
89+
return name
90+
91+
92+
def _maybe_match_name(a, b):
93+
"""
94+
Try to find a name to attach to the result of an operation between
95+
a and b. If only one of these has a `name` attribute, return that
96+
name. Otherwise return a consensus name if they match of None if
97+
they have different names.
98+
99+
Parameters
100+
----------
101+
a : object
102+
b : object
103+
104+
Returns
105+
-------
106+
name : str or None
107+
108+
See Also
109+
--------
110+
pandas.core.common.consensus_name_attr
111+
"""
112+
a_has = hasattr(a, "name")
113+
b_has = hasattr(b, "name")
114+
if a_has and b_has:
115+
if a.name == b.name:
116+
return a.name
117+
else:
118+
# TODO: what if they both have np.nan for their names?
119+
return None
120+
elif a_has:
121+
return a.name
122+
elif b_has:
123+
return b.name
124+
return None

pandas/tests/test_common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def test_random_state():
106106
],
107107
)
108108
def test_maybe_match_name(left, right, expected):
109-
assert ops._maybe_match_name(left, right) == expected
109+
assert ops.common._maybe_match_name(left, right) == expected
110110

111111

112112
def test_dict_compat():

0 commit comments

Comments
 (0)