diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 5094f6f07d534..13bd95004445d 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -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] @@ -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 @@ -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: @@ -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 @@ -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] @@ -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 diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index c96a65cdff525..f4d495de26600 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -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 @@ -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] @@ -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] @@ -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 diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index 3d04562cb73c3..c2637db23293e 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -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( cnp.PyArray_DATA(arr), @@ -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((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