Skip to content

Commit 4adc5fa

Browse files
REGR: fix Series construction from dict for subclasses (#52516)
1 parent 8932173 commit 4adc5fa

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
@@ -561,12 +561,7 @@ def _init_dict(
561561
keys, values = (), []
562562

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

571566
# Now we just make sure the order is respected, if any
572567
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)