reindex doesn't fill with NaN

戏子无情 提交于 2020-06-13 07:01:29

问题


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

来源:https://stackoverflow.com/questions/62214123/reindex-doesnt-fill-with-nan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!