Skip to main content

__ Back to top

__ ` Ctrl ` \+ ` K `

[ ![pandas 2.2.1 documentation - Home](../_static/pandas.svg) ](../index.html)

Site Navigation

  * [ Getting started ](../getting_started/index.html)
  * [ User Guide ](index.html)
  * [ API reference ](../reference/index.html)
  * [ Development ](../development/index.html)
  * [ Release notes ](../whatsnew/index.html)

  * [ __ GitHub  ](https://github.com/pandas-dev/pandas "GitHub")
  * [ __ Twitter  ](https://twitter.com/pandas_dev "Twitter")
  * [ __ Mastodon  ](https://fosstodon.org/@pandas_dev "Mastodon")

Site Navigation

  * [ Getting started ](../getting_started/index.html)
  * [ User Guide ](index.html)
  * [ API reference ](../reference/index.html)
  * [ Development ](../development/index.html)
  * [ Release notes ](../whatsnew/index.html)

  * [ __ GitHub  ](https://github.com/pandas-dev/pandas "GitHub")
  * [ __ Twitter  ](https://twitter.com/pandas_dev "Twitter")
  * [ __ Mastodon  ](https://fosstodon.org/@pandas_dev "Mastodon")

  * [ 10 minutes to pandas ](10min.html)
  * [ Intro to data structures ](dsintro.html)
  * [ Essential basic functionality ](basics.html)
  * [ IO tools (text, CSV, HDF5, â¦) ](io.html)
  * [ PyArrow Functionality ](pyarrow.html)
  * [ Indexing and selecting data ](indexing.html)
  * [ MultiIndex / advanced indexing ](advanced.html)
  * [ Copy-on-Write (CoW) ](copy_on_write.html)
  * [ Merge, join, concatenate and compare ](merging.html)
  * [ Reshaping and pivot tables ](reshaping.html)
  * [ Working with text data ](text.html)
  * [ Working with missing data ](missing_data.html)
  * [ Duplicate Labels ](duplicates.html)
  * [ Categorical data ](categorical.html)
  * [ Nullable integer data type ](integer_na.html)
  * [ Nullable Boolean data type ](boolean.html)
  * [ Chart visualization ](visualization.html)
  * [ Table Visualization ](style.html)
  * [ Group by: split-apply-combine ](groupby.html)
  * [ Windowing operations ](window.html)
  * [ Time series / date functionality ](timeseries.html)
  * [ Time deltas ](timedeltas.html)
  * [ Options and settings ](options.html)
  * Enhancing performance 
  * [ Scaling to large datasets ](scale.html)
  * [ Sparse data structures ](sparse.html)
  * [ Frequently Asked Questions (FAQ) ](gotchas.html)
  * [ Cookbook ](cookbook.html)

  * [ __ ](../index.html)
  * [ User Guide ](index.html)
  * Enhancing... 

#  Enhancing performance  #

In this part of the tutorial, we will investigate how to speed up certain
functions operating on pandas [ ` DataFrame  `
](../reference/api/pandas.DataFrame.html#pandas.DataFrame "pandas.DataFrame")
using Cython, Numba and [ ` pandas.eval()  `
](../reference/api/pandas.eval.html#pandas.eval "pandas.eval") . Generally,
using Cython and Numba can offer a larger speedup than using [ ` pandas.eval()
` ](../reference/api/pandas.eval.html#pandas.eval "pandas.eval") but will
require a lot more code.

Note

In addition to following the steps in this tutorial, users interested in
enhancing performance are highly encouraged to install the [ recommended
dependencies  ](../getting_started/install.html#install-recommended-
dependencies) for pandas. These dependencies are often not installed by
default, but will offer speed improvements if present.

##  Cython (writing C extensions for pandas)  #

For many use cases writing pandas in pure Python and NumPy is sufficient. In
some computationally heavy applications however, it can be possible to achieve
sizable speed-ups by offloading work to [ cython ](https://cython.org/) .

This tutorial assumes you have refactored as much as possible in Python, for
example by trying to remove for-loops and making use of NumPy vectorization.
Itâs always worth optimising in Python first.

This tutorial walks through a âtypicalâ process of cythonizing a slow
computation. We use an [ example from the Cython documentation
](https://docs.cython.org/en/latest/src/quickstart/cythonize.html) but in the
context of pandas. Our final