[Numpy-discussion] Should ndarray subclasses support the keepdims arg?

Nathaniel Smith njs at pobox.com
Tue May 5 13:55:07 EDT 2015

AFAICT the only real solution here is for np.sum and friends to propagate
the keepdims argument if and only if it was explicitly passed to them (or
maybe the slightly different, if and only if it has a non-default value).
If we just started requiring code to handle it and passing it
unconditionally, then as soon as someone upgraded numpy all their existing
code might break for no good reason.
On May 5, 2015 8:13 AM, "Allan Haldane" <allanhaldane at gmail.com> wrote:

> Hello all,
> A question:
> Many ndarray methods (eg sum, mean, any, min) have a "keepdims" keyword
> argument, but ndarray subclass methods sometimes don't. The 'matrix'
> subclass doesn't, and numpy functions like 'np.sum' intentionally
> drop/ignore the keepdims argument when called with an ndarray subclass
> as first argument.
> This means you can't always use ndarray subclasses as 'drop in'
> replacement for ndarrays if the code uses keepdims (even indirectly),
> and it means code that deals with keepdims (eg np.sum and more) has to
> detect ndarray subclasses and drop keepdims even if the subclass
> supports it (since there is no good way to detect support). It seems to
> me that if we are going to use inheritance, subclass methods should keep
> the signature of the parent class methods. What does the list think?
> ---- Details: ----
> This problem comes up in a PR I'm working on (#5706) to add the keepdims
> arg to masked array methods. In order to support masked matrices (which
> a lot of unit tests check), I would have to detect and drop the keepdims
> arg to avoid an exception. This would be solved if the matrix class
> supported keepdims (plus an update to np.sum). Similarly,
> `np.sum(mymaskedarray, keepdims=True)` does not respect keepdims, but it
> could work if all subclasses supported keepdims.
> I do not foresee immediate problems with adding keepdims to the matrix
> methods, except that it would be an unused argument. Modifying `np.sum`
> to always pass on the keepdims arg is trickier, since it would break any
> code that tried to np.sum a subclass that doesn't support keepdims, eg
> pandas.DataFrame. **kwargs tricks might work. But if it's permissible I
> think it would be better to require subclasses to support all the
> keyword args ndarray supports.
> Allan
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150505/463a1c17/attachment.html>

More information about the NumPy-Discussion mailing list