Skip to content

CLN: is_mixed_type, is_homogeneous_type #54008

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
merged 1 commit into from
Jul 6, 2023
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
29 changes: 4 additions & 25 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,13 +961,6 @@ def _is_homogeneous_type(self) -> bool:
-------
bool

See Also
--------
Index._is_homogeneous_type : Whether the object has a single
dtype.
MultiIndex._is_homogeneous_type : Whether all the levels of a
MultiIndex have the same dtype.

Examples
--------
>>> DataFrame({"A": [1, 2], "B": [3, 4]})._is_homogeneous_type
Expand All @@ -983,12 +976,8 @@ def _is_homogeneous_type(self) -> bool:
... "B": np.array([1, 2], dtype=np.int64)})._is_homogeneous_type
False
"""
if isinstance(self._mgr, ArrayManager):
return len({arr.dtype for arr in self._mgr.arrays}) == 1
if self._mgr.any_extension_types:
return len({block.dtype for block in self._mgr.blocks}) == 1
else:
return not self._is_mixed_type
# The "<" part of "<=" here is for empty DataFrame cases
return len({arr.dtype for arr in self._mgr.arrays}) <= 1

@property
def _can_fast_transpose(self) -> bool:
Expand Down Expand Up @@ -4958,7 +4947,7 @@ def _reindex_multi(
if row_indexer is not None and col_indexer is not None:
# Fastpath. By doing two 'take's at once we avoid making an
# unnecessary copy.
# We only get here with `not self._is_mixed_type`, which (almost)
# We only get here with `self._can_fast_transpose`, which (almost)
# ensures that self.values is cheap. It may be worth making this
# condition more specific.
indexer = row_indexer, col_indexer
Expand Down Expand Up @@ -10849,17 +10838,7 @@ def count(self, axis: Axis = 0, numeric_only: bool = False):
if len(frame._get_axis(axis)) == 0:
result = self._constructor_sliced(0, index=frame._get_agg_axis(axis))
else:
if frame._is_mixed_type or frame._mgr.any_extension_types:
# the or any_extension_types is really only hit for single-
# column frames with an extension array
result = notna(frame).sum(axis=axis)
else:
# GH13407
series_counts = notna(frame).sum(axis=axis)
counts = series_counts._values
result = self._constructor_sliced(
counts, index=frame._get_agg_axis(axis), copy=False
)
result = notna(frame).sum(axis=axis)

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

Expand Down
11 changes: 5 additions & 6 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5536,12 +5536,9 @@ def _needs_reindex_multi(self, axes, method, level: Level | None) -> bool_t:
(common.count_not_none(*axes.values()) == self._AXIS_LEN)
and method is None
and level is None
and not self._is_mixed_type
and not (
self.ndim == 2
and len(self.dtypes) == 1
and isinstance(self.dtypes.iloc[0], ExtensionDtype)
)
# reindex_multi calls self.values, so we only want to go
# down that path when doing so is cheap.
and self._can_fast_transpose
)

def _reindex_multi(self, axes, copy, fill_value):
Expand Down Expand Up @@ -6266,9 +6263,11 @@ def _consolidate(self):
self
)

@final
@property
def _is_mixed_type(self) -> bool_t:
if self._mgr.is_single_block:
# Includes all Series cases
return False

if self._mgr.any_extension_types:
Expand Down
4 changes: 0 additions & 4 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,6 @@ def _convert(arr):
def to_native_types(self, **kwargs) -> Self:
return self.apply(to_native_types, **kwargs)

@property
def is_mixed_type(self) -> bool:
return True

@property
def any_extension_types(self) -> bool:
"""Whether any of the blocks in this manager are extension blocks"""
Expand Down
5 changes: 0 additions & 5 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1383,7 +1383,6 @@ def _maybe_update_cacher(
return
cacher = getattr(self, "_cacher", None)
if cacher is not None:
assert self.ndim == 1
ref: DataFrame = cacher[1]()

# we are trying to reference a dead referent, hence
Expand All @@ -1407,10 +1406,6 @@ def _maybe_update_cacher(
# ----------------------------------------------------------------------
# Unsorted

@property
def _is_mixed_type(self) -> bool:
return False

def repeat(self, repeats: int | Sequence[int], axis: None = None) -> Series:
"""
Repeat elements of a Series.
Expand Down