Skip to content

Fix for #18178 and #18187 by changing the concat of empty RangeIndex #18191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 10, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Bug Fixes
- Bug in :class:`DatetimeIndex` subtracting datetimelike from DatetimeIndex could fail to overflow (:issue:`18020`)
- Bug in ``pd.Series.rolling.skew()`` and ``rolling.kurt()`` with all equal values has floating issue (:issue:`18044`)
- Bug in ``pd.DataFrameGroupBy.count()`` when counting over a datetimelike column (:issue:`13393`)
- Bug in ``pd.concat`` when empty and non-empty DataFrames or Series are concatenated (:issue:`18178` :issue:`18187`)

Conversion
^^^^^^^^^^
Expand Down
12 changes: 9 additions & 3 deletions pandas/core/dtypes/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,11 +572,13 @@ def _concat_rangeindex_same_dtype(indexes):
indexes = [RangeIndex(3), RangeIndex(4, 6)] -> Int64Index([0,1,2,4,5])
"""

start = step = next = None
start = step = next = last_non_empty = None

for obj in indexes:
if not len(obj):
continue
# Remember the last non-empty index for the stop value
last_non_empty = obj
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe filter out he non empties first? then i think this loop should be ok?


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

if start is None:
if last_non_empty is None:
# Here all "indexes" had 0 length, i.e. were empty.
# Simply take start, stop, and step from the last "obj".
start = obj._start
step = obj._step
stop = obj._stop if next is None else next
stop = obj._stop
else:
stop = last_non_empty._stop if next is None else next
return indexes[0].__class__(start, stop, step)
18 changes: 18 additions & 0 deletions pandas/tests/reshape/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1983,3 +1983,21 @@ def test_concat_will_upcast(dt, pdt):
pdt(np.array([5], dtype=dt, ndmin=dims))]
x = pd.concat(dfs)
assert x.values.dtype == 'float64'


def test_concat_empty_and_non_empty_frame_regression():
# GH 18178 regression test
df1 = pd.DataFrame({'foo': [1]})
df2 = pd.DataFrame({'foo': []})
expected = pd.DataFrame({'foo': [1.0]})
result = pd.concat([df1, df2])
assert_frame_equal(result, expected)


def test_concat_empty_and_non_empty_series_regression():
# GH 18187 regression test
s1 = pd.Series([1])
s2 = pd.Series([])
expected = s1
result = pd.concat([s1, s2])
tm.assert_series_equal(result, expected)