Skip to content

BUG: Series.__setitem__ fails with non range index while upcasting dtype #45070

Closed
@LucasG0

Description

@LucasG0

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 master branch of pandas.

Reproducible Example

>>> s = pd.Series([1, 2, 3], index=["a", "b", "c"])
>>> s
a    1
b    2
c    3
dtype: int64
>>> s[0] = "X"
Traceback (most recent call last):
  File "/home/lucas/.local/lib/python3.8/site-packages/pandas/core/series.py", line 1000, in __setitem__
    self._set_with_engine(key, value)
  File "/home/lucas/.local/lib/python3.8/site-packages/pandas/core/series.py", line 1033, in _set_with_engine
    loc = self.index._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1675, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1683, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/lucas/.local/lib/python3.8/site-packages/pandas/core/series.py", line 1005, in __setitem__
    values[key] = value
ValueError: invalid literal for int() with base 10: 'X'

Issue Description

Currently it raises an error, I think it is considered to be a bug while 39584 is not decided.

Expected Behavior

Either correctly set the value and upcast dtype to object, either keep raising an error depending on 39584 output.

Installed Versions

INSTALLED VERSIONS

commit : db08276
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.11.0-43-generic
Version : #47~20.04.2-Ubuntu SMP Mon Dec 13 11:06:56 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : fr_FR.UTF-8
LOCALE : fr_FR.UTF-8

pandas : 1.1.3
numpy : 1.19.5
pytz : 2019.3
dateutil : 2.8.2
pip : 20.0.2
setuptools : 45.2.0
Cython : None
pytest : 6.2.4
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.25.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 2021.10.1
fastparquet : 0.7.1
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 6.0.0
pytables : None
pyxlsb : None
s3fs : 2021.10.1
scipy : None
sqlalchemy : None
tables : None
tabulate : 0.8.9
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions