Skip to content

Commit e5081fc

Browse files
committed
BUG: Switched shapes in ValueError msg in DataFrame construct (pandas-dev#20742)
1 parent 3fe28fc commit e5081fc

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,7 @@ Other
18521852
^^^^^
18531853

18541854
- Bug where C variables were declared with external linkage causing import errors if certain other C libraries were imported before Pandas. (:issue:`24113`)
1855+
- Switched shape in ``ValueError`` message when constructiong a :class:`DataFrame` with parameters ``columns`` and ``index`` not matching the shape of the input data. (:issue:`20742`)
18551856

18561857
.. _whatsnew_0.24.0.contributors:
18571858

pandas/core/internals/managers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1673,8 +1673,8 @@ def create_block_manager_from_arrays(arrays, names, axes):
16731673

16741674
def construction_error(tot_items, block_shape, axes, e=None):
16751675
""" raise a helpful message about our construction """
1676-
passed = tuple(map(int, [tot_items] + list(block_shape)))
1677-
implied = tuple(map(int, [len(ax) for ax in axes]))
1676+
passed = tuple(map(int, reversed([tot_items] + list(block_shape))))
1677+
implied = tuple(map(int, reversed([len(ax) for ax in axes])))
16781678
if passed == implied and e is not None:
16791679
raise e
16801680
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+
# see issue #20742
396+
msg = r"Shape of passed values is \(1, 3\), indices imply \(1, 4\)"
397+
with pytest.raises(ValueError, match=msg):
398+
df = pd.DataFrame(index=[0], columns=range(0, 4), data=np.array([[4,5,6]]))
399+
400+
# see issue #20742
401+
msg = r"Shape of passed values is \(3, 1\), indices imply \(1, 4\)"
402+
with pytest.raises(ValueError, match=msg):
403+
df = pd.DataFrame(index=[0], columns=range(0, 4), data=np.array([4,5,6]))
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

0 commit comments

Comments
 (0)