From 41f384c72eec06ef618a5d97368c64419636047f Mon Sep 17 00:00:00 2001 From: onesandzeroes Date: Sun, 27 Apr 2014 10:06:31 +1000 Subject: [PATCH] ERR: Add check for iterators when creating DataFrame Add test case for iterator data argument Move the type check up to where other checks are performed Use asserRaisesRegexp for more specific checking Add fix to the release notes --- doc/source/release.rst | 2 ++ pandas/core/frame.py | 2 ++ pandas/tests/test_frame.py | 4 ++++ 3 files changed, 8 insertions(+) diff --git a/doc/source/release.rst b/doc/source/release.rst index fce5f2f93e68b..705d31695bd23 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -178,6 +178,8 @@ API Changes - change ``AssertionError`` to ``TypeError`` for invalid types passed to ``concat`` (:issue:`6583`) - Add :class:`~pandas.io.parsers.ParserWarning` class for fallback and option validation warnings in :func:`read_csv`/:func:`read_table` (:issue:`6607`) +- Raise a ``TypeError`` when ``DataFrame`` is passed an iterator as the + ``data`` argument (:issue:`5357`) Deprecations ~~~~~~~~~~~~ diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 10a0c9050af50..ba730c0c0fe41 100755 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -253,6 +253,8 @@ def __init__(self, data=None, index=None, columns=None, dtype=None, else: mgr = self._init_ndarray(data, index, columns, dtype=dtype, copy=copy) + elif isinstance(data, collections.Iterator): + raise TypeError("data argument can't be an iterator") else: try: arr = np.array(data, dtype=dtype, copy=copy) diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index d96fbb9ec05d7..fc68449f75e0f 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -3135,6 +3135,10 @@ def test_constructor_miscast_na_int_dtype(self): expected = DataFrame([[np.nan, 1], [1, 0]]) assert_frame_equal(df, expected) + def test_constructor_iterator_failure(self): + with assertRaisesRegexp(TypeError, 'iterator'): + df = DataFrame(iter([1, 2, 3])) + def test_constructor_column_duplicates(self): # it works! #2079 df = DataFrame([[8, 5]], columns=['a', 'a'])