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 all 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
19 changes: 14 additions & 5 deletions pandas/core/dtypes/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,10 @@ def _concat_rangeindex_same_dtype(indexes):

start = step = next = None

for obj in indexes:
if not len(obj):
continue
# Filter the empty indexes
non_empty_indexes = [obj for obj in indexes if len(obj)]

for obj in non_empty_indexes:

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

if start is None:
if non_empty_indexes:
# Get the stop value from "next" or alternatively
# from the last non-empty index
stop = non_empty_indexes[-1]._stop if next is None else next
else:
# Here all "indexes" had 0 length, i.e. were empty.
# Simply take start, stop, and step from the last empty index.
obj = indexes[-1]
start = obj._start
step = obj._step
stop = obj._stop if next is None else next
stop = obj._stop

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)