Skip to content

Commit 1b1431a

Browse files
simonjayhawkinsjreback
authored andcommitted
ERR: User-facing AssertionError in DataFrame Constructor (#26440)
1 parent a7d3cc9 commit 1b1431a

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

pandas/core/internals/construction.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,13 @@ def _list_to_arrays(data, columns, coerce_float=False, dtype=None):
424424
else:
425425
# list of lists
426426
content = list(lib.to_object_array(data).T)
427-
return _convert_object_array(content, columns, dtype=dtype,
428-
coerce_float=coerce_float)
427+
# gh-26429 do not raise user-facing AssertionError
428+
try:
429+
result = _convert_object_array(content, columns, dtype=dtype,
430+
coerce_float=coerce_float)
431+
except AssertionError as e:
432+
raise ValueError(e) from e
433+
return result
429434

430435

431436
def _list_of_series_to_arrays(data, columns, coerce_float=False, dtype=None):

pandas/tests/frame/test_constructors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,11 @@ def test_constructor_error_msgs(self):
435435
with pytest.raises(ValueError, match=msg):
436436
DataFrame(np.random.rand(2, 3), columns=['A', 'B'], index=[1, 2])
437437

438+
# gh-26429
439+
msg = "2 columns passed, passed data had 10 columns"
440+
with pytest.raises(ValueError, match=msg):
441+
DataFrame((range(10), range(10, 20)), columns=('ones', 'twos'))
442+
438443
msg = ("If using all scalar "
439444
"values, you must pass "
440445
"an index")

pandas/tests/io/json/test_pandas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ def test_frame_from_json_bad_data(self):
353353
'"index":["1","2","3"],'
354354
'"data":[[1.0,"1"],[2.0,"2"],[null,"3"]]}')
355355
msg = "3 columns passed, passed data had 2 columns"
356-
with pytest.raises(AssertionError, match=msg):
356+
with pytest.raises(ValueError, match=msg):
357357
read_json(json, orient="split")
358358

359359
# bad key

0 commit comments

Comments
 (0)