(v)
| 39 | # Utility functions |
| 40 | # ----------------- |
| 41 | def to_scalar_or_list(v): |
| 42 | # Handle the case where 'v' is a non-native scalar-like type, |
| 43 | # such as numpy.float32. Without this case, the object might be |
| 44 | # considered numpy-convertable and therefore promoted to a |
| 45 | # 0-dimensional array, but we instead want it converted to a |
| 46 | # Python native scalar type ('float' in the example above). |
| 47 | # We explicitly check if is has the 'item' method, which conventionally |
| 48 | # converts these types to native scalars. |
| 49 | np = get_module("numpy", should_load=False) |
| 50 | pd = get_module("pandas", should_load=False) |
| 51 | if np and np.isscalar(v) and hasattr(v, "item"): |
| 52 | return to_non_numpy_type(np, v) |
| 53 | if isinstance(v, (list, tuple)): |
| 54 | return [to_scalar_or_list(e) for e in v] |
| 55 | elif np and isinstance(v, np.ndarray): |
| 56 | if v.ndim == 0: |
| 57 | return to_non_numpy_type(np, v) |
| 58 | return [to_scalar_or_list(e) for e in v] |
| 59 | elif pd and isinstance(v, (pd.Series, pd.Index)): |
| 60 | return [to_scalar_or_list(e) for e in v] |
| 61 | elif is_numpy_convertable(v): |
| 62 | return to_scalar_or_list(np.array(v)) |
| 63 | else: |
| 64 | return v |
| 65 | |
| 66 | |
| 67 | def copy_to_readonly_numpy_array(v, kind=None, force_numeric=False): |
no test coverage detected