> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Replace apply With NumPy Operation

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

#### Description

Replace `apply` with a NumPy operation.

#### Before

```python

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

#### After

```python

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.

#### Related Rule

- [replace-apply-with-method-call](/reference/refactorings/python/replace-apply-with-method-call/) to use the
  aggregation methods of `Series` and `DataFrame` instead of `apply`
