Skip to content

Commit 08f92c4

Browse files
meiermarkTomAugspurger
authored andcommitted
BUG: Switched shapes in ValueError msg in DataFrame construct (#20742) (#24725)
* BUG: Switched shapes in ValueError msg in DataFrame construct (#20742) * DOC: improved docu of the value error msg changes TST: adjusted panel test for change in value error msg in block manager TST: adjusted dataframe init test from json for change in value error msg in block manager * BUG: printed a list instead of a tuple in the ValueError msg * CLN: removed unnecessary list comprehension * DOC: Improved whatsnew message * CLN: readded deleted tests CLN: removed issue number above new tests * Bug: No changes for panel * DOC: improved whatsnew message for reversed shapes * DOC: improved comments and whatsnew message for df construction error * DOC: improved whatsnew message
1 parent 2bbe418 commit 08f92c4

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

doc/source/whatsnew/v0.24.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -1817,6 +1817,7 @@ Reshaping
18171817
- Bug in :func:`DataFrame.unstack` where a ``ValueError`` was raised when unstacking timezone aware values (:issue:`18338`)
18181818
- Bug in :func:`DataFrame.stack` where timezone aware values were converted to timezone naive values (:issue:`19420`)
18191819
- Bug in :func:`merge_asof` where a ``TypeError`` was raised when ``by_col`` were timezone aware values (:issue:`21184`)
1820+
- Bug showing an incorrect shape when throwing error during ``DataFrame`` construction. (:issue:`20742`)
18201821

18211822
.. _whatsnew_0240.bug_fixes.sparse:
18221823

@@ -1854,6 +1855,7 @@ Other
18541855

18551856
- Bug where C variables were declared with external linkage causing import errors if certain other C libraries were imported before Pandas. (:issue:`24113`)
18561857

1858+
18571859
.. _whatsnew_0.24.0.contributors:
18581860

18591861
Contributors

pandas/core/internals/managers.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1674,7 +1674,15 @@ def create_block_manager_from_arrays(arrays, names, axes):
16741674
def construction_error(tot_items, block_shape, axes, e=None):
16751675
""" raise a helpful message about our construction """
16761676
passed = tuple(map(int, [tot_items] + list(block_shape)))
1677-
implied = tuple(map(int, [len(ax) for ax in axes]))
1677+
# Correcting the user facing error message during dataframe construction
1678+
if len(passed) <= 2:
1679+
passed = passed[::-1]
1680+
1681+
implied = tuple(len(ax) for ax in axes)
1682+
# Correcting the user facing error message during dataframe construction
1683+
if len(implied) <= 2:
1684+
implied = implied[::-1]
1685+
16781686
if passed == implied and e is not None:
16791687
raise e
16801688
if block_shape[0] == 0:

pandas/tests/frame/test_constructors.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -386,25 +386,35 @@ def test_constructor_error_msgs(self):
386386
'B': ['a', 'b', 'c']})
387387

388388
# wrong size ndarray, GH 3105
389-
msg = r"Shape of passed values is \(3, 4\), indices imply \(3, 3\)"
389+
msg = r"Shape of passed values is \(4, 3\), indices imply \(3, 3\)"
390390
with pytest.raises(ValueError, match=msg):
391391
DataFrame(np.arange(12).reshape((4, 3)),
392392
columns=['foo', 'bar', 'baz'],
393393
index=pd.date_range('2000-01-01', periods=3))
394394

395+
arr = np.array([[4, 5, 6]])
396+
msg = r"Shape of passed values is \(1, 3\), indices imply \(1, 4\)"
397+
with pytest.raises(ValueError, match=msg):
398+
DataFrame(index=[0], columns=range(0, 4), data=arr)
399+
400+
arr = np.array([4, 5, 6])
401+
msg = r"Shape of passed values is \(3, 1\), indices imply \(1, 4\)"
402+
with pytest.raises(ValueError, match=msg):
403+
DataFrame(index=[0], columns=range(0, 4), data=arr)
404+
395405
# higher dim raise exception
396406
with pytest.raises(ValueError, match='Must pass 2-d input'):
397407
DataFrame(np.zeros((3, 3, 3)), columns=['A', 'B', 'C'], index=[1])
398408

399409
# wrong size axis labels
400410
msg = ("Shape of passed values "
401-
r"is \(3, 2\), indices "
402-
r"imply \(3, 1\)")
411+
r"is \(2, 3\), indices "
412+
r"imply \(1, 3\)")
403413
with pytest.raises(ValueError, match=msg):
404414
DataFrame(np.random.rand(2, 3), columns=['A', 'B', 'C'], index=[1])
405415

406416
msg = ("Shape of passed values "
407-
r"is \(3, 2\), indices "
417+
r"is \(2, 3\), indices "
408418
r"imply \(2, 2\)")
409419
with pytest.raises(ValueError, match=msg):
410420
DataFrame(np.random.rand(2, 3), columns=['A', 'B'], index=[1, 2])
@@ -638,10 +648,10 @@ def _check_basic_constructor(self, empty):
638648
assert frame.values.dtype == np.int64
639649

640650
# wrong size axis labels
641-
msg = r'Shape of passed values is \(3, 2\), indices imply \(3, 1\)'
651+
msg = r'Shape of passed values is \(2, 3\), indices imply \(1, 3\)'
642652
with pytest.raises(ValueError, match=msg):
643653
DataFrame(mat, columns=['A', 'B', 'C'], index=[1])
644-
msg = r'Shape of passed values is \(3, 2\), indices imply \(2, 2\)'
654+
msg = r'Shape of passed values is \(2, 3\), indices imply \(2, 2\)'
645655
with pytest.raises(ValueError, match=msg):
646656
DataFrame(mat, columns=['A', 'B'], index=[1, 2])
647657

@@ -1805,7 +1815,7 @@ def test_from_records_to_records(self):
18051815
tm.assert_frame_equal(DataFrame.from_records(arr2), DataFrame(arr2))
18061816

18071817
# wrong length
1808-
msg = r'Shape of passed values is \(3, 2\), indices imply \(3, 1\)'
1818+
msg = r'Shape of passed values is \(2, 3\), indices imply \(1, 3\)'
18091819
with pytest.raises(ValueError, match=msg):
18101820
DataFrame.from_records(arr, index=index[:-1])
18111821

pandas/tests/io/json/test_pandas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def test_frame_from_json_bad_data(self):
346346
json = StringIO('{"columns":["A","B"],'
347347
'"index":["2","3"],'
348348
'"data":[[1.0,"1"],[2.0,"2"],[null,"3"]]}')
349-
msg = r"Shape of passed values is \(2, 3\), indices imply \(2, 2\)"
349+
msg = r"Shape of passed values is \(3, 2\), indices imply \(2, 2\)"
350350
with pytest.raises(ValueError, match=msg):
351351
read_json(json, orient="split")
352352

0 commit comments

Comments
 (0)