Skip to content

Commit

Permalink
Merge pull request #3826 from stuartarchibald/wip/np116
Browse files Browse the repository at this point in the history
NumPy 1.16 support
  • Loading branch information
stuartarchibald committed Mar 21, 2019
2 parents 533dd42 + 95970ce commit 80d82b0
Show file tree
Hide file tree
Showing 13 changed files with 397 additions and 131 deletions.
2 changes: 0 additions & 2 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ exclude =
numba/runtests.py
numba/pythonapi.py
numba/extending.py
numba/npdatetime.py
numba/decorators.py
numba/typeconv/typeconv.py
numba/typeconv/rules.py
Expand All @@ -87,7 +86,6 @@ exclude =
numba/targets/cmathimpl.py
numba/targets/tupleobj.py
numba/targets/mathimpl.py
numba/targets/ufunc_db.py
numba/targets/registry.py
numba/targets/imputils.py
numba/targets/builtins.py
Expand Down
10 changes: 0 additions & 10 deletions docs/source/reference/numpysupported.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@
Supported NumPy features
========================

.. note::
The vast majority of NumPy 1.16 behaviour is supported, however
``datetime`` and ``timedelta`` use involving ``NaT`` matches the behaviour
present in earlier release. The ufunc suite has not been extending to
accommodate the two new time computation related additions present in NumPy
1.16. In addition the functions ``ediff1d`` and ``interp`` have known minor
issues in replicating outputs exactly when ``NaN``'s occur in certain input
patterns.


One objective of Numba is having a seamless integration with `NumPy`_.
NumPy arrays provide an efficient storage method for homogeneous sets of
data. NumPy dtypes provide type information useful when compiling, and
Expand Down
2 changes: 1 addition & 1 deletion docs/source/user/installing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Compatibility
-------------

Numba is compatible with Python 2.7 and 3.5 or later, and Numpy versions 1.7 to
1.16 (see :ref:`this note <numpy-support>` for 1.16 support restrictions).
1.16.

Our supported platforms are:

Expand Down
16 changes: 10 additions & 6 deletions numba/npdatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
's': 7, # Seconds
'ms': 8, # Milliseconds
'us': 9, # Microseconds
'ns': 10, # Nanoseconds
'ps': 11, # Picoseconds
'fs': 12, # Femtoseconds
'as': 13, # Attoseconds
'ns': 10, # Nanoseconds
'ps': 11, # Picoseconds
'fs': 12, # Femtoseconds
'as': 13, # Attoseconds
'': 14, # "generic", i.e. unit-less
}

Expand Down Expand Up @@ -81,6 +81,7 @@ def can_cast_timedelta_units(src, dest):
12: (13, 1000),
}


def _get_conversion_multiplier(big_unit_code, small_unit_code):
"""
Return an integer multiplier allowing to convert from *big_unit_code*
Expand All @@ -106,6 +107,7 @@ def _get_conversion_multiplier(big_unit_code, small_unit_code):
else:
return None


def get_timedelta_conversion_factor(src_unit, dest_unit):
"""
Return an integer multiplier allowing to convert from timedeltas
Expand All @@ -114,6 +116,7 @@ def get_timedelta_conversion_factor(src_unit, dest_unit):
return _get_conversion_multiplier(DATETIME_UNITS[src_unit],
DATETIME_UNITS[dest_unit])


def get_datetime_timedelta_conversion(datetime_unit, timedelta_unit):
"""
Compute a possible conversion for combining *datetime_unit* and
Expand All @@ -139,11 +142,11 @@ def get_datetime_timedelta_conversion(datetime_unit, timedelta_unit):
# the factor averaged over the 400 year leap year cycle."""
if dt_unit_code == 0:
if td_unit_code >= 4:
dt_factor = 97 + 400 * 365
dt_factor = 97 + 400 * 365
td_factor = 400
dt_unit_code = 4
elif td_unit_code == 2:
dt_factor = 97 + 400 * 365
dt_factor = 97 + 400 * 365
td_factor = 400 * 7
dt_unit_code = 2
elif dt_unit_code == 1:
Expand Down Expand Up @@ -185,6 +188,7 @@ def combine_datetime_timedelta_units(datetime_unit, timedelta_unit):
else:
return timedelta_unit


def get_best_unit(unit_a, unit_b):
"""
Get the best (i.e. finer-grained) of two units.
Expand Down
37 changes: 37 additions & 0 deletions numba/targets/arraymath.py
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,23 @@ def _prepare_array_impl(arr):
else:
return lambda arr: _asarray(arr).ravel()

def _dtype_of_compound(inobj):
obj = inobj
while True:
if isinstance(obj, (types.Number, types.Boolean)):
return as_dtype(obj)
l = getattr(obj, '__len__', None)
if l is not None and l() == 0: # empty tuple or similar
return np.float64
dt = getattr(obj, 'dtype', None)
if dt is None:
raise TypeError("type has no dtype attr")
if isinstance(obj, types.Sequence):
obj = obj.dtype
else:
return as_dtype(dt)


if numpy_version >= (1, 12): # replicate behaviour of NumPy 1.12 bugfix release
@overload(np.ediff1d)
def np_ediff1d(ary, to_end=None, to_begin=None):
Expand All @@ -1362,6 +1379,26 @@ def np_ediff1d(ary, to_end=None, to_begin=None):
# Numpy tries to do this: return ary[1:] - ary[:-1] which results in a
# TypeError exception being raised

# since np 1.16 there are casting checks for to_end and to_begin to make
# sure they are compatible with the ary
if numpy_version >= (1, 16):
ary_dt = _dtype_of_compound(ary)
to_begin_dt = None
if not(_is_nonelike(to_begin)):
to_begin_dt = _dtype_of_compound(to_begin)
to_end_dt = None
if not(_is_nonelike(to_end)):
to_end_dt = _dtype_of_compound(to_end)

if to_begin_dt is not None and not np.can_cast(to_begin_dt, ary_dt):
msg = "dtype of to_begin must be compatible with input ary"
raise TypeError(msg)

if to_end_dt is not None and not np.can_cast(to_end_dt, ary_dt):
msg = "dtype of to_end must be compatible with input ary"
raise TypeError(msg)


def np_ediff1d_impl(ary, to_end=None, to_begin=None):
# transform each input into an equivalent 1d array
start = _prepare_array(to_begin)
Expand Down

0 comments on commit 80d82b0

Please sign in to comment.