From a15fee01e5e5755c425433d9d476602c3e67376c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jostein=20B=C3=B8=20Fl=C3=B8ystad?= Date: Thu, 23 Apr 2015 21:39:05 +0200 Subject: [PATCH 1/3] TST: Check for type promotion in numeric operations on indexes. --- pandas/tests/test_index.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pandas/tests/test_index.py b/pandas/tests/test_index.py index 3c9dbd2e48cb6..889f0b473866e 100644 --- a/pandas/tests/test_index.py +++ b/pandas/tests/test_index.py @@ -1909,6 +1909,19 @@ def test_astype(self): self.assertTrue(i.equals(result)) self.check_is_index(result) + def test_promote_type(self): + # GH-9966 + index_int = Int64Index(np.arange(5)) + operators = (operator.add, + operator.sub, + operator.mul, + operator.truediv, + operator.floordiv) + for op in operators: + index_promoted = op(index_int, np.pi) + self.assertEqual(index_promoted.dtype, np.float64) + self.assertIsInstance(index_promoted, Float64Index) + def test_equals(self): i = Float64Index([1.0,2.0]) From d9c96cda1f76d5815cabb606fffc5acfd2cb10e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jostein=20B=C3=B8=20Fl=C3=B8ystad?= Date: Thu, 23 Apr 2015 21:52:46 +0200 Subject: [PATCH 2/3] TST: Test inplace operations on indexes that require type promotion. --- pandas/tests/test_index.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pandas/tests/test_index.py b/pandas/tests/test_index.py index 889f0b473866e..9f84f5db2a3c9 100644 --- a/pandas/tests/test_index.py +++ b/pandas/tests/test_index.py @@ -1922,6 +1922,33 @@ def test_promote_type(self): self.assertEqual(index_promoted.dtype, np.float64) self.assertIsInstance(index_promoted, Float64Index) + def test_promote_type_inplace(self): + # Related to GH-9966 + index = Int64Index(np.arange(5)) + index += np.pi + self.assertEqual(index.dtype, np.float64) + self.assertIsInstance(index, Float64Index) + + index = Int64Index(np.arange(5)) + index -= np.pi + self.assertEqual(index.dtype, np.float64) + self.assertIsInstance(index, Float64Index) + + index = Int64Index(np.arange(5)) + index *= np.pi + self.assertEqual(index.dtype, np.float64) + self.assertIsInstance(index, Float64Index) + + index = Int64Index(np.arange(5)) + index /= np.pi + self.assertEqual(index.dtype, np.float64) + self.assertIsInstance(index, Float64Index) + + index = Int64Index(np.arange(5)) + index //= np.pi + self.assertEqual(index.dtype, np.float64) + self.assertIsInstance(index, Float64Index) + def test_equals(self): i = Float64Index([1.0,2.0]) From c5ff42434dda061b2027604bc1eef413ad6fde37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jostein=20B=C3=B8=20Fl=C3=B8ystad?= Date: Sat, 25 Apr 2015 13:06:06 +0200 Subject: [PATCH 3/3] BUG: Run index through type coercion after numeric operations. Closes #9966. --- pandas/core/index.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/index.py b/pandas/core/index.py index 8b650fea9b440..bd56bf9808eb9 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -2531,7 +2531,7 @@ def _evaluate_numeric_binop(self, other): if reversed: values, other = other, values - return self._shallow_copy(op(values, other)) + return Index(op(values, other), **self._get_attributes_dict()) return _evaluate_numeric_binop