Skip to content

Commit eb58298

Browse files
authored
BLD: remove libreduction (#52660)
1 parent 80001a0 commit eb58298

File tree

5 files changed

+34
-49
lines changed

5 files changed

+34
-49
lines changed

pandas/_libs/reduction.pyi

-6
This file was deleted.

pandas/_libs/reduction.pyx

-33
This file was deleted.

pandas/core/groupby/generic.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
from pandas._libs import (
3131
Interval,
3232
lib,
33-
reduction as libreduction,
3433
)
3534
from pandas.errors import SpecificationError
3635
from pandas.util._decorators import (
@@ -66,7 +65,10 @@
6665
)
6766
import pandas.core.common as com
6867
from pandas.core.frame import DataFrame
69-
from pandas.core.groupby import base
68+
from pandas.core.groupby import (
69+
base,
70+
ops,
71+
)
7072
from pandas.core.groupby.groupby import (
7173
GroupBy,
7274
GroupByPlot,
@@ -437,10 +439,10 @@ def _aggregate_named(self, func, *args, **kwargs):
437439
object.__setattr__(group, "name", name)
438440

439441
output = func(group, *args, **kwargs)
440-
output = libreduction.extract_result(output)
442+
output = ops.extract_result(output)
441443
if not initialized:
442444
# We only do this validation on the first iteration
443-
libreduction.check_result_array(output, group.dtype)
445+
ops.check_result_array(output, group.dtype)
444446
initialized = True
445447
result[name] = output
446448

pandas/core/groupby/ops.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
lib,
2727
)
2828
import pandas._libs.groupby as libgroupby
29-
import pandas._libs.reduction as libreduction
3029
from pandas._typing import (
3130
ArrayLike,
3231
AxisInt,
@@ -75,6 +74,31 @@
7574
from pandas.core.generic import NDFrame
7675

7776

77+
def check_result_array(obj, dtype):
78+
# Our operation is supposed to be an aggregation/reduction. If
79+
# it returns an ndarray, this likely means an invalid operation has
80+
# been passed. See test_apply_without_aggregation, test_agg_must_agg
81+
if isinstance(obj, np.ndarray):
82+
if dtype != object:
83+
# If it is object dtype, the function can be a reduction/aggregation
84+
# and still return an ndarray e.g. test_agg_over_numpy_arrays
85+
raise ValueError("Must produce aggregated value")
86+
87+
88+
def extract_result(res):
89+
"""
90+
Extract the result object, it might be a 0-dim ndarray
91+
or a len-1 0-dim, or a scalar
92+
"""
93+
if hasattr(res, "_values"):
94+
# Preserve EA
95+
res = res._values
96+
if res.ndim == 1 and len(res) == 1:
97+
# see test_agg_lambda_with_timezone, test_resampler_grouper.py::test_apply
98+
res = res[0]
99+
return res
100+
101+
78102
class WrappedCythonOp:
79103
"""
80104
Dispatch logic for functions defined in _libs.groupby
@@ -836,11 +860,11 @@ def _aggregate_series_pure_python(
836860

837861
for i, group in enumerate(splitter):
838862
res = func(group)
839-
res = libreduction.extract_result(res)
863+
res = extract_result(res)
840864

841865
if not initialized:
842866
# We only do this validation on the first iteration
843-
libreduction.check_result_array(res, group.dtype)
867+
check_result_array(res, group.dtype)
844868
initialized = True
845869

846870
result[i] = res
@@ -948,7 +972,7 @@ def __init__(
948972
self.indexer = indexer
949973

950974
# These lengths must match, otherwise we could call agg_series
951-
# with empty self.bins, which would raise in libreduction.
975+
# with empty self.bins, which would raise later.
952976
assert len(self.binlabels) == len(self.bins)
953977

954978
@cache_readonly

setup.py

-2
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ class CheckSDist(sdist_class):
204204
"pandas/_libs/interval.pyx",
205205
"pandas/_libs/hashing.pyx",
206206
"pandas/_libs/missing.pyx",
207-
"pandas/_libs/reduction.pyx",
208207
"pandas/_libs/testing.pyx",
209208
"pandas/_libs/sparse.pyx",
210209
"pandas/_libs/ops.pyx",
@@ -486,7 +485,6 @@ def srcpath(name=None, suffix=".pyx", subdir="src"):
486485
"pandas/_libs/src/pd_parser.h",
487486
],
488487
},
489-
"_libs.reduction": {"pyxfile": "_libs/reduction"},
490488
"_libs.ops": {"pyxfile": "_libs/ops"},
491489
"_libs.ops_dispatch": {"pyxfile": "_libs/ops_dispatch"},
492490
"_libs.properties": {"pyxfile": "_libs/properties"},

0 commit comments

Comments
 (0)