2

I have a series.

index = pd.MultiIndex.from_tuples([('bar', 'one'),
            ('bar', 'two'),
            ('baz', 'one'),
            ('baz', 'two'),
            ('foo', 'one'),
            ('foo', 'two'),
            ('qux', 'one'),
            ('qux', 'two')],
           names=['first', 'second'])
np.random.seed(0)
s = pd.Series(np.random.randn(8), index=index)

first  second
bar    one       1.764052
       two       0.400157
baz    one       0.978738
       two       2.240893
foo    one       1.867558
       two      -0.977278
qux    one       0.950088
       two      -0.151357
dtype: float64

When I reindex by level 1.

s.reindex(['two','one','three'],level=1)
first  second
bar    two       0.400157
       one       1.764052
baz    two       2.240893
       one       0.978738
foo    two      -0.977278
       one       1.867558
qux    two      -0.151357
       one       0.950088
dtype: float64

I expected to get

first  second
bar    two       0.400157
       one       1.764052
       three       NaN
baz    two       2.240893
       one       0.978738
       three        NaN
foo    two      -0.977278
       one       1.867558
       three        NaN
qux    two      -0.151357
       one       0.950088
       three        NaN
dtype: float64

Shouldn't reindex fill them with NaNs?

EDIT:

This is a bug and has an open issue#25460

Ch3steR
  • 20,090
  • 4
  • 28
  • 58

0 Answers0