Skip to content

Commit ef2ebc3

Browse files
Backport PR #32758: BUG: resample.agg with read-only data (#32765)
Co-authored-by: jbrockmendel <[email protected]>
1 parent efa701d commit ef2ebc3

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

doc/source/whatsnew/v1.0.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ including other versions of pandas.
1515

1616
Fixed regressions
1717
~~~~~~~~~~~~~~~~~
18+
- Fixed regression in ``resample.agg`` when the underlying data is non-writeable (:issue:`31710`)
1819

1920
.. _whatsnew_103.bug_fixes:
2021

pandas/_libs/groupby.pyx

+8-4
Original file line numberDiff line numberDiff line change
@@ -849,11 +849,13 @@ cdef inline bint _treat_as_na(rank_t val, bint is_datetimelike) nogil:
849849
return val != val
850850

851851

852+
# GH#31710 use memorviews once cython 0.30 is released so we can
853+
# use `const rank_t[:, :] values`
852854
@cython.wraparound(False)
853855
@cython.boundscheck(False)
854856
def group_last(rank_t[:, :] out,
855857
int64_t[:] counts,
856-
rank_t[:, :] values,
858+
ndarray[rank_t, ndim=2] values,
857859
const int64_t[:] labels,
858860
Py_ssize_t min_count=-1):
859861
"""
@@ -938,11 +940,13 @@ def group_last(rank_t[:, :] out,
938940
raise RuntimeError("empty group with uint64_t")
939941

940942

943+
# GH#31710 use memorviews once cython 0.30 is released so we can
944+
# use `const rank_t[:, :] values`
941945
@cython.wraparound(False)
942946
@cython.boundscheck(False)
943947
def group_nth(rank_t[:, :] out,
944948
int64_t[:] counts,
945-
rank_t[:, :] values,
949+
ndarray[rank_t, ndim=2] values,
946950
const int64_t[:] labels, int64_t rank=1,
947951
Py_ssize_t min_count=-1):
948952
"""
@@ -1236,7 +1240,7 @@ ctypedef fused groupby_t:
12361240
@cython.boundscheck(False)
12371241
def group_max(groupby_t[:, :] out,
12381242
int64_t[:] counts,
1239-
groupby_t[:, :] values,
1243+
ndarray[groupby_t, ndim=2] values,
12401244
const int64_t[:] labels,
12411245
Py_ssize_t min_count=-1):
12421246
"""
@@ -1309,7 +1313,7 @@ def group_max(groupby_t[:, :] out,
13091313
@cython.boundscheck(False)
13101314
def group_min(groupby_t[:, :] out,
13111315
int64_t[:] counts,
1312-
groupby_t[:, :] values,
1316+
ndarray[groupby_t, ndim=2] values,
13131317
const int64_t[:] labels,
13141318
Py_ssize_t min_count=-1):
13151319
"""

pandas/tests/resample/test_resample_api.py

+24
Original file line numberDiff line numberDiff line change
@@ -580,3 +580,27 @@ def test_agg_with_datetime_index_list_agg_func(col_name):
580580
columns=pd.MultiIndex(levels=[[col_name], ["mean"]], codes=[[0], [0]]),
581581
)
582582
tm.assert_frame_equal(result, expected)
583+
584+
585+
def test_resample_agg_readonly():
586+
# GH#31710 cython needs to allow readonly data
587+
index = pd.date_range("2020-01-01", "2020-01-02", freq="1h")
588+
arr = np.zeros_like(index)
589+
arr.setflags(write=False)
590+
591+
ser = pd.Series(arr, index=index)
592+
rs = ser.resample("1D")
593+
594+
expected = pd.Series([pd.Timestamp(0), pd.Timestamp(0)], index=index[::24])
595+
596+
result = rs.agg("last")
597+
tm.assert_series_equal(result, expected)
598+
599+
result = rs.agg("first")
600+
tm.assert_series_equal(result, expected)
601+
602+
result = rs.agg("max")
603+
tm.assert_series_equal(result, expected)
604+
605+
result = rs.agg("min")
606+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)