Skip to content

PERF: object-dtype iloc setitem with datetimelike list-like indexers is slow on large arrays #64250

Description

@hyoj0942

Pandas version checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this issue exists on the latest version of pandas.
  • I have confirmed this issue exists on the main branch of pandas.

Reproducible Example

import numpy as np
import pandas as pd
from time import perf_counter


def bench(nrows: int, indexer_kind: str, selection_fraction: float, repeat: int = 3) -> float:
    n_set = max(1, int(nrows * selection_fraction))
    take_idx = np.random.default_rng(0).choice(nrows, size=n_set, replace=False)

    if indexer_kind == "bool":
        indexer = np.zeros(nrows, dtype=bool)
        indexer[take_idx] = True
    else:
        indexer = take_idx

    value = np.datetime64("2020-01-01", "ns")
    times_ms = []
    for _ in range(repeat):
        ser = pd.Series(np.empty(nrows, dtype=object))
        t0 = perf_counter()
        ser.iloc[indexer] = value
        times_ms.append((perf_counter() - t0) * 1000)

    return min(times_ms)


for case in [
    (1_000_000, "bool", 0.01),
    (1_000_000, "bool", 0.5),
    (10_000_000, "bool", 0.01),
    (10_000_000, "ndarray", 0.01),
]:
    print(case, f"{bench(*case):.1f} ms")

On pandas main (d2cde1425f0a428bacec3e1ac0840a76efddc05f), the run is consistently slow for large object arrays with list-like indexers.

Representative output from the same setup (main branch):

  • (1_000_000, "bool", 0.01) -> ~0.96 ms
  • (1_000_000, "bool", 0.5) -> ~15.2 ms
  • (10_000_000, "bool", 0.01) -> ~15.9 ms
  • (10_000_000, "ndarray", 0.01) -> ~3.18 ms

Installed Versions

Details

INSTALLED VERSIONS

commit : d2cde14
python : 3.13.9
python-bits : 64
OS : Darwin
OS-release : 25.2.0
Version : Darwin Kernel Version 25.2.0: Tue Nov 18 21:09:56 PST 2025; root:xnu-12377.61.12~1/RELEASE_ARM64_T6041
machine : arm64
processor : arm
byteorder : little
LC_ALL : C.UTF-8
LANG : ko_KR.UTF-8
LOCALE : C.UTF-8

pandas : 3.1.0.dev0+196.gd2cde1425f.dirty
numpy : 2.3.5
dateutil : 2.9.0.post0
pip : 25.3
Cython : 3.2.4
sphinx : 8.2.3
IPython : 9.7.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.13.5
bottleneck : 1.4.2
fastparquet : 2025.12.0
fsspec : 2026.2.0
html5lib : 1.1
hypothesis : 6.151.9
gcsfs : 2026.2.0
jinja2 : 3.1.6
lxml.etree : 5.3.0
matplotlib : 3.10.6
numba : 0.62.1
numexpr : 2.14.1
odfpy : None
openpyxl : 3.1.5
psycopg2 : 2.9.11
pymysql : 1.4.6
pyarrow : 21.0.0
pyiceberg : 0.11.0
pyreadstat : 1.3.3
pytest : 8.4.2
python-calamine : None
pytz : 2025.2
pyxlsb : 1.0.10
s3fs : 2026.2.0
scipy : 1.16.3
sqlalchemy : 2.0.43
tables : 3.10.2
tabulate : 0.9.0
xarray : 2025.10.1
xlrd : 2.0.2
xlsxwriter : 3.2.9
zstandard : 0.24.0
qtpy : 2.4.3
pyqt5 : None

Prior Performance

I have not identified a prior pandas release where this path is materially faster.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DatetimeDatetime data dtypeIndexingRelated to indexing on series/frames, not to indexes themselvesPerformanceMemory or execution speed performance

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions