Registry / data / swifter

swifter

JSON →
library1.4.0pypypi✓ verified 24d ago

Swifter is a Python package designed to significantly speed up `apply` operations on pandas DataFrames and Series. It achieves this by automatically determining the fastest available method for applying a function, leveraging vectorized pandas operations, Dask for parallel processing, or multi-threading/multi-processing. Swifter integrates directly into pandas objects, offering a seamless way to optimize user-defined functions without extensive code changes. The current version is 1.4.0, released in July 2023, and the project maintains an active development and release cadence.

pip install swifter
INSTALL
IMPORT
SIG · SWIFTER
S
swifter
datapythonv1.4.0
Install
16.9s avg
Import
3042ms
Disk
505MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.4.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.915 runs
installs and imports cleanly · install 0.0s · import 2.969s · 440.9MB
glibc
py 3.103.915 runs
installs and imports cleanly · install 16.9s · import 3.116s · 570MB
505MB installed
● package 505MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

swifter
import swifter
Imports swifter to add the `.swifter` accessor to pandas objects.
pandas
import pandas as pd
Standard import for pandas functionality, required for swifter integration.

This quickstart demonstrates how to apply a custom, computationally intensive function to a pandas Series and DataFrame using `swifter.apply()`. Swifter automatically chooses the most efficient execution backend (vectorized, Dask, or multiprocessing) based on the data size and function complexity.

import pandas as pd import swifter # Create a sample DataFrame df = pd.DataFrame({ 'value': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'category': ['A', 'B', 'A', 'C', 'B', 'A', 'C', 'B', 'A', 'C'] }) # Define a custom function def complex_calculation(x): import time time.sleep(0.001) # Simulate a time-consuming operation return x * x + 1 # Use swifter.apply() on a Series df['squared_value'] = df['value'].swifter.apply(complex_calculation) # Use swifter.apply() on a DataFrame (row-wise) df['sum_squared'] = df.swifter.apply(lambda row: row['value']**2 + row['value'], axis=1) print(df.head())
Debug
Known issues
gotchaAvoid using swifter with functions that modify external variables. Swifter performs 'sample applies' to optimize performance, which can lead to erroneous modifications of external variables in addition to the final apply operation.
fix
Ensure that functions passed to `swifter.apply()` are pure functions without side effects on external state.
affects: All versions
gotchaWhen `swifter` is called from a forked process, its progress bar may become confused. It is advisable to disable the progress bar in such scenarios.
fix
Disable the progress bar using `swifter.disable_progress_bar()` or by setting the `SWIFTER_PROGRESS_BAR` environment variable to `False` when running in forked processes.
affects: All versions
gotchaFor compatibility with Modin DataFrames, `modin.pandas` must be imported *before* `swifter`, or `swifter.register_modin()` must be called explicitly after importing both.
fix
Order imports as `import modin.pandas as pd; import swifter` or call `swifter.register_modin()` if `swifter` is imported first.
affects: All versions
breakingSwifter relies on recent features of the pandas extension API. Older versions of pandas (e.g., pre-1.0) may not be fully compatible or may cause unexpected behavior.
fix
Always ensure you are using a recent and updated version of pandas (`pip install -U pandas`).
affects: < 1.0.0 (pandas)
gotchaWhen using Dask as a backend for large datasets, `swifter` is limited to `axis=1` (row-wise application) for `df.swifter.apply()`. Attempting `axis=0` with large Dask-backed DataFrames may not use Dask or might result in errors.
fix
Structure your `apply` operations to be row-wise (`axis=1`) when dealing with large datasets that would trigger Dask usage, or consider alternative Dask-native operations for column-wise transforms.
affects: All versions
Errors
Common errors & fixes
AttributeError: 'DataFrame' object has no attribute 'swifter'
The `swifter` library was not imported, or it was imported in a way that prevented the `.swifter` accessor from being patched onto pandas DataFrame or Series objects.
fix
Ensure `import swifter` is called at the beginning of your script, typically right after `import pandas as pd`.
ModuleNotFoundError: No module named 'swifter'
The `swifter` package is not installed in the Python environment currently being used.
fix
Install the package using pip: `pip install swifter`
_pickle.PicklingError: Can't pickle local object '<lambda>'
Swifter uses multiprocessing for some operations, but Python's multiprocessing requires functions to be picklable, which local or lambda functions are not.
fix
Define the function at the module's top level (not inside another function or as a lambda) so it can be properly serialized for multiprocessing.
ValueError: The Dask backend requires Dask to be installed. Please install Dask with 'pip install dask[complete]'
Swifter attempted to use Dask for parallel processing, but the `dask` package (or its full set of dependencies) is not installed in the current environment.
fix
Install Dask with all its components using pip: `pip install dask[complete]`
Upgrade
Version history
1.4.0latest on PyPI · released Jul 31, 2023
Audit
Dependencies
pandasrequiredCore dependency for DataFrame and Series operations.
tqdmoptionalOptional dependency for displaying rich progress bars, especially in Jupyter notebooks. Included with `swifter[notebook]`.
daskoptionalOptional dependency for distributed computing and parallel processing when functions cannot be vectorized. Used internally by swifter.
psutiloptionalUsed internally by swifter for system resource monitoring and optimization decisions.
rayoptionalOptional dependency for optimized `groupby.apply` functionality, specifically for swifter versions >= 1.3.2. Included with `swifter[groupby]`.
Agent activity
19 hits · last 30 days
node
14
OpenAI (training)
1
Resources