Skip to content

Inconsistent integer column indexing #6546

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

Closed
alexfields opened this issue Mar 4, 2014 · 3 comments · Fixed by #6548
Closed

Inconsistent integer column indexing #6546

alexfields opened this issue Mar 4, 2014 · 3 comments · Fixed by #6548
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@alexfields
Copy link

When columns have an integer as their name, indexing them becomes inconsistent:

>>> import pandas as pd
>>> df = pd.DataFrame({1:[1,1],2:[2,2],'a':['a','a']})
>>> df
   1  2  a
0  1  2  a
1  1  2  a

[2 rows x 3 columns]
>>> df[1]
0    1
1    1
Name: 1, dtype: int64
>>> df[['a',1]]
   a  1
0  a  1
1  a  1

[2 rows x 2 columns]
>>> df[[1,2]]
   2  a
0  2  a
1  2  a

[2 rows x 2 columns]

Desired behavior would be:

>>> df[[1,2]]
   1  2
0  1  2
1  1  2

[2 rows x 2 columns]

This is in pandas 0.13.1

@jreback
Copy link
Contributor

jreback commented Mar 4, 2014

Actually it IS consistent with using ix. You are pretty much asking for troublie mixing
positions and labels, here are some solns. Read the docs: http://pandas.pydata.org/pandas-docs/stable/indexing.html#different-choices-for-indexing-loc-iloc-and-ix

Things are laid out very explicty, and you have all of the tools to do what you want. However, pandas cannot guess at what you are after; you need to be more explicit.

In [5]: df.ix[:,[1,2]]
Out[5]: 
   1  2
0  1  2
1  1  2

[2 rows x 2 columns]

In [6]: df.iloc[:,[1,2]]
Out[6]: 
   2  a
0  2  a
1  2  a

[2 rows x 2 columns]

In [7]: df.loc[:,[1,2]]
Out[7]: 
   1  2
0  1  2
1  1  2

[2 rows x 2 columns]

@jreback jreback closed this as completed Mar 4, 2014
@alexfields
Copy link
Author

Thanks for the response.

Perhaps you can comment on this behavior?

>>> df.loc[0,[1,2]]
1    1
2    2
Name: 0, dtype: object
>>> df.loc[0,[1,2]] = [3,4]
>>> df
   1  2  a
0  1  3  4
1  1  2  a

[2 rows x 3 columns]

Apparently loc selects a different part of the dataframe for getting than it does for setting?

@jreback
Copy link
Contributor

jreback commented Mar 5, 2014

this was a hidden bug; the index code has an amazing number of cases! fixed by #6548

@jreback jreback reopened this Mar 5, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants