Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unbounded sequence issue in DataFrame constructor #13811

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,13 @@ def _init_from_list_like(self, data, index=None, columns=None):
data = DataFrame.from_pandas(pd.DataFrame(data))
self._data = data._data
else:
data = list(itertools.zip_longest(*data))

if all(
isinstance(a, (abc.Iterable, abc.Sequence))
for a in data
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
):
data = list(itertools.zip_longest(*data))
else:
raise TypeError("Inputs should be an iterable or sequence.")
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
if columns is not None and len(data) == 0:
data = [
cudf.core.column.column_empty(row_count=0, dtype=None)
Expand Down
12 changes: 12 additions & 0 deletions python/cudf/cudf/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()})