PERF: DataFrame.clip / Series.clip by lukemanley · Pull Request #51472 · pandas-dev/pandas (original) (raw)

This moves away from CoW syntax now:

Both objects should share memory, if clip is a no-op:

df = DataFrame({"a": [1.5, 2, 3]})
df_copy = df.copy()
arr_a = get_array(df, "a")
view = df[:]
df.clip(lower=1, inplace=True)
print(np.shares_memory(get_array(df, "a"), arr_a))

Also with inplace and without references, this can still modify the existing array inplace.

Your current pr avoids this (we don't have tests for this yet, because we did not get to it and we covered where extensively :))

Additionally, this introduces bugs when setting other dtypes:

df = DataFrame({"a": [1, 2, 3]})
df = df.clip(lower=1.5)

This is a no-op on this branch but works on main (should probably add a test)

In general, I'd prefer doing this on the block level. This keeps CoW handling in more or less on place.