Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>>> 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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading