From 58a051393bc6f1d4dee5b19bf9bc7643a71063f3 Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 18 Oct 2020 15:50:36 -0700 Subject: [PATCH 1/2] REF: move get_op_result_name out of ops.__init__ --- pandas/core/ops/__init__.py | 68 +++---------------------------------- pandas/core/ops/common.py | 57 +++++++++++++++++++++++++++++++ pandas/tests/test_common.py | 2 +- 3 files changed, 63 insertions(+), 64 deletions(-) diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index fb3005484b2f1..2b159c607b0a0 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -14,7 +14,7 @@ from pandas.util._decorators import Appender from pandas.core.dtypes.common import is_array_like, is_list_like -from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries +from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries from pandas.core.dtypes.missing import isna from pandas.core import algorithms @@ -25,7 +25,10 @@ get_array_op, logical_op, ) -from pandas.core.ops.common import unpack_zerodim_and_defer # noqa:F401 +from pandas.core.ops.common import ( # noqa:F401 + get_op_result_name, + unpack_zerodim_and_defer, +) from pandas.core.ops.docstrings import ( _flex_comp_doc_FRAME, _op_descriptions, @@ -76,67 +79,6 @@ COMPARISON_BINOPS: Set[str] = {"eq", "ne", "lt", "gt", "le", "ge"} -# ----------------------------------------------------------------------------- -# Ops Wrapping Utilities - - -def get_op_result_name(left, right): - """ - Find the appropriate name to pin to an operation result. This result - should always be either an Index or a Series. - - Parameters - ---------- - left : {Series, Index} - right : object - - Returns - ------- - name : object - Usually a string - """ - # `left` is always a Series when called from within ops - if isinstance(right, (ABCSeries, ABCIndexClass)): - name = _maybe_match_name(left, right) - else: - name = left.name - return name - - -def _maybe_match_name(a, b): - """ - Try to find a name to attach to the result of an operation between - a and b. If only one of these has a `name` attribute, return that - name. Otherwise return a consensus name if they match of None if - they have different names. - - Parameters - ---------- - a : object - b : object - - Returns - ------- - name : str or None - - See Also - -------- - pandas.core.common.consensus_name_attr - """ - a_has = hasattr(a, "name") - b_has = hasattr(b, "name") - if a_has and b_has: - if a.name == b.name: - return a.name - else: - # TODO: what if they both have np.nan for their names? - return None - elif a_has: - return a.name - elif b_has: - return b.name - return None - # ----------------------------------------------------------------------------- # Masking NA values and fallbacks for operations numpy does not support diff --git a/pandas/core/ops/common.py b/pandas/core/ops/common.py index 515a0a5198d74..a6bcab44e5519 100644 --- a/pandas/core/ops/common.py +++ b/pandas/core/ops/common.py @@ -65,3 +65,60 @@ def new_method(self, other): return method(self, other) return new_method + + +def get_op_result_name(left, right): + """ + Find the appropriate name to pin to an operation result. This result + should always be either an Index or a Series. + + Parameters + ---------- + left : {Series, Index} + right : object + + Returns + ------- + name : object + Usually a string + """ + if isinstance(right, (ABCSeries, ABCIndexClass)): + name = _maybe_match_name(left, right) + else: + name = left.name + return name + + +def _maybe_match_name(a, b): + """ + Try to find a name to attach to the result of an operation between + a and b. If only one of these has a `name` attribute, return that + name. Otherwise return a consensus name if they match of None if + they have different names. + + Parameters + ---------- + a : object + b : object + + Returns + ------- + name : str or None + + See Also + -------- + pandas.core.common.consensus_name_attr + """ + a_has = hasattr(a, "name") + b_has = hasattr(b, "name") + if a_has and b_has: + if a.name == b.name: + return a.name + else: + # TODO: what if they both have np.nan for their names? + return None + elif a_has: + return a.name + elif b_has: + return b.name + return None diff --git a/pandas/tests/test_common.py b/pandas/tests/test_common.py index 17d7527a2b687..366a1970f6f64 100644 --- a/pandas/tests/test_common.py +++ b/pandas/tests/test_common.py @@ -106,7 +106,7 @@ def test_random_state(): ], ) def test_maybe_match_name(left, right, expected): - assert ops._maybe_match_name(left, right) == expected + assert ops.common._maybe_match_name(left, right) == expected def test_dict_compat(): From 361d0e82130c58278766df59eceece258fbd6788 Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 18 Oct 2020 18:58:20 -0700 Subject: [PATCH 2/2] dummy commit to force CI