File tree Expand file tree Collapse file tree 3 files changed +33
-5
lines changed Expand file tree Collapse file tree 3 files changed +33
-5
lines changed Original file line number Diff line number Diff 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
6566Conversion
6667^^^^^^^^^^
Original file line number Diff line number Diff line change @@ -574,9 +574,10 @@ def _concat_rangeindex_same_dtype(indexes):
574574
575575 start = step = next = None
576576
577- for obj in indexes :
578- if not len (obj ):
579- continue
577+ # Filter the empty indexes
578+ non_empty_indexes = [obj for obj in indexes if len (obj )]
579+
580+ for obj in non_empty_indexes :
580581
581582 if start is None :
582583 # This is set by the first non-empty index
@@ -599,8 +600,16 @@ def _concat_rangeindex_same_dtype(indexes):
599600 if step is not None :
600601 next = obj [- 1 ] + step
601602
602- if start is None :
603+ if non_empty_indexes :
604+ # Get the stop value from "next" or alternatively
605+ # from the last non-empty index
606+ stop = non_empty_indexes [- 1 ]._stop if next is None else next
607+ else :
608+ # Here all "indexes" had 0 length, i.e. were empty.
609+ # Simply take start, stop, and step from the last empty index.
610+ obj = indexes [- 1 ]
603611 start = obj ._start
604612 step = obj ._step
605- stop = obj ._stop if next is None else next
613+ stop = obj ._stop
614+
606615 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):
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 )
You can’t perform that action at this time.
0 commit comments