Skip to content

Commit abcd440

Browse files
authored
BUG: RangeIndex.union(sort=True) with another RangeIndex (#53495)
1 parent 7287eaf commit abcd440

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
@@ -3315,7 +3315,7 @@ def union(self, other, sort=None):
33153315

33163316
return self._wrap_setop_result(other, result)
33173317

3318-
def _union(self, other: Index, sort):
3318+
def _union(self, other: Index, sort: bool | None):
33193319
"""
33203320
Specific union logic should go here. In subclasses, union behavior
33213321
should be overwritten here rather than in `self.union`.
@@ -3326,6 +3326,7 @@ def _union(self, other: Index, sort):
33263326
sort : False or None, default False
33273327
Whether to sort the resulting index.
33283328
3329+
* True : sort the result
33293330
* False : do not sort the result.
33303331
* None : sort the result, except when `self` and `other` are equal
33313332
or when the values cannot be compared.
@@ -3338,7 +3339,7 @@ def _union(self, other: Index, sort):
33383339
rvals = other._values
33393340

33403341
if (
3341-
sort is None
3342+
sort in (None, True)
33423343
and self.is_monotonic_increasing
33433344
and other.is_monotonic_increasing
33443345
and not (self.has_duplicates and other.has_duplicates)

pandas/core/indexes/range.py

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

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

pandas/tests/indexes/test_setops.py

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

579579

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

0 commit comments

Comments
 (0)