File: algos_common_helper.pxi.in

package info (click to toggle)
pandas 2.2.3%2Bdfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 66,784 kB
  • sloc: python: 422,228; ansic: 9,190; sh: 270; xml: 102; makefile: 83
file content (73 lines) | stat: -rw-r--r-- 2,249 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
Template for each `dtype` helper function using 1-d template

WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
"""

# ----------------------------------------------------------------------
# ensure_dtype
# ----------------------------------------------------------------------


def ensure_platform_int(object arr):
    # GH3033, GH1392
    # platform int is the size of the int pointer, e.g. np.intp
    if util.is_array(arr):
        if (<ndarray>arr).descr.type_num == cnp.NPY_INTP:
            return arr
        else:
            # equiv: arr.astype(np.intp)
            return cnp.PyArray_Cast(<ndarray>arr, cnp.NPY_INTP)
    else:
        return np.array(arr, dtype=np.intp)


def ensure_object(object arr):
    if util.is_array(arr):
        if (<ndarray>arr).descr.type_num == NPY_OBJECT:
            return arr
        else:
            # equiv: arr.astype(object)
            return cnp.PyArray_Cast(<ndarray>arr, NPY_OBJECT)
    else:
        return np.array(arr, dtype=np.object_)

{{py:

# name, c_type, dtype
dtypes = [('float64', 'FLOAT64', 'float64'),
          # ('float32', 'FLOAT32', 'float32'),  # disabling bc unused
          ('int8', 'INT8', 'int8'),
          ('int16', 'INT16', 'int16'),
          ('int32', 'INT32', 'int32'),
          ('int64', 'INT64', 'int64'),
          ('uint64', 'UINT64', 'uint64'),
          # Disabling uint and complex dtypes because we do not use them
          #  (and compiling them increases wheel size) (except uint64)
          # ('uint8', 'UINT8', 'uint8'),
          # ('uint16', 'UINT16', 'uint16'),
          # ('uint32', 'UINT32', 'uint32'),
          # ('complex64', 'COMPLEX64', 'complex64'),
          # ('complex128', 'COMPLEX128', 'complex128')
]

def get_dispatch(dtypes):

    for name, c_type, dtype in dtypes:
        yield name, c_type, dtype
}}

{{for name, c_type, dtype in get_dispatch(dtypes)}}


def ensure_{{name}}(object arr):
    if util.is_array(arr):
        if (<ndarray>arr).descr.type_num == NPY_{{c_type}}:
            return arr
        else:
            # equiv: arr.astype(np.{{dtype}})
            return cnp.PyArray_Cast(<ndarray>arr, cnp.NPY_{{c_type}})
    else:
        return np.asarray(arr, dtype=np.{{dtype}})

{{endfor}}