Skip to content

Commit

Permalink
Raise ValueError if DataFrame column length does match data
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke committed Sep 20, 2023
1 parent c016b58 commit 67703d1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions python/cudf/cudf/core/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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(
Expand Down

0 comments on commit 67703d1

Please sign in to comment.