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
class Example(pd.Series):
@property
def _constructor(self):
def _new(*args, **kwargs):
if self.name == 'test':
return pd.Series(*args, **kwargs)
return Example(*args, **kwargs)
return _new
Example({'A': 1, 'B': 2, 'C': 3})
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:
Traceback (most recent call last):
File "C:\Dateien\Code\Formula1\Fast-F1\exp2.py", line 15, in <module>
Example({'A': 1, 'B': 2, 'C': 3})
File "C:\Dateien\Code\Formula1\Fast-F1\venv\lib\site-packages\pandas\core\series.py", line 472, in __init__
data, index = self._init_dict(data, index, dtype)
File "C:\Dateien\Code\Formula1\Fast-F1\venv\lib\site-packages\pandas\core\series.py", line 566, in _init_dict
s = self._constructor(
File "C:\Dateien\Code\Formula1\Fast-F1\exp2.py", line 8, in _new
if self.name == 'test':
File "C:\Dateien\Code\Formula1\Fast-F1\venv\lib\site-packages\pandas\core\generic.py", line 5989, in __getattr__
return object.__getattribute__(self, name)
File "C:\Dateien\Code\Formula1\Fast-F1\venv\lib\site-packages\pandas\core\series.py", line 674, in name
return self._name
File "C:\Dateien\Code\Formula1\Fast-F1\venv\lib\site-packages\pandas\core\generic.py", line 5989, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'Example' object has no attribute '_name'
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