Skip to content

standardize cimports of numpy as "cnp" #19405

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 3 commits into from
Jan 27, 2018
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
25 changes: 10 additions & 15 deletions pandas/_libs/algos.pyx
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
# cython: profile=False

cimport numpy as np
import numpy as np

cimport cython
from cython cimport Py_ssize_t

np.import_array()

cdef float64_t FP_ERR = 1e-13

cimport util

from libc.stdlib cimport malloc, free
from libc.string cimport memmove
from libc.math cimport fabs, sqrt

import numpy as np
cimport numpy as cnp
from numpy cimport (ndarray,
NPY_INT64, NPY_UINT64, NPY_INT32, NPY_INT16, NPY_INT8,
NPY_FLOAT32, NPY_FLOAT64,
NPY_OBJECT,
int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t,
uint32_t, uint64_t, float32_t, float64_t,
double_t)
cnp.import_array()


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

from libc.math cimport fabs, sqrt

# this is our util.pxd
cimport util
from util cimport numeric, get_nat

import missing

cdef float64_t FP_ERR = 1e-13

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

cdef int64_t iNaT = get_nat()

cdef:
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/algos_rank_helper.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def rank_1d_{{dtype}}(object in_arr, ties_method='average', ascending=True,

ndarray[float64_t] ranks
ndarray[int64_t] argsorted
ndarray[np.uint8_t, cast=True] sorted_mask
ndarray[uint8_t, cast=True] sorted_mask

{{if dtype == 'uint64'}}
{{ctype}} val
Expand Down
38 changes: 19 additions & 19 deletions pandas/_libs/hashtable.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
# cython: profile=False

from cpython cimport PyObject, Py_INCREF, PyList_Check, PyTuple_Check
cimport cython

from cpython cimport (PyObject, Py_INCREF, PyList_Check, PyTuple_Check,
PyMem_Malloc, PyMem_Realloc, PyMem_Free,
PyString_Check, PyBytes_Check,
PyUnicode_Check)

from libc.stdlib cimport malloc, free

import numpy as np
cimport numpy as cnp
from numpy cimport ndarray, uint8_t, uint32_t
cnp.import_array()

cdef extern from "numpy/npy_math.h":
double NAN "NPY_NAN"


from khash cimport (
khiter_t,
Expand All @@ -23,29 +39,13 @@ from khash cimport (
kh_put_pymap, kh_resize_pymap)


from numpy cimport ndarray, uint8_t, uint32_t

from libc.stdlib cimport malloc, free
from cpython cimport (PyMem_Malloc, PyMem_Realloc, PyMem_Free,
PyString_Check, PyBytes_Check,
PyUnicode_Check)

from util cimport _checknan
cimport util

import numpy as np
nan = np.nan

cdef extern from "numpy/npy_math.h":
double NAN "NPY_NAN"

cimport cython
cimport numpy as cnp

from missing cimport checknull

cnp.import_array()
cnp.import_ufunc()

nan = np.nan

cdef int64_t iNaT = util.get_nat()
_SIZE_HINT_LIMIT = (1 << 20) + 7
Expand Down
18 changes: 8 additions & 10 deletions pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# cython: profile=False
from datetime import datetime, timedelta, date

from numpy cimport (ndarray, float64_t, int32_t, int64_t, uint8_t, uint64_t,
NPY_DATETIME, NPY_TIMEDELTA)
cimport cython

cimport numpy as cnp
from cpython cimport PyTuple_Check, PyList_Check
from cpython.slice cimport PySlice_Check

import numpy as np
cimport numpy as cnp
from numpy cimport (ndarray, float64_t, int32_t, int64_t, uint8_t, uint64_t,
NPY_DATETIME, NPY_TIMEDELTA)
cnp.import_array()
cnp.import_ufunc()

cimport util

import numpy as np
cimport util

from tslibs.conversion cimport maybe_datetimelike_to_i8

Expand All @@ -20,10 +22,6 @@ from hashtable cimport HashTable
from pandas._libs import algos, hashtable as _hash
from pandas._libs.tslibs import period as periodlib
from pandas._libs.tslib import Timestamp, Timedelta
from datetime import datetime, timedelta, date

from cpython cimport PyTuple_Check, PyList_Check
from cpython.slice cimport PySlice_Check

cdef int64_t iNaT = util.get_nat()

Expand Down
1 change: 0 additions & 1 deletion pandas/_libs/internals.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ cdef extern from "Python.h":
Py_ssize_t PY_SSIZE_T_MAX

import numpy as np
cimport numpy as np
from numpy cimport int64_t

cdef extern from "compat_helper.h":
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/interval.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cimport numpy as np
cimport numpy as cnp
import numpy as np

cimport util
Expand Down
9 changes: 4 additions & 5 deletions pandas/_libs/join.pyx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# cython: profile=False

cimport numpy as np
import numpy as np

cimport cython
from cython cimport Py_ssize_t

np.import_array()

import numpy as np
cimport numpy as cnp
from numpy cimport (ndarray,
int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t,
uint32_t, uint64_t, float32_t, float64_t)
cnp.import_array()


cdef double NaN = <double> np.NaN
cdef double nan = NaN
Expand Down
10 changes: 4 additions & 6 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ cimport cython
from cython cimport Py_ssize_t

import numpy as np
cimport numpy as np
cimport numpy as cnp
from numpy cimport (ndarray, PyArray_NDIM, PyArray_GETITEM,
PyArray_ITER_DATA, PyArray_ITER_NEXT, PyArray_IterNew,
flatiter, NPY_OBJECT,
int64_t,
float32_t, float64_t,
uint8_t, uint64_t,
complex128_t)
# initialize numpy
np.import_array()
np.import_ufunc()
cnp.import_array()

from cpython cimport (Py_INCREF, PyTuple_SET_ITEM,
PyList_Check, PyFloat_Check,
Expand Down Expand Up @@ -95,7 +93,7 @@ cpdef bint is_scalar(object val):

"""

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

for i in range(n):
v = obj[i]
if not (PyList_Check(v) or np.PyArray_Check(v) or hasattr(v, '_data')):
if not (PyList_Check(v) or util.is_array(v) or hasattr(v, '_data')):
all_arrays = 0
break

Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/missing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ cimport cython
from cython cimport Py_ssize_t

import numpy as np
cimport numpy as np
cimport numpy as cnp
from numpy cimport ndarray, int64_t, uint8_t
np.import_array()
cnp.import_array()

cimport util

Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ from cpython cimport Py_INCREF
from libc.stdlib cimport malloc, free

import numpy as np
cimport numpy as np
cimport numpy as cnp
from numpy cimport (ndarray,
int64_t,
PyArray_SETITEM,
PyArray_ITER_NEXT, PyArray_ITER_DATA, PyArray_IterNew,
flatiter)
np.import_array()
cnp.import_array()

cimport util
from lib import maybe_convert_objects
Expand Down
9 changes: 4 additions & 5 deletions pandas/_libs/reshape.pyx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# cython: profile=False

cimport numpy as np
import numpy as np

cimport cython
from cython cimport Py_ssize_t

np.import_array()

import numpy as np
cimport numpy as cnp
from numpy cimport (ndarray,
int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t,
uint32_t, uint64_t, float32_t, float64_t)
cnp.import_array()


cdef double NaN = <double> np.NaN
cdef double nan = NaN
Expand Down
12 changes: 6 additions & 6 deletions pandas/_libs/skiplist.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@

from libc.math cimport log

import numpy as np
cimport numpy as cnp
from numpy cimport double_t
cnp.import_array()


# MSVC does not have log2!

cdef double Log2(double x):
return log(x) / log(2.)

cimport numpy as np
import numpy as np
from numpy cimport double_t

from random import random

# initialize numpy
np.import_array()

# TODO: optimize this, make less messy

cdef class Node:
Expand Down
15 changes: 8 additions & 7 deletions pandas/_libs/sparse.pyx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from numpy cimport (ndarray, uint8_t, int64_t, int32_t, int16_t, int8_t,
float64_t, float32_t)
cimport numpy as np
# -*- coding: utf-8 -*-
import operator
import sys

cimport cython

import numpy as np
import operator
import sys
cimport numpy as cnp
from numpy cimport (ndarray, uint8_t, int64_t, int32_t, int16_t, int8_t,
float64_t, float32_t)
cnp.import_array()


from distutils.version import LooseVersion

Expand All @@ -15,8 +18,6 @@ _np_version = np.version.short_version
_np_version_under1p10 = LooseVersion(_np_version) < LooseVersion('1.10')
_np_version_under1p11 = LooseVersion(_np_version) < LooseVersion('1.11')

np.import_array()
np.import_ufunc()

# -----------------------------------------------------------------------------
# Preamble stuff
Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/src/inference.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -609,13 +609,13 @@ cdef class Validator:

cdef:
Py_ssize_t n
np.dtype dtype
cnp.dtype dtype
bint skipna

def __cinit__(
self,
Py_ssize_t n,
np.dtype dtype=np.dtype(np.object_),
cnp.dtype dtype=np.dtype(np.object_),
bint skipna=False
):
self.n = n
Expand Down Expand Up @@ -823,7 +823,7 @@ cdef class TemporalValidator(Validator):
def __cinit__(
self,
Py_ssize_t n,
np.dtype dtype=np.dtype(np.object_),
cnp.dtype dtype=np.dtype(np.object_),
bint skipna=False
):
self.n = n
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-
# cython: profile=False

cimport numpy as np
cimport numpy as cnp
from numpy cimport int64_t, ndarray, float64_t
import numpy as np
np.import_array()
cnp.import_array()


from cpython cimport PyFloat_Check
Expand Down
5 changes: 2 additions & 3 deletions pandas/_libs/tslibs/ccalendar.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ Cython implementations of functions resembling the stdlib calendar module
cimport cython
from cython cimport Py_ssize_t

import numpy as np
cimport numpy as np
cimport numpy as cnp
from numpy cimport int64_t, int32_t
np.import_array()
cnp.import_array()


# ----------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ cimport cython
from cython cimport Py_ssize_t

import numpy as np
cimport numpy as np
cimport numpy as cnp
from numpy cimport int64_t, int32_t, ndarray
np.import_array()
cnp.import_array()

import pytz

Expand Down
Loading