diff --git a/doc/source/whatsnew/v2.0.1.rst b/doc/source/whatsnew/v2.0.1.rst index bf21d6402d621..fd00f3ed1ff0a 100644 --- a/doc/source/whatsnew/v2.0.1.rst +++ b/doc/source/whatsnew/v2.0.1.rst @@ -13,7 +13,7 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ -- +- Fixed regression for subclassed Series when constructing from a dictionary (:issue:`52445`) .. --------------------------------------------------------------------------- .. _whatsnew_201.bug_fixes: diff --git a/pandas/core/series.py b/pandas/core/series.py index f18751781765c..04927d64a602d 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -561,12 +561,7 @@ def _init_dict( keys, values = (), [] # Input is now list-like, so rely on "standard" construction: - - s = self._constructor( - values, - index=keys, - dtype=dtype, - ) + s = Series(values, index=keys, dtype=dtype) # Now we just make sure the order is respected, if any if data and index is not None: diff --git a/pandas/tests/series/test_subclass.py b/pandas/tests/series/test_subclass.py index a5620de7de65b..a3550c6de6780 100644 --- a/pandas/tests/series/test_subclass.py +++ b/pandas/tests/series/test_subclass.py @@ -58,3 +58,21 @@ def test_equals(self): s2 = tm.SubclassedSeries([1, 2, 3]) assert s1.equals(s2) assert s2.equals(s1) + + +class SubclassedSeries(pd.Series): + @property + def _constructor(self): + def _new(*args, **kwargs): + # some constructor logic that accesses the Series' name + if self.name == "test": + return pd.Series(*args, **kwargs) + return SubclassedSeries(*args, **kwargs) + + return _new + + +def test_constructor_from_dict(): + # https://github.com/pandas-dev/pandas/issues/52445 + result = SubclassedSeries({"a": 1, "b": 2, "c": 3}) + assert isinstance(result, SubclassedSeries)