File tree 3 files changed +33
-5
lines changed
3 files changed +33
-5
lines changed Original file line number Diff line number Diff line change @@ -61,6 +61,7 @@ Bug Fixes
61
61
- Bug in :class:`DatetimeIndex` subtracting datetimelike from DatetimeIndex could fail to overflow (:issue:`18020`)
62
62
- Bug in ``pd.Series.rolling.skew()`` and ``rolling.kurt()`` with all equal values has floating issue (:issue:`18044`)
63
63
- 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`)
64
65
65
66
Conversion
66
67
^^^^^^^^^^
Original file line number Diff line number Diff line change @@ -569,9 +569,10 @@ def _concat_rangeindex_same_dtype(indexes):
569
569
570
570
start = step = next = None
571
571
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 :
575
576
576
577
if start is None :
577
578
# This is set by the first non-empty index
@@ -595,8 +596,16 @@ def _concat_rangeindex_same_dtype(indexes):
595
596
if step is not None :
596
597
next = obj [- 1 ] + step
597
598
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 ]
599
607
start = obj ._start
600
608
step = obj ._step
601
- stop = obj ._stop if next is None else next
609
+ stop = obj ._stop
610
+
602
611
return indexes [0 ].__class__ (start , stop , step )
Original file line number Diff line number Diff line change @@ -1983,3 +1983,21 @@ def test_concat_will_upcast(dt, pdt):
1983
1983
pdt (np .array ([5 ], dtype = dt , ndmin = dims ))]
1984
1984
x = pd .concat (dfs )
1985
1985
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 )
You can’t perform that action at this time.
0 commit comments