From 67703d16a649525e96af7caa0d6b3c01d6b8a2b7 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Tue, 19 Sep 2023 18:32:20 -0700 Subject: [PATCH] Raise ValueError if DataFrame column length does match data --- python/cudf/cudf/core/dataframe.py | 5 +++++ python/cudf/cudf/tests/test_dataframe.py | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/python/cudf/cudf/core/dataframe.py b/python/cudf/cudf/core/dataframe.py index 84c16b71997..b79a4f30092 100644 --- a/python/cudf/cudf/core/dataframe.py +++ b/python/cudf/cudf/core/dataframe.py @@ -894,6 +894,11 @@ def _init_from_dict_like( self, data, index=None, columns=None, nan_as_null=None ): if columns is not None: + if len(data) != len(columns): + raise ValueError( + f"Length of values ({len(data)}) does not " + f"match length of columns ({len(columns)})" + ) # remove all entries in data that are not in columns, # inserting new empty columns for entries in columns that # are not in data diff --git a/python/cudf/cudf/tests/test_dataframe.py b/python/cudf/cudf/tests/test_dataframe.py index cbef9bfa2d8..1c2f05a014b 100644 --- a/python/cudf/cudf/tests/test_dataframe.py +++ b/python/cudf/cudf/tests/test_dataframe.py @@ -10334,6 +10334,12 @@ def test_dataframe_nlargest_nsmallest_str_error(attr): ) +def test_dictlike_data_column_length_mismatch(): + ser = cudf.Series(range(5)) + with pytest.raises(ValueError): + cudf.DataFrame(ser, columns=[1, 2]) + + @pytest.mark.parametrize("digits", [0, 1, 3, 4, 10]) def test_dataframe_round_builtin(digits): pdf = pd.DataFrame(