Skip to content

Empty subtypes of Index return their type, rather than Index #10687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)



Expand Down
2 changes: 1 addition & 1 deletion pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:]
Expand Down
6 changes: 3 additions & 3 deletions pandas/tseries/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions pandas/tseries/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down