Skip to content

Commit 476c37a

Browse files
author
Joris Vankerschaver
committed
MAINT: Make take_1d accept readonly buffers.
This is a straightforward port of GH#10070 to 1d arrays.
1 parent 449ab6b commit 476c37a

File tree

4 files changed

+675
-39
lines changed

4 files changed

+675
-39
lines changed

doc/source/whatsnew/v0.18.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,5 @@ Bug Fixes
508508

509509
- Removed ``millisecond`` property of ``DatetimeIndex``. This would always raise
510510
a ``ValueError`` (:issue:`12019`).
511+
512+
- Bug in Series constructor with read-only data (:issue:`11502`)

pandas/src/generate_code.py

+28-7
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,7 @@
8888
"""
8989

9090

91-
take_1d_template = """
92-
@cython.wraparound(False)
93-
@cython.boundscheck(False)
94-
def take_1d_%(name)s_%(dest)s(%(c_type_in)s[:] values,
95-
int64_t[:] indexer,
96-
%(c_type_out)s[:] out,
97-
fill_value=np.nan):
91+
inner_take_1d_template = """\
9892
cdef:
9993
Py_ssize_t i, n, idx
10094
%(c_type_out)s fv
@@ -112,6 +106,33 @@ def take_1d_%(name)s_%(dest)s(%(c_type_in)s[:] values,
112106
%(tab)s out[i] = %(preval)svalues[idx]%(postval)s
113107
"""
114108

109+
take_1d_template = """\
110+
@cython.wraparound(False)
111+
@cython.boundscheck(False)
112+
cdef inline take_1d_%(name)s_%(dest)s_memview(%(c_type_in)s[:] values,
113+
int64_t[:] indexer,
114+
%(c_type_out)s[:] out,
115+
fill_value=np.nan):
116+
""" + inner_take_1d_template + """
117+
118+
@cython.wraparound(False)
119+
@cython.boundscheck(False)
120+
def take_1d_%(name)s_%(dest)s(ndarray[%(c_type_in)s, ndim=1] values,
121+
int64_t[:] indexer,
122+
%(c_type_out)s[:] out,
123+
fill_value=np.nan):
124+
125+
if values.flags.writeable:
126+
# We can call the memoryview version of the code
127+
take_1d_%(name)s_%(dest)s_memview(values, indexer, out,
128+
fill_value=fill_value)
129+
return
130+
131+
# We cannot use the memoryview version on readonly-buffers due to
132+
# a limitation of Cython's typed memoryviews. Instead we can use
133+
# the slightly slower Cython ndarray type directly.
134+
""" + inner_take_1d_template
135+
115136
inner_take_2d_axis0_template = """\
116137
cdef:
117138
Py_ssize_t i, j, k, n, idx

0 commit comments

Comments
 (0)