diff --git a/python/cudf/cudf/core/dataframe.py b/python/cudf/cudf/core/dataframe.py index a510e6829d1..d421258b06b 100644 --- a/python/cudf/cudf/core/dataframe.py +++ b/python/cudf/cudf/core/dataframe.py @@ -843,6 +843,12 @@ def _init_from_list_like(self, data, index=None, columns=None): data = DataFrame.from_pandas(pd.DataFrame(data)) self._data = data._data else: + if any( + not isinstance(col, (abc.Iterable, abc.Sequence)) + for col in data + ): + raise TypeError("Inputs should be an iterable or sequence.") + data = list(itertools.zip_longest(*data)) if columns is not None and len(data) == 0: diff --git a/python/cudf/cudf/tests/test_dataframe.py b/python/cudf/cudf/tests/test_dataframe.py index aad0b757177..e35ab147bf4 100644 --- a/python/cudf/cudf/tests/test_dataframe.py +++ b/python/cudf/cudf/tests/test_dataframe.py @@ -10243,3 +10243,15 @@ def test_dataframe_init_columns_named_index(): pdf = pd.DataFrame(data, columns=columns) assert_eq(gdf, pdf) + + +def test_dataframe_constructor_unbounded_sequence(): + class A: + def __getitem__(self, key): + return 1 + + with pytest.raises(TypeError): + cudf.DataFrame([A()]) + + with pytest.raises(TypeError): + cudf.DataFrame({"a": A()})