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

BUG: TypeError: 'dict_values' object is not subscriptable on updating multiple rows in a single column #52175

Open
3 tasks done
kepricon opened this issue Mar 24, 2023 · 2 comments
Assignees
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@kepricon
Copy link

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

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 : f00ed8f
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

@kepricon kepricon added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 24, 2023
@kepricon
Copy link
Author

take

@phofl
Copy link
Member

phofl commented Mar 24, 2023

dict_keys and dict_values is not officially supported and I am not sure if that should work

@rhshadrach rhshadrach added the Indexing Related to indexing on series/frames, not to indexes themselves label Mar 29, 2023
@DeaMariaLeon DeaMariaLeon removed the Needs Triage Issue that has not been reviewed by a pandas team member label Mar 29, 2023
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants