BUG: indexing with `at` and Multi-index gives unexpected error

This issue has been tracked since 2022-09-20.

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

qwe = pd.DataFrame({"b":[3,4,5], "n":[5,4,3], "a":[3,4,5], "c":[5,4,3]})
qwe.columns = pd.MultiIndex.from_tuples([("A", "b"),("A", "n"),("B", "a"),("B", "c")])
qwe["q"] = "asd"
# demo of getter error
print(f" 'loc' getter works fine for qwe.at[0, ('A', 'n')] {qwe.loc[0, ('A', 'n')]}")
print(f" 'at' getter works fine for qwe.at[0, ('A', 'n')] {qwe.at[0, ('A', 'n')]}")
print(f" 'loc' getter works fine for qwe.loc[0, 'q'] {qwe.loc[0, 'q']}")
print(f" 'at' getter doesnt work for qwe.at[0, 'q'] {qwe.at[0, 'q']}")
#demo of setter error
qwe.at[0, ("A", "n")] = 6 # Works fine!
qwe.at[0, "q"] = 6 # Works fine!
qwe.at[0, "q"] = [9,8,7] # Error

Issue Description

Given a dataframe with a multiindex, I can index individual cells using at. I would expect the indexing to work analogously to loc. I have encountered some related errors:

  1. getting an individual cell with at doesn't work when the second level of the multi-index is empty. This is okay to do in loc. i.e. qwe.at[0, 'q'] for getting does not work is the second level of q is empty, but for qwe.loc[0, 'q'] it works.
  2. The same kind of indexing using at for setting does not produce an error when setting a scalar, i.e. qwe.at[0, 'q']=5 is ok
  3. The same kind of indexing using at for setting does produce an error when setting a list, i.e. qwe.at[0, 'q']=[1,2,3] is not ok

Screenshot 2022-09-20 at 16 30 17

Expected Behavior

I would expect at to work analogously to loc for indexing and for the getters and setters to either both work or both not work. I would also expect to be able to insert lists into a cell, even when the dataframe has a multiindex.

Installed Versions

INSTALLED VERSIONS

commit : 87cfe4e
python : 3.10.3.final.0
python-bits : 64
OS : Darwin
OS-release : 21.4.0
Version : Darwin Kernel Version 21.4.0: Fri Mar 18 00:45:05 PDT 2022; root:xnu-8020.101.4~15/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : None
LOCALE : en_US.UTF-8

pandas : 1.5.0
numpy : 1.22.4
pytz : 2022.1
dateutil : 2.8.2
setuptools : 58.1.0
pip : 22.1.2
Cython : None
pytest : 7.1.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.4.0
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.2
numba : 0.55.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : 2022.3.0
xlrd : None
xlwt : None
zstandard : None
tzdata : None

Clear Cell Output
Convert to Markdown Cell
Delete Cell
Add Cell Above
Add Cell Below
Visibility
Input
Output
Extended Output
Parameterized Cell
1

Clear Cell Output
Convert to Markdown Cell
Delete Cell
Add Cell Above
Add Cell Below
Visibility
Input
Output
Extended Output
Parameterized Cell
1

phofl wrote this answer on 2022-09-20

Hi, thanks for your report.
Can you please produce the initial df without concat? Hard to see what's going on here

lschwetlick wrote this answer on 2022-09-20

@phofl I did it with pd.MultiIndex.from_tuples. Is that better? I also fixed some details in the description.

lschwetlick wrote this answer on 2022-09-20

Doing it, I also noticed the following, possibly related, difference between implicitly and explicitly setting the multi index. In the implicit version the name of the unused index is an empty string. When setting it explicitely it is NaN.

qwe = pd.DataFrame({"b":[3,4,5], "n":[5,4,3], "a":[3,4,5], "c":[5,4,3]})
qwe.columns = pd.MultiIndex.from_tuples([("A", "b"),("A", "n"),("B", "a"),("B", "c")])
qwe["q"] = "asd"
qwe.columns

->

MultiIndex([('A', 'b'),
            ('A', 'n'),
            ('B', 'a'),
            ('B', 'c'),
            ('q',  '')],
           )

and

qwe = pd.DataFrame({"b":[3,4,5], "n":[5,4,3], "a":[3,4,5], "c":[5,4,3], "q":"asd"})
qwe.columns = pd.MultiIndex.from_tuples([("A", "b"),("A", "n"),("B", "a"),("B", "c"), ("q")])
qwe["q"] = "asd"
qwe.columns

->

MultiIndex([('A', 'b'),
            ('A', 'n'),
            ('B', 'a'),
            ('B', 'c'),
            ('q', nan)],
           )
More Details About Repo
Owner Name pandas-dev
Repo Name pandas
Full Name pandas-dev/pandas
Language Python
Created Date 2010-08-24
Updated Date 2022-10-04
Star Count 35430
Watcher Count 1120
Fork Count 15089
Issue Count 3589

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date