Skip to content

ERR: User-facing AssertionError in DataFrame Constructor #26440

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

Merged
Show file tree
Hide file tree
Changes from all 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 pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,13 @@ def _list_to_arrays(data, columns, coerce_float=False, dtype=None):
else:
# list of lists
content = list(lib.to_object_array(data).T)
return _convert_object_array(content, columns, dtype=dtype,
coerce_float=coerce_float)
# gh-26429 do not raise user-facing AssertionError
try:
result = _convert_object_array(content, columns, dtype=dtype,
coerce_float=coerce_float)
except AssertionError as e:
raise ValueError(e) from e
return result


def _list_of_series_to_arrays(data, columns, coerce_float=False, dtype=None):
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,11 @@ def test_constructor_error_msgs(self):
with pytest.raises(ValueError, match=msg):
DataFrame(np.random.rand(2, 3), columns=['A', 'B'], index=[1, 2])

# gh-26429
msg = "2 columns passed, passed data had 10 columns"
with pytest.raises(ValueError, match=msg):
DataFrame((range(10), range(10, 20)), columns=('ones', 'twos'))

msg = ("If using all scalar "
"values, you must pass "
"an index")
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def test_frame_from_json_bad_data(self):
'"index":["1","2","3"],'
'"data":[[1.0,"1"],[2.0,"2"],[null,"3"]]}')
msg = "3 columns passed, passed data had 2 columns"
with pytest.raises(AssertionError, match=msg):
with pytest.raises(ValueError, match=msg):
read_json(json, orient="split")

# bad key
Expand Down