pandas.Index.values#

property Index.values[source]#

Return an array representing the data in the Index.

Warning

We recommend using Index.array or Index.to_numpy(), depending on whether you need a reference to the underlying data or a NumPy array. The return type of .values depends on the dtype: it is a numpy.ndarray for some dtypes (for example int64 or float64) and an ExtensionArray for others (for example interval, category, or nullable Int64), which makes it harder to write code that works across dtypes. Index.array always returns the underlying array as an ExtensionArray, and Index.to_numpy() always returns a numpy.ndarray and accepts dtype, copy, and na_value arguments to control the conversion.

Changed in version 3.0.0: The returned array is read-only.

Returns:
array: numpy.ndarray or ExtensionArray

See also

Index.array

Reference to the underlying data.

Index.to_numpy

A NumPy array representing the underlying data.

Examples

For an Index backed by a NumPy dtype such as int64, .values returns a numpy.ndarray:

>>> idx = pd.Index([1, 2, 3])
>>> idx
Index([1, 2, 3], dtype='int64')
>>> idx.values
array([1, 2, 3])

For an Index backed by an extension dtype such as interval, .values returns an ExtensionArray instead, while Index.to_numpy() always returns a numpy.ndarray:

>>> idx = pd.interval_range(start=0, end=5)
>>> idx.values
<IntervalArray>
[(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]]
Length: 5, dtype: interval[int64, right]
>>> idx.to_numpy()
array([Interval(0, 1, closed='right'), Interval(1, 2, closed='right'),
       Interval(2, 3, closed='right'), Interval(3, 4, closed='right'),
       Interval(4, 5, closed='right')], dtype=object)