Skip to content

Commit 1e5ab7a

Browse files
authored
BUG: RangeIndex.union (#44019)
1 parent 6c35a62 commit 1e5ab7a

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ Styler
578578
Other
579579
^^^^^
580580
- Bug in :meth:`CustomBusinessMonthBegin.__add__` (:meth:`CustomBusinessMonthEnd.__add__`) not applying the extra ``offset`` parameter when beginning (end) of the target month is already a business day (:issue:`41356`)
581+
- Bug in :meth:`RangeIndex.union` with another ``RangeIndex`` with matching (even) ``step`` and starts differing by strictly less than ``step / 2`` (:issue:`44019`)
581582

582583
.. ***DO NOT USE THIS SECTION***
583584

pandas/core/indexes/range.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -635,10 +635,13 @@ def _union(self, other: Index, sort):
635635
return type(self)(start_r, end_r + step_s, step_s)
636636
if (
637637
(step_s % 2 == 0)
638-
and (abs(start_s - start_o) <= step_s / 2)
639-
and (abs(end_s - end_o) <= step_s / 2)
638+
and (abs(start_s - start_o) == step_s / 2)
639+
and (abs(end_s - end_o) == step_s / 2)
640640
):
641+
# e.g. range(0, 10, 2) and range(1, 11, 2)
642+
# but not range(0, 20, 4) and range(1, 21, 4) GH#44019
641643
return type(self)(start_r, end_r + step_s / 2, step_s / 2)
644+
642645
elif step_o % step_s == 0:
643646
if (
644647
(start_o - start_s) % step_s == 0

pandas/tests/indexes/ranges/test_setops.py

+9
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,15 @@ def test_union_sorted(self, unions):
290290
tm.assert_index_equal(res2, expected_sorted, exact=True)
291291
tm.assert_index_equal(res3, expected_sorted, exact="equiv")
292292

293+
def test_union_same_step_misaligned(self):
294+
# GH#44019
295+
left = RangeIndex(range(0, 20, 4))
296+
right = RangeIndex(range(1, 21, 4))
297+
298+
result = left.union(right)
299+
expected = Int64Index([0, 1, 4, 5, 8, 9, 12, 13, 16, 17])
300+
tm.assert_index_equal(result, expected, exact=True)
301+
293302
def test_difference(self):
294303
# GH#12034 Cases where we operate against another RangeIndex and may
295304
# get back another RangeIndex

0 commit comments

Comments
 (0)