Pandas: Pandas .fillna() should handle "inf"

Created on 12 Feb 2013  路  5Comments  路  Source: pandas-dev/pandas

Division by 0 in pandas will give the value "inf". But the .fillna() method doesn't recognize that. We should make .fillna() handle "inf" the same way it handles "NaN'. (for reference, the numpy.isfinite() method treats NaN and Inf interchangably -- pandas should do the same).

import pandas
vals = [ 1.1, 1.1, 1.1]
p = pandas.DataFrame( { 'first' : vals }, columns=['first'])
p['first'] = p['first'] / 0 #Creates a bunch of "inf" values
p['first'].fillna(0, inplace=True) #Has no effect

Most helpful comment

In [2]: p
Out[2]: 
   first
0    inf
1    inf
2    inf

In [3]: p.replace(np.inf, 0)
Out[3]: 
   first
0      0
1      0
2      0

All 5 comments

Possibly you're looking for

In [37]: pd.describe_option("use_inf_as_null")
mode.use_inf_as_null: 
: boolean
        True means treat None, NaN, INF, -INF as null (old way),
        False means None and NaN are null, but INF, -INF are not null
        (new way).

GH tip: you can quote your snippets to get syntax highlighting. pretty colors.

In [2]: p
Out[2]: 
   first
0    inf
1    inf
2    inf

In [3]: p.replace(np.inf, 0)
Out[3]: 
   first
0      0
1      0
2      0

@wesm and then you have to remember 'method' otherwise it silently fails... nvm, long story
@tavistmorph this whole issue highlights how unpythonic the current implementation is. Adding a kwarg to include np.inf and -np.inf would've been much easier.

So what I want is that: I can change inf to nan or 0 directly not import numpy.

I try to use
pandas.options.mode.use_inf_as_na = True
to set inf to nan,but I got this:
'You can only set the value of existing options'

pd.set_option('use_inf_as_na', True)

```python
pd.describe_option('use_inf_as_na')

```python
mode.use_inf_as_na : boolean
    True means treat None, NaN, INF, -INF as NA (old way),
    False means None and NaN are null, but INF, -INF are not NA
    (new way).
    [default: False] [currently: True]
Was this page helpful?
0 / 5 - 0 ratings