Skip to content

Commit bcaa5da

Browse files
jbrockmendeljreback
authored andcommitted
separate _libs/src/reduce.pyx to _libs.reduction (pandas-dev#19306)
1 parent ae96187 commit bcaa5da

File tree

6 files changed

+48
-29
lines changed

6 files changed

+48
-29
lines changed

pandas/_libs/lib.pyx

+1-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from cython cimport Py_ssize_t
66

77
import numpy as np
88
cimport numpy as np
9-
from numpy cimport (ndarray, PyArray_NDIM, PyArray_GETITEM, PyArray_SETITEM,
9+
from numpy cimport (ndarray, PyArray_NDIM, PyArray_GETITEM,
1010
PyArray_ITER_DATA, PyArray_ITER_NEXT, PyArray_IterNew,
1111
flatiter, NPY_OBJECT,
1212
int64_t,
@@ -57,8 +57,6 @@ cimport util
5757
cdef int64_t NPY_NAT = util.get_nat()
5858
from util cimport is_array, _checknull
5959

60-
from libc.math cimport fabs, sqrt
61-
6260

6361
def values_from_object(object o):
6462
""" return my values or the object if we are say an ndarray """
@@ -1119,5 +1117,4 @@ def indices_fast(object index, ndarray[int64_t] labels, list keys,
11191117
return result
11201118

11211119

1122-
include "reduce.pyx"
11231120
include "inference.pyx"

pandas/_libs/src/reduce.pyx renamed to pandas/_libs/reduction.pyx

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
# -*- coding: utf-8 -*-
22
# cython: profile=False
3-
import numpy as np
4-
53
from distutils.version import LooseVersion
64

5+
from cython cimport Py_ssize_t
6+
from cpython cimport Py_INCREF
7+
8+
from libc.stdlib cimport malloc, free
9+
10+
import numpy as np
11+
cimport numpy as np
12+
from numpy cimport (ndarray,
13+
int64_t,
14+
PyArray_SETITEM,
15+
PyArray_ITER_NEXT, PyArray_ITER_DATA, PyArray_IterNew,
16+
flatiter)
17+
np.import_array()
18+
19+
cimport util
20+
from lib import maybe_convert_objects
21+
722
is_numpy_prior_1_6_2 = LooseVersion(np.__version__) < '1.6.2'
823

924

pandas/core/apply.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import numpy as np
22
from pandas import compat
3-
from pandas._libs import lib
3+
from pandas._libs import reduction
44
from pandas.core.dtypes.common import (
55
is_extension_type,
66
is_sequence)
@@ -114,7 +114,7 @@ def apply_empty_result(self):
114114

115115
def apply_raw(self):
116116
try:
117-
result = lib.reduce(self.values, self.f, axis=self.axis)
117+
result = reduction.reduce(self.values, self.f, axis=self.axis)
118118
except Exception:
119119
result = np.apply_along_axis(self.f, self.axis, self.values)
120120

@@ -150,10 +150,10 @@ def apply_standard(self):
150150

151151
try:
152152
labels = self.agg_axis
153-
result = lib.reduce(values, self.f,
154-
axis=self.axis,
155-
dummy=dummy,
156-
labels=labels)
153+
result = reduction.reduce(values, self.f,
154+
axis=self.axis,
155+
dummy=dummy,
156+
labels=labels)
157157
return Series(result, index=labels)
158158
except Exception:
159159
pass

pandas/core/groupby.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@
6363

6464
from pandas.plotting._core import boxplot_frame_groupby
6565

66-
from pandas._libs import lib, groupby as libgroupby, Timestamp, NaT, iNaT
66+
from pandas._libs import (lib, reduction,
67+
groupby as libgroupby,
68+
Timestamp, NaT, iNaT)
6769
from pandas._libs.lib import count_level_2d
6870

6971
_doc_template = """
@@ -1978,7 +1980,7 @@ def apply(self, f, data, axis=0):
19781980
try:
19791981
values, mutated = splitter.fast_apply(f, group_keys)
19801982
return group_keys, values, mutated
1981-
except (lib.InvalidApply):
1983+
except reduction.InvalidApply:
19821984
# we detect a mutation of some kind
19831985
# so take slow path
19841986
pass
@@ -2401,8 +2403,8 @@ def _aggregate_series_fast(self, obj, func):
24012403
obj = obj._take(indexer, convert=False).to_dense()
24022404
group_index = algorithms.take_nd(
24032405
group_index, indexer, allow_fill=False)
2404-
grouper = lib.SeriesGrouper(obj, func, group_index, ngroups,
2405-
dummy)
2406+
grouper = reduction.SeriesGrouper(obj, func, group_index, ngroups,
2407+
dummy)
24062408
result, counts = grouper.get_result()
24072409
return result, counts
24082410

@@ -2615,7 +2617,7 @@ def groupings(self):
26152617

26162618
def agg_series(self, obj, func):
26172619
dummy = obj[:0]
2618-
grouper = lib.SeriesBinGrouper(obj, func, self.bins, dummy)
2620+
grouper = reduction.SeriesBinGrouper(obj, func, self.bins, dummy)
26192621
return grouper.get_result()
26202622

26212623
# ----------------------------------------------------------------------
@@ -4755,7 +4757,8 @@ def fast_apply(self, f, names):
47554757
return [], True
47564758

47574759
sdata = self._get_sorted_data()
4758-
results, mutated = lib.apply_frame_axis0(sdata, f, names, starts, ends)
4760+
results, mutated = reduction.apply_frame_axis0(sdata, f, names,
4761+
starts, ends)
47594762

47604763
return results, mutated
47614764

pandas/tests/groupby/test_bin_groupby.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pandas import Index, isna
1010
from pandas.util.testing import assert_almost_equal
1111
import pandas.util.testing as tm
12-
from pandas._libs import lib, groupby
12+
from pandas._libs import lib, groupby, reduction
1313

1414

1515
def test_series_grouper():
@@ -19,7 +19,7 @@ def test_series_grouper():
1919

2020
labels = np.array([-1, -1, -1, 0, 0, 0, 1, 1, 1, 1], dtype=np.int64)
2121

22-
grouper = lib.SeriesGrouper(obj, np.mean, labels, 2, dummy)
22+
grouper = reduction.SeriesGrouper(obj, np.mean, labels, 2, dummy)
2323
result, counts = grouper.get_result()
2424

2525
expected = np.array([obj[3:6].mean(), obj[6:].mean()])
@@ -36,7 +36,7 @@ def test_series_bin_grouper():
3636

3737
bins = np.array([3, 6])
3838

39-
grouper = lib.SeriesBinGrouper(obj, np.mean, bins, dummy)
39+
grouper = reduction.SeriesBinGrouper(obj, np.mean, bins, dummy)
4040
result, counts = grouper.get_result()
4141

4242
expected = np.array([obj[:3].mean(), obj[3:6].mean(), obj[6:].mean()])
@@ -127,26 +127,27 @@ def test_int_index(self):
127127
from pandas.core.series import Series
128128

129129
arr = np.random.randn(100, 4)
130-
result = lib.reduce(arr, np.sum, labels=Index(np.arange(4)))
130+
result = reduction.reduce(arr, np.sum, labels=Index(np.arange(4)))
131131
expected = arr.sum(0)
132132
assert_almost_equal(result, expected)
133133

134-
result = lib.reduce(arr, np.sum, axis=1, labels=Index(np.arange(100)))
134+
result = reduction.reduce(arr, np.sum, axis=1,
135+
labels=Index(np.arange(100)))
135136
expected = arr.sum(1)
136137
assert_almost_equal(result, expected)
137138

138139
dummy = Series(0., index=np.arange(100))
139-
result = lib.reduce(arr, np.sum, dummy=dummy,
140-
labels=Index(np.arange(4)))
140+
result = reduction.reduce(arr, np.sum, dummy=dummy,
141+
labels=Index(np.arange(4)))
141142
expected = arr.sum(0)
142143
assert_almost_equal(result, expected)
143144

144145
dummy = Series(0., index=np.arange(4))
145-
result = lib.reduce(arr, np.sum, axis=1, dummy=dummy,
146-
labels=Index(np.arange(100)))
146+
result = reduction.reduce(arr, np.sum, axis=1, dummy=dummy,
147+
labels=Index(np.arange(100)))
147148
expected = arr.sum(1)
148149
assert_almost_equal(result, expected)
149150

150-
result = lib.reduce(arr, np.sum, axis=1, dummy=dummy,
151-
labels=Index(np.arange(100)))
151+
result = reduction.reduce(arr, np.sum, axis=1, dummy=dummy,
152+
labels=Index(np.arange(100)))
152153
assert_almost_equal(result, expected)

setup.py

+3
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ class CheckSDist(sdist_class):
309309
'pandas/_libs/interval.pyx',
310310
'pandas/_libs/hashing.pyx',
311311
'pandas/_libs/missing.pyx',
312+
'pandas/_libs/reduction.pyx',
312313
'pandas/_libs/testing.pyx',
313314
'pandas/_libs/window.pyx',
314315
'pandas/_libs/skiplist.pyx',
@@ -506,6 +507,8 @@ def pxd(name):
506507
'pandas/_libs/src/numpy_helper.h'],
507508
'sources': ['pandas/_libs/src/parser/tokenizer.c',
508509
'pandas/_libs/src/parser/io.c']},
510+
'_libs.reduction': {
511+
'pyxfile': '_libs/reduction'},
509512
'_libs.tslibs.period': {
510513
'pyxfile': '_libs/tslibs/period',
511514
'pxdfiles': ['_libs/src/util',

0 commit comments

Comments
 (0)