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

Robert Kern robert.kern@gmail....
Thu Sep 9 08:44:28 CDT 2010

On Thu, Sep 9, 2010 at 05:05, Chris Ball <ceball@gmail.com> wrote:
> Robert Kern <robert.kern <at> gmail.com> writes:
>> On Wed, Sep 8, 2010 at 14:42, Chris Ball <ceball <at> gmail.com> wrote:
>> > Robert Kern <robert.kern <at> gmail.com> writes:
>> >
> ...
>> >>>> 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.
> ...
>> > 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
>> upcast):
>>   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.
> Thanks for explaining that. It all makes sense if clip() requires both bounds to
> be passed, which is what the documentation indicates, so there is not really a
> problem. I was just confused because of the post and associated commit from
> Travis a while back implying that he'd altered clip() to support only one bound
> ("Allow clip method to have either min or max passed in", 4566).

I believe that the clip function was overhauled sometime after that.
The enhancement probably got lost then.

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