[Numpy-discussion] Difference between shape=() and shape=(1,)
Keith Goodman
kwgoodman@gmail....
Tue Jul 13 13:09:04 CDT 2010
On Tue, Jul 13, 2010 at 10:45 AM, Kurt Smith <kwmsmith@gmail.com> wrote:
> You could make use of np.atleast_1d, and then everything would be canonicalized:
>
> In [33]: a = np.array(np.nan)
>
> In [34]: a
> Out[34]: array(nan)
>
> In [35]: a1d = np.atleast_1d(a)
>
> In [36]: a1d
> Out[36]: array([ NaN])
>
> In [37]: a
> Out[37]: array(nan)
>
> In [38]: a1d.base is a
> Out[38]: True
>
> In [39]: a1d[np.isnan(a1d)] = 0.
>
> In [40]: a1d
> Out[40]: array([ 0.])
>
> In [41]: a
> Out[41]: array(0.0)
>
> So Keith's nan_replace would be:
>
> In [42]: def nan_replace(a, fill=0.0):
> ....: a_ = np.atleast_1d(a)
> ....: a_[np.isnan(a_)] = fill
> ....:
Neat. The docstring for atleast_1d says "Copies are made only if
necessary". I don't know when a copy is necessay, but watch out for
it.
More information about the NumPy-Discussion
mailing list