[Numpy-discussion] preferred way of testing empty arrays
Chris Barker
chris.barker@noaa....
Mon Jan 30 18:35:05 CST 2012
On Fri, Jan 27, 2012 at 1:29 PM, Robert Kern <robert.kern@gmail.com> wrote:
> Well, if you really need to do this in more than one place, define a
> utility function and call it a day.
>
> def should_not_plot(x):
> if x is None:
> return True
> elif isinstance(x, np.ndarray):
> return x.size == 0
> else:
> return bool(x)
I tend to do things like:
def convert_to_plotable(x):
if x is None:
return None
else:
x = np.asarray(x)
if b.size == 0:
return None
return x
it does mean you need to check for None later anyway, but I like to
convert to an array early in the process -- then you know you have
either an array or None at that point.
NOTE: you could also raise and handle an exception instead.
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov
More information about the NumPy-Discussion
mailing list