Description
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
s1 = pd.Series(dtype='O', index=['a', 'b'])
s1['a'] = pd.Series()
s1.loc['b'] = pd.Series()
# >> s1
# a Series([], dtype: float64)
# b [] <--------------- ?? wrong cast to `np.ndarray`
# dtype: object
# It works if the index is **not** specified
s2 = pd.Series(dtype='O', index=None)
s2['a'] = pd.Series()
s2.loc['b'] = pd.Series()
# >> s2
# a Series([], dtype: float64)
# b Series([], dtype: float64)
# dtype: object
Issue Description
A series with object dtype and a predefined index, cast items of type pd.Series
to np.ndarray
instead of preserving the series(-type). This apply to all accessors loc
, iloc
, at
, iat
and only works correctly with __setitem__
..
Expected Behavior
The elements in a series of dtype object should not be casted in any case.
Installed Versions
INSTALLED VERSIONS
commit : 87cfe4e
python : 3.10.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-48-generic
Version : #54-Ubuntu SMP Fri Aug 26 13:26:29 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.5.0
numpy : 1.23.3
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 59.8.0
pip : 22.2.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.0
numba : 0.56.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 9.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None