From 79fc22eaaabc51f851190d46c06dacfba051fac4 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 24 Sep 2019 10:55:13 -0700 Subject: [PATCH 1/3] REF/TST: Corner cases for op(DataFrame, Series) --- pandas/core/ops/__init__.py | 9 -------- pandas/tests/frame/test_arithmetic.py | 30 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index 4f027843fbac1..70fe690d687c9 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -786,15 +786,6 @@ def _combine_series_frame(self, other, func, fill_value=None, axis=None, level=N else: return self._combine_match_columns(other, func, level=level) else: - if not len(other): - return self * np.nan - - if not len(self): - # Ambiguous case, use _series so works with DataFrame - return self._constructor( - data=self._series, index=self.index, columns=self.columns - ) - # default axis is columns return self._combine_match_columns(other, func, level=level) diff --git a/pandas/tests/frame/test_arithmetic.py b/pandas/tests/frame/test_arithmetic.py index fc3640503e385..f585bbe8ba776 100644 --- a/pandas/tests/frame/test_arithmetic.py +++ b/pandas/tests/frame/test_arithmetic.py @@ -663,3 +663,33 @@ def test_operations_with_interval_categories_index(self, all_arithmetic_operator result = getattr(df, op)(num) expected = pd.DataFrame([[getattr(n, op)(num) for n in data]], columns=ind) tm.assert_frame_equal(result, expected) + + +def test_frame_with_zero_len_series_corner_cases(): + # easy all-float case + df = pd.DataFrame(np.random.randn(6).reshape(3, 2), columns=["A", "B"]) + ser = pd.Series(dtype=np.float64) + + result = df + ser + expected = pd.DataFrame(df.values * np.nan, columns=df.columns) + tm.assert_frame_equal(result, expected) + + result = df == ser + expected = pd.DataFrame(False, index=df.index, columns=df.columns) + tm.assert_frame_equal(result, expected) + + # non-float case should not raise on comparison + df2 = pd.DataFrame(df.values.view("M8[ns]"), columns=df.columns) + result = df2 == ser + expected = pd.DataFrame(False, index=df.index, columns=df.columns) + tm.assert_frame_equal(result, expected) + + +def test_zero_len_frame_with_series_corner_cases(): + + df = pd.DataFrame(columns=["A", "B"], dtype=np.float64) + ser = pd.Series([1, 2], index=["A", "B"]) + + result = df + ser + expected = df + tm.assert_frame_equal(result, expected) From 8bc4616728a30f718e602080e70250cf0b095a90 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 24 Sep 2019 14:07:34 -0700 Subject: [PATCH 2/3] flake8 fixup --- pandas/tests/groupby/test_categorical.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index e09af3fd48ee6..fcc0aa3b1c015 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -782,7 +782,7 @@ def test_categorical_no_compress(): def test_sort(): - # http://stackoverflow.com/questions/23814368/sorting-pandas-categorical-labels-after-groupby # noqa: flake8 + # http://stackoverflow.com/questions/23814368/sorting-pandas-categorical-labels-after-groupby # noqa: E501 # This should result in a properly sorted Series so that the plot # has a sorted x axis # self.cat.groupby(['value_group'])['value_group'].count().plot(kind='bar') From 131d832eb1e47a9ee4139d062bb4a7ec2aa60a5f Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 25 Sep 2019 11:23:53 -0700 Subject: [PATCH 3/3] requested edits --- pandas/core/ops/__init__.py | 6 +++--- pandas/tests/frame/test_arithmetic.py | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas/core/ops/__init__.py b/pandas/core/ops/__init__.py index 70fe690d687c9..1f658296a559e 100644 --- a/pandas/core/ops/__init__.py +++ b/pandas/core/ops/__init__.py @@ -785,9 +785,9 @@ def _combine_series_frame(self, other, func, fill_value=None, axis=None, level=N return self._combine_match_index(other, func, level=level) else: return self._combine_match_columns(other, func, level=level) - else: - # default axis is columns - return self._combine_match_columns(other, func, level=level) + + # default axis is columns + return self._combine_match_columns(other, func, level=level) def _align_method_FRAME(left, right, axis): diff --git a/pandas/tests/frame/test_arithmetic.py b/pandas/tests/frame/test_arithmetic.py index f585bbe8ba776..da399750c9bcd 100644 --- a/pandas/tests/frame/test_arithmetic.py +++ b/pandas/tests/frame/test_arithmetic.py @@ -666,6 +666,7 @@ def test_operations_with_interval_categories_index(self, all_arithmetic_operator def test_frame_with_zero_len_series_corner_cases(): + # GH#28600 # easy all-float case df = pd.DataFrame(np.random.randn(6).reshape(3, 2), columns=["A", "B"]) ser = pd.Series(dtype=np.float64) @@ -686,7 +687,7 @@ def test_frame_with_zero_len_series_corner_cases(): def test_zero_len_frame_with_series_corner_cases(): - + # GH#28600 df = pd.DataFrame(columns=["A", "B"], dtype=np.float64) ser = pd.Series([1, 2], index=["A", "B"])