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

Robert Kern robert.kern@gmail....
Wed Sep 8 15:30:25 CDT 2010

On Wed, Sep 8, 2010 at 14:42, Chris Ball <ceball@gmail.com> wrote:
> 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
> (http://thread.gmane.org/gmane.comp.python.numeric.general/17844/focus=17877).
> 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__
> '2.0.0.dev8698'
>>>> 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

The usual type-matching semantics. a.clip() tries to find a common
type that fits `a`, 2 and None. Since None only fits in an object
dtype, that's what you get.

> (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])

clip(a, low, high) boils down to this in the C code itself (or at
least the code path that it goes down when the input array needs to be

  maximum(minimum(a, high), low)

Since all integer objects compare > None based on the default Python
2.x implementation of comparison across types, you would just get the
low value out of that.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco

More information about the NumPy-Discussion mailing list