Skip to content

Commit 1d7b075

Browse files
jbrockmendeljreback
authored andcommitted
standardize cimports of numpy as "cnp" (#19405)
1 parent dfc6054 commit 1d7b075

29 files changed

+119
-136
lines changed

pandas/_libs/algos.pyx

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,34 @@
11
# cython: profile=False
22

3-
cimport numpy as np
4-
import numpy as np
5-
63
cimport cython
74
from cython cimport Py_ssize_t
85

9-
np.import_array()
10-
11-
cdef float64_t FP_ERR = 1e-13
12-
13-
cimport util
14-
156
from libc.stdlib cimport malloc, free
167
from libc.string cimport memmove
8+
from libc.math cimport fabs, sqrt
179

10+
import numpy as np
11+
cimport numpy as cnp
1812
from numpy cimport (ndarray,
1913
NPY_INT64, NPY_UINT64, NPY_INT32, NPY_INT16, NPY_INT8,
2014
NPY_FLOAT32, NPY_FLOAT64,
2115
NPY_OBJECT,
2216
int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t,
2317
uint32_t, uint64_t, float32_t, float64_t,
2418
double_t)
19+
cnp.import_array()
2520

2621

27-
cdef double NaN = <double> np.NaN
28-
cdef double nan = NaN
29-
30-
from libc.math cimport fabs, sqrt
31-
32-
# this is our util.pxd
22+
cimport util
3323
from util cimport numeric, get_nat
3424

3525
import missing
3626

27+
cdef float64_t FP_ERR = 1e-13
28+
29+
cdef double NaN = <double> np.NaN
30+
cdef double nan = NaN
31+
3732
cdef int64_t iNaT = get_nat()
3833

3934
cdef:

pandas/_libs/algos_rank_helper.pxi.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def rank_1d_{{dtype}}(object in_arr, ties_method='average', ascending=True,
5050

5151
ndarray[float64_t] ranks
5252
ndarray[int64_t] argsorted
53-
ndarray[np.uint8_t, cast=True] sorted_mask
53+
ndarray[uint8_t, cast=True] sorted_mask
5454

5555
{{if dtype == 'uint64'}}
5656
{{ctype}} val

pandas/_libs/hashtable.pyx

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
# cython: profile=False
22

3-
from cpython cimport PyObject, Py_INCREF, PyList_Check, PyTuple_Check
3+
cimport cython
4+
5+
from cpython cimport (PyObject, Py_INCREF, PyList_Check, PyTuple_Check,
6+
PyMem_Malloc, PyMem_Realloc, PyMem_Free,
7+
PyString_Check, PyBytes_Check,
8+
PyUnicode_Check)
9+
10+
from libc.stdlib cimport malloc, free
11+
12+
import numpy as np
13+
cimport numpy as cnp
14+
from numpy cimport ndarray, uint8_t, uint32_t
15+
cnp.import_array()
16+
17+
cdef extern from "numpy/npy_math.h":
18+
double NAN "NPY_NAN"
19+
420

521
from khash cimport (
622
khiter_t,
@@ -23,29 +39,13 @@ from khash cimport (
2339
kh_put_pymap, kh_resize_pymap)
2440

2541

26-
from numpy cimport ndarray, uint8_t, uint32_t
27-
28-
from libc.stdlib cimport malloc, free
29-
from cpython cimport (PyMem_Malloc, PyMem_Realloc, PyMem_Free,
30-
PyString_Check, PyBytes_Check,
31-
PyUnicode_Check)
32-
3342
from util cimport _checknan
3443
cimport util
3544

36-
import numpy as np
37-
nan = np.nan
38-
39-
cdef extern from "numpy/npy_math.h":
40-
double NAN "NPY_NAN"
41-
42-
cimport cython
43-
cimport numpy as cnp
44-
4545
from missing cimport checknull
4646

47-
cnp.import_array()
48-
cnp.import_ufunc()
47+
48+
nan = np.nan
4949

5050
cdef int64_t iNaT = util.get_nat()
5151
_SIZE_HINT_LIMIT = (1 << 20) + 7

pandas/_libs/index.pyx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
# cython: profile=False
2+
from datetime import datetime, timedelta, date
23

3-
from numpy cimport (ndarray, float64_t, int32_t, int64_t, uint8_t, uint64_t,
4-
NPY_DATETIME, NPY_TIMEDELTA)
54
cimport cython
65

7-
cimport numpy as cnp
6+
from cpython cimport PyTuple_Check, PyList_Check
7+
from cpython.slice cimport PySlice_Check
88

9+
import numpy as np
10+
cimport numpy as cnp
11+
from numpy cimport (ndarray, float64_t, int32_t, int64_t, uint8_t, uint64_t,
12+
NPY_DATETIME, NPY_TIMEDELTA)
913
cnp.import_array()
10-
cnp.import_ufunc()
1114

12-
cimport util
1315

14-
import numpy as np
16+
cimport util
1517

1618
from tslibs.conversion cimport maybe_datetimelike_to_i8
1719

@@ -20,10 +22,6 @@ from hashtable cimport HashTable
2022
from pandas._libs import algos, hashtable as _hash
2123
from pandas._libs.tslibs import period as periodlib
2224
from pandas._libs.tslib import Timestamp, Timedelta
23-
from datetime import datetime, timedelta, date
24-
25-
from cpython cimport PyTuple_Check, PyList_Check
26-
from cpython.slice cimport PySlice_Check
2725

2826
cdef int64_t iNaT = util.get_nat()
2927

pandas/_libs/internals.pyx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ cdef extern from "Python.h":
1010
Py_ssize_t PY_SSIZE_T_MAX
1111

1212
import numpy as np
13-
cimport numpy as np
1413
from numpy cimport int64_t
1514

1615
cdef extern from "compat_helper.h":

pandas/_libs/interval.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cimport numpy as np
1+
cimport numpy as cnp
22
import numpy as np
33

44
cimport util

pandas/_libs/join.pyx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
# cython: profile=False
22

3-
cimport numpy as np
4-
import numpy as np
5-
63
cimport cython
74
from cython cimport Py_ssize_t
85

9-
np.import_array()
10-
6+
import numpy as np
7+
cimport numpy as cnp
118
from numpy cimport (ndarray,
129
int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t,
1310
uint32_t, uint64_t, float32_t, float64_t)
11+
cnp.import_array()
12+
1413

1514
cdef double NaN = <double> np.NaN
1615
cdef double nan = NaN

pandas/_libs/lib.pyx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@ cimport cython
55
from cython cimport Py_ssize_t
66

77
import numpy as np
8-
cimport numpy as np
8+
cimport numpy as cnp
99
from numpy cimport (ndarray, PyArray_NDIM, PyArray_GETITEM,
1010
PyArray_ITER_DATA, PyArray_ITER_NEXT, PyArray_IterNew,
1111
flatiter, NPY_OBJECT,
1212
int64_t,
1313
float32_t, float64_t,
1414
uint8_t, uint64_t,
1515
complex128_t)
16-
# initialize numpy
17-
np.import_array()
18-
np.import_ufunc()
16+
cnp.import_array()
1917

2018
from cpython cimport (Py_INCREF, PyTuple_SET_ITEM,
2119
PyList_Check, PyFloat_Check,
@@ -95,7 +93,7 @@ cpdef bint is_scalar(object val):
9593
9694
"""
9795

98-
return (np.PyArray_IsAnyScalar(val)
96+
return (cnp.PyArray_IsAnyScalar(val)
9997
# As of numpy-1.9, PyArray_IsAnyScalar misses bytearrays on Py3.
10098
or PyBytes_Check(val)
10199
# We differ from numpy (as of 1.10), which claims that None is
@@ -710,7 +708,7 @@ def clean_index_list(list obj):
710708

711709
for i in range(n):
712710
v = obj[i]
713-
if not (PyList_Check(v) or np.PyArray_Check(v) or hasattr(v, '_data')):
711+
if not (PyList_Check(v) or util.is_array(v) or hasattr(v, '_data')):
714712
all_arrays = 0
715713
break
716714

pandas/_libs/missing.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ cimport cython
77
from cython cimport Py_ssize_t
88

99
import numpy as np
10-
cimport numpy as np
10+
cimport numpy as cnp
1111
from numpy cimport ndarray, int64_t, uint8_t
12-
np.import_array()
12+
cnp.import_array()
1313

1414
cimport util
1515

pandas/_libs/reduction.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ from cpython cimport Py_INCREF
88
from libc.stdlib cimport malloc, free
99

1010
import numpy as np
11-
cimport numpy as np
11+
cimport numpy as cnp
1212
from numpy cimport (ndarray,
1313
int64_t,
1414
PyArray_SETITEM,
1515
PyArray_ITER_NEXT, PyArray_ITER_DATA, PyArray_IterNew,
1616
flatiter)
17-
np.import_array()
17+
cnp.import_array()
1818

1919
cimport util
2020
from lib import maybe_convert_objects

0 commit comments

Comments
 (0)