Skip to content

Commit 130471e

Browse files
jbrockmendelim-vinicius
authored and
im-vinicius
committed
CLN: is_mixed_type, is_homogeneous_type (pandas-dev#54008)
CLN: is_mixed_type
1 parent a4154b6 commit 130471e

File tree

4 files changed

+9
-40
lines changed

4 files changed

+9
-40
lines changed

pandas/core/frame.py

+4-25
Original file line numberDiff line numberDiff line change
@@ -961,13 +961,6 @@ def _is_homogeneous_type(self) -> bool:
961961
-------
962962
bool
963963
964-
See Also
965-
--------
966-
Index._is_homogeneous_type : Whether the object has a single
967-
dtype.
968-
MultiIndex._is_homogeneous_type : Whether all the levels of a
969-
MultiIndex have the same dtype.
970-
971964
Examples
972965
--------
973966
>>> DataFrame({"A": [1, 2], "B": [3, 4]})._is_homogeneous_type
@@ -983,12 +976,8 @@ def _is_homogeneous_type(self) -> bool:
983976
... "B": np.array([1, 2], dtype=np.int64)})._is_homogeneous_type
984977
False
985978
"""
986-
if isinstance(self._mgr, ArrayManager):
987-
return len({arr.dtype for arr in self._mgr.arrays}) == 1
988-
if self._mgr.any_extension_types:
989-
return len({block.dtype for block in self._mgr.blocks}) == 1
990-
else:
991-
return not self._is_mixed_type
979+
# The "<" part of "<=" here is for empty DataFrame cases
980+
return len({arr.dtype for arr in self._mgr.arrays}) <= 1
992981

993982
@property
994983
def _can_fast_transpose(self) -> bool:
@@ -4958,7 +4947,7 @@ def _reindex_multi(
49584947
if row_indexer is not None and col_indexer is not None:
49594948
# Fastpath. By doing two 'take's at once we avoid making an
49604949
# unnecessary copy.
4961-
# We only get here with `not self._is_mixed_type`, which (almost)
4950+
# We only get here with `self._can_fast_transpose`, which (almost)
49624951
# ensures that self.values is cheap. It may be worth making this
49634952
# condition more specific.
49644953
indexer = row_indexer, col_indexer
@@ -10849,17 +10838,7 @@ def count(self, axis: Axis = 0, numeric_only: bool = False):
1084910838
if len(frame._get_axis(axis)) == 0:
1085010839
result = self._constructor_sliced(0, index=frame._get_agg_axis(axis))
1085110840
else:
10852-
if frame._is_mixed_type or frame._mgr.any_extension_types:
10853-
# the or any_extension_types is really only hit for single-
10854-
# column frames with an extension array
10855-
result = notna(frame).sum(axis=axis)
10856-
else:
10857-
# GH13407
10858-
series_counts = notna(frame).sum(axis=axis)
10859-
counts = series_counts._values
10860-
result = self._constructor_sliced(
10861-
counts, index=frame._get_agg_axis(axis), copy=False
10862-
)
10841+
result = notna(frame).sum(axis=axis)
1086310842

1086410843
return result.astype("int64").__finalize__(self, method="count")
1086510844

pandas/core/generic.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -5543,12 +5543,9 @@ def _needs_reindex_multi(self, axes, method, level: Level | None) -> bool_t:
55435543
(common.count_not_none(*axes.values()) == self._AXIS_LEN)
55445544
and method is None
55455545
and level is None
5546-
and not self._is_mixed_type
5547-
and not (
5548-
self.ndim == 2
5549-
and len(self.dtypes) == 1
5550-
and isinstance(self.dtypes.iloc[0], ExtensionDtype)
5551-
)
5546+
# reindex_multi calls self.values, so we only want to go
5547+
# down that path when doing so is cheap.
5548+
and self._can_fast_transpose
55525549
)
55535550

55545551
def _reindex_multi(self, axes, copy, fill_value):
@@ -6273,9 +6270,11 @@ def _consolidate(self):
62736270
self
62746271
)
62756272

6273+
@final
62766274
@property
62776275
def _is_mixed_type(self) -> bool_t:
62786276
if self._mgr.is_single_block:
6277+
# Includes all Series cases
62796278
return False
62806279

62816280
if self._mgr.any_extension_types:

pandas/core/internals/array_manager.py

-4
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,6 @@ def _convert(arr):
347347
def to_native_types(self, **kwargs) -> Self:
348348
return self.apply(to_native_types, **kwargs)
349349

350-
@property
351-
def is_mixed_type(self) -> bool:
352-
return True
353-
354350
@property
355351
def any_extension_types(self) -> bool:
356352
"""Whether any of the blocks in this manager are extension blocks"""

pandas/core/series.py

-5
Original file line numberDiff line numberDiff line change
@@ -1383,7 +1383,6 @@ def _maybe_update_cacher(
13831383
return
13841384
cacher = getattr(self, "_cacher", None)
13851385
if cacher is not None:
1386-
assert self.ndim == 1
13871386
ref: DataFrame = cacher[1]()
13881387

13891388
# we are trying to reference a dead referent, hence
@@ -1407,10 +1406,6 @@ def _maybe_update_cacher(
14071406
# ----------------------------------------------------------------------
14081407
# Unsorted
14091408

1410-
@property
1411-
def _is_mixed_type(self) -> bool:
1412-
return False
1413-
14141409
def repeat(self, repeats: int | Sequence[int], axis: None = None) -> Series:
14151410
"""
14161411
Repeat elements of a Series.

0 commit comments

Comments
 (0)