Skip to content

Commit a4358bd

Browse files
authored
CLN: cleanups in DataFrame._reduce (#36674)
1 parent ab3e466 commit a4358bd

File tree

1 file changed

+20
-30
lines changed

1 file changed

+20
-30
lines changed

pandas/core/frame.py

+20-30
Original file line numberDiff line numberDiff line change
@@ -638,10 +638,10 @@ def _can_fast_transpose(self) -> bool:
638638
"""
639639
Can we transpose this DataFrame without creating any new array objects.
640640
"""
641-
if self._data.any_extension_types:
641+
if self._mgr.any_extension_types:
642642
# TODO(EA2D) special case would be unnecessary with 2D EAs
643643
return False
644-
return len(self._data.blocks) == 1
644+
return len(self._mgr.blocks) == 1
645645

646646
# ----------------------------------------------------------------------
647647
# Rendering Methods
@@ -2879,7 +2879,7 @@ def _get_column_array(self, i: int) -> ArrayLike:
28792879
Get the values of the i'th column (ndarray or ExtensionArray, as stored
28802880
in the Block)
28812881
"""
2882-
return self._data.iget_values(i)
2882+
return self._mgr.iget_values(i)
28832883

28842884
def _iter_column_arrays(self) -> Iterator[ArrayLike]:
28852885
"""
@@ -4911,7 +4911,7 @@ def _maybe_casted_values(index, labels=None):
49114911

49124912
@doc(NDFrame.isna, klass=_shared_doc_kwargs["klass"])
49134913
def isna(self) -> DataFrame:
4914-
result = self._constructor(self._data.isna(func=isna))
4914+
result = self._constructor(self._mgr.isna(func=isna))
49154915
return result.__finalize__(self, method="isna")
49164916

49174917
@doc(NDFrame.isna, klass=_shared_doc_kwargs["klass"])
@@ -8575,6 +8575,7 @@ def _reduce(
85758575
):
85768576

85778577
assert filter_type is None or filter_type == "bool", filter_type
8578+
out_dtype = "bool" if filter_type == "bool" else None
85788579

85798580
dtype_is_dt = np.array(
85808581
[
@@ -8594,10 +8595,9 @@ def _reduce(
85948595
cols = self.columns[~dtype_is_dt]
85958596
self = self[cols]
85968597

8597-
# TODO: Make other agg func handle axis=None properly
8598+
# TODO: Make other agg func handle axis=None properly GH#21597
85988599
axis = self._get_axis_number(axis)
85998600
labels = self._get_agg_axis(axis)
8600-
constructor = self._constructor
86018601
assert axis in [0, 1]
86028602

86038603
def func(values):
@@ -8606,18 +8606,19 @@ def func(values):
86068606
else:
86078607
return op(values, axis=axis, skipna=skipna, **kwds)
86088608

8609+
def blk_func(values):
8610+
if isinstance(values, ExtensionArray):
8611+
return values._reduce(name, skipna=skipna, **kwds)
8612+
else:
8613+
return op(values, axis=1, skipna=skipna, **kwds)
8614+
86098615
def _get_data() -> DataFrame:
86108616
if filter_type is None:
86118617
data = self._get_numeric_data()
8612-
elif filter_type == "bool":
8618+
else:
86138619
# GH#25101, GH#24434
8620+
assert filter_type == "bool"
86148621
data = self._get_bool_data()
8615-
else: # pragma: no cover
8616-
msg = (
8617-
f"Generating numeric_only data with filter_type {filter_type} "
8618-
"not supported."
8619-
)
8620-
raise NotImplementedError(msg)
86218622
return data
86228623

86238624
if numeric_only is not None:
@@ -8628,14 +8629,6 @@ def _get_data() -> DataFrame:
86288629
df = df.T
86298630
axis = 0
86308631

8631-
out_dtype = "bool" if filter_type == "bool" else None
8632-
8633-
def blk_func(values):
8634-
if isinstance(values, ExtensionArray):
8635-
return values._reduce(name, skipna=skipna, **kwds)
8636-
else:
8637-
return op(values, axis=1, skipna=skipna, **kwds)
8638-
86398632
# After possibly _get_data and transposing, we are now in the
86408633
# simple case where we can use BlockManager.reduce
86418634
res = df._mgr.reduce(blk_func)
@@ -8651,11 +8644,10 @@ def blk_func(values):
86518644
if not self._is_homogeneous_type or self._mgr.any_extension_types:
86528645
# try to avoid self.values call
86538646

8654-
if filter_type is None and axis == 0 and len(self) > 0:
8647+
if filter_type is None and axis == 0:
86558648
# operate column-wise
86568649

86578650
# numeric_only must be None here, as other cases caught above
8658-
# require len(self) > 0 bc frame_apply messes up empty prod/sum
86598651

86608652
# this can end up with a non-reduction
86618653
# but not always. if the types are mixed
@@ -8691,19 +8683,17 @@ def blk_func(values):
86918683
with np.errstate(all="ignore"):
86928684
result = func(values)
86938685

8694-
if is_object_dtype(result.dtype):
8686+
if filter_type == "bool" and notna(result).all():
8687+
result = result.astype(np.bool_)
8688+
elif filter_type is None and is_object_dtype(result.dtype):
86958689
try:
8696-
if filter_type is None:
8697-
result = result.astype(np.float64)
8698-
elif filter_type == "bool" and notna(result).all():
8699-
result = result.astype(np.bool_)
8690+
result = result.astype(np.float64)
87008691
except (ValueError, TypeError):
87018692
# try to coerce to the original dtypes item by item if we can
87028693
if axis == 0:
87038694
result = coerce_to_dtypes(result, data.dtypes)
87048695

8705-
if constructor is not None:
8706-
result = self._constructor_sliced(result, index=labels)
8696+
result = self._constructor_sliced(result, index=labels)
87078697
return result
87088698

87098699
def nunique(self, axis=0, dropna=True) -> Series:

0 commit comments

Comments
 (0)