diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index 690e6b8f725ad..927aac72e1948 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -419,6 +419,7 @@ Numeric ^^^^^^^ - Bug in :func:`to_numeric` where float precision was incorrect (:issue:`31364`) - Bug in :meth:`DataFrame.any` with ``axis=1`` and ``bool_only=True`` ignoring the ``bool_only`` keyword (:issue:`32432`) +- Bug in :meth:`DataFrame.any` and :meth:`DataFrame.all` with object-dtype columns sometimes returning larger :class:`Series` when operating on a subset of columns (:issue:`34918`) - Bug in :meth:`Series.equals` where a ``ValueError`` was raised when numpy arrays were compared to scalars (:issue:`35267`) - Bug in :class:`Series` where two :class:`Series` each have a :class:`DatetimeIndex` with different timezones having those indexes incorrectly changed when performing arithmetic operations (:issue:`33671`) - Bug in :meth:`pd._testing.assert_almost_equal` was incorrect for complex numeric types (:issue:`28235`) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index a06d57e268fe2..d459c4f50e3c9 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -27,6 +27,7 @@ ) from pandas.core.dtypes.common import ( DT64NS_DTYPE, + is_bool_dtype, is_dtype_equal, is_extension_array_dtype, is_list_like, @@ -713,8 +714,9 @@ def get_bool_data(self, copy: bool = False) -> "BlockManager": copy : bool, default False Whether to copy the blocks """ - self._consolidate_inplace() - return self._combine([b for b in self.blocks if b.is_bool], copy) + # Note: use is_bool_dtype instead of blk.is_bool to exclude + # object-dtype blocks containing all-bool entries. + return self._combine([b for b in self.blocks if is_bool_dtype(b.dtype)], copy) def get_numeric_data(self, copy: bool = False) -> "BlockManager": """ diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index bddc50a3cbcc1..c0a9ebb01d002 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -687,6 +687,20 @@ def test_get_bool_data(self): np.array([True, False, True]), ) + def test_get_bool_data_consistency(self): + # GH#34918 + ser = Series([True, False, True], dtype=object) + ser2 = Series(["A", "B", "C"]) + df = ser.to_frame("A") + + expected = DataFrame(index=ser.index) + bd1 = df._get_bool_data() + tm.assert_frame_equal(bd1, expected) + + df["B"] = ser2 + bd2 = df._get_bool_data() + tm.assert_frame_equal(bd2, expected) + def test_unicode_repr_doesnt_raise(self): repr(create_mgr("b,\u05d0: object"))