From 5351732faf4b7186628579709439e1b29494a560 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 16 Mar 2020 18:34:15 -0700 Subject: [PATCH] BUG: resample.agg with read-only data (#32758) --- doc/source/whatsnew/v1.0.3.rst | 1 + pandas/_libs/groupby.pyx | 12 +++++++---- pandas/tests/resample/test_resample_api.py | 24 ++++++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v1.0.3.rst b/doc/source/whatsnew/v1.0.3.rst index 17f1bdc365518..482222fbddbb8 100644 --- a/doc/source/whatsnew/v1.0.3.rst +++ b/doc/source/whatsnew/v1.0.3.rst @@ -15,6 +15,7 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ +- Fixed regression in ``resample.agg`` when the underlying data is non-writeable (:issue:`31710`) .. _whatsnew_103.bug_fixes: diff --git a/pandas/_libs/groupby.pyx b/pandas/_libs/groupby.pyx index 27b3095d8cb4f..35a6963165194 100644 --- a/pandas/_libs/groupby.pyx +++ b/pandas/_libs/groupby.pyx @@ -848,11 +848,13 @@ cdef inline bint _treat_as_na(rank_t val, bint is_datetimelike) nogil: return val != val +# GH#31710 use memorviews once cython 0.30 is released so we can +# use `const rank_t[:, :] values` @cython.wraparound(False) @cython.boundscheck(False) def group_last(rank_t[:, :] out, int64_t[:] counts, - rank_t[:, :] values, + ndarray[rank_t, ndim=2] values, const int64_t[:] labels, Py_ssize_t min_count=-1): """ @@ -937,11 +939,13 @@ def group_last(rank_t[:, :] out, raise RuntimeError("empty group with uint64_t") +# GH#31710 use memorviews once cython 0.30 is released so we can +# use `const rank_t[:, :] values` @cython.wraparound(False) @cython.boundscheck(False) def group_nth(rank_t[:, :] out, int64_t[:] counts, - rank_t[:, :] values, + ndarray[rank_t, ndim=2] values, const int64_t[:] labels, int64_t rank=1, Py_ssize_t min_count=-1): """ @@ -1235,7 +1239,7 @@ ctypedef fused groupby_t: @cython.boundscheck(False) def group_max(groupby_t[:, :] out, int64_t[:] counts, - groupby_t[:, :] values, + ndarray[groupby_t, ndim=2] values, const int64_t[:] labels, Py_ssize_t min_count=-1): """ @@ -1308,7 +1312,7 @@ def group_max(groupby_t[:, :] out, @cython.boundscheck(False) def group_min(groupby_t[:, :] out, int64_t[:] counts, - groupby_t[:, :] values, + ndarray[groupby_t, ndim=2] values, const int64_t[:] labels, Py_ssize_t min_count=-1): """ diff --git a/pandas/tests/resample/test_resample_api.py b/pandas/tests/resample/test_resample_api.py index d552241f9126f..6389c88c99f73 100644 --- a/pandas/tests/resample/test_resample_api.py +++ b/pandas/tests/resample/test_resample_api.py @@ -580,3 +580,27 @@ def test_agg_with_datetime_index_list_agg_func(col_name): columns=pd.MultiIndex(levels=[[col_name], ["mean"]], codes=[[0], [0]]), ) tm.assert_frame_equal(result, expected) + + +def test_resample_agg_readonly(): + # GH#31710 cython needs to allow readonly data + index = pd.date_range("2020-01-01", "2020-01-02", freq="1h") + arr = np.zeros_like(index) + arr.setflags(write=False) + + ser = pd.Series(arr, index=index) + rs = ser.resample("1D") + + expected = pd.Series([pd.Timestamp(0), pd.Timestamp(0)], index=index[::24]) + + result = rs.agg("last") + tm.assert_series_equal(result, expected) + + result = rs.agg("first") + tm.assert_series_equal(result, expected) + + result = rs.agg("max") + tm.assert_series_equal(result, expected) + + result = rs.agg("min") + tm.assert_series_equal(result, expected)