Skip to content

Commit 38aeacb

Browse files
committed
Remove set difference fix
1 parent ba9ccdd commit 38aeacb

File tree

8 files changed

+7
-45
lines changed

8 files changed

+7
-45
lines changed

doc/source/whatsnew/v0.23.0.txt

-1
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,6 @@ Indexing
838838
- Bug in :class:`IntervalIndex` where set operations that returned an empty ``IntervalIndex`` had the wrong dtype (:issue:`19101`)
839839
- Bug in :meth:`DataFrame.drop_duplicates` where no ``KeyError`` is raised when passing in columns that don't exist on the ``DataFrame`` (issue:`19726`)
840840
- Bug in :func:`Index.union` and :func:`Index.intersection` where name of the ``Index`` of the result was not computed correctly for certain cases (:issue:`9943`, :issue:`9862`)
841-
- Bug in :func:`Index.difference` when taking difference of ``Index`` and itself as type was not preserved. (:issue:`19849`)
842841

843842

844843
MultiIndex

pandas/core/indexes/base.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -2454,13 +2454,6 @@ def intersection(self, other):
24542454
taken.name = None
24552455
return taken
24562456

2457-
def _create_empty_index(self, name):
2458-
"""
2459-
Returns an empty index. Overridden as necessary by
2460-
subclasses that have different constructors.
2461-
"""
2462-
return self.__class__([], name=name)
2463-
24642457
def difference(self, other):
24652458
"""
24662459
Return a new Index with elements from the index that are not in
@@ -2489,7 +2482,7 @@ def difference(self, other):
24892482
self._assert_can_do_setop(other)
24902483

24912484
if self.equals(other):
2492-
return self._create_empty_index(get_op_result_name(self, other))
2485+
return Index([], name=self.name)
24932486

24942487
other, result_name = self._convert_can_do_setop(other)
24952488

pandas/core/indexes/category.py

-7
Original file line numberDiff line numberDiff line change
@@ -723,13 +723,6 @@ def insert(self, loc, item):
723723
codes = np.concatenate((codes[:loc], code, codes[loc:]))
724724
return self._create_from_codes(codes)
725725

726-
def _create_empty_index(self, name):
727-
"""
728-
Returns an empty index using categories and ordered of self
729-
"""
730-
return CategoricalIndex([], categories=self.categories,
731-
ordered=self.ordered, name=name)
732-
733726
def _concat(self, to_concat, name):
734727
# if calling index is category, don't check dtype of others
735728
return CategoricalIndex._concat_same_dtype(self, to_concat, name)

pandas/core/indexes/multi.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2733,7 +2733,7 @@ def intersection(self, other):
27332733
other_tuples = other._ndarray_values
27342734
uniq_tuples = sorted(set(self_tuples) & set(other_tuples))
27352735
if len(uniq_tuples) == 0:
2736-
return MultiIndex(levels=self.levels,
2736+
return MultiIndex(levels=[[]] * self.nlevels,
27372737
labels=[[]] * self.nlevels,
27382738
names=result_names, verify_integrity=False)
27392739
else:
@@ -2755,7 +2755,7 @@ def difference(self, other):
27552755
return self
27562756

27572757
if self.equals(other):
2758-
return MultiIndex(levels=self.levels,
2758+
return MultiIndex(levels=[[]] * self.nlevels,
27592759
labels=[[]] * self.nlevels,
27602760
names=result_names, verify_integrity=False)
27612761

pandas/core/indexes/period.py

-6
Original file line numberDiff line numberDiff line change
@@ -963,12 +963,6 @@ def _convert_tolerance(self, tolerance, target):
963963
'target index size')
964964
return self._maybe_convert_timedelta(tolerance)
965965

966-
def _create_empty_index(self, name):
967-
"""
968-
Returns an empty index using freq of self
969-
"""
970-
return PeriodIndex([], freq=self.freq, name=name)
971-
972966
def insert(self, loc, item):
973967
if not isinstance(item, Period) or self.freq != item.freq:
974968
return self.astype(object).insert(loc, item)

pandas/core/indexes/range.py

-6
Original file line numberDiff line numberDiff line change
@@ -479,12 +479,6 @@ def join(self, other, how='left', level=None, return_indexers=False,
479479
def _concat_same_dtype(self, indexes, name):
480480
return _concat._concat_rangeindex_same_dtype(indexes).rename(name)
481481

482-
def _create_empty_index(self, name):
483-
"""
484-
Returns an empty index using step size of self
485-
"""
486-
return RangeIndex(start=None, stop=None, step=self._step, name=name)
487-
488482
def __len__(self):
489483
"""
490484
return the length of the RangeIndex

pandas/tests/indexes/test_base.py

-11
Original file line numberDiff line numberDiff line change
@@ -997,17 +997,6 @@ def test_iadd_string(self):
997997
index += '_x'
998998
assert 'a_x' in index
999999

1000-
def test_difference_type(self):
1001-
# GH 9943 9862
1002-
# If taking difference of a set and itself, it
1003-
# needs to preserve the type of the index
1004-
skip_index_keys = ['repeats']
1005-
for key, id in self.indices.items():
1006-
if key not in skip_index_keys:
1007-
result = id.difference(id)
1008-
expected = self.create_empty_index(id)
1009-
tm.assert_index_equal(result, expected)
1010-
10111000
def test_difference(self):
10121001

10131002
first = self.strIndex[5:20]

pandas/tests/reshape/test_concat.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -2055,10 +2055,10 @@ def test_concat_categoricalindex(self):
20552055

20562056
result = pd.concat([a, b, c], axis=1)
20572057

2058-
exp_idx = pd.CategoricalIndex([9, 0, 1, 2], categories=categories)
2059-
exp = pd.DataFrame({0: [1, 1, np.nan, np.nan],
2060-
1: [np.nan, 2, 2, np.nan],
2061-
2: [np.nan, np.nan, 3, 3]},
2058+
exp_idx = pd.CategoricalIndex([0, 1, 2, 9])
2059+
exp = pd.DataFrame({0: [1, np.nan, np.nan, 1],
2060+
1: [2, 2, np.nan, np.nan],
2061+
2: [np.nan, 3, 3, np.nan]},
20622062
columns=[0, 1, 2],
20632063
index=exp_idx)
20642064
tm.assert_frame_equal(result, exp)

0 commit comments

Comments
 (0)