I have this following pd.DataFrame
:
JavaScript
x
9
1
>>> df = pd.DataFrame({'a': [1, 2, 3, 4], 'b': ['a', 'b', 'c', 'd'], 'c': [1.2, 3.4, 5.6, 7.8], 'd': [ , , , ]})
2
>>> df
3
a b c d
4
0 1 a 1.2 Ellipsis
5
1 2 b 3.4 Ellipsis
6
2 3 c 5.6 Ellipsis
7
3 4 d 7.8 Ellipsis
8
>>>
9
I am trying to replace the Ellipsis with 1
.
I know I could do something like:
JavaScript
1
8
1
>>> df.mask(df == Ellipsis, 1)
2
a b c d
3
0 1 a 1.2 1
4
1 2 b 3.4 1
5
2 3 c 5.6 1
6
3 4 d 7.8 1
7
>>>
8
But, for some reason. If I do:
JavaScript
1
2
1
df.replace( , 1)
2
Or:
JavaScript
1
2
1
df.replace(Ellipsis, 1)
2
I get the following error:
JavaScript
1
2
1
TypeError: Expecting 'to_replace' to be either a scalar, array-like, dict or None, got invalid type 'ellipsis'
2
Why doesn’t replace
allow me to replace Ellipsis
?
I know how to fix it, I want to know why this happens.
The strange thing here is that, I actually can replace numbers with Ellipsis, but not vice versa.
Example:
JavaScript
1
8
1
>>> df.replace(1, )
2
a b c d
3
0 Ellipsis a 1.2 Ellipsis
4
1 2 b 3.4 Ellipsis
5
2 3 c 5.6 Ellipsis
6
3 4 d 7.8 Ellipsis
7
>>>
8
The even stranger thing here mentioned by @jezrael and @phœnix is that:
JavaScript
1
2
1
df.replace({Ellipsis: 1})
2
Also:
JavaScript
1
2
1
df.replace({ : 1})
2
As well as:
JavaScript
1
2
1
df.replace([Ellipsis], 1)
2
And:
JavaScript
1
2
1
df.replace([ ], 1)
2
Work as expected!
It gives:
JavaScript
1
6
1
a b c d
2
0 1 a 1.2 1
3
1 2 b 3.4 1
4
2 3 c 5.6 1
5
3 4 d 7.8 1
6
Advertisement
Answer
In my opinion it is bug, so reported BUG: Can’t I replace Ellipsis in DataFrame.replace like scalar #50373 .