Skip to content

PERF: faster constructors from ea scalars #45854

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 7 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
23 changes: 23 additions & 0 deletions asv_bench/benchmarks/frame_ctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import pandas as pd
from pandas import (
NA,
Categorical,
DataFrame,
Float64Dtype,
MultiIndex,
Series,
Timestamp,
Expand Down Expand Up @@ -138,6 +140,27 @@ def time_frame_from_range(self):
self.df = DataFrame(self.data)


class FromScalar:
def setup(self):
self.nrows = 100_000

def time_frame_from_scalar_ea_float64(self):
DataFrame(
1.0,
index=range(self.nrows),
columns=list("abc"),
dtype=Float64Dtype(),
)

def time_frame_from_scalar_ea_float64_na(self):
DataFrame(
NA,
index=range(self.nrows),
columns=list("abc"),
dtype=Float64Dtype(),
)


class FromArrays:

goal_time = 0.2
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ Performance improvements
- Performance improvement in :meth:`DataFrame.duplicated` when subset consists of only one column (:issue:`45236`)
- Performance improvement in :meth:`.GroupBy.transform` when broadcasting values for user-defined functions (:issue:`45708`)
- Performance improvement in :meth:`.GroupBy.transform` for user-defined functions when only a single group exists (:issue:`44977`)
- Performance improvement in :class:`DataFrame` and :class:`Series` constructors for extension dtype scalars (:issue:`45854`)
-

.. ---------------------------------------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1652,7 +1652,9 @@ def construct_1d_arraylike_from_scalar(

if isinstance(dtype, ExtensionDtype):
cls = dtype.construct_array_type()
subarr = cls._from_sequence([value] * length, dtype=dtype)
subarr = cls._from_sequence([value], dtype=dtype)
taker = np.broadcast_to(np.intp(0), length)
Copy link
Contributor

@jreback jreback Feb 7, 2022

Choose a reason for hiding this comment

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

can we just use .repeat()? (I think we define that generally on EA)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that's even better - just updated. Thanks for pointing it out.

subarr = subarr.take(taker)

else:

Expand Down