Repository: pandas-dev/pandas

Issue:
BUG: pd.concat returns empty series if called on mixture of empty and non-empty series
#### Code Sample

```python
import pandas as pd

s1 = pd.Series([1])
s2 = pd.Series([])

pd.concat([s1, s2])
```
returns an empty series `Series([], dtype: int64)`.

#### Problem description

Concatenating an non-empty and empty series returns an empty series.
If this is supposed to be the intended behavior, it is very counterintuitive. I would rather expect that the
result of `pd.concat([s1, s2])` is simply equal to `s1`. In fact, this used to be the behavior in older versions like 0.20.3.

Btw, I stumbled upon this error while I was trying to fix #18178. Out of curiosity I checked what happens in case of concatenating non-empty and empty series instead of frames.

#### Expected Output

Series equal to `s1`, i.e. `repr(pd.concat([s1, s2]))` returns `'0    1\ndtype: int64'`.

#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-98-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.21.0
pytest: None
pip: 9.0.1
setuptools: 36.5.0.post20170921
Cython: None
numpy: 1.13.3
scipy: None
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None



</details>



Discussion:
Yes, this is certainly a bug, so a regression from 0.20.3. Probably it has the same cause as https://github.com/pandas-dev/pandas/issues/18178

Resolving patch:
diff --git a/pandas/core/dtypes/concat.py b/pandas/core/dtypes/concat.py
--- a/pandas/core/dtypes/concat.py
+++ b/pandas/core/dtypes/concat.py
@@ -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
@@ -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)


Test patch:
diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py
--- a/pandas/tests/reshape/test_concat.py
+++ b/pandas/tests/reshape/test_concat.py
@@ -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)