Skip to content

PERF: avoid extraneous extract_array #43147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions asv_bench/benchmarks/frame_ctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pandas as pd
from pandas import (
Categorical,
DataFrame,
MultiIndex,
Series,
Expand Down Expand Up @@ -31,6 +32,9 @@ def setup(self):
self.dict_list = frame.to_dict(orient="records")
self.data2 = {i: {j: float(j) for j in range(100)} for i in range(2000)}

# arrays which we wont consolidate
self.dict_of_categoricals = {i: Categorical(np.arange(N)) for i in range(K)}

def time_list_of_dict(self):
DataFrame(self.dict_list)

Expand All @@ -50,6 +54,10 @@ def time_nested_dict_int64(self):
# nested dict, integer indexes, regression described in #621
DataFrame(self.data2)

def time_dict_of_categoricals(self):
# dict of arrays that we wont consolidate
DataFrame(self.dict_of_categoricals)


class FromSeries:
def setup(self):
Expand Down
1 change: 1 addition & 0 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def arrays_to_mgr(

else:
index = ensure_index(index)
arrays = [extract_array(x, extract_numpy=True) for x in arrays]

# Reached via DataFrame._from_arrays; we do validation here
for arr in arrays:
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1809,21 +1809,21 @@ def create_block_manager_from_blocks(


def create_block_manager_from_column_arrays(
arrays,
arrays: list[ArrayLike],
axes: list[Index],
consolidate: bool = True,
) -> BlockManager:
# Assertions disabled for performance (caller is responsible for verifying)
# assert isinstance(axes, list)
# assert all(isinstance(x, Index) for x in axes)
# assert all(isinstance(x, (np.ndarray, ExtensionArray)) for x in arrays)
# assert all(type(x) is not PandasArray for x in arrays)
# assert all(x.ndim == 1 for x in arrays)
# assert all(len(x) == len(axes[1]) for x in arrays)
# assert len(arrays) == len(axes[0])
# These last three are sufficient to allow us to safely pass
# verify_integrity=False below.

arrays = [extract_array(x, extract_numpy=True) for x in arrays]

try:
blocks = _form_blocks(arrays, consolidate)
mgr = BlockManager(blocks, axes, verify_integrity=False)
Expand Down