From 1cd1399fd808a248db9dc0fd17d1f141f0e430c7 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Tue, 2 Jan 2018 17:02:15 -0800 Subject: [PATCH 1/7] Tests for TDI issues already fixed fix a couple others caused by failing to return NotImplemented or to override names correctly --- doc/source/whatsnew/v0.23.0.txt | 4 +- pandas/core/indexes/base.py | 4 ++ pandas/core/ops.py | 6 ++ .../indexes/timedeltas/test_arithmetic.py | 10 +-- pandas/tests/series/test_operators.py | 65 +++++++++++++++++++ 5 files changed, 84 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index bd3bee507baa3..1b3ab79f3a548 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -300,7 +300,9 @@ Conversion - Bug in :class:`FY5253` where ``datetime`` addition and subtraction incremented incorrectly for dates on the year-end but not normalized to midnight (:issue:`18854`) - Bug in :class:`DatetimeIndex` where adding or subtracting an array-like of ``DateOffset`` objects either raised (``np.array``, ``pd.Index``) or broadcast incorrectly (``pd.Series``) (:issue:`18849`) - Bug in :class:`Series` floor-division where operating on a scalar ``timedelta`` raises an exception (:issue:`18846`) - +- Bug in :class:`Series`` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` had results cast to ``dtype='int64'`` (:issue:`17250`) +- Bug in :class:`TimedeltaIndex` where multiplication or division by a ``Series`` would return a ``TimedeltaIndex`` instead of a ``Series`` (issue:`18963`) +- Bug in :class:`Series` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` could return a ``Series`` with an incorrect name (issue:`19043`) Indexing ^^^^^^^^ diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 52c4a1ad9865a..4a9ff3f1474e7 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4020,6 +4020,10 @@ def _add_numeric_methods_binary(cls): def _make_evaluate_binop(op, opstr, reversed=False, constructor=Index): def _evaluate_numeric_binop(self, other): + if (isinstance(other, ABCSeries) and + self.dtype == "timedelta64[ns]"): + # GH#19042 + return NotImplemented other = self._validate_for_numeric_binop(other, op, opstr) # handle time-based others diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 0229f7c256464..75a3672b30272 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -775,6 +775,12 @@ def wrapper(left, right, name=name, na_op=na_op): res_name = left.name result = wrap_results(safe_na_op(lvalues, rvalues)) + try: + # if res_name is None we may need to override `result.name` + result.name = res_name + except AttributeError: + # np.ndarray has no name + pass return construct_result( left, result, diff --git a/pandas/tests/indexes/timedeltas/test_arithmetic.py b/pandas/tests/indexes/timedeltas/test_arithmetic.py index 3ecfcaff63bc5..ea577ebfd6a4b 100644 --- a/pandas/tests/indexes/timedeltas/test_arithmetic.py +++ b/pandas/tests/indexes/timedeltas/test_arithmetic.py @@ -70,11 +70,13 @@ def test_numeric_compat(self): tm.assert_index_equal(result, didx) result = idx * Series(np.arange(5, dtype='int64')) - tm.assert_index_equal(result, didx) + expected = Series(didx) + tm.assert_series_equal(result, expected) - result = idx * Series(np.arange(5, dtype='float64') + 0.1) - tm.assert_index_equal(result, self._holder(np.arange( - 5, dtype='float64') * (np.arange(5, dtype='float64') + 0.1))) + rng5 = np.arange(5, dtype='float64') + result = idx * Series(rng5 + 0.1) + expected = Series(self._holder(rng5 * (rng5 + 0.1))) + tm.assert_series_equal(result, expected) # invalid pytest.raises(TypeError, lambda: idx * idx) diff --git a/pandas/tests/series/test_operators.py b/pandas/tests/series/test_operators.py index a421f2cb15bba..3f6dce8b8dca0 100644 --- a/pandas/tests/series/test_operators.py +++ b/pandas/tests/series/test_operators.py @@ -1038,6 +1038,71 @@ def test_timedelta_floordiv(self, scalar_td): expected = Series([0, 0, np.nan]) tm.assert_series_equal(result, expected) + @pytest.mark.parametrize('names', [(None, None, None), + ('Egon', 'Venkman', None), + ('NCC1701D', 'NCC1701D', 'NCC1701D')]) + def test_td64_series_with_tdi(self, names): + # GH#17250 make sure result dtype is correct + # GH#19043 make sure names are propogated correctly + tdi = pd.TimedeltaIndex(['0 days', '1 day'], name=names[0]) + ser = Series([Timedelta(hours=3), Timedelta(hours=4)], name=names[1]) + expected = Series([Timedelta(hours=3), Timedelta(days=1, hours=4)], + name=names[2]) + + result = tdi + ser + tm.assert_series_equal(result, expected) + assert result.dtype == 'timedelta64[ns]' + + result = ser + tdi + tm.assert_series_equal(result, expected) + assert result.dtype == 'timedelta64[ns]' + + expected = Series([Timedelta(hours=-3), Timedelta(days=1, hours=-4)], + name=names[2]) + + result = tdi - ser + tm.assert_series_equal(result, expected) + assert result.dtype == 'timedelta64[ns]' + + result = ser - tdi + tm.assert_series_equal(result, -expected) + assert result.dtype == 'timedelta64[ns]' + + @pytest.mark.parametrize('names', [(None, None, None), + ('Egon', 'Venkman', None), + ('NCC1701D', 'NCC1701D', 'NCC1701D')]) + def test_tdi_mul_int_series(self, names): + # GH#19042 + tdi = pd.TimedeltaIndex(['0days', '1day', '2days', '3days', '4days'], + name=names[0]) + ser = Series([0, 1, 2, 3, 4], dtype=np.int64, name=names[1]) + + expected = Series(['0days', '1day', '4days', '9days', '16days'], + dtype='timedelta64[ns]', + name=names[2]) + + result = tdi * ser + tm.assert_series_equal(result, expected) + + result = ser * tdi + tm.assert_series_equal(result, expected) + + @pytest.mark.parametrize('names', [(None, None, None), + ('Egon', 'Venkman', None), + ('NCC1701D', 'NCC1701D', 'NCC1701D')]) + def test_tdi_div_float_series(self, names): + # GH#19042 + tdi = pd.TimedeltaIndex(['0days', '1day', '2days', '3days', '4days'], + name=names[0]) + ser = Series([1.5, 3, 4.5, 6, 7.5], dtype=np.float64, name=names[1]) + + expected = Series([tdi[n] / ser[n] for n in range(len(ser))], + dtype='timedelta64[ns]', + name=names[2]) + + result = tdi / ser + tm.assert_series_equal(result, expected) + class TestDatetimeSeriesArithmetic(object): @pytest.mark.parametrize( From 4d2333f3ecdeae9ed6ae6148da08cd05be7f4937 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Tue, 2 Jan 2018 19:13:00 -0800 Subject: [PATCH 2/7] implement xfail test for index classes that dont defer to Series correctly --- doc/source/whatsnew/v0.23.0.txt | 2 +- pandas/core/indexes/base.py | 3 ++- pandas/tests/indexes/test_numeric.py | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index 1b3ab79f3a548..f5d0669c7d523 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -301,7 +301,7 @@ Conversion - Bug in :class:`DatetimeIndex` where adding or subtracting an array-like of ``DateOffset`` objects either raised (``np.array``, ``pd.Index``) or broadcast incorrectly (``pd.Series``) (:issue:`18849`) - Bug in :class:`Series` floor-division where operating on a scalar ``timedelta`` raises an exception (:issue:`18846`) - Bug in :class:`Series`` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` had results cast to ``dtype='int64'`` (:issue:`17250`) -- Bug in :class:`TimedeltaIndex` where multiplication or division by a ``Series`` would return a ``TimedeltaIndex`` instead of a ``Series`` (issue:`18963`) +- Bug in :class:`TimedeltaIndex` where multiplication or division by a ``Series`` would return a ``TimedeltaIndex`` instead of a ``Series`` (issue:`19042`) - Bug in :class:`Series` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` could return a ``Series`` with an incorrect name (issue:`19043`) Indexing diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 4a9ff3f1474e7..f3f98fc3348ab 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4022,7 +4022,8 @@ def _make_evaluate_binop(op, opstr, reversed=False, constructor=Index): def _evaluate_numeric_binop(self, other): if (isinstance(other, ABCSeries) and self.dtype == "timedelta64[ns]"): - # GH#19042 + # GH#19042 This needs to be changed for all Index classes, + # but is only being handled for TimedeltaIndex in this PR. return NotImplemented other = self._validate_for_numeric_binop(other, op, opstr) diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index dcd592345b91c..5426ff39ef5ec 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -114,6 +114,21 @@ def test_numeric_compat(self): result = idx**2.0 tm.assert_index_equal(result, expected) + @pytest.mark.xfail(reason="Index._add_numeric_methods_binary does not " + "return NotImplemented when operating against " + "a Series.") + def test_index_with_series(self): + # GH#19044 + idx = self.create_index() + didx = idx * idx + result = idx * Series(np.arange(5, dtype=arr_dtype)) + tm.assert_series_equal(result, Series(didx)) + + result = idx * Series(np.arange(5, dtype='float64') + 0.1) + expected = Series(Float64Index(np.arange(5, dtype='float64') * + (np.arange(5, dtype='float64') + 0.1))) + tm.assert_series_equal(result, expected) + def test_explicit_conversions(self): # GH 8608 From 3b02815341391d5a6c0ce3f8b9c38c65573cc806 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Tue, 2 Jan 2018 19:26:48 -0800 Subject: [PATCH 3/7] flake8 fixup --- pandas/tests/indexes/test_numeric.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index 5426ff39ef5ec..d04246c342156 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -115,18 +115,19 @@ def test_numeric_compat(self): tm.assert_index_equal(result, expected) @pytest.mark.xfail(reason="Index._add_numeric_methods_binary does not " - "return NotImplemented when operating against " - "a Series.") + "return NotImplemented when operating against " + "a Series.") def test_index_with_series(self): # GH#19044 idx = self.create_index() didx = idx * idx + arr_dtype = 'uint64' if isinstance(idx, UInt64Index) else 'int64' result = idx * Series(np.arange(5, dtype=arr_dtype)) tm.assert_series_equal(result, Series(didx)) result = idx * Series(np.arange(5, dtype='float64') + 0.1) expected = Series(Float64Index(np.arange(5, dtype='float64') * - (np.arange(5, dtype='float64') + 0.1))) + (np.arange(5, dtype='float64') + 0.1))) tm.assert_series_equal(result, expected) def test_explicit_conversions(self): From 0cd06e4c8feaec2a5619eb86263cfcf945602a36 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Wed, 3 Jan 2018 09:06:16 -0800 Subject: [PATCH 4/7] Suggested edits --- doc/source/whatsnew/v0.23.0.txt | 1 + pandas/core/indexes/base.py | 5 ----- pandas/core/indexes/timedeltas.py | 19 +++++++++++++++++++ pandas/core/ops.py | 12 +++++------- pandas/tests/indexes/test_numeric.py | 16 ---------------- .../indexes/timedeltas/test_arithmetic.py | 3 +-- 6 files changed, 26 insertions(+), 30 deletions(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index f5d0669c7d523..c06282ec039b9 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -303,6 +303,7 @@ Conversion - Bug in :class:`Series`` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` had results cast to ``dtype='int64'`` (:issue:`17250`) - Bug in :class:`TimedeltaIndex` where multiplication or division by a ``Series`` would return a ``TimedeltaIndex`` instead of a ``Series`` (issue:`19042`) - Bug in :class:`Series` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` could return a ``Series`` with an incorrect name (issue:`19043`) +- Indexing ^^^^^^^^ diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index f3f98fc3348ab..52c4a1ad9865a 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4020,11 +4020,6 @@ def _add_numeric_methods_binary(cls): def _make_evaluate_binop(op, opstr, reversed=False, constructor=Index): def _evaluate_numeric_binop(self, other): - if (isinstance(other, ABCSeries) and - self.dtype == "timedelta64[ns]"): - # GH#19042 This needs to be changed for all Index classes, - # but is only being handled for TimedeltaIndex in this PR. - return NotImplemented other = self._validate_for_numeric_binop(other, op, opstr) # handle time-based others diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index d28a09225e8b8..109b08c244e74 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -371,6 +371,9 @@ def _add_delta(self, delta): return result def _evaluate_with_timedelta_like(self, other, op, opstr): + if isinstance(other, ABCSeries): + # GH#19042 + return NotImplemented # allow division by a timedelta if opstr in ['__div__', '__truediv__', '__floordiv__']: @@ -912,6 +915,22 @@ def delete(self, loc): TimedeltaIndex._add_datetimelike_methods() +def _override_tdi_arith_methods(): + # GH#19042 + # TODO: Eventually just do this correctly in indexes.base + tdi_mul = TimedeltaIndex.__mul__ + + def __mul__(self, other): + if isinstance(other, ABCSeries): + return NotImplemented + return tdi_mul(self, other) + + TimedeltaIndex.__mul__ = __mul__ + + +_override_tdi_arith_methods() + + def _is_convertible_to_index(other): """ return a boolean whether I can attempt conversion to a TimedeltaIndex diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 75a3672b30272..201c3842f1e44 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -684,7 +684,11 @@ def _align_method_SERIES(left, right, align_asobject=False): def _construct_result(left, result, index, name, dtype): - return left._constructor(result, index=index, name=name, dtype=dtype) + out = left._constructor(result, index=index, name=name, dtype=dtype) + + # If `result` has a non-None name and name is None, we need to override. + out.name = name + return out def _construct_divmod_result(left, result, index, name, dtype): @@ -775,12 +779,6 @@ def wrapper(left, right, name=name, na_op=na_op): res_name = left.name result = wrap_results(safe_na_op(lvalues, rvalues)) - try: - # if res_name is None we may need to override `result.name` - result.name = res_name - except AttributeError: - # np.ndarray has no name - pass return construct_result( left, result, diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index d04246c342156..dcd592345b91c 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -114,22 +114,6 @@ def test_numeric_compat(self): result = idx**2.0 tm.assert_index_equal(result, expected) - @pytest.mark.xfail(reason="Index._add_numeric_methods_binary does not " - "return NotImplemented when operating against " - "a Series.") - def test_index_with_series(self): - # GH#19044 - idx = self.create_index() - didx = idx * idx - arr_dtype = 'uint64' if isinstance(idx, UInt64Index) else 'int64' - result = idx * Series(np.arange(5, dtype=arr_dtype)) - tm.assert_series_equal(result, Series(didx)) - - result = idx * Series(np.arange(5, dtype='float64') + 0.1) - expected = Series(Float64Index(np.arange(5, dtype='float64') * - (np.arange(5, dtype='float64') + 0.1))) - tm.assert_series_equal(result, expected) - def test_explicit_conversions(self): # GH 8608 diff --git a/pandas/tests/indexes/timedeltas/test_arithmetic.py b/pandas/tests/indexes/timedeltas/test_arithmetic.py index ea577ebfd6a4b..017a64f3dafbb 100644 --- a/pandas/tests/indexes/timedeltas/test_arithmetic.py +++ b/pandas/tests/indexes/timedeltas/test_arithmetic.py @@ -70,8 +70,7 @@ def test_numeric_compat(self): tm.assert_index_equal(result, didx) result = idx * Series(np.arange(5, dtype='int64')) - expected = Series(didx) - tm.assert_series_equal(result, expected) + tm.assert_series_equal(result, Series(didx)) rng5 = np.arange(5, dtype='float64') result = idx * Series(rng5 + 0.1) From d6a57528da8fedeb5f7baa8497d70d88a94774fd Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Wed, 3 Jan 2018 16:56:50 -0800 Subject: [PATCH 5/7] requested edits, un-fix __mul__ --- doc/source/whatsnew/v0.23.0.txt | 2 +- pandas/core/indexes/timedeltas.py | 16 ---------------- pandas/core/ops.py | 6 +++++- .../tests/indexes/timedeltas/test_arithmetic.py | 9 ++++----- 4 files changed, 10 insertions(+), 23 deletions(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index a76b92abc735d..6a91a1a398ab1 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -309,7 +309,7 @@ Conversion - Bug in :class:`Series` floor-division where operating on a scalar ``timedelta`` raises an exception (:issue:`18846`) - Bug in :class:`Index` constructor with ``dtype=CategoricalDtype(...)`` where ``categories`` and ``ordered`` are not maintained (issue:`19032`) - Bug in :class:`Series`` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` had results cast to ``dtype='int64'`` (:issue:`17250`) -- Bug in :class:`TimedeltaIndex` where multiplication or division by a ``Series`` would return a ``TimedeltaIndex`` instead of a ``Series`` (issue:`19042`) +- Bug in :class:`TimedeltaIndex` where division by a ``Series`` would return a ``TimedeltaIndex`` instead of a ``Series`` (issue:`19042`) - Bug in :class:`Series` with ``dtype='timedelta64[ns]`` where addition or subtraction of ``TimedeltaIndex`` could return a ``Series`` with an incorrect name (issue:`19043`) - diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index 109b08c244e74..39ca4f5c8fbdf 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -915,22 +915,6 @@ def delete(self, loc): TimedeltaIndex._add_datetimelike_methods() -def _override_tdi_arith_methods(): - # GH#19042 - # TODO: Eventually just do this correctly in indexes.base - tdi_mul = TimedeltaIndex.__mul__ - - def __mul__(self, other): - if isinstance(other, ABCSeries): - return NotImplemented - return tdi_mul(self, other) - - TimedeltaIndex.__mul__ = __mul__ - - -_override_tdi_arith_methods() - - def _is_convertible_to_index(other): """ return a boolean whether I can attempt conversion to a TimedeltaIndex diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 28426e0e5e577..00da9a4257ea6 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -597,9 +597,13 @@ def _align_method_SERIES(left, right, align_asobject=False): def _construct_result(left, result, index, name, dtype): + """ + If the raw op result has a non-None name (e.g. it is an Index object) and + the name argument is None, then passing name to the constructor will + not be enough; we still need to override the name attribute. + """ out = left._constructor(result, index=index, name=name, dtype=dtype) - # If `result` has a non-None name and name is None, we need to override. out.name = name return out diff --git a/pandas/tests/indexes/timedeltas/test_arithmetic.py b/pandas/tests/indexes/timedeltas/test_arithmetic.py index 017a64f3dafbb..3ecfcaff63bc5 100644 --- a/pandas/tests/indexes/timedeltas/test_arithmetic.py +++ b/pandas/tests/indexes/timedeltas/test_arithmetic.py @@ -70,12 +70,11 @@ def test_numeric_compat(self): tm.assert_index_equal(result, didx) result = idx * Series(np.arange(5, dtype='int64')) - tm.assert_series_equal(result, Series(didx)) + tm.assert_index_equal(result, didx) - rng5 = np.arange(5, dtype='float64') - result = idx * Series(rng5 + 0.1) - expected = Series(self._holder(rng5 * (rng5 + 0.1))) - tm.assert_series_equal(result, expected) + result = idx * Series(np.arange(5, dtype='float64') + 0.1) + tm.assert_index_equal(result, self._holder(np.arange( + 5, dtype='float64') * (np.arange(5, dtype='float64') + 0.1))) # invalid pytest.raises(TypeError, lambda: idx * idx) From bf09444e73e8809778c731ae690a1492922e5de4 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Wed, 3 Jan 2018 23:06:16 -0800 Subject: [PATCH 6/7] weaken tests for un-fixed methods --- pandas/tests/series/test_operators.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pandas/tests/series/test_operators.py b/pandas/tests/series/test_operators.py index 6aaac797228d5..7ccc9f7ff6676 100644 --- a/pandas/tests/series/test_operators.py +++ b/pandas/tests/series/test_operators.py @@ -1088,17 +1088,20 @@ def test_tdi_mul_int_series(self, names): dtype='timedelta64[ns]', name=names[2]) - result = tdi * ser + result = ser * tdi tm.assert_series_equal(result, expected) - result = ser * tdi + # The direct operation tdi * ser still needs to be fixed. + result = ser.__rmul__(tdi) tm.assert_series_equal(result, expected) @pytest.mark.parametrize('names', [(None, None, None), ('Egon', 'Venkman', None), ('NCC1701D', 'NCC1701D', 'NCC1701D')]) - def test_tdi_div_float_series(self, names): + def test_float_series_rdiv_tdi(self, names): # GH#19042 + # TODO: the direct operation TimedeltaIndex / Series still + # needs to be fixed. tdi = pd.TimedeltaIndex(['0days', '1day', '2days', '3days', '4days'], name=names[0]) ser = Series([1.5, 3, 4.5, 6, 7.5], dtype=np.float64, name=names[1]) @@ -1107,7 +1110,7 @@ def test_tdi_div_float_series(self, names): dtype='timedelta64[ns]', name=names[2]) - result = tdi / ser + result = ser.__rdiv__(tdi) tm.assert_series_equal(result, expected) From 6f7e5b25bf0ce2b1647773eb8aa9791ecb15bcac Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Thu, 4 Jan 2018 09:25:58 -0800 Subject: [PATCH 7/7] requested edits --- pandas/core/ops.py | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 00da9a4257ea6..99f7e7309d463 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -602,7 +602,7 @@ def _construct_result(left, result, index, name, dtype): the name argument is None, then passing name to the constructor will not be enough; we still need to override the name attribute. """ - out = left._constructor(result, index=index, name=name, dtype=dtype) + out = left._constructor(result, index=index, dtype=dtype) out.name = name return out @@ -695,21 +695,10 @@ def wrapper(left, right, name=name, na_op=na_op): not isinstance(lvalues, ABCDatetimeIndex)): lvalues = lvalues.values - if isinstance(right, (ABCSeries, pd.Index)): - # `left` is always a Series object - res_name = _maybe_match_name(left, right) - else: - res_name = left.name - result = wrap_results(safe_na_op(lvalues, rvalues)) res_name = _get_series_op_result_name(left, right) - return construct_result( - left, - result, - index=left.index, - name=res_name, - dtype=dtype, - ) + return construct_result(left, result, + index=left.index, name=res_name, dtype=dtype) return wrapper