Table Of Contents

Search

Enter search terms or a module, class or function name.

pandas.Index.to_numpy

Index.to_numpy()[source]

A NumPy ndarray representing the values in this Series or Index.

New in version 0.24.0.

The returned array will be the same up to equality (values equal in self will be equal in the returned array; likewise for values that are not equal). When self contains an ExtensionArray, the dtype may be different. For example, for a category-dtype Series, to_numpy() will return a NumPy array and the categorical dtype will be lost.

Returns:
numpy.ndarray

See also

Series.array
Get the actual data stored within.
Index.array
Get the actual data stored within.
DataFrame.to_numpy
Similar method for DataFrame.

Notes

For NumPy dtypes, this will be a reference to the actual data stored in this Series or Index. Modifying the result in place will modify the data stored in the Series or Index (not that we recommend doing that).

For extension types, to_numpy() may require copying data and coercing the result to a NumPy type (possibly object), which may be expensive. When you need a no-copy reference to the underlying data, Series.array should be used instead.

This table lays out the different dtypes and return types of to_numpy() for various dtypes within pandas.

dtype array type
category[T] ndarray[T] (same dtype as input)
period ndarray[object] (Periods)
interval ndarray[object] (Intervals)
IntegerNA ndarray[object]
datetime64[ns, tz] ndarray[object] (Timestamps)

Examples

>>> ser = pd.Series(pd.Categorical(['a', 'b', 'a']))
>>> ser.to_numpy()
array(['a', 'b', 'a'], dtype=object)
Scroll To Top