Skip to content

Commit

Permalink
Restrict iterables of DataFrame's as input to DataFrame construct…
Browse files Browse the repository at this point in the history
…or (#14118)

Fixes: #14094 
This PR raises an error when an iterates of `DataFrame`'s is detected in `DataFrame` constructor.

Authors:
  - GALI PREM SAGAR (https://github.com/galipremsagar)

Approvers:
  - Matthew Roeschke (https://github.com/mroeschke)

URL: #14118
  • Loading branch information
galipremsagar authored Sep 18, 2023
1 parent 8e081c0 commit 4467066
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
11 changes: 6 additions & 5 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,12 +852,13 @@ def _init_from_list_like(self, data, index=None, columns=None):
elif len(data) > 0 and isinstance(data[0], pd._libs.interval.Interval):
data = DataFrame.from_pandas(pd.DataFrame(data))
self._data = data._data
elif any(
not isinstance(col, (abc.Iterable, abc.Sequence)) for col in data
):
raise TypeError("Inputs should be an iterable or sequence.")
elif len(data) > 0 and not can_convert_to_column(data[0]):
raise ValueError("Must pass 2-d input.")
else:
if any(
not isinstance(col, (abc.Iterable, abc.Sequence))
for col in data
):
raise TypeError("Inputs should be an iterable or sequence.")
if (
len(data) > 0
and columns is None
Expand Down
6 changes: 6 additions & 0 deletions python/cudf/cudf/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -10260,6 +10260,12 @@ def __getitem__(self, key):
cudf.DataFrame({"a": A()})


def test_dataframe_constructor_dataframe_list():
df = cudf.DataFrame(range(2))
with pytest.raises(ValueError):
cudf.DataFrame([df])


def test_dataframe_constructor_from_namedtuple():
Point1 = namedtuple("Point1", ["a", "b", "c"])
Point2 = namedtuple("Point1", ["x", "y"])
Expand Down

0 comments on commit 4467066

Please sign in to comment.