I stumbled upon a weird and inconsistent behavior for Pandas replace
function when using it to swap two values of a column. When using it to swap integers in a column we have
df = pd.DataFrame({'A': [0, 1]})
df.A.replace({0: 1, 1: 0})
This yields the result:
df
A
1
0
However, when using the same commands for string values
df = pd.DataFrame({'B': ['a', 'b']})
df.B.replace({'a': 'b', 'b': 'a'})
We get
df
B
'a'
'a'
Can anyone explain me this difference in behavior, or point me to a page in the docs that deals with inconsistencies when using integers and strings in pandas?