Search code examples
python-3.xpandasseriesreindex

pandasObject.index() Vs reindexing using series


The functionality of reindexing in python pandas can also be done python Series as below.

import pandas as pd
order = ['a','c','b']
series_data = pd.Series([1,2,3],index=order)
series_data

In that case why do we explicitly go for reindex?


Solution

  • Let's take an example using index available in Series

    s = pd.Series([1,2,3], index=['k','f','t'])
    s
    # k    1
    # f    2
    # t    3
    # dtype: int64
    

    We can state that above series got assigned index with a datatype of int64.


    Now let's proceed with reindex:

    order = ['k','c','b']
    s.reindex(order)
    # k    1.0
    # c    NaN
    # b    NaN
    # dtype: float64
    

    As you can observe we passed two new index c and b which were not there in original series, so those values are assigned equal to NaN. Since NaN has dtype of float64, hence a final series results into only three indexes k, c and b with dtype as float64.

    I hope this clears how index inside Series is different from reindex outside.