This works as expected:
a,b = (0, ) * 2
print('Before')
print(a,b)
a = 1
print('\nAfter')
print(a,b)
# Before
# 0 0
#
# After
# 1 0
It does not work the same way in pandas, but appears that sb
is merely a reference to the same series as sa
:
sa, sb = (pd.Series(np.zeros(2)), ) * 2
print('Before')
for s in (sa,sb):
print(s)
sa[0] = 1
print('\nAfter')
for s in (sa,sb):
print(s)
# Before
# 0 0.0
# 1 0.0
# dtype: float64
# 0 0.0
# 1 0.0
# dtype: float64
# After
# 0 1.0
# 1 0.0
# dtype: float64
# 0 1.0 <- Note: sb[0] has also changed
# 1 0.0
# dtype: float64
Is this the expected behavior, and is it documented? It seems to violate the principle of least astonishment.
What's the most convenient work-around? Obviously this works:
sa = pd.Series(np.zeros(2))
sb = sa.copy() # note deep=True by default
But it's a bit verbose since I need to generate several series.
This does not work:
sa, sb = (pd.Series(np.zeros(2)).copy(), ) * 2