@@ -656,26 +656,37 @@ class DataFrame(NDFrame, OpsMixin):
656656 def _constructor (self ) -> Callable [..., DataFrame ]:
657657 return DataFrame
658658
659- def _constructor_from_mgr (self , mgr , axes ):
660- if self ._constructor is DataFrame :
661- # we are pandas.DataFrame (or a subclass that doesn't override _constructor)
662- return DataFrame ._from_mgr (mgr , axes = axes )
663- else :
664- assert axes is mgr .axes
659+ def _constructor_from_mgr (self , mgr , axes ) -> DataFrame :
660+ df = DataFrame ._from_mgr (mgr , axes = axes )
661+
662+ if type (self ) is DataFrame :
663+ # This would also work `if self._constructor is DataFrame`, but
664+ # this check is slightly faster, benefiting the most-common case.
665+ return df
666+
667+ elif type (self ).__name__ == "GeoDataFrame" :
668+ # Shim until geopandas can override their _constructor_from_mgr
669+ # bc they have different behavior for Managers than for DataFrames
665670 return self ._constructor (mgr )
666671
672+ # We assume that the subclass __init__ knows how to handle a
673+ # pd.DataFrame object.
674+ return self ._constructor (df )
675+
667676 _constructor_sliced : Callable [..., Series ] = Series
668677
669- def _sliced_from_mgr (self , mgr , axes ) -> Series :
670- return Series ._from_mgr (mgr , axes )
678+ def _constructor_sliced_from_mgr (self , mgr , axes ) -> Series :
679+ ser = Series ._from_mgr (mgr , axes )
680+ ser ._name = None # caller is responsible for setting real name
671681
672- def _constructor_sliced_from_mgr (self , mgr , axes ):
673- if self ._constructor_sliced is Series :
674- ser = self ._sliced_from_mgr (mgr , axes )
675- ser ._name = None # caller is responsible for setting real name
682+ if type (self ) is DataFrame :
683+ # This would also work `if self._constructor_sliced is Series`, but
684+ # this check is slightly faster, benefiting the most-common case.
676685 return ser
677- assert axes is mgr .axes
678- return self ._constructor_sliced (mgr )
686+
687+ # We assume that the subclass __init__ knows how to handle a
688+ # pd.Series object.
689+ return self ._constructor_sliced (ser )
679690
680691 # ----------------------------------------------------------------------
681692 # Constructors
@@ -1403,7 +1414,8 @@ def _get_values_for_csv(
14031414 na_rep = na_rep ,
14041415 quoting = quoting ,
14051416 )
1406- return self ._constructor_from_mgr (mgr , axes = mgr .axes )
1417+ # error: Incompatible return value type (got "DataFrame", expected "Self")
1418+ return self ._constructor_from_mgr (mgr , axes = mgr .axes ) # type: ignore[return-value]
14071419
14081420 # ----------------------------------------------------------------------
14091421
@@ -5077,7 +5089,8 @@ def predicate(arr: ArrayLike) -> bool:
50775089 return True
50785090
50795091 mgr = self ._mgr ._get_data_subset (predicate ).copy (deep = None )
5080- return self ._constructor_from_mgr (mgr , axes = mgr .axes ).__finalize__ (self )
5092+ # error: Incompatible return value type (got "DataFrame", expected "Self")
5093+ return self ._constructor_from_mgr (mgr , axes = mgr .axes ).__finalize__ (self ) # type: ignore[return-value]
50815094
50825095 def insert (
50835096 self ,
0 commit comments