-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumpy_ndarray_as.py
More file actions
58 lines (48 loc) · 1.82 KB
/
numpy_ndarray_as.py
File metadata and controls
58 lines (48 loc) · 1.82 KB
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
"""Viewers of numpy ndarray
"""
from .utils import get_bitmap
def random(size, nulls=False):
"""Return random numpy.ndarray instance of 64 bit floats.
"""
import numpy as np
r = np.random.random(size)
if nulls:
idx = np.random.randint(0, r.size, size=max(1, r.size//4))
r[idx] = np.nan
return r
def pyarrow_array(arr, nan_to_null=False):
"""Return pyarrow.Array view of a numpy ndarray.
In floating arrays, all nan values are interpreted as nulls.
In complex arrays, if real or imaginary part of an array item
value is nan, the value is interpreted as null.
"""
import numpy as np
import pyarrow as pa
if nan_to_null and issubclass(arr.dtype.type,
(np.floating, np.complexfloating)):
isnan = np.isnan(arr)
if isnan.any():
pa_nul = pa.py_buffer(get_bitmap(isnan))
return pa.Array.from_buffers(pa.from_numpy_dtype(arr.dtype),
arr.size,
[pa_nul, pa.py_buffer(arr)])
return pa.Array.from_buffers(pa.from_numpy_dtype(arr.dtype),
arr.size,
[None, pa.py_buffer(arr)])
def pandas_series(arr, nan_to_null=False):
"""Return pandas.Series view of a numpy ndarray.
"""
import pandas as pd
return pd.Series(arr, copy=False)
def xnd_xnd(arr, nan_to_null=False):
"""Return xnd.xnd view of a numpy ndarray.
"""
import numpy as np
import xnd
xd = xnd.xnd.from_buffer(arr)
if nan_to_null and issubclass(arr.dtype.type,
(np.floating, np.complexfloating)):
isnan = np.isnan(arr)
if isnan.any():
raise NotImplementedError('xnd view of numpy ndarray with nans')
return xd