From a5d5028b1477aa8ab1a4e43d9c1ffb49f0363921 Mon Sep 17 00:00:00 2001 From: cel4 Date: Tue, 30 Dec 2014 14:28:51 +0100 Subject: [PATCH 1/3] improved error message when concatenating an empty sequence of dataframes --- doc/source/whatsnew/v0.17.0.txt | 1 + pandas/tools/merge.py | 7 ++++++- pandas/tools/tests/test_merge.py | 17 +++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.17.0.txt b/doc/source/whatsnew/v0.17.0.txt index 8320f3cbc8e76..80e7f2a3f585d 100644 --- a/doc/source/whatsnew/v0.17.0.txt +++ b/doc/source/whatsnew/v0.17.0.txt @@ -526,6 +526,7 @@ Series with a ``CategoricalIndex`` (:issue:`10704`) ``return np.datetime64('NaT')`` ``to_datetime64`` (unchanged) ``raise ValueError`` All other public methods (names not beginning with underscores) =============================== =============================================================== +- Improved error message when concatenating an empty iterable of dataframes (:issue:`9157`) .. _whatsnew_0170.deprecations: diff --git a/pandas/tools/merge.py b/pandas/tools/merge.py index 430828a3db31b..c8a7b9366d564 100644 --- a/pandas/tools/merge.py +++ b/pandas/tools/merge.py @@ -775,9 +775,14 @@ def __init__(self, objs, axis=0, join='outer', join_axes=None, if keys is None: keys = sorted(objs) objs = [objs[k] for k in keys] + else: + objs = list(objs) + + if len(objs) == 0: + raise ValueError('No objects to concatenate') if keys is None: - objs = [obj for obj in objs if obj is not None ] + objs = [obj for obj in objs if obj is not None] else: # #1649 clean_keys = [] diff --git a/pandas/tools/tests/test_merge.py b/pandas/tools/tests/test_merge.py index 8b1457e7fd490..236157d028db3 100644 --- a/pandas/tools/tests/test_merge.py +++ b/pandas/tools/tests/test_merge.py @@ -2552,6 +2552,23 @@ def _constructor(self): tm.assertIsInstance(result, NotADataFrame) + def test_empty_sequence_concat(self): + # GH 9157 + empty_pat = "[Nn]o objects" + none_pat = "objects.*None" + test_cases = [ + ((), empty_pat), + ([], empty_pat), + ({}, empty_pat), + ([None], none_pat), + ([None, None], none_pat) + ] + for df_seq, pattern in test_cases: + assertRaisesRegexp(ValueError, pattern, pd.concat, df_seq) + + pd.concat([pd.DataFrame()]) + pd.concat([None, pd.DataFrame()]) + pd.concat([pd.DataFrame(), None]) if __name__ == '__main__': nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'], From 56e18de52d95e51f54304cb2f106ebc28bb98d07 Mon Sep 17 00:00:00 2001 From: cel4 Date: Fri, 14 Aug 2015 13:52:08 +0200 Subject: [PATCH 2/3] removed obsolete imports in merge.py --- pandas/tools/merge.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pandas/tools/merge.py b/pandas/tools/merge.py index c8a7b9366d564..5ee774635e59e 100644 --- a/pandas/tools/merge.py +++ b/pandas/tools/merge.py @@ -1,7 +1,6 @@ """ SQL-style merge routines """ -import types import numpy as np from pandas.compat import range, long, lrange, lzip, zip, map, filter @@ -17,11 +16,9 @@ concatenate_block_managers) from pandas.util.decorators import Appender, Substitution from pandas.core.common import ABCSeries -from pandas.io.parsers import TextFileReader import pandas.core.common as com -import pandas.lib as lib import pandas.algos as algos import pandas.hashtable as _hash From ef1f81885de77bd9658b77738b06b3f45cc3ccbd Mon Sep 17 00:00:00 2001 From: cel4 Date: Sat, 15 Aug 2015 10:19:34 +0200 Subject: [PATCH 3/3] removed newline for proper formatting --- doc/source/whatsnew/v0.17.0.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.17.0.txt b/doc/source/whatsnew/v0.17.0.txt index 80e7f2a3f585d..8d4ad7f155423 100644 --- a/doc/source/whatsnew/v0.17.0.txt +++ b/doc/source/whatsnew/v0.17.0.txt @@ -494,8 +494,7 @@ Other API Changes ^^^^^^^^^^^^^^^^^ - 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`) -- Calling the ``.value_counts`` method on a Series with ``categorical`` dtype now returns a -Series with a ``CategoricalIndex`` (:issue:`10704`) +- Calling the ``.value_counts`` method on a Series with ``categorical`` dtype now returns a Series with a ``CategoricalIndex`` (:issue:`10704`) - Enable writing Excel files in :ref:`memory <_io.excel_writing_buffer>` using StringIO/BytesIO (:issue:`7074`) - Enable serialization of lists and dicts to strings in ExcelWriter (:issue:`8188`) - Allow passing `kwargs` to the interpolation methods (:issue:`10378`).