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

Can set but not get item with tuple index #21574

Open
jebob opened this issue Jun 21, 2018 · 6 comments
Open

Can set but not get item with tuple index #21574

jebob opened this issue Jun 21, 2018 · 6 comments
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.).
Projects

Comments

@jebob
Copy link
Contributor

jebob commented Jun 21, 2018

Code Sample, a copy-pastable example if possible

import pandas as pd

set1 = ['a', 'b', 'c']
set2 = ['1', '2', '3']
set3 = ['test']

tuple_index = [(s1, s2) for s1 in set1 for s2 in set2]
df = pd.DataFrame(index=tuple_index, columns=set3)

df.loc[('a', '1'), 'test'] = 5
print(df)  # Shows assignment occurs
print(df.loc[('a', '1'), 'test'])  # Raises KeyError: "None of [('a', '1')] are in the [index]"

Problem description

Indexing with tuples is permitted only for setting but not getting.

Expected Output

Should either:

  1. Refuse to set the item because using tuple indicies is a silly idea, see Using tuples as dataframe index, containing single entry makes rows impossible to select #20991
  2. Successfully get the item.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.2.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.22.0
pytest: 3.2.3
pip: 10.0.1
setuptools: 38.4.0
Cython: 0.27.3
numpy: 1.14.2
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.3.1
sphinx: None
patsy: 0.5.0
dateutil: 2.7.0
pytz: 2018.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.2
openpyxl: 2.4.8
xlrd: 1.1.0
xlwt: None
xlsxwriter: None
lxml: 4.2.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Credit @JamesHowse for finding.
@gfyoung gfyoung added Indexing Related to indexing on series/frames, not to indexes themselves API Design labels Jun 21, 2018
@gfyoung
Copy link
Member

gfyoung commented Jun 21, 2018

That seems like a reasonable suggestion.

cc @jreback @chris-b1

@toobaz
Copy link
Member

toobaz commented Jun 22, 2018

Definitely a bug. I expect it to be easy to fix once #21594 is merged

@alimcmaster1
Copy link
Member

I’m happy to have a look at this unless someone has already done so

@gfyoung
Copy link
Member

gfyoung commented Jun 26, 2018

@alimcmaster1 : Go for it!

@toobaz
Copy link
Member

toobaz commented Jun 26, 2018

I’m happy to have a look at this unless someone has already done so

I already have a branch that I think fixes this, once rebased on master. Will check in few hours.

toobaz added a commit to toobaz/pandas that referenced this issue Jun 26, 2018
@toobaz
Copy link
Member

toobaz commented Jun 26, 2018

That's the branch... it's not ready but I think it's the right direction (handling tuples as ordinary keys). @alimcmaster1 feel free to play with it if you like (I won't have time at least for some days).

@jbrockmendel jbrockmendel added this to Tuples in Indexing Feb 18, 2020
@jbrockmendel jbrockmendel added the Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.). label Sep 22, 2020
@mroeschke mroeschke added Bug and removed API Design labels Jun 20, 2021
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 Nested Data Data where the values are collections (lists, sets, dicts, objects, etc.).
Projects
No open projects
Indexing
Tuples
Development

No branches or pull requests

6 participants