You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A recent alternative to statically compiling cython code, is to use a *dynamic jit-compiler*, ``numba``.
309
+
A recent alternative to statically compiling Cython code, is to use a *dynamic jit-compiler*, Numba.
302
310
303
311
Numba gives you the power to speed up your applications with high performance functions written directly in Python. With a few annotations, array-oriented and math-heavy Python code can be just-in-time compiled to native machine instructions, similar in performance to C, C++ and Fortran, without having to switch languages or Python interpreters.
304
312
305
313
Numba works by generating optimized machine code using the LLVM compiler infrastructure at import time, runtime, or statically (using the included pycc tool). Numba supports compilation of Python to run on either CPU or GPU hardware, and is designed to integrate with the Python scientific software stack.
306
314
307
315
.. note::
308
316
309
-
You will need to install ``numba``. This is easy with ``conda``, by using: ``conda install numba``, see :ref:`installing using miniconda<install.miniconda>`.
317
+
You will need to install Numba. This is easy with ``conda``, by using: ``conda install numba``, see :ref:`installing using miniconda<install.miniconda>`.
310
318
311
319
.. note::
312
320
313
-
As of ``numba`` version 0.20, pandas objects cannot be passed directly to numba-compiled functions. Instead, one must pass the ``numpy`` array underlying the ``pandas`` object to the numba-compiled function as demonstrated below.
321
+
As of Numba version 0.20, pandas objects cannot be passed directly to Numba-compiled functions. Instead, one must pass the NumPy array underlying the pandas object to the Numba-compiled function as demonstrated below.
314
322
315
323
Jit
316
324
~~~
317
325
318
-
Using ``numba`` to just-in-time compile your code. We simply take the plain Python code from above and annotate with the ``@jit`` decorator.
326
+
We demonstrate how to use Numba to just-in-time compile our code. We simply
327
+
take the plain Python code from above and annotate with the ``@jit`` decorator.
319
328
320
329
.. code-block:: python
321
330
@@ -346,17 +355,19 @@ Using ``numba`` to just-in-time compile your code. We simply take the plain Pyth
346
355
result = apply_integrate_f_numba(df['a'].values, df['b'].values, df['N'].values)
Note that we directly pass ``numpy`` arrays to the numba function. ``compute_numba`` is just a wrapper that provides a nicer interface by passing/returning pandas objects.
358
+
Note that we directly pass NumPy arrays to the Numba function. ``compute_numba`` is just a wrapper that provides a nicer interface by passing/returning pandas objects.
350
359
351
360
.. code-block:: ipython
352
361
353
362
In [4]: %timeit compute_numba(df)
354
363
1000 loops, best of 3: 798 us per loop
355
364
365
+
In this example, using Numba was faster than Cython.
366
+
356
367
Vectorize
357
368
~~~~~~~~~
358
369
359
-
``numba`` can also be used to write vectorized functions that do not require the user to explicitly
370
+
Numba can also be used to write vectorized functions that do not require the user to explicitly
360
371
loop over the observations of a vector; a vectorized function will be applied to each row automatically.
361
372
Consider the following toy example of doubling each observation:
362
373
@@ -389,13 +400,23 @@ Caveats
389
400
390
401
.. note::
391
402
392
-
``numba`` will execute on any function, but can only accelerate certain classes of functions.
403
+
Numba will execute on any function, but can only accelerate certain classes of functions.
393
404
394
-
``numba`` is best at accelerating functions that apply numerical functions to NumPy arrays. When passed a function that only uses operations it knows how to accelerate, it will execute in ``nopython`` mode.
405
+
Numba is best at accelerating functions that apply numerical functions to NumPy
406
+
arrays. When passed a function that only uses operations it knows how to
407
+
accelerate, it will execute in ``nopython`` mode.
395
408
396
-
If ``numba`` is passed a function that includes something it doesn't know how to work with -- a category that currently includes sets, lists, dictionaries, or string functions -- it will revert to ``object mode``. In ``object mode``, numba will execute but your code will not speed up significantly. If you would prefer that ``numba`` throw an error if it cannot compile a function in a way that speeds up your code, pass numba the argument ``nopython=True`` (e.g. ``@numba.jit(nopython=True)``). For more on troubleshooting ``numba`` modes, see the `numba troubleshooting page <http://numba.pydata.org/numba-doc/0.20.0/user/troubleshoot.html#the-compiled-code-is-too-slow>`__.
409
+
If Numba is passed a function that includes something it doesn't know how to
410
+
work with -- a category that currently includes sets, lists, dictionaries, or
411
+
string functions -- it will revert to ``object mode``. In ``object mode``,
412
+
Numba will execute but your code will not speed up significantly. If you would
413
+
prefer that Numba throw an error if it cannot compile a function in a way that
414
+
speeds up your code, pass Numba the argument
415
+
``nopython=True`` (e.g. ``@numba.jit(nopython=True)``). For more on
416
+
troubleshooting Numba modes, see the `Numba troubleshooting page
Copy file name to clipboardExpand all lines: doc/source/whatsnew/v0.23.0.txt
+1
Original file line number
Diff line number
Diff line change
@@ -343,6 +343,7 @@ Other API Changes
343
343
- Addition and subtraction of ``NaN`` from a :class:`Series` with ``dtype='timedelta64[ns]'`` will raise a ``TypeError` instead of treating the ``NaN`` as ``NaT`` (:issue:`19274`)
344
344
- Set operations (union, difference...) on :class:`IntervalIndex` with incompatible index types will now raise a ``TypeError`` rather than a ``ValueError`` (:issue:`19329`)
345
345
- :class:`DateOffset` objects render more simply, e.g. "<DateOffset: days=1>" instead of "<DateOffset: kwds={'days': 1}>" (:issue:`19403`)
346
+
- :func:`pandas.merge` provides a more informative error message when trying to merge on timezone-aware and timezone-naive columns (:issue:`15800`)
0 commit comments