Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Assigning Panel item using .ix/.loc no longer works #7763

Closed
creeson opened this issue Jul 15, 2014 · 7 comments · Fixed by #8399
Closed

BUG: Assigning Panel item using .ix/.loc no longer works #7763

creeson opened this issue Jul 15, 2014 · 7 comments · Fixed by #8399
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@creeson
Copy link

creeson commented Jul 15, 2014

I am updating a codebase from an OLD version of pandas (0.7.3) to current version (0.14.1).

pandas: 0.14.1
numpy: 1.8.0

The following code has stopped working correctly (using .ix[] or .loc[]):

import numpy as np
import pandas as ps

np.random.seed(0)

index=range(3)
columns = list('abc')

panel = ps.Panel({'A' : ps.DataFrame(np.random.randn(3, 3), index=index, columns=columns),
                  'B' : ps.DataFrame(np.random.randn(3, 3), index=index, columns=columns),
                  'C' : ps.DataFrame(np.random.randn(3, 3), index=index, columns=columns)
                  })

replace = ps.DataFrame(np.eye(3,3), index=range(3), columns=columns)

print panel['A']

for idx in list('ABC'):
    panel.loc[idx,:,:] = replace

print panel['A']

Output:

          a         b         c
0  1.764052  0.400157  0.978738
1  2.240893  1.867558 -0.977278
2  0.950088 -0.151357 -0.103219
    a   b   c
0 NaN NaN NaN
1 NaN NaN NaN
2 NaN NaN NaN

However, if I simply do this:

for idx in list('ABC'):
    panel[idx] = replace

print panel['A']

It behaves as expected:

   a  b  c
0  1  0  0
1  0  1  0
2  0  0  1

Also, if I assign more than just the item, it works fine:

panel.loc['A', 0, 'a'] = 1.0
print panel['A']
          a         b         c
0  1.000000  0.400157  0.978738
1  2.240893  1.867558 -0.977278
2  0.950088 -0.151357 -0.103219

Thanks.

@jreback
Copy link
Contributor

jreback commented Jul 15, 2014

hmm, must be an alignment issue. thanks, marking as a bug.

note that

panel.loc['A'] = replace works as well. Its when specifiying multiple indexers it
gets confused.

@jreback jreback added this to the 0.15.0 milestone Jul 15, 2014
@spitz-dan-l
Copy link

Any update on this? I have a situation where I need to replace the columns of a 'minor'-oriented panel, necessitating p.loc[:, :, idx] = replace

Any suggested workarounds for this?

@jreback
Copy link
Contributor

jreback commented Sep 3, 2014

that syntax should work
give an example is it does not

@spitz-dan-l
Copy link

Investigation showed what's different about my use case. My issue is I am trying to add columns. For the example above, my panel was set up like so:

panel = ps.Panel.from_dict({
                  'A' : ps.DataFrame(np.random.randn(3, 3), index=index, columns=columns),
                  'B' : ps.DataFrame(np.random.randn(3, 3), index=index, columns=columns),
                  'C' : ps.DataFrame(np.random.randn(3, 3), index=index, columns=columns)
                  }, orient='minor')

I'm trying to add a 'd' column to the panel:

replace = ps.DataFrame(np.eye(4,4), index=range(4), columns=list('abcd'))

such that only the 'A' label gets its values replaced. For B and C, the 'd' column should be NaN. The way I'd thought to do it was:

panel.loc[:, :, 'A'] = replace

But this doesn't add 'd'. I end up with:

print panel.loc[:, :, 'A']

   a  b  c
0  1  0  0
1  0  1  0
2  0  0  1

Is this how it ought to behave? Am I asking for what I want the wrong way?

@spitz-dan-l
Copy link

@jreback
Copy link
Contributor

jreback commented Sep 3, 2014

@spitz-dan-l
the indicated behavior is correct; the panel will reindex the provide data to the same as the panel.

prob best to just reindex the axes to the union of the axes you want

e.g. something like

panel = panel.reindex(items=panel.items|replace.columns,major_axis=panel.major_axis|replace.index)
panel.loc[:,:,'A'] = replace

or you can use concat and update. .loc is not the right tool here because of the ambiguity. pandas normally aligns the input (special case on the 0th axis where it can enlarge).

@spitz-dan-l
Copy link

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants