Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
62 changes: 53 additions & 9 deletions pandas/src/generate_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,7 @@ def take_1d_%(name)s_%(dest)s(ndarray[%(c_type_in)s] values,

"""

take_2d_axis0_template = """@cython.wraparound(False)
@cython.boundscheck(False)
def take_2d_axis0_%(name)s_%(dest)s(%(c_type_in)s[:, :] values,
ndarray[int64_t] indexer,
%(c_type_out)s[:, :] out,
fill_value=np.nan):
inner_take_2d_axis0_template = """\
cdef:
Py_ssize_t i, j, k, n, idx
%(c_type_out)s fv
Expand Down Expand Up @@ -140,12 +135,34 @@ def take_2d_axis0_%(name)s_%(dest)s(%(c_type_in)s[:, :] values,

"""

take_2d_axis1_template = """@cython.wraparound(False)
take_2d_axis0_template = """\
@cython.wraparound(False)
@cython.boundscheck(False)
def take_2d_axis1_%(name)s_%(dest)s(%(c_type_in)s[:, :] values,
cdef inline take_2d_axis0_%(name)s_%(dest)s_memview(%(c_type_in)s[:, :] values,
ndarray[int64_t] indexer,
%(c_type_out)s[:, :] out,
fill_value=np.nan):
""" + inner_take_2d_axis0_template + """

@cython.wraparound(False)
@cython.boundscheck(False)
def take_2d_axis0_%(name)s_%(dest)s(ndarray[%(c_type_in)s, ndim=2] values,
ndarray[int64_t] indexer,
%(c_type_out)s[:, :] out,
fill_value=np.nan):
if values.flags.writeable:
# We can call the memoryview version of the code
take_2d_axis0_%(name)s_%(dest)s_memview(values, indexer, out,
fill_value=fill_value)
return

# We cannot use the memoryview version on readonly-buffers due to
# a limitation of Cython's typed memoryviews. Instead we can use
# the slightly slower Cython ndarray type directly.
""" + inner_take_2d_axis0_template


inner_take_2d_axis1_template = """\
cdef:
Py_ssize_t i, j, k, n, idx
%(c_type_out)s fv
Expand All @@ -165,9 +182,36 @@ def take_2d_axis1_%(name)s_%(dest)s(%(c_type_in)s[:, :] values,
out[i, j] = fv
else:
out[i, j] = %(preval)svalues[i, idx]%(postval)s

"""

take_2d_axis1_template = """\
@cython.wraparound(False)
@cython.boundscheck(False)
cdef inline take_2d_axis1_%(name)s_%(dest)s_memview(%(c_type_in)s[:, :] values,
ndarray[int64_t] indexer,
%(c_type_out)s[:, :] out,
fill_value=np.nan):
""" + inner_take_2d_axis1_template + """

@cython.wraparound(False)
@cython.boundscheck(False)
def take_2d_axis1_%(name)s_%(dest)s(ndarray[%(c_type_in)s, ndim=2] values,
ndarray[int64_t] indexer,
%(c_type_out)s[:, :] out,
fill_value=np.nan):

if values.flags.writeable:
# We can call the memoryview version of the code
take_2d_axis1_%(name)s_%(dest)s_memview(values, indexer, out,
fill_value=fill_value)
return

# We cannot use the memoryview version on readonly-buffers due to
# a limitation of Cython's typed memoryviews. Instead we can use
# the slightly slower Cython ndarray type directly.
""" + inner_take_2d_axis1_template


take_2d_multi_template = """@cython.wraparound(False)
@cython.boundscheck(False)
def take_2d_multi_%(name)s_%(dest)s(ndarray[%(c_type_in)s, ndim=2] values,
Expand Down
Loading