Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-indexed pandas dataframe subset assignment

The first issue here is a bug in 0.12, but was fixed in 0.13.0rc1. The second issue here is not fixed though and is at least an inconsistency.

These two scenarios work fine:

a = DataFrame(np.zeros((2, 2), dtype=float),columns=[['a', 'B'],[1, 2]])
b = DataFrame(np.zeros((2, 2), dtype=float),columns=[['a', 'B']])
b[['a']]=a[['a']]

and

a = DataFrame(np.zeros((2, 2), dtype=float),columns=[['a', 'b'],[1, 2]])
b = DataFrame(np.zeros((2, 2), dtype=float),columns=[['a', 'b'],[1, 2]])
b[['a']]=a[['a']]

However,

a = DataFrame(np.zeros((2, 2), dtype=float),columns=[['a', 'B'],[1, 2]])
b = DataFrame(np.zeros((2, 2), dtype=float),columns=[['a', 'B'],[1, 2]])
b[['a']]=a[['a']]

generates an AttributeError: _ref_locs

Similar situation with:

b = DataFrame(np.zeros((2, 2)),columns=[['a', 'c'],[1,2]])
b.drop('a', axis=1)

works fine, but

b = DataFrame(np.zeros((2, 2)),columns=[['a', 'C'],[1,2]])
b.drop('a', axis=1)

gives AttributeError: 'FrozenNDArray' object has no attribute 'start'

like image 305
user3053186 Avatar asked Feb 02 '26 18:02

user3053186


1 Answers

Since you didn't specify, you are probably using pandas <= 0.12

This works in 0.13rc1 (final release coming soon), and was a bug in 0.12

You example from above (using positional references for clarity)

In [3]: a = DataFrame(np.arange(0,4).reshape((2,2)),columns=[['a', 'B'],[1, 2]])

In [4]: b = DataFrame(np.arange(4,8).reshape((2,2)),columns=[['a', 'B'],[1, 2]])

In [5]: a
Out[5]: 
   a  B
   1  2
0  0  1
1  2  3

[2 rows x 2 columns]

In [6]: b
Out[6]: 
   a  B
   1  2
0  4  5
1  6  7

[2 rows x 2 columns]

In [7]: b[['a']] = a[['a']]

In [8]: b
Out[8]: 
   a  B
   1  2
0  0  5
1  2  7

[2 rows x 2 columns]

The second part is not a bug; rather you are not specifying the label fully (you are only specifying a single-level), instead you need to specify the complete label (via a tuple):

In [12]: b = DataFrame(np.zeros((2, 2)),columns=[['a', 'C'],[1,2]])

In [13]: b.drop([('a',1)],axis=1)
Out[13]: 
   C
   2
0  0
1  0

[2 rows x 1 columns]
like image 170
Jeff Avatar answered Feb 05 '26 07:02

Jeff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!