@@ -656,26 +656,37 @@ class DataFrame(NDFrame, OpsMixin):
656
656
def _constructor (self ) -> Callable [..., DataFrame ]:
657
657
return DataFrame
658
658
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
665
670
return self ._constructor (mgr )
666
671
672
+ # We assume that the subclass __init__ knows how to handle a
673
+ # pd.DataFrame object.
674
+ return self ._constructor (df )
675
+
667
676
_constructor_sliced : Callable [..., Series ] = Series
668
677
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
671
681
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.
676
685
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 )
679
690
680
691
# ----------------------------------------------------------------------
681
692
# Constructors
@@ -1403,7 +1414,8 @@ def _get_values_for_csv(
1403
1414
na_rep = na_rep ,
1404
1415
quoting = quoting ,
1405
1416
)
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]
1407
1419
1408
1420
# ----------------------------------------------------------------------
1409
1421
@@ -5077,7 +5089,8 @@ def predicate(arr: ArrayLike) -> bool:
5077
5089
return True
5078
5090
5079
5091
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]
5081
5094
5082
5095
def insert (
5083
5096
self ,
0 commit comments