pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.4k stars 17.83k forks source link

Can set but not get item with tuple index #21574

Open jebob opened 6 years ago

jebob commented 6 years ago

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 #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 commented 6 years ago

That seems like a reasonable suggestion.

cc @jreback @chris-b1

toobaz commented 6 years ago

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

alimcmaster1 commented 6 years ago

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

gfyoung commented 6 years ago

@alimcmaster1 : Go for it!

toobaz commented 6 years ago

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 commented 6 years ago

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).