Skip to content

Replace apply With NumPy Operation

Sourcery refactoring id: replace-apply-with-numpy-operation

Description

Replace apply with a NumPy operation.

Before

import pandas as pd

series_ = pd.Series([5, 8, 13, 21, 34])
other = series_.apply(lambda num: num + 5)

After

import pandas as pd

series_ = pd.Series([5, 8, 13, 21, 34])
other = series_ + 5

Explanation

For numeric operations, using pandas directly gives you better performance than apply. The bigger your dataset, the bigger this performance gain tends to be. This is because pandas uses fast, vectorized NumPy code, but apply operates on each value of your Series or DataFrame separately.

apply is a versatile method, that can be used for various different use cases. However, there's often a less verbose or more performant alternative. Try to check out those alternatives - especially when you're working with a big dataset.