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

AttributeError 'Series' object has no attribute 'to_numeric'

I have A pandas dataframe, and I want to change the content of a column, depending on its current value. If the record has the value ‘INFINITY’, assign a constant, elsewhere, assign its current value casted to number. This is my code so far:

data_frame['my_column'] = np.where(data_frame['my_column'] == 'INFINITY', 999999999999, data_frame['my_column'].to_numeric())

The problem is that this code raises this exception:

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

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
C:\Users\HUGO~1.VIL\AppData\Local\Temp/ipykernel_28924/1872682007.py in <module>
----> 1 data_frama['my_column'] = np.where(data_frame['my_column'] == 'INFINITY', 999999999999, data_frame['my_column'].to_numeric())

~\Documents\code\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   5485         ):
   5486             return self[name]
-> 5487         return object.__getattribute__(self, name)
   5488 
   5489     def __setattr__(self, name: str, value) -> None:

AttributeError: 'Series' object has no attribute 'to_numeric'

How can I instruct NumPy where to take the current value and cast it to number?

>Solution :

Your issue had nothing to do with where. to_numeric is not a valid Series method. However, the top level pandas.to_numeric method exists.

Thus, you should replace data_frame['my_column'].to_numeric() with:

import pandas as pd
pd.to_numeric(data_frame['my_column'])

In your context:

data_frame['my_column'] = np.where(data_frame['my_column'] == 'INFINITY', 999999999999, pd.to_numeric(data_frame['my_column']))
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