PERF: copy cached attributes on extension index shallow_copy by topper-123 · Pull Request #32640 · pandas-dev/pandas (original) (raw)
Follow-up to #32568.
Copies ._cache
also when copying using .shallow_copy
for:
- CategoricalIndex
- DatetimeIndex
- PeriodIndex
- DateTimeIndex
- IntervalIndex
After this PR, only MultiIndex._shallow_copy
is missing this optimization. MultiIndex._shallow_copy
is a bit special and might require a refactor so I'd like to take that in a seperate PR.
Example performance improvement:
>>> idx = pd.CategoricalIndex(np.arange(100_000))
>>> %timeit idx.get_loc(99_999)
4.46 µs ± 62.6 ns per loop # master and this PR
>>> %timeit idx._shallow_copy().get_loc(99_999)
4.19 ms ± 117 µs per loop # master
8.58 µs ± 254 ns per loop # this PR