Skip to content

Commit 58db03e

Browse files
cel4jreback
authored andcommitted
ERR: improved error message when concatenating an empty sequence of dataframes, #9157
1 parent bcc7b0b commit 58db03e

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

doc/source/whatsnew/v0.17.0.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,7 @@ Other API Changes
494494
^^^^^^^^^^^^^^^^^
495495

496496
- Line and kde plot with ``subplots=True`` now uses default colors, not all black. Specify ``color='k'`` to draw all lines in black (:issue:`9894`)
497-
- Calling the ``.value_counts`` method on a Series with ``categorical`` dtype now returns a
498-
Series with a ``CategoricalIndex`` (:issue:`10704`)
497+
- Calling the ``.value_counts`` method on a Series with ``categorical`` dtype now returns a Series with a ``CategoricalIndex`` (:issue:`10704`)
499498
- Enable writing Excel files in :ref:`memory <_io.excel_writing_buffer>` using StringIO/BytesIO (:issue:`7074`)
500499
- Enable serialization of lists and dicts to strings in ExcelWriter (:issue:`8188`)
501500
- Allow passing `kwargs` to the interpolation methods (:issue:`10378`).
@@ -527,6 +526,8 @@ Series with a ``CategoricalIndex`` (:issue:`10704`)
527526
``raise ValueError`` All other public methods (names not beginning with underscores)
528527
=============================== ===============================================================
529528

529+
- Improved error message when concatenating an empty iterable of dataframes (:issue:`9157`)
530+
530531
.. _whatsnew_0170.deprecations:
531532

532533
Deprecations

pandas/tools/merge.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""
22
SQL-style merge routines
33
"""
4-
import types
54

65
import numpy as np
76
from pandas.compat import range, long, lrange, lzip, zip, map, filter
@@ -17,11 +16,9 @@
1716
concatenate_block_managers)
1817
from pandas.util.decorators import Appender, Substitution
1918
from pandas.core.common import ABCSeries
20-
from pandas.io.parsers import TextFileReader
2119

2220
import pandas.core.common as com
2321

24-
import pandas.lib as lib
2522
import pandas.algos as algos
2623
import pandas.hashtable as _hash
2724

@@ -775,9 +772,14 @@ def __init__(self, objs, axis=0, join='outer', join_axes=None,
775772
if keys is None:
776773
keys = sorted(objs)
777774
objs = [objs[k] for k in keys]
775+
else:
776+
objs = list(objs)
777+
778+
if len(objs) == 0:
779+
raise ValueError('No objects to concatenate')
778780

779781
if keys is None:
780-
objs = [obj for obj in objs if obj is not None ]
782+
objs = [obj for obj in objs if obj is not None]
781783
else:
782784
# #1649
783785
clean_keys = []

pandas/tools/tests/test_merge.py

+17
Original file line numberDiff line numberDiff line change
@@ -2552,6 +2552,23 @@ def _constructor(self):
25522552

25532553
tm.assertIsInstance(result, NotADataFrame)
25542554

2555+
def test_empty_sequence_concat(self):
2556+
# GH 9157
2557+
empty_pat = "[Nn]o objects"
2558+
none_pat = "objects.*None"
2559+
test_cases = [
2560+
((), empty_pat),
2561+
([], empty_pat),
2562+
({}, empty_pat),
2563+
([None], none_pat),
2564+
([None, None], none_pat)
2565+
]
2566+
for df_seq, pattern in test_cases:
2567+
assertRaisesRegexp(ValueError, pattern, pd.concat, df_seq)
2568+
2569+
pd.concat([pd.DataFrame()])
2570+
pd.concat([None, pd.DataFrame()])
2571+
pd.concat([pd.DataFrame(), None])
25552572

25562573
if __name__ == '__main__':
25572574
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

0 commit comments

Comments
 (0)