Skip to content

Commit 75bab9a

Browse files
mroeschkemeeseeksmachine
authored andcommitted
Backport PR pandas-dev#53495: BUG: RangeIndex.union(sort=True) with another RangeIndex
1 parent 4b2b518 commit 75bab9a

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

doc/source/whatsnew/v2.0.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Fixed regressions
2121

2222
Bug fixes
2323
~~~~~~~~~
24+
- Bug in :func:`RangeIndex.union` when using ``sort=True`` with another :class:`RangeIndex` (:issue:`53490`)
2425
- Bug in :func:`read_csv` when defining ``dtype`` with ``bool[pyarrow]`` for the ``"c"`` and ``"python"`` engines (:issue:`53390`)
2526
- Bug in :meth:`Series.str.split` and :meth:`Series.str.rsplit` with ``expand=True`` for :class:`ArrowDtype` with ``pyarrow.string`` (:issue:`53532`)
2627
-

pandas/core/indexes/base.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -3201,7 +3201,7 @@ def union(self, other, sort=None):
32013201

32023202
return self._wrap_setop_result(other, result)
32033203

3204-
def _union(self, other: Index, sort):
3204+
def _union(self, other: Index, sort: bool | None):
32053205
"""
32063206
Specific union logic should go here. In subclasses, union behavior
32073207
should be overwritten here rather than in `self.union`.
@@ -3212,6 +3212,7 @@ def _union(self, other: Index, sort):
32123212
sort : False or None, default False
32133213
Whether to sort the resulting index.
32143214
3215+
* True : sort the result
32153216
* False : do not sort the result.
32163217
* None : sort the result, except when `self` and `other` are equal
32173218
or when the values cannot be compared.
@@ -3224,7 +3225,7 @@ def _union(self, other: Index, sort):
32243225
rvals = other._values
32253226

32263227
if (
3227-
sort is None
3228+
sort in (None, True)
32283229
and self.is_monotonic_increasing
32293230
and other.is_monotonic_increasing
32303231
and not (self.has_duplicates and other.has_duplicates)

pandas/core/indexes/range.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -607,17 +607,17 @@ def _range_in_self(self, other: range) -> bool:
607607
return False
608608
return other.start in self._range and other[-1] in self._range
609609

610-
def _union(self, other: Index, sort):
610+
def _union(self, other: Index, sort: bool | None):
611611
"""
612612
Form the union of two Index objects and sorts if possible
613613
614614
Parameters
615615
----------
616616
other : Index or array-like
617617
618-
sort : False or None, default None
618+
sort : bool or None, default None
619619
Whether to sort (monotonically increasing) the resulting index.
620-
``sort=None`` returns a ``RangeIndex`` if possible or a sorted
620+
``sort=None|True`` returns a ``RangeIndex`` if possible or a sorted
621621
``Index`` with a int64 dtype if not.
622622
``sort=False`` can return a ``RangeIndex`` if self is monotonically
623623
increasing and other is fully contained in self. Otherwise, returns
@@ -628,7 +628,7 @@ def _union(self, other: Index, sort):
628628
union : Index
629629
"""
630630
if isinstance(other, RangeIndex):
631-
if sort is None or (
631+
if sort in (None, True) or (
632632
sort is False and self.step > 0 and self._range_in_self(other._range)
633633
):
634634
# GH 47557: Can still return a RangeIndex

pandas/tests/indexes/test_setops.py

+37
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,43 @@ def test_union_nan_in_both(dup):
578578
tm.assert_index_equal(result, expected)
579579

580580

581+
def test_union_rangeindex_sort_true():
582+
# GH 53490
583+
idx1 = RangeIndex(1, 100, 6)
584+
idx2 = RangeIndex(1, 50, 3)
585+
result = idx1.union(idx2, sort=True)
586+
expected = Index(
587+
[
588+
1,
589+
4,
590+
7,
591+
10,
592+
13,
593+
16,
594+
19,
595+
22,
596+
25,
597+
28,
598+
31,
599+
34,
600+
37,
601+
40,
602+
43,
603+
46,
604+
49,
605+
55,
606+
61,
607+
67,
608+
73,
609+
79,
610+
85,
611+
91,
612+
97,
613+
]
614+
)
615+
tm.assert_index_equal(result, expected)
616+
617+
581618
def test_union_with_duplicate_index_not_subset_and_non_monotonic(
582619
any_dtype_for_small_pos_integer_indexes,
583620
):

0 commit comments

Comments
 (0)