Skip to content

Can set but not get item with tuple index #21574

Open
@jebob

Description

@jebob

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesNested DataData where the values are collections (lists, sets, dicts, objects, etc.).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions