MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / _unpack_to_numpy

Function _unpack_to_numpy

lib/matplotlib/cbook.py:2495–2522  ·  view source on GitHub ↗

Internal helper to extract data from e.g. pandas and xarray objects.

(x)

Source from the content-addressed store, hash-verified

2493
2494
2495def _unpack_to_numpy(x):
2496 """Internal helper to extract data from e.g. pandas and xarray objects."""
2497 if isinstance(x, np.ndarray):
2498 # If numpy, return directly
2499 return x
2500 if hasattr(x, 'to_numpy'):
2501 # Assume that any to_numpy() method actually returns a numpy array
2502 return x.to_numpy()
2503 if hasattr(x, 'values'):
2504 xtmp = x.values
2505 # For example a dict has a 'values' attribute, but it is not a property
2506 # so in this case we do not want to return a function
2507 if isinstance(xtmp, np.ndarray):
2508 return xtmp
2509 if _is_torch_array(x) \
2510 or _is_jax_array(x) \
2511 or _is_tensorflow_array(x) \
2512 or _is_mlx_array(x):
2513 # using np.asarray() instead of explicitly __array__(), as the latter is
2514 # only _one_ of many methods, and it's the last resort, see also
2515 # https://numpy.org/devdocs/user/basics.interoperability.html#using-arbitrary-objects-in-numpy
2516 # therefore, let arrays do better if they can
2517 xtmp = np.asarray(x)
2518
2519 # In case np.asarray method does not return a numpy array in future
2520 if isinstance(xtmp, np.ndarray):
2521 return xtmp
2522 return x
2523
2524
2525def _auto_format_str(fmt, value):

Callers 2

_check_1dFunction · 0.85
_reshape_2DFunction · 0.85

Calls 4

_is_torch_arrayFunction · 0.85
_is_jax_arrayFunction · 0.85
_is_tensorflow_arrayFunction · 0.85
_is_mlx_arrayFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…