Skip to content

Commit fa1d2e2

Browse files
jbrockmendelSeeminSyed
authored andcommitted
BUG: resample.agg with read-only data (pandas-dev#32758)
1 parent 091cd7e commit fa1d2e2

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
@@ -848,11 +848,13 @@ cdef inline bint _treat_as_na(rank_t val, bint is_datetimelike) nogil:
848848
return val != val
849849

850850

851+
# GH#31710 use memorviews once cython 0.30 is released so we can
852+
# use `const rank_t[:, :] values`
851853
@cython.wraparound(False)
852854
@cython.boundscheck(False)
853855
def group_last(rank_t[:, :] out,
854856
int64_t[:] counts,
855-
rank_t[:, :] values,
857+
ndarray[rank_t, ndim=2] values,
856858
const int64_t[:] labels,
857859
Py_ssize_t min_count=-1):
858860
"""
@@ -937,11 +939,13 @@ def group_last(rank_t[:, :] out,
937939
raise RuntimeError("empty group with uint64_t")
938940

939941

942+
# GH#31710 use memorviews once cython 0.30 is released so we can
943+
# use `const rank_t[:, :] values`
940944
@cython.wraparound(False)
941945
@cython.boundscheck(False)
942946
def group_nth(rank_t[:, :] out,
943947
int64_t[:] counts,
944-
rank_t[:, :] values,
948+
ndarray[rank_t, ndim=2] values,
945949
const int64_t[:] labels, int64_t rank=1,
946950
Py_ssize_t min_count=-1):
947951
"""
@@ -1235,7 +1239,7 @@ ctypedef fused groupby_t:
12351239
@cython.boundscheck(False)
12361240
def group_max(groupby_t[:, :] out,
12371241
int64_t[:] counts,
1238-
groupby_t[:, :] values,
1242+
ndarray[groupby_t, ndim=2] values,
12391243
const int64_t[:] labels,
12401244
Py_ssize_t min_count=-1):
12411245
"""
@@ -1308,7 +1312,7 @@ def group_max(groupby_t[:, :] out,
13081312
@cython.boundscheck(False)
13091313
def group_min(groupby_t[:, :] out,
13101314
int64_t[:] counts,
1311-
groupby_t[:, :] values,
1315+
ndarray[groupby_t, ndim=2] values,
13121316
const int64_t[:] labels,
13131317
Py_ssize_t min_count=-1):
13141318
"""

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)