Skip to content

BUG: Handle readonly arrays in period_array #25556

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

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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/v0.24.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Fixed Regressions
- Fixed regression in subtraction between :class:`Series` objects with ``datetime64[ns]`` dtype incorrectly raising ``OverflowError`` when the `Series` on the right contains null values (:issue:`25317`)
- Fixed regression in :class:`TimedeltaIndex` where `np.sum(index)` incorrectly returned a zero-dimensional object instead of a scalar (:issue:`25282`)
- Fixed regression in ``IntervalDtype`` construction where passing an incorrect string with 'Interval' as a prefix could result in a ``RecursionError``. (:issue:`25338`)
- Fixed regression in creating a period-dtype array from a read-only NumPy array of period objects. (:issue:`25403`)

- Fixed regression in :class:`Categorical`, where constructing it from a categorical ``Series`` and an explicit ``categories=`` that differed from that in the ``Series`` created an invalid object which could trigger segfaults. (:issue:`25318`)

Expand Down
8 changes: 6 additions & 2 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,9 @@ cdef accessor _get_accessor_func(int code):

@cython.wraparound(False)
@cython.boundscheck(False)
def extract_ordinals(object[:] values, freq):
def extract_ordinals(ndarray[object] values, freq):
# TODO: Change type to const object[:] when Cython supports that.

cdef:
Py_ssize_t i, n = len(values)
int64_t[:] ordinals = np.empty(n, dtype=np.int64)
Expand Down Expand Up @@ -1472,7 +1474,9 @@ def extract_ordinals(object[:] values, freq):
return ordinals.base # .base to access underlying np.ndarray


def extract_freq(object[:] values):
def extract_freq(ndarray[object] values):
# TODO: Change type to const object[:] when Cython supports that.

cdef:
Py_ssize_t i, n = len(values)
object p
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/arrays/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ def test_period_array_ok(data, freq, expected):
tm.assert_numpy_array_equal(result, expected)


def test_period_array_readonly_object():
# https://github.com/pandas-dev/pandas/issues/25403
pa = period_array([pd.Period('2019-01-01')])
arr = np.asarray(pa, dtype='object')
arr.setflags(write=False)

result = period_array(arr)
tm.assert_period_array_equal(result, pa)

result = pd.Series(arr)
tm.assert_series_equal(result, pd.Series(pa))

result = pd.DataFrame({"A": arr})
tm.assert_frame_equal(result, pd.DataFrame({"A": pa}))


def test_from_datetime64_freq_changes():
# https://github.com/pandas-dev/pandas/issues/23438
arr = pd.date_range("2017", periods=3, freq="D")
Expand Down