narwhals-dev / narwhals

Lightweight and extensible compatibility layer between dataframe libraries!
https://narwhals-dev.github.io/narwhals/
MIT License
592 stars 89 forks source link

perf: make `maybe_reset_index` a no-op if object has default index #1244

Closed MarcoGorelli closed 3 weeks ago

MarcoGorelli commented 3 weeks ago

In pandas < 3.0 without copy-on-write enabled, reset_index always creates a copy. This makes sense in pandas, as the pandas api has quite a few in-place methods

In the Narwhals API, we have zero in-place methods, so we can optimise maybe_reset_index by doing an early return of the object if:

This should help with performance in the plotly PR