diff --git a/pandas/core/apply.py b/pandas/core/apply.py index a0351cb687d02..a013434491589 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -166,9 +166,8 @@ def get_result(self): elif isinstance(self.f, np.ufunc): with np.errstate(all="ignore"): results = self.obj._mgr.apply("apply", func=self.f) - return self.obj._constructor( - data=results, index=self.index, columns=self.columns, copy=False - ) + # _constructor will retain self.index and self.columns + return self.obj._constructor(data=results) # broadcasting if self.result_type == "broadcast": diff --git a/pandas/core/frame.py b/pandas/core/frame.py index aedbba755227d..aa190c35c4c18 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -434,6 +434,11 @@ def __init__( data = data._mgr if isinstance(data, BlockManager): + if index is None and columns is None and dtype is None and copy is False: + # GH#33357 fastpath + NDFrame.__init__(self, data) + return + mgr = self._init_mgr( data, axes=dict(index=index, columns=columns), dtype=dtype, copy=copy ) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 5a7da0cfb29ab..056ee70b851ec 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -213,13 +213,13 @@ def __init__( object.__setattr__(self, "_attrs", attrs) @classmethod - def _init_mgr(cls, mgr, axes=None, dtype=None, copy=False): + def _init_mgr(cls, mgr, axes, dtype=None, copy: bool = False) -> BlockManager: """ passed a manager and a axes dict """ for a, axe in axes.items(): if axe is not None: - mgr = mgr.reindex_axis( - axe, axis=cls._get_block_manager_axis(a), copy=False - ) + axe = ensure_index(axe) + bm_axis = cls._get_block_manager_axis(a) + mgr = mgr.reindex_axis(axe, axis=bm_axis, copy=False) # make a copy if explicitly requested if copy: diff --git a/pandas/core/series.py b/pandas/core/series.py index c9684d0985173..2f4ca61a402dc 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -204,6 +204,17 @@ def __init__( self, data=None, index=None, dtype=None, name=None, copy=False, fastpath=False ): + if ( + isinstance(data, SingleBlockManager) + and index is None + and dtype is None + and copy is False + ): + # GH#33357 called with just the SingleBlockManager + NDFrame.__init__(self, data) + self.name = name + return + # we are called internally, so short-circuit if fastpath: @@ -827,9 +838,8 @@ def take(self, indices, axis=0, is_copy=None, **kwargs) -> "Series": new_index = self.index.take(indices) new_values = self._values.take(indices) - return self._constructor( - new_values, index=new_index, fastpath=True - ).__finalize__(self, method="take") + result = self._constructor(new_values, index=new_index, fastpath=True) + return result.__finalize__(self, method="take") def _take_with_is_copy(self, indices, axis=0): """