Skip to content

Commit

Permalink
BUG: resample.agg with read-only data (pandas-dev#32758)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and SeeminSyed committed Mar 22, 2020
1 parent 091cd7e commit fa1d2e2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
12 changes: 8 additions & 4 deletions pandas/_libs/groupby.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/resample/test_resample_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit fa1d2e2

Please sign in to comment.