Python Reindex Producing Nan

Adam Warner picture Adam Warner · Jan 30, 2016 · Viewed 14.2k times · Source

Here is the code that I am working with:

import pandas as pd

test3 = pd.Series([1,2,3], index = ['a','b','c'])
test3 = test3.reindex(index = ['f','g','z'])

So originally every thing is fine and test3 has an index of 'a' 'b' 'c' and values 1,2,3. But then when I got to reindex test3 I get that my values 1 2 3 are lost. Why is that? The desired output would be:

f 1
g 2
z 3

Answer

EdChum picture EdChum · Jan 30, 2016

The docs are clear on this behaviour :

Conform Series to new index with optional filling logic, placing NA/NaN in locations having no value in the previous index

if you just want to overwrite the index values then do:

In [32]:
test3.index  = ['f','g','z']

test3
Out[32]:
f    1
g    2
z    3
dtype: int64