Skip to content

BUG: Cannot call np.median on datetime array #26216

@guilhermeleobas

Description

@guilhermeleobas

Describe the issue:

Not sure if this is by design or actually a bug but one cannot call np.median on datetime arrays.

This was also reported on StackOverflow some years ago.

Reproduce the code example:

import numpy as np
a = np.array([np.datetime64('2005-02-25'), np.datetime64(1, 'Y')])
np.median(a)

Error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 180, in median
  File "/Users/guilhermeleobas/micromamba/envs/numba/lib/python3.9/site-packages/numpy/lib/function_base.py", line 3793, in median
    r, k = _ureduce(a, func=_median, axis=axis, out=out,
  File "/Users/guilhermeleobas/micromamba/envs/numba/lib/python3.9/site-packages/numpy/lib/function_base.py", line 3702, in _ureduce
    r = func(a, **kwargs)
  File "/Users/guilhermeleobas/micromamba/envs/numba/lib/python3.9/site-packages/numpy/lib/function_base.py", line 3847, in _median
    rout = mean(part[indexer], axis=axis, out=out)
  File "<__array_function__ internals>", line 180, in mean
  File "/Users/guilhermeleobas/micromamba/envs/numba/lib/python3.9/site-packages/numpy/core/fromnumeric.py", line 3474, in mean
    return _methods._mean(a, axis=axis, dtype=dtype,
  File "/Users/guilhermeleobas/micromamba/envs/numba/lib/python3.9/site-packages/numpy/core/_methods.py", line 179, in _mean
    ret = umr_sum(arr, axis, dtype, out, keepdims, where=where)
numpy.core._exceptions._UFuncBinaryResolutionError: ufunc 'add' cannot use operands with types dtype('<M8[D]') and dtype('<M8[D]')

Python and NumPy Versions:

1.22.4
3.9.18 | packaged by conda-forge | (main, Dec 23 2023, 16:35:41)
[Clang 16.0.6 ]

Also tried on Numpy 1.24, 1.25 and 1.26.

Runtime Environment:

No response

Context for the issue:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions