Skip to content

Commit fa28ede

Browse files
authored
PERF: fastpath DataFrame constructor from BlockManager (#33357)
1 parent 9ce0057 commit fa28ede

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

pandas/core/apply.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,8 @@ def get_result(self):
166166
elif isinstance(self.f, np.ufunc):
167167
with np.errstate(all="ignore"):
168168
results = self.obj._mgr.apply("apply", func=self.f)
169-
return self.obj._constructor(
170-
data=results, index=self.index, columns=self.columns, copy=False
171-
)
169+
# _constructor will retain self.index and self.columns
170+
return self.obj._constructor(data=results)
172171

173172
# broadcasting
174173
if self.result_type == "broadcast":

pandas/core/frame.py

+5
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,11 @@ def __init__(
434434
data = data._mgr
435435

436436
if isinstance(data, BlockManager):
437+
if index is None and columns is None and dtype is None and copy is False:
438+
# GH#33357 fastpath
439+
NDFrame.__init__(self, data)
440+
return
441+
437442
mgr = self._init_mgr(
438443
data, axes=dict(index=index, columns=columns), dtype=dtype, copy=copy
439444
)

pandas/core/generic.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,13 @@ def __init__(
213213
object.__setattr__(self, "_attrs", attrs)
214214

215215
@classmethod
216-
def _init_mgr(cls, mgr, axes=None, dtype=None, copy=False):
216+
def _init_mgr(cls, mgr, axes, dtype=None, copy: bool = False) -> BlockManager:
217217
""" passed a manager and a axes dict """
218218
for a, axe in axes.items():
219219
if axe is not None:
220-
mgr = mgr.reindex_axis(
221-
axe, axis=cls._get_block_manager_axis(a), copy=False
222-
)
220+
axe = ensure_index(axe)
221+
bm_axis = cls._get_block_manager_axis(a)
222+
mgr = mgr.reindex_axis(axe, axis=bm_axis, copy=False)
223223

224224
# make a copy if explicitly requested
225225
if copy:

pandas/core/series.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,17 @@ def __init__(
204204
self, data=None, index=None, dtype=None, name=None, copy=False, fastpath=False
205205
):
206206

207+
if (
208+
isinstance(data, SingleBlockManager)
209+
and index is None
210+
and dtype is None
211+
and copy is False
212+
):
213+
# GH#33357 called with just the SingleBlockManager
214+
NDFrame.__init__(self, data)
215+
self.name = name
216+
return
217+
207218
# we are called internally, so short-circuit
208219
if fastpath:
209220

@@ -827,9 +838,8 @@ def take(self, indices, axis=0, is_copy=None, **kwargs) -> "Series":
827838
new_index = self.index.take(indices)
828839
new_values = self._values.take(indices)
829840

830-
return self._constructor(
831-
new_values, index=new_index, fastpath=True
832-
).__finalize__(self, method="take")
841+
result = self._constructor(new_values, index=new_index, fastpath=True)
842+
return result.__finalize__(self, method="take")
833843

834844
def _take_with_is_copy(self, indices, axis=0):
835845
"""

0 commit comments

Comments
 (0)