diff --git a/doc/source/whatsnew/v0.17.0.txt b/doc/source/whatsnew/v0.17.0.txt index 322f431a37a79..b50ac38d81ade 100644 --- a/doc/source/whatsnew/v0.17.0.txt +++ b/doc/source/whatsnew/v0.17.0.txt @@ -372,6 +372,7 @@ Bug Fixes - Bug in ``pd.get_dummies`` with `sparse=True` not returning ``SparseDataFrame`` (:issue:`10531`) - Bug in ``Index`` subtypes (such as ``PeriodIndex``) not returning their own type for ``.drop`` and ``.insert`` methods (:issue:`10620`) +- Bug in subclasses of ``Index`` with no values returned Index objects rather than their own classes, in some cases (:issue:`10596`) diff --git a/pandas/core/index.py b/pandas/core/index.py index ea9d1f38b92c1..1ac0cc1730fa2 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -1538,7 +1538,7 @@ def difference(self, other): self._assert_can_do_setop(other) if self.equals(other): - return Index([], name=self.name) + return self._shallow_copy(np.asarray([])) other, result_name = self._convert_can_do_setop(other) diff --git a/pandas/tests/test_index.py b/pandas/tests/test_index.py index 81c6366b4cb41..3c1fd2144c101 100644 --- a/pandas/tests/test_index.py +++ b/pandas/tests/test_index.py @@ -367,6 +367,12 @@ def test_difference_base(self): with tm.assertRaisesRegexp(TypeError, msg): result = first.difference([1, 2, 3]) + # GH 10596 - empty difference retains index's type + + result = idx.difference(idx) + expected = idx[0:0] + self.assertTrue(result.equals(expected)) + def test_symmetric_diff(self): for name, idx in compat.iteritems(self.indices): first = idx[1:] diff --git a/pandas/tseries/period.py b/pandas/tseries/period.py index 242d9a7757556..ac0f9b6cf05c9 100644 --- a/pandas/tseries/period.py +++ b/pandas/tseries/period.py @@ -733,9 +733,9 @@ def __getitem__(self, key): # values = np.asarray(list(values), dtype=object) # return values.reshape(result.shape) - return PeriodIndex(result, name=self.name, freq=self.freq) + return self._shallow_copy(result) - return PeriodIndex(result, name=self.name, freq=self.freq) + return self._shallow_copy(result) def _format_native_types(self, na_rep=u('NaT'), **kwargs): @@ -796,7 +796,7 @@ def append(self, other): to_concat = [x.asobject.values for x in to_concat] else: cat_values = np.concatenate([x.values for x in to_concat]) - return PeriodIndex(cat_values, freq=self.freq, name=name) + return self._shallow_copy(cat_values, name=name) to_concat = [x.values if isinstance(x, Index) else x for x in to_concat] diff --git a/pandas/tseries/tests/test_base.py b/pandas/tseries/tests/test_base.py index 1b38f51ed4f71..9219616f20cdc 100644 --- a/pandas/tseries/tests/test_base.py +++ b/pandas/tseries/tests/test_base.py @@ -644,7 +644,7 @@ def test_dti_dti_deprecated_ops(self): with tm.assert_produces_warning(FutureWarning): result = dti-dti - expected = Index([]) + expected = dti[0:0] tm.assert_index_equal(result,expected) with tm.assert_produces_warning(FutureWarning): @@ -654,7 +654,7 @@ def test_dti_dti_deprecated_ops(self): with tm.assert_produces_warning(FutureWarning): result = dti_tz-dti_tz - expected = Index([]) + expected = dti_tz[0:0] tm.assert_index_equal(result,expected) with tm.assert_produces_warning(FutureWarning):