Skip to content

Commit 41f384c

Browse files
committed
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
1 parent 951f055 commit 41f384c

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

doc/source/release.rst

+2
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ API Changes
178178
- change ``AssertionError`` to ``TypeError`` for invalid types passed to ``concat`` (:issue:`6583`)
179179
- Add :class:`~pandas.io.parsers.ParserWarning` class for fallback and option
180180
validation warnings in :func:`read_csv`/:func:`read_table` (:issue:`6607`)
181+
- Raise a ``TypeError`` when ``DataFrame`` is passed an iterator as the
182+
``data`` argument (:issue:`5357`)
181183

182184
Deprecations
183185
~~~~~~~~~~~~

pandas/core/frame.py

+2
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
253253
else:
254254
mgr = self._init_ndarray(data, index, columns, dtype=dtype,
255255
copy=copy)
256+
elif isinstance(data, collections.Iterator):
257+
raise TypeError("data argument can't be an iterator")
256258
else:
257259
try:
258260
arr = np.array(data, dtype=dtype, copy=copy)

pandas/tests/test_frame.py

+4
Original file line numberDiff line numberDiff line change
@@ -3135,6 +3135,10 @@ def test_constructor_miscast_na_int_dtype(self):
31353135
expected = DataFrame([[np.nan, 1], [1, 0]])
31363136
assert_frame_equal(df, expected)
31373137

3138+
def test_constructor_iterator_failure(self):
3139+
with assertRaisesRegexp(TypeError, 'iterator'):
3140+
df = DataFrame(iter([1, 2, 3]))
3141+
31383142
def test_constructor_column_duplicates(self):
31393143
# it works! #2079
31403144
df = DataFrame([[8, 5]], columns=['a', 'a'])

0 commit comments

Comments
 (0)