It's technically possible, but I'm not sure it meets all your requirements.
To the best of my knowledge, pandas doesn’t offer an assign operation without a deep copy.
However, it does allow you to create a new DataFrame without making a deep copy.
import numpy as np
import pandas as pd
arr1 = np.arange(5) # Result of computation #1.
# Create a new DataFrame without copying.
df = pd.DataFrame({"arr1": arr1}, copy=False)
# Verify it’s a shallow copy.
arr1[0] = 7
print(df)
arr1
0 7
1 1
2 2
3 3
4 4
You can also create a new DataFrame from an existing one.
Although this isn’t in-place, it produces the same result as an assign.
arr2 = np.ones(5, dtype=np.float32) # Result of computation #2.
# Create a new DataFrame from the existing one without copying.
df = pd.DataFrame({**{c: df[c] for c in df}, "arr2": arr2}, copy=False)
# Verify it’s a shallow copy.
arr1[0] = 7
arr2[0] = 8
print(df)
arr1 arr2
0 7 8.0
1 1 1.0
2 2 1.0
3 3 1.0
4 4 1.0
One important thing to note is that the copy=False option does NOT guarantee that no deep copy will be made.
For example, if the data is a Python list rather than a numpy array, it just makes the copy silently.
So, you need to take extra care to avoid passing such an object.
Another option is to first create a column in the DataFrame and write the results directly there.
arr1 = np.arange(5) # Result of computation #1.
# Create the output buffer in the data frame first.
df = pd.DataFrame({"arr1": arr1, "arr2": np.zeros_like(arr1, dtype=np.float32)})
# Use the out argument to directly write out the calculation results.
# Most numpy operations support the out argument.
np.power(arr1, 2, out=df["arr2"].array)
print(df)
Although this is not a direct answer to your question, it may offer better performance.
arrafter you have made a copy? Usedel arrorarr = whatever_elseto deletearrand thus free up the memory? Maybe also useimport gcand dogc.collect()afterdel arr.