Skip to content

Commit 5904807

Browse files
Backport PR pandas-dev#35590: BUG: validate index/data length match in DataFrame construction (pandas-dev#35597)
Co-authored-by: jbrockmendel <[email protected]>
1 parent d8cdcf0 commit 5904807

File tree

4 files changed

+11
-4
lines changed

4 files changed

+11
-4
lines changed

doc/source/whatsnew/v1.1.1.rst

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ Categorical
5050

5151
-
5252

53+
**DataFrame**
54+
- Bug in :class:`DataFrame` constructor failing to raise ``ValueError`` in some cases when data and index have mismatched lengths (:issue:`33437`)
55+
-
56+
5357
.. ---------------------------------------------------------------------------
5458
5559
.. _whatsnew_111.contributors:

pandas/core/internals/blocks.py

-3
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ class Block(PandasObject):
105105
is_extension = False
106106
_can_hold_na = False
107107
_can_consolidate = True
108-
_verify_integrity = True
109108
_validate_ndim = True
110109

111110
@classmethod
@@ -1525,7 +1524,6 @@ class ExtensionBlock(Block):
15251524
"""
15261525

15271526
_can_consolidate = False
1528-
_verify_integrity = False
15291527
_validate_ndim = False
15301528
is_extension = True
15311529

@@ -2613,7 +2611,6 @@ def _replace_coerce(
26132611
class CategoricalBlock(ExtensionBlock):
26142612
__slots__ = ()
26152613
is_categorical = True
2616-
_verify_integrity = True
26172614
_can_hold_na = True
26182615

26192616
should_store = Block.should_store

pandas/core/internals/managers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ def _verify_integrity(self) -> None:
312312
mgr_shape = self.shape
313313
tot_items = sum(len(x.mgr_locs) for x in self.blocks)
314314
for block in self.blocks:
315-
if block._verify_integrity and block.shape[1:] != mgr_shape[1:]:
315+
if block.shape[1:] != mgr_shape[1:]:
316316
raise construction_error(tot_items, block.shape[1:], self.axes)
317317
if len(self.items) != tot_items:
318318
raise AssertionError(

pandas/tests/frame/test_constructors.py

+6
Original file line numberDiff line numberDiff line change
@@ -2619,6 +2619,12 @@ class DatetimeSubclass(datetime):
26192619
data = pd.DataFrame({"datetime": [DatetimeSubclass(2020, 1, 1, 1, 1)]})
26202620
assert data.datetime.dtype == "datetime64[ns]"
26212621

2622+
def test_with_mismatched_index_length_raises(self):
2623+
# GH#33437
2624+
dti = pd.date_range("2016-01-01", periods=3, tz="US/Pacific")
2625+
with pytest.raises(ValueError, match="Shape of passed values"):
2626+
DataFrame(dti, index=range(4))
2627+
26222628

26232629
class TestDataFrameConstructorWithDatetimeTZ:
26242630
def test_from_dict(self):

0 commit comments

Comments
 (0)