Skip to content

Commit 07372e3

Browse files
reuse blk_func for column-wise, inline _iter_arrays
1 parent a9706e0 commit 07372e3

File tree

1 file changed

+11
-31
lines changed

1 file changed

+11
-31
lines changed

pandas/core/frame.py

+11-31
Original file line numberDiff line numberDiff line change
@@ -7915,12 +7915,18 @@ def _get_data(axis_matters):
79157915
raise NotImplementedError(msg)
79167916
return data
79177917

7918+
def blk_func(values):
7919+
if isinstance(values, ExtensionArray):
7920+
return values._reduce(name, skipna=skipna, **kwds)
7921+
else:
7922+
return op(values, axis=1, skipna=skipna, **kwds)
7923+
79187924
if axis == 0 and column_wise:
79197925
# column-wise reduction
79207926
df = self
79217927
if numeric_only is True:
79227928
df = _get_data(axis_matters=True)
7923-
return df._reduce_columns(op, name, skipna=skipna, **kwds)
7929+
return df._reduce_columns(blk_func)
79247930

79257931
if numeric_only is not None and axis in [0, 1]:
79267932
df = self
@@ -7932,12 +7938,6 @@ def _get_data(axis_matters):
79327938

79337939
out_dtype = "bool" if filter_type == "bool" else None
79347940

7935-
def blk_func(values):
7936-
if isinstance(values, ExtensionArray):
7937-
return values._reduce(name, skipna=skipna, **kwds)
7938-
else:
7939-
return op(values, axis=1, skipna=skipna, **kwds)
7940-
79417941
# After possibly _get_data and transposing, we are now in the
79427942
# simple case where we can use BlockManager._reduce
79437943
res = df._data.reduce(blk_func)
@@ -8018,47 +8018,27 @@ def blk_func(values):
80188018
result = self._constructor_sliced(result, index=labels)
80198019
return result
80208020

8021-
def _reduce_columns(self, op, name, skipna=True, **kwds):
8021+
def _reduce_columns(self, op):
80228022
"""
80238023
Reduce DataFrame column-wise.
80248024
80258025
Parameters
80268026
----------
80278027
op : func
8028-
The reducing function to be called on the values. Only used
8029-
for columns backed by a numpy ndarray.
8030-
name : str
8031-
The name of the reduction.
8032-
skipna, **kwds : keywords to pass to the `op` function
8028+
The reducing function to be called on the values.
80338029
80348030
Returns
80358031
-------
80368032
Series
80378033
"""
80388034
result = []
80398035

8040-
for arr in self._iter_arrays():
8041-
if isinstance(arr, ExtensionArray):
8042-
# dispatch to ExtensionArray interface
8043-
val = arr._reduce(name, skipna=skipna, **kwds)
8044-
else:
8045-
# dispatch to numpy arrays
8046-
with np.errstate(all="ignore"):
8047-
val = op(arr, skipna=skipna, **kwds)
8048-
8036+
for i in range(len(self.columns)):
8037+
val = op(self._data.iget_values(i))
80498038
result.append(val)
80508039

80518040
return self._constructor_sliced(result, index=self.columns)
80528041

8053-
def _iter_arrays(self):
8054-
"""
8055-
Iterate over the arrays of all columns in order.
8056-
8057-
This returns the values as stored in the Block (ndarray or ExtensionArray).
8058-
"""
8059-
for i in range(len(self.columns)):
8060-
yield self._data.iget_values(i)
8061-
80628042
def nunique(self, axis=0, dropna=True) -> Series:
80638043
"""
80648044
Count distinct observations over requested axis.

0 commit comments

Comments
 (0)