Skip to content

Commit 082f47e

Browse files
TomAugspurgerMeeseeksDev[bot]
authored and
MeeseeksDev[bot]
committed
Backport PR pandas-dev#25556: BUG: Handle readonly arrays in period_array
1 parent f99f44a commit 082f47e

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

doc/source/whatsnew/v0.24.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Fixed Regressions
2929
- 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`)
3030
- Fixed regression in :class:`TimedeltaIndex` where ``np.sum(index)`` incorrectly returned a zero-dimensional object instead of a scalar (:issue:`25282`)
3131
- Fixed regression in ``IntervalDtype`` construction where passing an incorrect string with 'Interval' as a prefix could result in a ``RecursionError``. (:issue:`25338`)
32+
- Fixed regression in creating a period-dtype array from a read-only NumPy array of period objects. (:issue:`25403`)
3233
- 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`)
3334

3435
.. _whatsnew_0242.enhancements:

pandas/_libs/tslibs/period.pyx

+6-2
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,9 @@ cdef accessor _get_accessor_func(int code):
14371437

14381438
@cython.wraparound(False)
14391439
@cython.boundscheck(False)
1440-
def extract_ordinals(object[:] values, freq):
1440+
def extract_ordinals(ndarray[object] values, freq):
1441+
# TODO: Change type to const object[:] when Cython supports that.
1442+
14411443
cdef:
14421444
Py_ssize_t i, n = len(values)
14431445
int64_t[:] ordinals = np.empty(n, dtype=np.int64)
@@ -1471,7 +1473,9 @@ def extract_ordinals(object[:] values, freq):
14711473
return ordinals.base # .base to access underlying np.ndarray
14721474

14731475

1474-
def extract_freq(object[:] values):
1476+
def extract_freq(ndarray[object] values):
1477+
# TODO: Change type to const object[:] when Cython supports that.
1478+
14751479
cdef:
14761480
Py_ssize_t i, n = len(values)
14771481
object p

pandas/tests/arrays/test_period.py

+16
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ def test_period_array_ok(data, freq, expected):
4141
tm.assert_numpy_array_equal(result, expected)
4242

4343

44+
def test_period_array_readonly_object():
45+
# https://github.com/pandas-dev/pandas/issues/25403
46+
pa = period_array([pd.Period('2019-01-01')])
47+
arr = np.asarray(pa, dtype='object')
48+
arr.setflags(write=False)
49+
50+
result = period_array(arr)
51+
tm.assert_period_array_equal(result, pa)
52+
53+
result = pd.Series(arr)
54+
tm.assert_series_equal(result, pd.Series(pa))
55+
56+
result = pd.DataFrame({"A": arr})
57+
tm.assert_frame_equal(result, pd.DataFrame({"A": pa}))
58+
59+
4460
def test_from_datetime64_freq_changes():
4561
# https://github.com/pandas-dev/pandas/issues/23438
4662
arr = pd.date_range("2017", periods=3, freq="D")

0 commit comments

Comments
 (0)