Skip to content

Commit

Permalink
REGR: Preserve order by default in Index.difference (#24967)
Browse files Browse the repository at this point in the history
Closes #24959
  • Loading branch information
TomAugspurger authored and jorisvandenbossche committed Jan 29, 2019
1 parent abf0824 commit ece58cb
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 13 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Fixed Regressions

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

.. _whatsnew_0241.enhancements:

Expand Down
8 changes: 6 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2333,7 +2333,7 @@ def union(self, other, sort=True):
def _wrap_setop_result(self, other, result):
return self._constructor(result, name=get_op_result_name(self, other))

def intersection(self, other, sort=True):
def intersection(self, other, sort=False):
"""
Form the intersection of two Index objects.
Expand All @@ -2342,11 +2342,15 @@ def intersection(self, other, sort=True):
Parameters
----------
other : Index or array-like
sort : bool, default True
sort : bool, default False
Sort the resulting index if possible
.. versionadded:: 0.24.0
.. versionchanged:: 0.24.1
Changed the default from ``True`` to ``False``.
Returns
-------
intersection : Index
Expand Down
10 changes: 9 additions & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,14 +594,22 @@ def _wrap_setop_result(self, other, result):
name = get_op_result_name(self, other)
return self._shallow_copy(result, name=name, freq=None, tz=self.tz)

def intersection(self, other, sort=True):
def intersection(self, other, sort=False):
"""
Specialized intersection for DatetimeIndex objects. May be much faster
than Index.intersection
Parameters
----------
other : DatetimeIndex or array-like
sort : bool, default True
Sort the resulting index if possible.
.. versionadded:: 0.24.0
.. versionchanged:: 0.24.1
Changed the default from ``True`` to ``False``.
Returns
-------
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1093,8 +1093,8 @@ def equals(self, other):
def overlaps(self, other):
return self._data.overlaps(other)

def _setop(op_name):
def func(self, other, sort=True):
def _setop(op_name, sort=True):
def func(self, other, sort=sort):
other = self._as_like_interval_index(other)

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

union = _setop('union')
intersection = _setop('intersection')
intersection = _setop('intersection', sort=False)
difference = _setop('difference')
symmetric_difference = _setop('symmetric_difference')

Expand Down
6 changes: 5 additions & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2910,7 +2910,7 @@ def union(self, other, sort=True):
return MultiIndex.from_arrays(lzip(*uniq_tuples), sortorder=0,
names=result_names)

def intersection(self, other, sort=True):
def intersection(self, other, sort=False):
"""
Form the intersection of two MultiIndex objects.
Expand All @@ -2922,6 +2922,10 @@ def intersection(self, other, sort=True):
.. versionadded:: 0.24.0
.. versionchanged:: 0.24.1
Changed the default from ``True`` to ``False``.
Returns
-------
Index
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def equals(self, other):

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

def intersection(self, other, sort=True):
def intersection(self, other, sort=False):
"""
Form the intersection of two Index objects.
Expand All @@ -355,6 +355,10 @@ def intersection(self, other, sort=True):
.. versionadded:: 0.24.0
.. versionchanged:: 0.24.1
Changed the default from ``True`` to ``False``.
Returns
-------
intersection : Index
Expand Down
22 changes: 17 additions & 5 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,11 @@ def test_intersect_str_dates(self, sort):

assert len(result) == 0

def test_intersect_nosort(self):
result = pd.Index(['c', 'b', 'a']).intersection(['b', 'a'])
expected = pd.Index(['b', 'a'])
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize("sort", [True, False])
def test_chained_union(self, sort):
# Chained unions handles names correctly
Expand Down Expand Up @@ -1595,20 +1600,27 @@ def test_drop_tuple(self, values, to_drop):
for drop_me in to_drop[1], [to_drop[1]]:
pytest.raises(KeyError, removed.drop, drop_me)

@pytest.mark.parametrize("method,expected", [
@pytest.mark.parametrize("method,expected,sort", [
('intersection', np.array([(1, 'A'), (2, 'A'), (1, 'B'), (2, 'B')],
dtype=[('num', int), ('let', 'a1')]),
False),
('intersection', np.array([(1, 'A'), (1, 'B'), (2, 'A'), (2, 'B')],
dtype=[('num', int), ('let', 'a1')])),
dtype=[('num', int), ('let', 'a1')]),
True),
('union', np.array([(1, 'A'), (1, 'B'), (1, 'C'), (2, 'A'), (2, 'B'),
(2, 'C')], dtype=[('num', int), ('let', 'a1')]))
(2, 'C')], dtype=[('num', int), ('let', 'a1')]),
True)
])
def test_tuple_union_bug(self, method, expected):
def test_tuple_union_bug(self, method, expected, sort):
index1 = Index(np.array([(1, 'A'), (2, 'A'), (1, 'B'), (2, 'B')],
dtype=[('num', int), ('let', 'a1')]))
index2 = Index(np.array([(1, 'A'), (2, 'A'), (1, 'B'),
(2, 'B'), (1, 'C'), (2, 'C')],
dtype=[('num', int), ('let', 'a1')]))

result = getattr(index1, method)(index2)
result = getattr(index1, method)(index2, sort=sort)
assert result.ndim == 1

expected = Index(expected)
Expand Down

0 comments on commit ece58cb

Please sign in to comment.