From 300c9978c02f072d4e28b8e015e0172e767a5c66 Mon Sep 17 00:00:00 2001 From: galipremsagar Date: Mon, 18 Sep 2023 06:50:50 -0700 Subject: [PATCH] Add additional checks in dataframe constructor --- python/cudf/cudf/core/dataframe.py | 11 ++++++----- python/cudf/cudf/tests/test_dataframe.py | 6 ++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/python/cudf/cudf/core/dataframe.py b/python/cudf/cudf/core/dataframe.py index 5a3d25a08a7..0557a671ecb 100644 --- a/python/cudf/cudf/core/dataframe.py +++ b/python/cudf/cudf/core/dataframe.py @@ -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 diff --git a/python/cudf/cudf/tests/test_dataframe.py b/python/cudf/cudf/tests/test_dataframe.py index 61372bab3ad..49419bc4322 100644 --- a/python/cudf/cudf/tests/test_dataframe.py +++ b/python/cudf/cudf/tests/test_dataframe.py @@ -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"])