-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy path_arrays.py
More file actions
53 lines (43 loc) · 1.37 KB
/
_arrays.py
File metadata and controls
53 lines (43 loc) · 1.37 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
"""
Utility functions for working with and reasoning about arrays.
"""
from typing import Any
def is_arraylike(arr: Any) -> bool:
"""
Return True iff the object is arraylike: possessing
.shape, .dtype, .__array__, and .ndim attributes.
:param arr: The object to check for arraylike properties
:return: True iff the object is arraylike
"""
return (
hasattr(arr, "shape")
and hasattr(arr, "dtype")
and hasattr(arr, "__array__")
and hasattr(arr, "ndim")
)
def is_memoryarraylike(arr: Any) -> bool:
"""
Return True iff the object is memoryarraylike:
an arraylike object whose .data type is memoryview.
:param arr: The object to check for memoryarraylike properties
:return: True iff the object is memoryarraylike
"""
return (
is_arraylike(arr)
and hasattr(arr, "data")
and type(arr.data).__name__ == "memoryview"
)
def is_xarraylike(xarr: Any) -> bool:
"""
Return True iff the object is xarraylike:
possessing .values, .dims, and .coords attributes,
and whose .values are arraylike.
:param arr: The object to check for xarraylike properties
:return: True iff the object is xarraylike
"""
return (
hasattr(xarr, "values")
and hasattr(xarr, "dims")
and hasattr(xarr, "coords")
and is_arraylike(xarr.values)
)