[Numpy-discussion] clip() with None as argument

Chris Ball ceball@gmail....
Wed Sep 8 14:42:25 CDT 2010

Robert Kern <robert.kern <at> gmail.com> writes:

> On Tue, Sep 7, 2010 at 15:12, Friedrich Romstedt
> <friedrichromstedt <at> gmail.com> wrote:
> > Ah, no need to answer, I do this myself:
> >
> > Friedrich, would you please use numpy.inf and -numpy.inf.
> But if you have an integer array, you will run into the same problem.
> The result will be upcast to float. I think we would accept a patch
> that interprets None to be the appropriate extreme bound given the
> input datatype. This will be tricky, though, since it must be done in
> C (PyArray_Clip in numpy/core/src/multiarray/calculation.c).

I've been a bit confused about what numpy's clip is
supposed to support. The documentation
(e.g. http://docs.scipy.org/doc/numpy/reference/generated/numpy.clip.html)
doesn't make any mention of supporting only one of min or max, but I
remember a message some time back from Travis Oliphant that seemed to
suggest it does

Right now, I'm surprised by two aspect's of clip's behavior, both
demonstrated below...

$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> numpy.__version__
>>> a = numpy.array([1,2,3,4,5])
>>> a.clip(2,None)
array([2, 2, 2, 2, 2], dtype=object)

I'm not sure why the returned array has a dtype of object (although that can be 
avoided by using clip's "out" argument), and also
why the values are not like this:

>>> numpy.maximum(a,2)
array([2, 2, 3, 4, 5])

I've been using minimum() and maximum() instead of clip() to have "clip upper" 
and "clip lower" functions, so it's not really a problem
for me, I'm just curious (and thought maybe this could be useful for
others searching in the future).


More information about the NumPy-Discussion mailing list