Skip to content

Commit de4be1f

Browse files
SmokinCaterpillarTomAugspurger
authored andcommitted
Fix for pandas-dev#18178 and pandas-dev#18187 by changing the concat of empty RangeIndex (pandas-dev#18191)
(cherry picked from commit 6b3641b)
1 parent adfdb89 commit de4be1f

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

doc/source/whatsnew/v0.21.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Bug Fixes
6161
- Bug in :class:`DatetimeIndex` subtracting datetimelike from DatetimeIndex could fail to overflow (:issue:`18020`)
6262
- Bug in ``pd.Series.rolling.skew()`` and ``rolling.kurt()`` with all equal values has floating issue (:issue:`18044`)
6363
- Bug in ``pd.DataFrameGroupBy.count()`` when counting over a datetimelike column (:issue:`13393`)
64+
- Bug in ``pd.concat`` when empty and non-empty DataFrames or Series are concatenated (:issue:`18178` :issue:`18187`)
6465

6566
Conversion
6667
^^^^^^^^^^

pandas/core/dtypes/concat.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -569,9 +569,10 @@ def _concat_rangeindex_same_dtype(indexes):
569569

570570
start = step = next = None
571571

572-
for obj in indexes:
573-
if not len(obj):
574-
continue
572+
# Filter the empty indexes
573+
non_empty_indexes = [obj for obj in indexes if len(obj)]
574+
575+
for obj in non_empty_indexes:
575576

576577
if start is None:
577578
# This is set by the first non-empty index
@@ -595,8 +596,16 @@ def _concat_rangeindex_same_dtype(indexes):
595596
if step is not None:
596597
next = obj[-1] + step
597598

598-
if start is None:
599+
if non_empty_indexes:
600+
# Get the stop value from "next" or alternatively
601+
# from the last non-empty index
602+
stop = non_empty_indexes[-1]._stop if next is None else next
603+
else:
604+
# Here all "indexes" had 0 length, i.e. were empty.
605+
# Simply take start, stop, and step from the last empty index.
606+
obj = indexes[-1]
599607
start = obj._start
600608
step = obj._step
601-
stop = obj._stop if next is None else next
609+
stop = obj._stop
610+
602611
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)