[Numpy-discussion] nan_to_num and bool arrays

Keith Goodman kwgoodman@gmail....
Fri Dec 11 16:09:48 CST 2009


On Fri, Dec 11, 2009 at 1:14 PM, Robert Kern <robert.kern@gmail.com> wrote:
> On Fri, Dec 11, 2009 at 14:41, Keith Goodman <kwgoodman@gmail.com> wrote:
>> On Fri, Dec 11, 2009 at 12:08 PM, Bruce Southey <bsouthey@gmail.com> wrote:
>
>>> So I agree that it should leave the input untouched when a non-float
>>> dtype is used for some array-like input.
>>
>> Would only one line need to be changed? Would changing
>>
>> if not issubclass(t, _nx.integer):
>>
>> to
>>
>> if not issubclass(t, _nx.integer) and not issubclass(t, _nx.bool_):
>>
>> do the trick?
>
> That still leaves strings, voids, and objects. I recommend:
>
>  if issubclass(t, _nx.inexact):
>
> Arguably, one should handle nan float objects in object arrays and
> float columns in structured arrays, but the current code does not
> handle either of those anyways.

Without your change both

>> np.nan_to_num(np.array([True, False]))
>> np.nan_to_num([1])

raise exceptions. With your change:

>> np.nan_to_num(np.array([True, False]))
   array([ True, False], dtype=bool)
>> np.nan_to_num([1])
   array([1])

On a separate note, this seems a little awkward:

>> np.nan_to_num(1.0)
   1.0
>> np.nan_to_num(1)
   array(1)
>> x = np.ones(1, dtype=np.int)
>> np.nan_to_num(x[0])
   1


More information about the NumPy-Discussion mailing list