Recursively produce a fill value for `dtype`, calling f on scalar dtypes
(dtype, f)
| 216 | del float_types_list |
| 217 | |
| 218 | def _recursive_fill_value(dtype, f): |
| 219 | """ |
| 220 | Recursively produce a fill value for `dtype`, calling f on scalar dtypes |
| 221 | """ |
| 222 | if dtype.names is not None: |
| 223 | # We wrap into `array` here, which ensures we use NumPy cast rules |
| 224 | # for integer casts, this allows the use of 99999 as a fill value |
| 225 | # for int8. |
| 226 | vals = [] |
| 227 | for name in dtype.names: |
| 228 | field_dtype = dtype[name] |
| 229 | val = _recursive_fill_value(field_dtype, f) |
| 230 | if np.issubdtype(field_dtype, np.datetime64): |
| 231 | if isinstance(val, dt.date): |
| 232 | val = np.datetime64(val) |
| 233 | val = np.array(val) |
| 234 | elif isinstance(val, (int, np.integer)): |
| 235 | val = np.array(val).astype(field_dtype) |
| 236 | else: |
| 237 | val = np.array(val) |
| 238 | else: |
| 239 | val = np.array(val) |
| 240 | vals.append(val) |
| 241 | return np.array(tuple(vals), dtype=dtype)[()] # decay to void scalar from 0d |
| 242 | elif dtype.subdtype: |
| 243 | subtype, shape = dtype.subdtype |
| 244 | subval = _recursive_fill_value(subtype, f) |
| 245 | return np.full(shape, subval) |
| 246 | else: |
| 247 | return f(dtype) |
| 248 | |
| 249 | |
| 250 | def _get_dtype_of(obj): |
no test coverage detected
searching dependent graphs…