2

I have a weird problem: I am trying to add a new row in my table with multi-indeces. However, even though I do it exactly as solved in here: adding a row to a MultiIndex DataFrame/Series . Their example and solution works, but it does not work with my data. Either I am doing something wrong, or there is a bug

import pandas as pd
import datetime as dt

Their code (working):

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

Output:

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

and

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

My code (not working):

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

Output:

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

and

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

Note the 5 appeared at df.loc[(0,2)] !

Community
  • 1
  • 1
P.R.
  • 3,785
  • 1
  • 27
  • 47

1 Answers1

4

This seems a bug in pandas to me, but it is apparantly fixed in the newly released 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

But I can confirm this was indeed not working in pandas 0.15.2. If can upgrade to pandas 0.16, I would also advice to explicitly use loc in this case (so it certainly does not fall back to the positional integer location). But note the bug is also in loc below pandas 0.16

joris
  • 133,120
  • 36
  • 247
  • 202
  • i confirm that the bug is fixed in the version you get with `pip install pandas` i.e. pandas 0.16.0 – P.R. Mar 27 '15 at 16:21