BUG: call to _constructor of Series before __init__ is finished if initialized from dict #52445
Closed
3 tasks done
Labels
Regression
Functionality that used to work in a prior pandas version
Subclassing
Subclassing pandas objects
Milestone
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
Issue Description
The constructor of a Series is expected to return a callable that in turn returns a Series or subclass of a Series. When a Series is initialized from a dictionary specifically (and only then), internally a call to
._constructor
is made before__init__
has finished. This results in the following unexpected behaviour:self.name
is undefined because instantiation of the object hasn't finished. Similarly, many other attributes and methods of self are unavailable because__init__
hasn't finished. In my opinion,._constructor
should not be used from within__init__
to prevent this.This problem does only concern Series, not DataFrames. It was introduced by 60013a2 in series.py/_init_dict and is new in 2.0.0.
Expected Behavior
In my opinion,
._constructor
is an instance method (or rather a property) that is intended to be overridden by subclasses of Series. It should therefore behave like an instance method at all times. But feel free to disagree with me on this point.Installed Versions
INSTALLED VERSIONS
commit : 478d340
python : 3.8.10.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19044
machine : AMD64
processor : AMD64 Family 23 Model 113 Stepping 0, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United Kingdom.1252
pandas : 2.0.0
numpy : 1.23.5
pytz : 2022.6
dateutil : 2.8.2
setuptools : 62.3.2
pip : 22.1.1
Cython : None
pytest : 7.2.0
hypothesis : None
sphinx : 6.1.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : None
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.3
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: