diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 70a5ac69011d1..1cde399149fc6 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -658,7 +658,6 @@ def _sliced_from_mgr(self, mgr, axes) -> Series: def _constructor_sliced_from_mgr(self, mgr, axes): ser = self._sliced_from_mgr(mgr, axes=axes) - ser._name = None # caller is responsible for setting real name if type(self) is DataFrame: # fastpath avoiding constructor call return ser diff --git a/pandas/core/series.py b/pandas/core/series.py index 724a54c85da9b..d94f6c6b24255 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -172,6 +172,7 @@ IndexKeyFunc, IndexLabel, Level, + Manager, MutableMappingT, NaPosition, NumpySorter, @@ -574,6 +575,27 @@ def __init__( self.name = name self._set_axis(0, index) + @classmethod + def _from_mgr(cls, mgr: Manager, axes: list[Index]) -> Self: + """ + Construct a new Series from a Manager object and axes. + + Parameters + ---------- + mgr : Manager + Must have the same ndim as cls. + axes : list[Index] + + Notes + ----- + The axes must match mgr.axes, but are required for future-proofing + in the event that axes are refactored out of the Manager objects. + """ + obj = cls.__new__(cls) + NDFrame.__init__(obj, mgr) + object.__setattr__(obj, "_name", None) + return obj + def _init_dict( self, data, index: Index | None = None, dtype: DtypeObj | None = None ): @@ -633,7 +655,6 @@ def _constructor(self) -> Callable[..., Series]: def _constructor_from_mgr(self, mgr, axes): ser = self._from_mgr(mgr, axes=axes) - ser._name = None # caller is responsible for setting real name if type(self) is Series: # fastpath avoiding constructor call return ser