Skip to content

Commit 5788685

Browse files
authored
REF: simplify _from_mgr constructors, use in more places (#53871)
1 parent eca8fa1 commit 5788685

File tree

5 files changed

+37
-29
lines changed

5 files changed

+37
-29
lines changed

pandas/core/frame.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -634,23 +634,28 @@ def _constructor(self) -> Callable[..., DataFrame]:
634634
return DataFrame
635635

636636
def _constructor_from_mgr(self, mgr, axes):
637-
if self._constructor is DataFrame:
638-
# we are pandas.DataFrame (or a subclass that doesn't override _constructor)
639-
return self._from_mgr(mgr, axes=axes)
637+
df = self._from_mgr(mgr, axes=axes)
638+
639+
if type(self) is DataFrame:
640+
# fastpath avoiding constructor call
641+
return df
640642
else:
641643
assert axes is mgr.axes
642-
return self._constructor(mgr)
644+
return self._constructor(df, copy=False)
643645

644646
_constructor_sliced: Callable[..., Series] = Series
645647

646648
def _sliced_from_mgr(self, mgr, axes) -> Series:
647649
return Series._from_mgr(mgr, axes)
648650

649651
def _constructor_sliced_from_mgr(self, mgr, axes):
650-
if self._constructor_sliced is Series:
651-
return self._sliced_from_mgr(mgr, axes)
652+
ser = self._sliced_from_mgr(mgr, axes=axes)
653+
ser._name = None # caller is responsible for setting real name
654+
if type(self) is DataFrame:
655+
# fastpath avoiding constructor call
656+
return ser
652657
assert axes is mgr.axes
653-
return self._constructor_sliced(mgr)
658+
return self._constructor_sliced(ser, copy=False)
654659

655660
# ----------------------------------------------------------------------
656661
# Constructors
@@ -11765,12 +11770,10 @@ def isin_(x):
1176511770
)
1176611771
return result.reshape(x.shape)
1176711772

11768-
res_values = self._mgr.apply(isin_)
11769-
result = self._constructor(
11770-
res_values,
11771-
self.index,
11772-
self.columns,
11773-
copy=False,
11773+
res_mgr = self._mgr.apply(isin_)
11774+
result = self._constructor_from_mgr(
11775+
res_mgr,
11776+
axes=res_mgr.axes,
1177411777
)
1177511778
return result.__finalize__(self, method="isin")
1177611779

pandas/core/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def _as_manager(self, typ: str, copy: bool_t = True) -> Self:
319319
new_mgr: Manager
320320
new_mgr = mgr_to_mgr(self._mgr, typ=typ, copy=copy)
321321
# fastpath of passing a manager doesn't check the option/manager class
322-
return self._constructor(new_mgr).__finalize__(self)
322+
return self._constructor_from_mgr(new_mgr, axes=new_mgr.axes).__finalize__(self)
323323

324324
@classmethod
325325
def _from_mgr(cls, mgr: Manager, axes: list[Index]) -> Self:
@@ -6919,7 +6919,7 @@ def _pad_or_backfill(
69196919
inplace=inplace,
69206920
downcast=downcast,
69216921
)
6922-
result = self._constructor(new_mgr)
6922+
result = self._constructor_from_mgr(new_mgr, axes=new_mgr.axes)
69236923
if inplace:
69246924
return self._update_inplace(result)
69256925
else:

pandas/core/groupby/groupby.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1519,11 +1519,11 @@ def _numba_agg_general(
15191519
aggregator = executor.generate_shared_aggregator(
15201520
func, dtype_mapping, **get_jit_arguments(engine_kwargs)
15211521
)
1522-
result = sorted_df._mgr.apply(
1522+
res_mgr = sorted_df._mgr.apply(
15231523
aggregator, start=starts, end=ends, **aggregator_kwargs
15241524
)
1525-
result.axes[1] = self.grouper.result_index
1526-
result = df._constructor(result)
1525+
res_mgr.axes[1] = self.grouper.result_index
1526+
result = df._constructor_from_mgr(res_mgr, axes=res_mgr.axes)
15271527

15281528
if data.ndim == 1:
15291529
result = result.squeeze("columns")

pandas/core/reshape/concat.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,8 @@ def get_result(self):
640640

641641
mgr = type(sample._mgr).from_array(res, index=new_index)
642642

643-
result = cons(mgr, name=name, fastpath=True)
643+
result = sample._constructor_from_mgr(mgr, axes=mgr.axes)
644+
result._name = name
644645
return result.__finalize__(self, method="concat")
645646

646647
# combine as columns in a frame

pandas/core/series.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -575,12 +575,14 @@ def _constructor(self) -> Callable[..., Series]:
575575
return Series
576576

577577
def _constructor_from_mgr(self, mgr, axes):
578-
if self._constructor is Series:
579-
# we are pandas.Series (or a subclass that doesn't override _constructor)
580-
return self._from_mgr(mgr, axes=axes)
578+
ser = self._from_mgr(mgr, axes=axes)
579+
ser._name = None # caller is responsible for setting real name
580+
if type(self) is Series:
581+
# fastpath avoiding constructor call
582+
return ser
581583
else:
582584
assert axes is mgr.axes
583-
return self._constructor(mgr)
585+
return self._constructor(ser, copy=False)
584586

585587
@property
586588
def _constructor_expanddim(self) -> Callable[..., DataFrame]:
@@ -599,15 +601,17 @@ def _expanddim_from_mgr(self, mgr, axes) -> DataFrame:
599601
# once downstream packages (geopandas) have had a chance to implement
600602
# their own overrides.
601603
# error: "Callable[..., DataFrame]" has no attribute "_from_mgr" [attr-defined]
602-
return self._constructor_expanddim._from_mgr( # type: ignore[attr-defined]
603-
mgr, axes=mgr.axes
604-
)
604+
from pandas import DataFrame
605+
606+
return DataFrame._from_mgr(mgr, axes=mgr.axes)
605607

606608
def _constructor_expanddim_from_mgr(self, mgr, axes):
607-
if self._constructor is Series:
608-
return self._expanddim_from_mgr(mgr, axes)
609+
df = self._expanddim_from_mgr(mgr, axes)
610+
if type(self) is Series:
611+
# fastpath avoiding constructor
612+
return df
609613
assert axes is mgr.axes
610-
return self._constructor_expanddim(mgr)
614+
return self._constructor_expanddim(df, copy=False)
611615

612616
# types
613617
@property

0 commit comments

Comments
 (0)