Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] I have confirmed this bug exists on the latest version of pandas.
-
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
data = [["tom", 10, 300], ["nick", 15, 400], ["juli", 14, 500]]
df = pd.DataFrame(data, columns=["Name", "Age", "Rating"])
d1 = {"Age": 20, "Rating": 1000}
d2 = {"Age": 20}
# update 1 row, 2 columns
df.loc[df.Name == "tom", d1.keys()] = d1.values() # work
# update 1 row, 1 column
df.loc[df.Name == "tom", d2.keys()] = d2.values() # work
# update 2 rows, 2 columns
df.loc[df.Name != "tom", d1.keys()] = d1.values() # work
# update 2 rows, 1 column
df.loc[df.Name != "tom", d2.keys()] = d2.values() # error
Issue Description
- If I update the data frame object with multiple rows and a single column, it throws
TypeError: 'dict_values' object is not subscriptable
a relevant error stack trace:
File "/var/task/pandas/core/indexing.py", line 723, in __setitem__
iloc._setitem_with_indexer(indexer, value, self.name)
File "/var/task/pandas/core/indexing.py", line 1730, in _setitem_with_indexer
self._setitem_with_indexer_split_path(indexer, value, name)
File "/var/task/pandas/core/indexing.py", line 1783, in _setitem_with_indexer_split_path
return self._setitem_with_indexer((pi, info_axis[0]), value[0])
TypeError: 'dict_values' object is not subscriptable
Expected Behavior
If I run this script below, it should return the result below.
import pandas as pd
data = [["tom", 10, 300], ["nick", 15, 400], ["juli", 14, 500]]
df = pd.DataFrame(data, columns=["Name", "Age", "Rating"])
d2 = {"Age": 20}
# update 2 rows, 1 column
df.loc[df.Name != "tom", d2.keys()] = d2.values() # error
print(df)
Name Age Rating
0 tom 10 300
1 nick 20 400
2 juli 20 500
Installed Versions
INSTALLED VERSIONS
------------------
commit : f00ed8f47020034e752baf0250483053340971b0
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-67-generic
Version : #74~20.04.1-Ubuntu SMP Wed Feb 22 14:52:34 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.3.0
numpy : 1.22.2
pytz : 2021.3
dateutil : 2.8.2
pip : 22.0.3
setuptools : 60.7.1
Cython : None
pytest : 6.2.4
hypothesis : None
sphinx : 4.1.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.9.1 (dt dec pq3 ext lo64)
jinja2 : 3.0.3
IPython : 7.26.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 2021.11.1
fastparquet : None
gcsfs : None
matplotlib : 3.4.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 6.0.1
pyxlsb : None
s3fs : 0.4.0
scipy : 1.7.1
sqlalchemy : 1.4.31
tables : None
tabulate : 0.8.9
xarray : 0.20.2
xlrd : None
xlwt : None
numba : None
Process finished with exit code 0
Comment From: kepricon
take
Comment From: phofl
dict_keys and dict_values is not officially supported and I am not sure if that should work