Skip to content

Backport PR #35590 on branch 1.1.x (BUG: validate index/data length match in DataFrame construction) #35597

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
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v1.1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ Categorical

-

**DataFrame**
- Bug in :class:`DataFrame` constructor failing to raise ``ValueError`` in some cases when data and index have mismatched lengths (:issue:`33437`)
-

.. ---------------------------------------------------------------------------
.. _whatsnew_111.contributors:
Expand Down
3 changes: 0 additions & 3 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ class Block(PandasObject):
is_extension = False
_can_hold_na = False
_can_consolidate = True
_verify_integrity = True
_validate_ndim = True

@classmethod
Expand Down Expand Up @@ -1525,7 +1524,6 @@ class ExtensionBlock(Block):
"""

_can_consolidate = False
_verify_integrity = False
_validate_ndim = False
is_extension = True

Expand Down Expand Up @@ -2613,7 +2611,6 @@ def _replace_coerce(
class CategoricalBlock(ExtensionBlock):
__slots__ = ()
is_categorical = True
_verify_integrity = True
_can_hold_na = True

should_store = Block.should_store
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def _verify_integrity(self) -> None:
mgr_shape = self.shape
tot_items = sum(len(x.mgr_locs) for x in self.blocks)
for block in self.blocks:
if block._verify_integrity and block.shape[1:] != mgr_shape[1:]:
if block.shape[1:] != mgr_shape[1:]:
raise construction_error(tot_items, block.shape[1:], self.axes)
if len(self.items) != tot_items:
raise AssertionError(
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2619,6 +2619,12 @@ class DatetimeSubclass(datetime):
data = pd.DataFrame({"datetime": [DatetimeSubclass(2020, 1, 1, 1, 1)]})
assert data.datetime.dtype == "datetime64[ns]"

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


class TestDataFrameConstructorWithDatetimeTZ:
def test_from_dict(self):
Expand Down