Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing Inf by NaN results in AttributeError

Tags:

python

pandas

I have a strange problem in Pandas. I want to replace any entry that has np.Inf with the value np.NaN. However, when I do:

df[df == np.Inf] = np.NaN

I get:

AttributeError: 'float' object has no attribute 'view'

The statement that produces the error specifically is:

df == np.Inf

I wonder if the problem is that I am running the above on a Dataframe with mixed types (see dtypes below). But if that is the case, how can I do this replacement still automatically?

In: df.dtypes
Out:
Year                            int64
Week                            int64
item_name                      object
item_uid                       object
Algorithm                      object
item Start                    float64
item 1/4                      float64
item 1/2                      float64
item 3/4                      float64
item Complete                 float64
Daily Nr Impressions          float64
date                   datetime64[ns]
Weekly rate                   float64
dtype: object
like image 725
Amelio Vazquez-Reina Avatar asked Oct 27 '25 06:10

Amelio Vazquez-Reina


1 Answers

You can use df.replace to replace your np.inf values.

In [9]: import pandas as pd

In [10]: df = pd.DataFrame([1, 2, np.inf])

In [11]: df.replace(np.inf, np.nan)
Out[11]:
    0
0   1
1   2
2 NaN

[3 rows x 1 columns]
like image 64
Ffisegydd Avatar answered Oct 28 '25 20:10

Ffisegydd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!