8000 Merge remote-tracking branch 'upstream/master' into 24959-union-default · pandas-dev/pandas@68b72a6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 68b72a6

Browse files
committed
Merge remote-tracking branch 'upstream/master' into 24959-union-default
2 parents 45c827c + ece58cb commit 68b72a6

File tree

7 files changed

+46
-13
lines changed

7 files changed

+46
-13
lines changed

doc/source/whatsnew/v0.24.1.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Fixed Regressions
2222

2323
- Bug in :meth:`DataFrame.itertuples` with ``records`` orient raising an ``AttributeError`` when the ``DataFrame`` contained more than 255 columns (:issue:`24939`)
2424
- Bug in :meth:`DataFrame.itertuples` orient converting integer column names to strings prepended with an underscore (:issue:`24940`)
25+
- Fixed regression in :class:`Index.intersection` incorrectly sorting the values by default (:issue:`24959`).
2526

2627
.. _whatsnew_0241.enhancements:
2728

pandas/core/indexes/base.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,7 +2358,7 @@ def union(self, other, sort=None):
23582358
def _wrap_setop_result(self, other, result):
23592359
return self._constructor(result, name=get_op_result_name(self, other))
23602360

2361-
def intersection(self, other, sort=True):
2361+
def intersection(self, other, sort=False):
23622362
"""
23632363
Form the intersection of two Index objects.
23642364
@@ -2367,11 +2367,15 @@ def intersection(self, other, sort=True):
23672367
Parameters
23682368
----------
23692369
other : Index or array-like
2370-
sort : bool, default True
2370+
sort : bool, default False
23712371
Sort the resulting index if possible
23722372
23732373
.. versionadded:: 0.24.0
23742374
2375+
.. versionchanged:: 0.24.1
2376+
2377+
Changed the default from ``True`` to ``False``.
2378+
23752379
Returns
23762380
-------
23772381
intersection : Index

pandas/core/indexes/datetimes.py

Lines changed: 9 additions & 1 deletion
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 index 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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2930,7 +2930,7 @@ def union(self, other, sort=None):
29302930
return MultiIndex.from_arrays(lzip(*uniq_tuples), sortorder=0,
29312931
names=result_names)
29322932

2933-
def intersection(self, other, sort=True):
2933+
def intersection(self, other, sort=False):
29342934
"""
29352935
Form the intersection of two MultiIndex objects.
29362936
@@ -2942,6 +2942,10 @@ def intersection(self, other, sort=True):
29422942
29432943
.. versionadded:: 0.24.0
29442944
2945+
.. versionchanged:: 0.24.1
2946+
2947+
Changed the default from ``True`` to ``False``.
2948+
29452949
Returns
29462950
-------
29472951
Index

pandas/core/indexes/range.py

Lines changed: 5 additions & 1 deletion
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

Lines changed: 17 additions & 5 deletions
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
@@ -1632,20 +1637,27 @@ def test_drop_tuple(self, values, to_drop):
16321637
for drop_me in to_drop[1], [to_drop[1]]:
16331638
pytest.raises(KeyError, removed.drop, drop_me)
16341639

1635-
@pytest.mark.parametrize("method,expected", [
1640+
@pytest.mark.parametrize("method,expected,sort", [
1641+
('intersection', np.array([(1, 'A'), (2, 'A'), (1, 'B'), (2, 'B')],
1642+
dtype=[('num', int), ('let', 'a1')]),
1643+
False),
1644+
16361645
('intersection', np.array([(1, 'A'), (1, 'B'), (2, 'A'), (2, 'B')],
1637-
dtype=[('num', int), ('let', 'a1')])),
1646+
dtype=[('num', int), ('let', 'a1')]),
1647+
True),
1648+
16381649
('union', np.array([(1, 'A'), (1, 'B'), (1, 'C'), (2, 'A'), (2, 'B'),
1639-
(2, 'C')], dtype=[('num', int), ('let', 'a1')]))
1650+
(2, 'C')], dtype=[('num', int), ('let', 'a1')]),
1651+
True)
16401652
])
1641-
def test_tuple_union_bug(self, method, expected):
1653+
def test_tuple_union_bug(self, method, expected, sort):
16421654
index1 = Index(np.array([(1, 'A'), (2, 'A'), (1, 'B'), (2, 'B')],
16431655
dtype=[('num', int), ('let', 'a1')]))
16441656
index2 = Index(np.array([(1, 'A'), (2, 'A'), (1, 'B'),
16451657
(2, 'B'), (1, 'C'), (2, 'C')],
16461658
dtype=[('num', int), ('let', 'a1')]))
16471659

1648-
result = getattr(index1, method)(index2)
1660+
result = getattr(index1, method)(index2, sort=sort)
16491661
assert result.ndim == 1
16501662

16511663
expected = Index(expected)

0 commit comments

Comments
 (0)
0