Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy pad array with nan, getting strange float instead

Tags:

python

numpy

I'm trying to pad an array with np.nan

import numpy as np
print np.version.version
# 1.10.2
combine = lambda real, theo: np.vstack((theo, np.pad(real, (0, theo.shape[0] - real.shape[0]), 'constant', constant_values=np.nan)))
real = np.arange(20)
theoretical = np.linspace(0, 20, 100)
result = combine(real, theoretical)
np.any(np.isnan(result))
# False

Inspecting result, it seems instead of np.nan, the array is getting padded with -9.22337204e+18. What's going on here? How can I get np.nan?

like image 398
user2561747 Avatar asked Feb 19 '16 21:02

user2561747


1 Answers

The result of pad has the same type as the input. np.nan is a float

In [874]: np.pad(np.ones(2,dtype=int),1,mode='constant',constant_values=(np.nan,))
Out[874]: array([-2147483648,           1,           1, -2147483648])

In [875]: np.pad(np.ones(2,dtype=float),1,mode='constant',constant_values=(np.nan,))
Out[875]: array([ nan,   1.,   1.,  nan])

The int pad is np.nan cast as an integer:

In [878]: np.array(np.nan).astype(int)
Out[878]: array(-2147483648)
like image 157
hpaulj Avatar answered Oct 18 '22 11:10

hpaulj