Skip to content

Commit 797ff80

Browse files
Backport PR #52516 on branch 2.0.x (REGR: fix Series construction from dict for subclasses) (#52520)
Backport PR #52516: REGR: fix Series construction from dict for subclasses Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent 49c020a commit 797ff80

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

doc/source/whatsnew/v2.0.1.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ including other versions of pandas.
1313

1414
Fixed regressions
1515
~~~~~~~~~~~~~~~~~
16-
-
16+
- Fixed regression for subclassed Series when constructing from a dictionary (:issue:`52445`)
1717

1818
.. ---------------------------------------------------------------------------
1919
.. _whatsnew_201.bug_fixes:

pandas/core/series.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -562,12 +562,7 @@ def _init_dict(
562562
keys, values = (), []
563563

564564
# Input is now list-like, so rely on "standard" construction:
565-
566-
s = self._constructor(
567-
values,
568-
index=keys,
569-
dtype=dtype,
570-
)
565+
s = Series(values, index=keys, dtype=dtype)
571566

572567
# Now we just make sure the order is respected, if any
573568
if data and index is not None:

pandas/tests/series/test_subclass.py

+18
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,21 @@ def test_equals(self):
5858
s2 = tm.SubclassedSeries([1, 2, 3])
5959
assert s1.equals(s2)
6060
assert s2.equals(s1)
61+
62+
63+
class SubclassedSeries(pd.Series):
64+
@property
65+
def _constructor(self):
66+
def _new(*args, **kwargs):
67+
# some constructor logic that accesses the Series' name
68+
if self.name == "test":
69+
return pd.Series(*args, **kwargs)
70+
return SubclassedSeries(*args, **kwargs)
71+
72+
return _new
73+
74+
75+
def test_constructor_from_dict():
76+
# https://github.com/pandas-dev/pandas/issues/52445
77+
result = SubclassedSeries({"a": 1, "b": 2, "c": 3})
78+
assert isinstance(result, SubclassedSeries)

0 commit comments

Comments
 (0)