Thanks to visit codestin.com
Credit goes to github.com

Skip to content

mean() for datetime64 #12901

Open
Open
@shoyer

Description

@shoyer

mean() works on timedelta64, but gives an inadvertent error message for datetime64:

In [16]: x = np.array(['2000-01-01', '2000-01-10'], dtype='datetime64[us]')

In [17]: np.mean(x)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-67ce1f9814a2> in <module>
----> 1 np.mean(x)

~/miniconda3/envs/numbagg-py37/lib/python3.7/site-packages/numpy/core/fromnumeric.py in mean(a, axis, dtype, out, keepdims)
   3116
   3117     return _methods._mean(a, axis=axis, dtype=dtype,
-> 3118                           out=out, **kwargs)
   3119
   3120

~/miniconda3/envs/numbagg-py37/lib/python3.7/site-packages/numpy/core/_methods.py in _mean(a, axis, dtype, out, keepdims)
     73             is_float16_result = True
     74
---> 75     ret = umr_sum(arr, axis, dtype, out, keepdims)
     76     if isinstance(ret, mu.ndarray):
     77         ret = um.true_divide(

TypeError: ufunc add cannot use operands with types dtype('<M8[us]') and dtype('<M8[us]')

The problem is that mean is implemented via sum, which cannot be sensibly defined for dates.

Fixing this will probably require some significant refactoring -- we would need to make mean something that could be overwritten explicitly based on the dtypes. Our usual mechanism for this is ufuncs, but mean is currently written in terms of ufuncs (add.reduce and divide) rather being a ufunc itself.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions