From e2745283aadd87dd2685f796a931cbf5b05534e8 Mon Sep 17 00:00:00 2001 From: maximilianr Date: Wed, 15 Jul 2015 20:16:51 -0400 Subject: [PATCH 1/2] create empty series by calling _shallow_copy rather than index constructor --- doc/source/whatsnew/v0.17.0.txt | 2 ++ pandas/core/index.py | 2 +- pandas/tests/test_index.py | 6 ++++++ pandas/tseries/period.py | 6 +++--- pandas/tseries/tests/test_base.py | 4 ++-- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v0.17.0.txt b/doc/source/whatsnew/v0.17.0.txt index 322f431a37a79..55e6b9504b43f 100644 --- a/doc/source/whatsnew/v0.17.0.txt +++ b/doc/source/whatsnew/v0.17.0.txt @@ -357,11 +357,13 @@ Bug Fixes - Bug in ``ExcelReader`` when worksheet is empty (:issue:`6403`) - Bug in ``Table.select_column`` where name is not preserved (:issue:`10392`) - Bug in ``offsets.generate_range`` where ``start`` and ``end`` have finer precision than ``offset`` (:issue:`9907`) +- Bug in ``Subclasses of Index with no values returned Index objects rather than their own classes, in some cases`` (:issue:`10596`) - Bug in ``pd.rolling_*`` where ``Series.name`` would be lost in the output (:issue:`10565`) + - Bug in ``DataFrame.interpolate`` with ``axis=1`` and ``inplace=True`` (:issue:`10395`) - Bug in ``io.sql.get_schema`` when specifying multiple columns as primary key (:issue:`10385`). 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): From e138c0a0f4f4eaa64792e4bd226ccda6b929a1c8 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Thu, 23 Jul 2015 23:49:31 -0400 Subject: [PATCH 2/2] what's new --- doc/source/whatsnew/v0.17.0.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.17.0.txt b/doc/source/whatsnew/v0.17.0.txt index 55e6b9504b43f..b50ac38d81ade 100644 --- a/doc/source/whatsnew/v0.17.0.txt +++ b/doc/source/whatsnew/v0.17.0.txt @@ -357,13 +357,11 @@ Bug Fixes - Bug in ``ExcelReader`` when worksheet is empty (:issue:`6403`) - Bug in ``Table.select_column`` where name is not preserved (:issue:`10392`) - Bug in ``offsets.generate_range`` where ``start`` and ``end`` have finer precision than ``offset`` (:issue:`9907`) -- Bug in ``Subclasses of Index with no values returned Index objects rather than their own classes, in some cases`` (:issue:`10596`) - Bug in ``pd.rolling_*`` where ``Series.name`` would be lost in the output (:issue:`10565`) - - Bug in ``DataFrame.interpolate`` with ``axis=1`` and ``inplace=True`` (:issue:`10395`) - Bug in ``io.sql.get_schema`` when specifying multiple columns as primary key (:issue:`10385`). @@ -374,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`)