Skip to content

Commit 17d0f92

Browse files
committed
REGR: Preserve order by default in Index.difference
Closes pandas-dev#24959
1 parent 2b16e2e commit 17d0f92

File tree

7 files changed

+52
-13
lines changed

7 files changed

+52
-13
lines changed

doc/source/whatsnew/v0.24.1.rst

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ These are the changes in pandas 0.24.1. See :ref:`release` for a full changelog
1616
including other versions of pandas.
1717

1818

19+
.. _whatsnew_0241.regressions:
20+
21+
Fixed Regressions
22+
^^^^^^^^^^^^^^^^^
23+
24+
- Fixed regression in :class:`Index.intersection` incorrectly sorting the values by default (:issue:`24959`).
25+
1926
.. _whatsnew_0241.enhancements:
2027

2128
Enhancements

pandas/core/indexes/base.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -2333,7 +2333,7 @@ def union(self, other, sort=True):
23332333
def _wrap_setop_result(self, other, result):
23342334
return self._constructor(result, name=get_op_result_name(self, other))
23352335

2336-
def intersection(self, other, sort=True):
2336+
def intersection(self, other, sort=False):
23372337
"""
23382338
Form the intersection of two Index objects.
23392339
@@ -2342,11 +2342,15 @@ def intersection(self, other, sort=True):
23422342
Parameters
23432343
----------
23442344
other : Index or array-like
2345-
sort : bool, default True
2345+
sort : bool, default False
23462346
Sort the resulting index if possible
23472347
23482348
.. versionadded:: 0.24.0
23492349
2350+
.. versionchanged:: 0.24.1
2351+
2352+
Changed the default from ``True`` to ``False``.
2353+
23502354
Returns
23512355
-------
23522356
intersection : Index

pandas/core/indexes/datetimes.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -594,14 +594,22 @@ def _wrap_setop_result(self, other, result):
594594
name = get_op_result_name(self, other)
595595
return self._shallow_copy(result, name=name, freq=None, tz=self.tz)
596596

597-
def intersection(self, other, sort=True):
597+
def intersection(self, other, sort=False):
598598
"""
599599
Specialized intersection for DatetimeIndex objects. May be much faster
600600
than Index.intersection
601601
602602
Parameters
603603
----------
604604
other : DatetimeIndex or array-like
605+
sort : bool, default True
606+
Sort the resulting MultiIndex if possible
607+
608+
.. versionadded:: 0.24.0
609+
610+
.. versionchanged:: 0.24.1
611+
612+
Changed the default from ``True`` to ``False``.
605613
606614
Returns
607615
-------

pandas/core/indexes/interval.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1093,8 +1093,8 @@ def equals(self, other):
10931093
def overlaps(self, other):
10941094
return self._data.overlaps(other)
10951095

1096-
def _setop(op_name):
1097-
def func(self, other, sort=True):
1096+
def _setop(op_name, sort=True):
1097+
def func(self, other, sort=sort):
10981098
other = self._as_like_interval_index(other)
10991099

11001100
# GH 19016: ensure set op will not return a prohibited dtype
@@ -1128,7 +1128,7 @@ def is_all_dates(self):
11281128
return False
11291129

11301130
union = _setop('union')
1131-
intersection = _setop('intersection')
1131+
intersection = _setop('intersection', sort=False)
11321132
difference = _setop('difference')
11331133
symmetric_difference = _setop('symmetric_difference')
11341134

pandas/core/indexes/multi.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2910,7 +2910,7 @@ def union(self, other, sort=True):
29102910
return MultiIndex.from_arrays(lzip(*uniq_tuples), sortorder=0,
29112911
names=result_names)
29122912

2913-
def intersection(self, other, sort=True):
2913+
def intersection(self, other, sort=False):
29142914
"""
29152915
Form the intersection of two MultiIndex objects.
29162916
@@ -2922,6 +2922,10 @@ def intersection(self, other, sort=True):
29222922
29232923
.. versionadded:: 0.24.0
29242924
2925+
.. versionchanged:: 0.24.1
2926+
2927+
Changed the default from ``True`` to ``False``.
2928+
29252929
Returns
29262930
-------
29272931
Index

pandas/core/indexes/range.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def equals(self, other):
343343

344344
return super(RangeIndex, self).equals(other)
345345

346-
def intersection(self, other, sort=True):
346+
def intersection(self, other, sort=False):
347347
"""
348348
Form the intersection of two Index objects.
349349
@@ -355,6 +355,10 @@ def intersection(self, other, sort=True):
355355
356356
.. versionadded:: 0.24.0
357357
358+
.. versionchanged:: 0.24.1
359+
360+
Changed the default from ``True`` to ``False``.
361+
358362
Returns
359363
-------
360364
intersection : Index

pandas/tests/indexes/test_base.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,11 @@ def test_intersect_str_dates(self, sort):
765765

766766
assert len(result) == 0
767767

768+
def test_intersect_nosort(self):
769+
result = pd.Index(['c', 'b', 'a']).intersection(['b', 'a'])
770+
expected = pd.Index(['b', 'a'])
771+
tm.assert_index_equal(result, expected)
772+
768773
@pytest.mark.parametrize("sort", [True, False])
769774
def test_chained_union(self, sort):
770775
# Chained unions handles names correctly
@@ -1595,20 +1600,27 @@ def test_drop_tuple(self, values, to_drop):
15951600
for drop_me in to_drop[1], [to_drop[1]]:
15961601
pytest.raises(KeyError, removed.drop, drop_me)
15971602

1598-
@pytest.mark.parametrize("method,expected", [
1603+
@pytest.mark.parametrize("method,expected,sort", [
1604+
('intersection', np.array([(1, 'A'), (2, 'A'), (1, 'B'), (2, 'B')],
1605+
dtype=[('num', int), ('let', 'a1')]),
1606+
False),
1607+
15991608
('intersection', np.array([(1, 'A'), (1, 'B'), (2, 'A'), (2, 'B')],
1600-
dtype=[('num', int), ('let', 'a1')])),
1609+
dtype=[('num', int), ('let', 'a1')]),
1610+
True),
1611+
16011612
('union', np.array([(1, 'A'), (1, 'B'), (1, 'C'), (2, 'A'), (2, 'B'),
1602-
(2, 'C')], dtype=[('num', int), ('let', 'a1')]))
1613+
(2, 'C')], dtype=[('num', int), ('let', 'a1')]),
1614+
True)
16031615
])
1604-
def test_tuple_union_bug(self, method, expected):
1616+
def test_tuple_union_bug(self, method, expected, sort):
16051617
index1 = Index(np.array([(1, 'A'), (2, 'A'), (1, 'B'), (2, 'B')],
16061618
dtype=[('num', int), ('let', 'a1')]))
16071619
index2 = Index(np.array([(1, 'A'), (2, 'A'), (1, 'B'),
16081620
(2, 'B'), (1, 'C'), (2, 'C')],
16091621
dtype=[('num', int), ('let', 'a1')]))
16101622

1611-
result = getattr(index1, method)(index2)
1623+
result = getattr(index1, method)(index2, sort=sort)
16121624
assert result.ndim == 1
16131625

16141626
expected = Index(expected)

0 commit comments

Comments
 (0)