Skip to content

Commit c68a96f

Browse files
authored
PERF: DatetimeIndex constructor from list (#48609)
1 parent 5de2448 commit c68a96f

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

asv_bench/benchmarks/ctors.py

+25
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
MultiIndex,
77
Series,
88
Timestamp,
9+
date_range,
910
)
1011

1112
from .pandas_vb_common import tm
@@ -121,4 +122,28 @@ def time_multiindex_from_iterables(self):
121122
MultiIndex.from_product(self.iterables)
122123

123124

125+
class DatetimeIndexConstructor:
126+
def setup(self):
127+
128+
N = 20_000
129+
dti = date_range("1900-01-01", periods=N)
130+
131+
self.list_of_timestamps = dti.tolist()
132+
self.list_of_dates = dti.date.tolist()
133+
self.list_of_datetimes = dti.to_pydatetime().tolist()
134+
self.list_of_str = dti.strftime("%Y-%m-%d").tolist()
135+
136+
def time_from_list_of_timestamps(self):
137+
DatetimeIndex(self.list_of_timestamps)
138+
139+
def time_from_list_of_dates(self):
140+
DatetimeIndex(self.list_of_dates)
141+
142+
def time_from_list_of_datetimes(self):
143+
DatetimeIndex(self.list_of_datetimes)
144+
145+
def time_from_list_of_str(self):
146+
DatetimeIndex(self.list_of_str)
147+
148+
124149
from .pandas_vb_common import setup # noqa: F401 isort:skip

doc/source/whatsnew/v1.6.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ Performance improvements
137137
- Performance improvement in :meth:`MultiIndex.isin` when ``level=None`` (:issue:`48622`)
138138
- Performance improvement for :meth:`Series.value_counts` with nullable dtype (:issue:`48338`)
139139
- Performance improvement for :class:`Series` constructor passing integer numpy array with nullable dtype (:issue:`48338`)
140+
- Performance improvement for :class:`DatetimeIndex` constructor passing a list (:issue:`48609`)
140141
- Performance improvement in :func:`merge` and :meth:`DataFrame.join` when joining on a sorted :class:`MultiIndex` (:issue:`48504`)
141142
- Performance improvement in :meth:`DataFrame.loc` and :meth:`Series.loc` for tuple-based indexing of a :class:`MultiIndex` (:issue:`48384`)
142143
- Performance improvement for :meth:`MultiIndex.unique` (:issue:`48335`)

pandas/core/arrays/datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2153,7 +2153,7 @@ def factorize( # type:ignore[override]
21532153
def ensure_arraylike_for_datetimelike(data, copy: bool, cls_name: str):
21542154
if not hasattr(data, "dtype"):
21552155
# e.g. list, tuple
2156-
if np.ndim(data) == 0:
2156+
if not isinstance(data, (list, tuple)) and np.ndim(data) == 0:
21572157
# i.e. generator
21582158
data = list(data)
21592159
data = np.asarray(data)

0 commit comments

Comments
 (0)