Skip to content

Commit 4fcc8d5

Browse files
authored
PERF: use C version of np.empty (#46878)
1 parent 619ce2d commit 4fcc8d5

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

pandas/_libs/lib.pyx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ def get_level_sorter(
873873
"""
874874
cdef:
875875
Py_ssize_t i, l, r
876-
ndarray[intp_t, ndim=1] out = np.empty(len(codes), dtype=np.intp)
876+
ndarray[intp_t, ndim=1] out = cnp.PyArray_EMPTY(1, codes.shape, cnp.NPY_INTP, 0)
877877

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

24802480
n = len(objects)
24812481

2482-
floats = np.empty(n, dtype='f8')
2483-
complexes = np.empty(n, dtype='c16')
2484-
ints = np.empty(n, dtype='i8')
2485-
uints = np.empty(n, dtype='u8')
2486-
bools = np.empty(n, dtype=np.uint8)
2482+
floats = cnp.PyArray_EMPTY(1, objects.shape, cnp.NPY_FLOAT64, 0)
2483+
complexes = cnp.PyArray_EMPTY(1, objects.shape, cnp.NPY_COMPLEX128, 0)
2484+
ints = cnp.PyArray_EMPTY(1, objects.shape, cnp.NPY_INT64, 0)
2485+
uints = cnp.PyArray_EMPTY(1, objects.shape, cnp.NPY_UINT64, 0)
2486+
bools = cnp.PyArray_EMPTY(1, objects.shape, cnp.NPY_UINT8, 0)
24872487
mask = np.full(n, False)
24882488

24892489
if convert_datetime:
@@ -2785,7 +2785,7 @@ cdef _infer_all_nats(dtype, ndarray datetimes, ndarray timedeltas):
27852785
else:
27862786
# ExtensionDtype
27872787
cls = dtype.construct_array_type()
2788-
i8vals = np.empty(len(datetimes), dtype="i8")
2788+
i8vals = cnp.PyArray_EMPTY(1, datetimes.shape, cnp.NPY_INT64, 0)
27892789
i8vals.fill(NPY_NAT)
27902790
result = cls(i8vals, dtype=dtype)
27912791
return result
@@ -2888,7 +2888,7 @@ def map_infer(
28882888
object val
28892889

28902890
n = len(arr)
2891-
result = np.empty(n, dtype=object)
2891+
result = cnp.PyArray_EMPTY(1, arr.shape, cnp.NPY_OBJECT, 0)
28922892
for i in range(n):
28932893
if ignore_na and checknull(arr[i]):
28942894
result[i] = arr[i]
@@ -3083,7 +3083,7 @@ cpdef ndarray eq_NA_compat(ndarray[object] arr, object key):
30833083
key is assumed to have `not isna(key)`
30843084
"""
30853085
cdef:
3086-
ndarray[uint8_t, cast=True] result = np.empty(len(arr), dtype=bool)
3086+
ndarray[uint8_t, cast=True] result = cnp.PyArray_EMPTY(arr.ndim, arr.shape, cnp.NPY_BOOL, 0)
30873087
Py_ssize_t i
30883088
object item
30893089

pandas/_libs/tslib.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def format_array_from_datetime(
125125
ndarray[int64_t] consider_values
126126
bint show_ms = False, show_us = False, show_ns = False
127127
bint basic_format = False
128-
ndarray[object] result = np.empty(N, dtype=object)
128+
ndarray[object] result = cnp.PyArray_EMPTY(values.ndim, values.shape, cnp.NPY_OBJECT, 0)
129129
object ts, res
130130
npy_datetimestruct dts
131131

@@ -351,7 +351,7 @@ def array_with_unit_to_datetime(
351351
# and are in ignore mode
352352
# redo as object
353353

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

@@ -671,7 +671,7 @@ cdef ndarray[object] ignore_errors_out_of_bounds_fallback(ndarray[object] values
671671
Py_ssize_t i, n = len(values)
672672
object val
673673

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

676676
for i in range(n):
677677
val = values[i]
@@ -733,7 +733,7 @@ cdef _array_to_datetime_object(
733733

734734
assert is_raise or is_ignore or is_coerce
735735

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

738738
# We return an object array and only attempt to parse:
739739
# 1) NaT or NaT-like values

pandas/_libs/tslibs/period.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ def period_asfreq_arr(ndarray[int64_t] arr, int freq1, int freq2, bint end):
10571057
cdef:
10581058
Py_ssize_t n = len(arr)
10591059
Py_ssize_t increment = arr.strides[0] // 8
1060-
ndarray[int64_t] result = np.empty(n, dtype=np.int64)
1060+
ndarray[int64_t] result = cnp.PyArray_EMPTY(arr.ndim, arr.shape, cnp.NPY_INT64, 0)
10611061

10621062
_period_asfreq(
10631063
<int64_t*>cnp.PyArray_DATA(arr),
@@ -1440,7 +1440,7 @@ def extract_ordinals(ndarray values, freq) -> np.ndarray:
14401440
cdef:
14411441
Py_ssize_t i, n = values.size
14421442
int64_t ordinal
1443-
ndarray ordinals = np.empty((<object>values).shape, dtype=np.int64)
1443+
ndarray ordinals = cnp.PyArray_EMPTY(values.ndim, values.shape, cnp.NPY_INT64, 0)
14441444
cnp.broadcast mi = cnp.PyArray_MultiIterNew2(ordinals, values)
14451445
object p
14461446

0 commit comments

Comments
 (0)