Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: resample.agg with read-only data #32758

Merged
merged 6 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this limitation really apply to any place where we are using memoryviews? Or just isolated to this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my understanding is that it applies to memoryviews with fused types

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
6 changes: 5 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,11 +840,15 @@ def _slice(self, slobj: slice, axis: int = 0) -> "Series":

def __getitem__(self, key):
key = com.apply_if_callable(key, self)
key = lib.item_from_zerodim(key)

if key is Ellipsis:
return self

key_is_scalar = is_scalar(key)
# check for is_list_like/slice instead of is_scalar to allow non-standard
# scalars through, e.g. cftime.datetime needed by xarray
# https://github.com/pydata/xarray/issues/3751
key_is_scalar = not is_list_like(key) and not isinstance(key, slice)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sneaked in from a different issue / pr?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like it, will revert, thanks

if isinstance(key, (list, tuple)):
key = unpack_1tuple(key)

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)