Skip to content

BUG: Series.loc setitem with Series raises ValueError #38303

Closed
@ma3da

Description

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

import pandas as pd

ser = pd.Series([0, 0, 0], dtype=object)

ser.loc[0] = pd.Series([42])  # ValueError: No axis named 1 for object type Series

Problem description

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/sources/official.clone/pandas/pandas/core/generic.py in _get_axis_number(cls, axis)
    456         try:
--> 457             return cls._AXIS_TO_AXIS_NUMBER[axis]
    458         except KeyError:

KeyError: 1

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-24-7c4b5d72ca25> in <module>
----> 1 ser.loc[0] = pd.Series([0])

~/sources/official.clone/pandas/pandas/core/indexing.py in __setitem__(self, key, value)
    689 
    690         iloc = self if self.name == "iloc" else self.obj.iloc
--> 691         iloc._setitem_with_indexer(indexer, value, self.name)
    692 
    693     def _validate_key(self, key, axis: int):

~/sources/official.clone/pandas/pandas/core/indexing.py in _setitem_with_indexer(self, indexer, value, name)
   1634             self._setitem_with_indexer_split_path(indexer, value, name)
   1635         else:
-> 1636             self._setitem_single_block(indexer, value, name)
   1637 
   1638     def _setitem_with_indexer_split_path(self, indexer, value, name: str):

~/sources/official.clone/pandas/pandas/core/indexing.py in _setitem_single_block(self, indexer, value, name)
   1848             # setting for extensionarrays that store dicts. Need to decide
   1849             # if it's worth supporting that.
-> 1850             value = self._align_series(indexer, Series(value))
   1851 
   1852         elif isinstance(value, ABCDataFrame) and name != "iloc":

~/sources/official.clone/pandas/pandas/core/indexing.py in _align_series(self, indexer, ser, multiindex_indexer)
   2018 
   2019         elif is_scalar(indexer):
-> 2020             ax = self.obj._get_axis(1)
   2021 
   2022             if ser.index.equals(ax):

~/sources/official.clone/pandas/pandas/core/generic.py in _get_axis(self, axis)
    467     @final
    468     def _get_axis(self, axis: Axis) -> Index:
--> 469         axis_number = self._get_axis_number(axis)
    470         assert axis_number in {0, 1}
    471         return self.index if axis_number == 0 else self.columns

~/sources/official.clone/pandas/pandas/core/generic.py in _get_axis_number(cls, axis)
    457             return cls._AXIS_TO_AXIS_NUMBER[axis]
    458         except KeyError:
--> 459             raise ValueError(f"No axis named {axis} for object type {cls.__name__}")
    460 
    461     @final

ValueError: No axis named 1 for object type Series

Expected Output

The same as with

ser.iloc[0] = pd.Series([42])
0    0    42
dtype: int64
1                       0
2                       0
dtype: object

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit           : 03e58585036c83ca3d4c86d7d3d7ede955c15130
python           : 3.7.3.final.0
python-bits      : 64
OS               : Linux
OS-release       : 5.8.0-0.bpo.2-amd64
Version          : #1 SMP Debian 5.8.10-1~bpo10+1 (2020-09-26)
machine          : x86_64
processor        : 
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.2.0.dev0+1465.g03e585850
numpy            : 1.19.4
pytz             : 2020.4
dateutil         : 2.8.1
pip              : 20.2.4
setuptools       : 40.8.0
Cython           : 0.29.21
pytest           : None
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : 2.8.6 (dt dec pq3 ext lo64)
jinja2           : 2.11.2
IPython          : 7.15.0
pandas_datareader: None
bs4              : 4.9.3
bottleneck       : None
fsspec           : None
fastparquet      : None
gcsfs            : None
matplotlib       : 3.3.3
numexpr          : None
odfpy            : None
openpyxl         : 3.0.5
pandas_gbq       : None
pyarrow          : None
pyxlsb           : None
s3fs             : None
scipy            : 1.5.3
sqlalchemy       : None
tables           : None
tabulate         : None
xarray           : None
xlrd             : None
xlwt             : None
numba            : None```
</details>

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesSeriesSeries data structure

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions