Skip to content

REF: initialize Series._name in _from_mgr to return valid Series object #55607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
IndexKeyFunc,
IndexLabel,
Level,
Manager,
MutableMappingT,
NaPosition,
NumpySorter,
Expand Down Expand Up @@ -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
):
Expand Down Expand Up @@ -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
Expand Down