pandas
pandas copied to clipboard
PERF: use copy=False in constructor in unstack (CoW)
xref https://github.com/pandas-dev/pandas/issues/57431
I think we are sure that the values
from self.get_new_values(..)
, which we pass here to the DataFrame constructor, are always new values owned by us (not shared by any other object), and so we can safely set copy=False
in that case.
Mimicking the time_unstack
benchmark:
m = 100
n = 1000
levels = np.arange(m)
index = pd.MultiIndex.from_product([levels] * 2)
columns = np.arange(n)
values = np.arange(m * m * n).reshape(m * m, n)
df = pd.DataFrame(values, index, columns)
In [2]: %timeit df.unstack()
316 ms ± 44.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) # main
228 ms ± 25.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) # PR