Pandas:向多索引添加新行 table 不起作用

Pandas: add new row to Multi index table does not work

我有一个奇怪的问题:我正在尝试在我的 table 中添加一个新的多索引行。但是,即使我完全按照此处解决的方法进行操作: adding a row to a MultiIndex DataFrame/Series 。他们的示例和解决方案有效,但不适用于我的数据。要么我做错了什么,要么有错误

import pandas as pd
import datetime as dt

他们的代码(有效):

df = pd.DataFrame({'Time': [dt.datetime(2013,2,3,9,0,1), dt.datetime(2013,2,3,9,0,1)], 
                   'hsec': [1,25], 'vals': [45,46]})
df.set_index(['Time','hsec'],inplace=True)

print df
df.ix[(dt.datetime(2013,2,3,9,0,2),0),:] = 5
print df

输出:

                          vals
Time                hsec      
2013-02-03 09:00:01 1       45
                    25      46

                          vals
Time                hsec      
2013-02-03 09:00:01 1       45
                    25      46
2013-02-03 09:00:02 0        5

我的代码(不工作):

d = [[0, 0, 2], [0, 2, 2], [1, 0, 2], [1, 2, 2]]
df = pd.DataFrame(d, columns=('frames', 'classID', 'amount'))
df.set_index(['frames', 'classID'], inplace=True)
print df

df.ix[(1,1),:] = 5
print df

输出:

                amount
frames classID        
0      0             2
       2             2
1      0             2
       2             2

                amount
frames classID        
0      0             2
       2             5
1      0             2
       2             2

请注意 5 出现在 df.loc[(0,2)] !

对我来说这似乎是 pandas 中的一个错误,但在新发布的 0.16 中显然已修复:

In [9]: pd.__version__
Out[9]: '0.16.0'

In [10]: df.ix[(1,1),:] = 5

In [11]: df
Out[11]:
                amount
frames classID
0      0             2
       2             2
1      0             2
       2             2
       1             5

但我可以确认这确实在 pandas 0.15.2 中不起作用。如果可以升级到 pandas 0.16,我还建议在这种情况下显式使用 loc(这样它肯定不会退回到位置整数位置)。但请注意,该错误也在 loc 下面 pandas 0.16