1

The pandas series has two closely related attributes: Series.index and Series.index.values.

The first of these two returns the current index of some pandas index type. It is mutable, and can be used to change the index for the Series (a good thing).

The second returns an numpy.ndarray. However, it is immutable, and retains the original index values regardless of subsequent changes to the index.

My question: what is the intent of the non-mutable Series.index.values?

Edit:

Mmmm. just ignore this question - i cannot repeat the confusing behaviours of s.index.values I was seeing last night.

Mark Graph
  • 4,969
  • 6
  • 25
  • 37

1 Answers1

3

Almost all .values properties (of Series, DataFrame, Panel, Index objects) return underlying numpy data. Index itself is a complex wrap up around this data, providing additional handy functionality, for example (sample DataFrame taken from this answer):

>>> s = df['A']
>>> s.index
MultiIndex
[(u'one', 1), (u'one', 2), (u'one', 3), (u'two', 1), (u'two', 2), (u'two', 3)]
>>> s.index.values
array([('one', 1L), ('one', 2L), ('one', 3L), ('two', 1L), ('two', 2L),
       ('two', 3L)], dtype=object)
>>> s.index.get_indexer([('one',1), ('two', 2)])
array([0, 4])

When you update index with some iterable for example, a new Index object is created under the hood:

>>> s.index = np.arange(6)
>>> s.index
Int64Index([0, 1, 2, 3, 4, 5], dtype=int64)
>>> s.index.get_indexer([0,4])
array([0, 4])
>>> s.index.values
array([0, 1, 2, 3, 4, 5], dtype=int64)
Community
  • 1
  • 1
alko
  • 46,136
  • 12
  • 94
  • 102