Skip to content

PERF: use C version of np.empty #46878

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 1 commit into from
Apr 27, 2022
Merged
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
28 changes: 14 additions & 14 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ def get_level_sorter(
"""
cdef:
Py_ssize_t i, l, r
ndarray[intp_t, ndim=1] out = np.empty(len(codes), dtype=np.intp)
ndarray[intp_t, ndim=1] out = cnp.PyArray_EMPTY(1, codes.shape, cnp.NPY_INTP, 0)

for i in range(len(starts) - 1):
l, r = starts[i], starts[i + 1]
Expand Down Expand Up @@ -2255,11 +2255,11 @@ def maybe_convert_numeric(
int status, maybe_int
Py_ssize_t i, n = values.size
Seen seen = Seen(coerce_numeric)
ndarray[float64_t, ndim=1] floats = np.empty(n, dtype='f8')
ndarray[complex128_t, ndim=1] complexes = np.empty(n, dtype='c16')
ndarray[int64_t, ndim=1] ints = np.empty(n, dtype='i8')
ndarray[uint64_t, ndim=1] uints = np.empty(n, dtype='u8')
ndarray[uint8_t, ndim=1] bools = np.empty(n, dtype='u1')
ndarray[float64_t, ndim=1] floats = cnp.PyArray_EMPTY(1, values.shape, cnp.NPY_FLOAT64, 0)
ndarray[complex128_t, ndim=1] complexes = cnp.PyArray_EMPTY(1, values.shape, cnp.NPY_COMPLEX128, 0)
ndarray[int64_t, ndim=1] ints = cnp.PyArray_EMPTY(1, values.shape, cnp.NPY_INT64, 0)
ndarray[uint64_t, ndim=1] uints = cnp.PyArray_EMPTY(1, values.shape, cnp.NPY_UINT64, 0)
ndarray[uint8_t, ndim=1] bools = cnp.PyArray_EMPTY(1, values.shape, cnp.NPY_UINT8, 0)
ndarray[uint8_t, ndim=1] mask = np.zeros(n, dtype="u1")
float64_t fval
bint allow_null_in_int = convert_to_masked_nullable
Expand Down Expand Up @@ -2479,11 +2479,11 @@ def maybe_convert_objects(ndarray[object] objects,

n = len(objects)

floats = np.empty(n, dtype='f8')
complexes = np.empty(n, dtype='c16')
ints = np.empty(n, dtype='i8')
uints = np.empty(n, dtype='u8')
bools = np.empty(n, dtype=np.uint8)
floats = cnp.PyArray_EMPTY(1, objects.shape, cnp.NPY_FLOAT64, 0)
complexes = cnp.PyArray_EMPTY(1, objects.shape, cnp.NPY_COMPLEX128, 0)
ints = cnp.PyArray_EMPTY(1, objects.shape, cnp.NPY_INT64, 0)
uints = cnp.PyArray_EMPTY(1, objects.shape, cnp.NPY_UINT64, 0)
bools = cnp.PyArray_EMPTY(1, objects.shape, cnp.NPY_UINT8, 0)
mask = np.full(n, False)

if convert_datetime:
Expand Down Expand Up @@ -2785,7 +2785,7 @@ cdef _infer_all_nats(dtype, ndarray datetimes, ndarray timedeltas):
else:
# ExtensionDtype
cls = dtype.construct_array_type()
i8vals = np.empty(len(datetimes), dtype="i8")
i8vals = cnp.PyArray_EMPTY(1, datetimes.shape, cnp.NPY_INT64, 0)
i8vals.fill(NPY_NAT)
result = cls(i8vals, dtype=dtype)
return result
Expand Down Expand Up @@ -2888,7 +2888,7 @@ def map_infer(
object val

n = len(arr)
result = np.empty(n, dtype=object)
result = cnp.PyArray_EMPTY(1, arr.shape, cnp.NPY_OBJECT, 0)
for i in range(n):
if ignore_na and checknull(arr[i]):
result[i] = arr[i]
Expand Down Expand Up @@ -3083,7 +3083,7 @@ cpdef ndarray eq_NA_compat(ndarray[object] arr, object key):
key is assumed to have `not isna(key)`
"""
cdef:
ndarray[uint8_t, cast=True] result = np.empty(len(arr), dtype=bool)
ndarray[uint8_t, cast=True] result = cnp.PyArray_EMPTY(arr.ndim, arr.shape, cnp.NPY_BOOL, 0)
Py_ssize_t i
object item

Expand Down
8 changes: 4 additions & 4 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def format_array_from_datetime(
ndarray[int64_t] consider_values
bint show_ms = False, show_us = False, show_ns = False
bint basic_format = False
ndarray[object] result = np.empty(N, dtype=object)
ndarray[object] result = cnp.PyArray_EMPTY(values.ndim, values.shape, cnp.NPY_OBJECT, 0)
object ts, res
npy_datetimestruct dts

Expand Down Expand Up @@ -349,7 +349,7 @@ def array_with_unit_to_datetime(
# and are in ignore mode
# redo as object

oresult = np.empty(n, dtype=object)
oresult = cnp.PyArray_EMPTY(values.ndim, values.shape, cnp.NPY_OBJECT, 0)
for i in range(n):
val = values[i]

Expand Down Expand Up @@ -668,7 +668,7 @@ cdef ndarray[object] ignore_errors_out_of_bounds_fallback(ndarray[object] values
Py_ssize_t i, n = len(values)
object val

oresult = np.empty(n, dtype=object)
oresult = cnp.PyArray_EMPTY(values.ndim, values.shape, cnp.NPY_OBJECT, 0)

for i in range(n):
val = values[i]
Expand Down Expand Up @@ -730,7 +730,7 @@ cdef _array_to_datetime_object(

assert is_raise or is_ignore or is_coerce

oresult = np.empty(n, dtype=object)
oresult = cnp.PyArray_EMPTY(values.ndim, values.shape, cnp.NPY_OBJECT, 0)

# We return an object array and only attempt to parse:
# 1) NaT or NaT-like values
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ def period_asfreq_arr(ndarray[int64_t] arr, int freq1, int freq2, bint end):
cdef:
Py_ssize_t n = len(arr)
Py_ssize_t increment = arr.strides[0] // 8
ndarray[int64_t] result = np.empty(n, dtype=np.int64)
ndarray[int64_t] result = cnp.PyArray_EMPTY(arr.ndim, arr.shape, cnp.NPY_INT64, 0)

_period_asfreq(
<int64_t*>cnp.PyArray_DATA(arr),
Expand Down Expand Up @@ -1440,7 +1440,7 @@ def extract_ordinals(ndarray values, freq) -> np.ndarray:
cdef:
Py_ssize_t i, n = values.size
int64_t ordinal
ndarray ordinals = np.empty((<object>values).shape, dtype=np.int64)
ndarray ordinals = cnp.PyArray_EMPTY(values.ndim, values.shape, cnp.NPY_INT64, 0)
cnp.broadcast mi = cnp.PyArray_MultiIterNew2(ordinals, values)
object p

Expand Down