Skip to content

PERF: faster placement creating extension blocks from arrays #32856

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 6 commits into from
Mar 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
36 changes: 36 additions & 0 deletions asv_bench/benchmarks/frame_ctor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np

import pandas as pd
from pandas import DataFrame, MultiIndex, Series, Timestamp, date_range

from .pandas_vb_common import tm
Expand Down Expand Up @@ -118,4 +119,39 @@ def time_frame_from_range(self):
self.df = DataFrame(self.data)


class FromArrays:

goal_time = 0.2

def setup(self):
N_rows = 1000
N_cols = 1000
self.float_arrays = [np.random.randn(N_rows) for _ in range(N_cols)]
self.sparse_arrays = [
pd.arrays.SparseArray(np.random.randint(0, 2, N_rows), dtype="float64")
for _ in range(N_cols)
]
self.int_arrays = [
pd.array(np.random.randint(1000, size=N_rows), dtype="Int64")
for _ in range(N_cols)
]
self.index = pd.Index(range(N_rows))
self.columns = pd.Index(range(N_cols))

def time_frame_from_arrays_float(self):
self.df = DataFrame._from_arrays(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't change this if no other feedback but I don't think you need the assignment here in any of the benchmarks

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True (was only mimicking the other benchmarks in this file)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm this is wrong, but yeah can clean this up in a followup (if you want to create a followup issue or PR to do it)

self.float_arrays, index=self.index, columns=self.columns
)

def time_frame_from_arrays_int(self):
self.df = DataFrame._from_arrays(
self.int_arrays, index=self.index, columns=self.columns
)

def time_frame_from_arrays_sparse(self):
self.df = DataFrame._from_arrays(
self.sparse_arrays, index=self.index, columns=self.columns
)


from .pandas_vb_common import setup # noqa: F401 isort:skip
8 changes: 5 additions & 3 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1780,23 +1780,25 @@ def form_blocks(arrays, names, axes):

if len(items_dict["CategoricalBlock"]) > 0:
cat_blocks = [
make_block(array, klass=CategoricalBlock, placement=[i])
make_block(array, klass=CategoricalBlock, placement=slice(i, i + 1, 1))
for i, _, array in items_dict["CategoricalBlock"]
]
blocks.extend(cat_blocks)

if len(items_dict["ExtensionBlock"]):

external_blocks = [
make_block(array, klass=ExtensionBlock, placement=[i])
make_block(array, klass=ExtensionBlock, placement=slice(i, i + 1, 1))
for i, _, array in items_dict["ExtensionBlock"]
]

blocks.extend(external_blocks)

if len(items_dict["ObjectValuesExtensionBlock"]):
external_blocks = [
make_block(array, klass=ObjectValuesExtensionBlock, placement=[i])
make_block(
array, klass=ObjectValuesExtensionBlock, placement=slice(i, i + 1, 1)
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than changing this here
simply convert a single integer into a slice at a lower level

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, indeed, I can create the slice inside BlockPlacement constructor. But don't we then want to explicitly pass a single integer instead of a list of 1 integer?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we require list / slice

Copy link
Member Author

@jorisvandenbossche jorisvandenbossche Mar 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To compare, I just pushed a commit that does both.
Personally, I like passing it as an integer. It's another 33% faster compared to passing it as a slice or 1-element list, and it makes it also explicit when constructing it that it is about a single column.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, I missed you proposed a single integer yourself (for some reason I thought you wanted to catch the single element list in BlockPlacement). Updated.

for i, _, array in items_dict["ObjectValuesExtensionBlock"]
]

Expand Down