# Buggy code
def array_argmin(context, builder, sig, args):
    ty = sig.args[0].dtype
    # NOTE: Under Numpy < 1.10, argmin() is inconsistent with min() on NaT values:
    # https://github.com/numpy/numpy/issues/6030

    if numpy_version >= (1, 10) and isinstance(
        ty, (types.NPDatetime, types.NPTimedelta)
    ):
        # NaT is smaller than every other value, but it is
        # ignored as far as argmin() is concerned.
        nat = ty("NaT")

        def array_argmin_impl(arry):
            min_value = nat
            min_idx = 0
            it = arry.flat
            idx = 0
            for v in it:
                if v != nat:
                    min_value = v
                    min_idx = idx
                    idx += 1
                    break
                idx += 1

            for v in it:
                if v != nat and v < min_value:
                    min_value = v
                    min_idx = idx
                idx += 1
            return min_idx

    else:

        def array_argmin_impl(arry):
            for v in arry.flat:
                min_value = v
                min_idx = 0
                break

            idx = 0
            for v in arry.flat:
                if v < min_value:
                    min_value = v
                    min_idx = idx
                idx += 1
            return min_idx

    res = context.compile_internal(builder, array_argmin_impl, sig, args)
    return impl_ret_untracked(context, builder, sig.return_type, res)


# Traceback (ValueError)
In [324]: def test_0d_min(X):
...:     return np.min(X)
...:
...:

In [325]: test_0d_min(np.array([]))  # correct behavior
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
... (omitted)
ValueError: zero-size array to reduction operation minimum which has no identity

In [326]: nb.jit(test_0d_min)(np.array([]))  # incorrect
Out[326]: 0.0

In [328]: nb.jit(test_0d_min, parallel=True)(np.array([]))  # incorrect
Out[328]: 1.7976931348623157e+308

# Fixed code
def array_argmin(context, builder, sig, args):
    ty = sig.args[0].dtype
    # NOTE: Under Numpy < 1.10, argmin() is inconsistent with min() on NaT values:
    # https://github.com/numpy/numpy/issues/6030

    if numpy_version >= (1, 10) and isinstance(
        ty, (types.NPDatetime, types.NPTimedelta)
    ):
        # NaT is smaller than every other value, but it is
        # ignored as far as argmin() is concerned.
        nat = ty("NaT")

        def array_argmin_impl(arry):
            if arry.size == 0:
                raise ValueError("attempt to get argmin of an empty sequence")
            min_value = nat
            min_idx = 0
            it = arry.flat
            idx = 0
            for v in it:
                if v != nat:
                    min_value = v
                    min_idx = idx
                    idx += 1
                    break
                idx += 1

            for v in it:
                if v != nat and v < min_value:
                    min_value = v
                    min_idx = idx
                idx += 1
            return min_idx

    else:

        def array_argmin_impl(arry):
            if arry.size == 0:
                raise ValueError("attempt to get argmin of an empty sequence")
            for v in arry.flat:
                min_value = v
                min_idx = 0
                break

            idx = 0
            for v in arry.flat:
                if v < min_value:
                    min_value = v
                    min_idx = idx
                idx += 1
            return min_idx

    res = context.compile_internal(builder, array_argmin_impl, sig, args)
    return impl_ret_untracked(context, builder, sig.return_type, res)