Skip to content

Commit b668933

Browse files
author
Robert Meyer
committed
Fix for pandas-dev#18178 and pandas-dev#18187 by changing the concat of empty RangeIndex
The `_concat_rangeindex_same_dtype` now keeps track of the last non-empty RangeIndex to extract the new stop value. This fixes two issues with concatenating non-empty and empty DataFrames and Series. Two regression tests were added as well.
1 parent 8dac633 commit b668933

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

pandas/core/dtypes/concat.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -572,11 +572,13 @@ def _concat_rangeindex_same_dtype(indexes):
572572
indexes = [RangeIndex(3), RangeIndex(4, 6)] -> Int64Index([0,1,2,4,5])
573573
"""
574574

575-
start = step = next = None
575+
start = step = next = last_non_empty = None
576576

577577
for obj in indexes:
578578
if not len(obj):
579579
continue
580+
# Remember the last non-empty index for the stop value
581+
last_non_empty = obj
580582

581583
if start is None:
582584
# This is set by the first non-empty index
@@ -599,8 +601,12 @@ def _concat_rangeindex_same_dtype(indexes):
599601
if step is not None:
600602
next = obj[-1] + step
601603

602-
if start is None:
604+
if last_non_empty is None:
605+
# Here all "indexes" had 0 length, i.e. were empty.
606+
# Simply take start, stop, and step from the last "obj".
603607
start = obj._start
604608
step = obj._step
605-
stop = obj._stop if next is None else next
609+
stop = obj._stop
610+
else:
611+
stop = last_non_empty._stop if next is None else next
606612
return indexes[0].__class__(start, stop, step)

pandas/tests/reshape/test_concat.py

+18
Original file line numberDiff line numberDiff line change
@@ -1983,3 +1983,21 @@ def test_concat_will_upcast(dt, pdt):
19831983
pdt(np.array([5], dtype=dt, ndmin=dims))]
19841984
x = pd.concat(dfs)
19851985
assert x.values.dtype == 'float64'
1986+
1987+
1988+
def test_concat_empty_and_non_empty_frame_regression():
1989+
# GH 18178 regression test
1990+
df1 = pd.DataFrame({'foo': [1]})
1991+
df2 = pd.DataFrame({'foo': []})
1992+
expected = pd.DataFrame({'foo': [1.0]})
1993+
result = pd.concat([df1, df2])
1994+
assert_frame_equal(result, expected)
1995+
1996+
1997+
def test_concat_empty_and_non_empty_series_regression():
1998+
# GH 18187 regression test
1999+
s1 = pd.Series([1])
2000+
s2 = pd.Series([])
2001+
expected = s1
2002+
result = pd.concat([s1, s2])
2003+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)