BUG: Handle readonly arrays in period_array by TomAugspurger · Pull Request #25556 · pandas-dev/pandas (original) (raw)
With
diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index a5a50ea59..a10f8bd09 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -1438,7 +1438,7 @@ cdef accessor _get_accessor_func(int code):
@cython.wraparound(False) @cython.boundscheck(False) -def extract_ordinals(object[:] values, freq): +def extract_ordinals(ndarray[:] values, freq): cdef: Py_ssize_t i, n = len(values) int64_t[:] ordinals = np.empty(n, dtype=np.int64) @@ -1472,7 +1472,7 @@ def extract_ordinals(object[:] values, freq): return ordinals.base # .base to access underlying np.ndarray
-def extract_freq(object[:] values): +def extract_freq(ndarray[:] values): cdef: Py_ssize_t i, n = len(values) object p
I see
In [6]: pa = pd.PeriodIndex([pd.Period('2019-01-01')]).to_numpy()
In [8]: pa.setflags(write=False)
In [9]: pd.DataFrame(dict(date=pa, x=[1])) ## -- End pasted text --
ValueError Traceback (most recent call last) in 8 pa.setflags(write=False) 9 ---> 10 pd.DataFrame(dict(date=pa, x=[1]))
~/sandbox/pandas/pandas/core/frame.py in init(self, data, index, columns, dtype, copy) 390 dtype=dtype, copy=copy) 391 elif isinstance(data, dict): --> 392 mgr = init_dict(data, index, columns, dtype=dtype) 393 elif isinstance(data, ma.MaskedArray): 394 import numpy.ma.mrecords as mrecords
~/sandbox/pandas/pandas/core/internals/construction.py in init_dict(data, index, columns, dtype) 204 arrays = [data[k] if not is_datetime64tz_dtype(data[k]) else 205 data[k].copy(deep=True) for k in keys] --> 206 return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype) 207 208
~/sandbox/pandas/pandas/core/internals/construction.py in arrays_to_mgr(arrays, arr_names, index, columns, dtype) 54 55 # don't force copy because getting jammed in an ndarray anyway ---> 56 arrays = _homogenize(arrays, index, dtype) 57 58 # from BlockManager perspective
~/sandbox/pandas/pandas/core/internals/construction.py in _homogenize(data, index, dtype) 269 val = lib.fast_multiget(val, oindex.values, default=np.nan) 270 val = sanitize_array(val, index, dtype=dtype, copy=False, --> 271 raise_cast_failure=False) 272 273 homogenized.append(val)
~/sandbox/pandas/pandas/core/internals/construction.py in sanitize_array(data, index, dtype, copy, raise_cast_failure) 669 if inferred == 'period': 670 try: --> 671 subarr = period_array(subarr) 672 except IncompatibleFrequency: 673 pass
~/sandbox/pandas/pandas/core/arrays/period.py in period_array(data, freq, copy) 786 data = ensure_object(data) 787 --> 788 return PeriodArray._from_sequence(data, dtype=dtype) 789 790
~/sandbox/pandas/pandas/core/arrays/period.py in _from_sequence(cls, scalars, dtype, copy) 197 periods = periods.copy() 198 --> 199 freq = freq or libperiod.extract_freq(periods) 200 ordinals = libperiod.extract_ordinals(periods, freq) 201 return cls(ordinals, freq=freq)
~/sandbox/pandas/pandas/_libs/tslibs/period.pyx in pandas._libs.tslibs.period.extract_freq() 1473 1474 -> 1475 def extract_freq(ndarray[:] values): 1476 cdef: 1477 Py_ssize_t i, n = len(values)
~/sandbox/pandas/pandas/_libs/tslibs/period.cpython-37m-darwin.so in View.MemoryView.memoryview_cwrapper()
~/sandbox/pandas/pandas/_libs/tslibs/period.cpython-37m-darwin.so in View.MemoryView.memoryview.cinit()
ValueError: buffer source array is read-only