Skip to content
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

PERF: DataFrame reductions with axis=None and EA dtypes #54308

Merged
merged 3 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion asv_bench/benchmarks/stat_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


class FrameOps:
params = [ops, ["float", "int", "Int64"], [0, 1]]
params = [ops, ["float", "int", "Int64"], [0, 1, None]]
param_names = ["op", "dtype", "axis"]

def setup(self, op, dtype, axis):
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ Performance improvements
- Performance improvement in :func:`concat` (:issue:`52291`, :issue:`52290`)
- :class:`Period`'s default formatter (`period_format`) is now significantly (~twice) faster. This improves performance of ``str(Period)``, ``repr(Period)``, and :meth:`Period.strftime(fmt=None)`, as well as ``PeriodArray.strftime(fmt=None)``, ``PeriodIndex.strftime(fmt=None)`` and ``PeriodIndex.format(fmt=None)``. Finally, ``to_csv`` operations involving :class:`PeriodArray` or :class:`PeriodIndex` with default ``date_format`` are also significantly accelerated. (:issue:`51459`)
- Performance improvement accessing :attr:`arrays.IntegerArrays.dtype` & :attr:`arrays.FloatingArray.dtype` (:issue:`52998`)
- Performance improvement in :class:`DataFrame` reductions with ``axis=None`` and extension dtypes (:issue:`54308`)
- Performance improvement in :class:`MultiIndex` and multi-column operations (e.g. :meth:`DataFrame.sort_values`, :meth:`DataFrame.groupby`, :meth:`Series.unstack`) when index/column values are already sorted (:issue:`53806`)
- Performance improvement in :class:`Series` reductions (:issue:`52341`)
- Performance improvement in :func:`concat` when ``axis=1`` and objects have different indexes (:issue:`52541`)
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
needs_i8_conversion,
pandas_dtype,
)
from pandas.core.dtypes.concat import concat_compat
from pandas.core.dtypes.dtypes import (
ArrowDtype,
BaseMaskedDtype,
Expand Down Expand Up @@ -11064,6 +11065,11 @@ def _get_data() -> DataFrame:
if numeric_only:
df = _get_data()
if axis is None:
dtype = find_common_type([arr.dtype for arr in df._mgr.arrays])
if isinstance(dtype, ExtensionDtype):
df = df.astype(dtype, copy=False)
arr = concat_compat(list(df._iter_column_arrays()))
return arr._reduce(name, skipna=skipna, keepdims=False, **kwds)
return func(df.values)
elif axis == 1:
if len(df.index) == 0:
Expand Down
12 changes: 8 additions & 4 deletions pandas/tests/frame/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1868,13 +1868,14 @@ def test_prod_sum_min_count_mixed_object():

@pytest.mark.parametrize("method", ["min", "max", "mean", "median", "skew", "kurt"])
@pytest.mark.parametrize("numeric_only", [True, False])
def test_reduction_axis_none_returns_scalar(method, numeric_only):
@pytest.mark.parametrize("dtype", ["float64", "Float64"])
def test_reduction_axis_none_returns_scalar(method, numeric_only, dtype):
# GH#21597 As of 2.0, axis=None reduces over all axes.

df = DataFrame(np.random.randn(4, 4))
df = DataFrame(np.random.randn(4, 4), dtype=dtype)

result = getattr(df, method)(axis=None, numeric_only=numeric_only)
np_arr = df.to_numpy()
np_arr = df.to_numpy(dtype=np.float64)
if method in {"skew", "kurt"}:
comp_mod = pytest.importorskip("scipy.stats")
if method == "kurt":
Expand All @@ -1883,7 +1884,10 @@ def test_reduction_axis_none_returns_scalar(method, numeric_only):
tm.assert_almost_equal(result, expected)
else:
expected = getattr(np, method)(np_arr, axis=None)
assert result == expected
if dtype == "float64":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this?

Copy link
Member Author

@lukemanley lukemanley Jul 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed, I removed it. I had been testing with pyarrow types and reductions of pyarrow types can have small floating point differences vs numpy.

assert result == expected
else:
tm.assert_almost_equal(result, expected)


@pytest.mark.parametrize(
Expand Down