Skip to content
Advertisement

Why can’t I replace Ellipsis using `pd.DataFrame.replace`?

I have this following pd.DataFrame:

>>> df = pd.DataFrame({'a': [1, 2, 3, 4], 'b': ['a', 'b', 'c', 'd'], 'c': [1.2, 3.4, 5.6, 7.8], 'd': [..., ..., ..., ...]})
>>> df
   a  b    c         d
0  1  a  1.2  Ellipsis
1  2  b  3.4  Ellipsis
2  3  c  5.6  Ellipsis
3  4  d  7.8  Ellipsis
>>> 

I am trying to replace the Ellipsis with 1.

I know I could do something like:

>>> df.mask(df == Ellipsis, 1)
   a  b    c  d
0  1  a  1.2  1
1  2  b  3.4  1
2  3  c  5.6  1
3  4  d  7.8  1
>>> 

But, for some reason. If I do:

df.replace(..., 1)

Or:

df.replace(Ellipsis, 1)

I get the following error:

TypeError: Expecting 'to_replace' to be either a scalar, array-like, dict or None, got invalid type 'ellipsis'

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:

>>> df.replace(1, ...)
          a  b    c         d
0  Ellipsis  a  1.2  Ellipsis
1         2  b  3.4  Ellipsis
2         3  c  5.6  Ellipsis
3         4  d  7.8  Ellipsis
>>> 

The even stranger thing here mentioned by @jezrael and @phœnix is that:

df.replace({Ellipsis: 1})

Also:

df.replace({...: 1})

As well as:

df.replace([Ellipsis], 1)

And:

df.replace([...], 1)

Work as expected!

It gives:

   a  b    c  d
0  1  a  1.2  1
1  2  b  3.4  1
2  3  c  5.6  1
3  4  d  7.8  1

Advertisement

Answer

In my opinion it is bug, so reported BUG: Can’t I replace Ellipsis in DataFrame.replace like scalar #50373 .

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement