Skip to main content

Sensible multi-core apply function for Pandas

Project description

mapply

build codecov pypi Version python downloads

mapply provides a sensible multi-core apply function for Pandas.

mapply vs. pandarallel vs. swifter

Where pandarallel relies on in-house multiprocessing and progressbars, and hard-codes 1 chunk per worker (which will cause idle CPUs when one chunk happens to be more expensive than the others), swifter relies on the heavy dask framework for multiprocessing (converting to Dask DataFrames and back). In an attempt to find the golden mean, mapply is highly customizable and remains lightweight, using tqdm for progressbars and leveraging the powerful pathos framework, which shadows Python's built-in multiprocessing module using dill for universal pickling. Chunks of work are assigned to worker processes "just in time" from a shared queue, allowing irregular workloads to finish faster.

Installation

This pure-Python, OS independent package is available on PyPI:

$ pip install mapply

Usage

readthedocs

For documentation, see mapply.readthedocs.io.

import pandas as pd
import mapply

mapply.init(
    n_workers=-1,
    chunk_size=100,
    max_chunks_per_worker=8,
    progressbar=False,
)

df = pd.DataFrame({"A": list(range(100))})

# avoid unnecessary multiprocessing:
# due to chunk_size=100, this will act as regular apply.
# set chunk_size=1 to skip this check and let max_chunks_per_worker decide.
df["squared"] = df.A.mapply(lambda x: x**2)

Development

gitmoji pre-commit ruff

Run make help for options like installing for development, linting, testing, and building docs.


BSD 3-Clause License

Copyright (c) 2024, ddelange, ddelange@delange.dev

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

SPDX-License-Identifier: BSD-3-Clause

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mapply-0.1.27.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

mapply-0.1.27-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file mapply-0.1.27.tar.gz.

File metadata

  • Download URL: mapply-0.1.27.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for mapply-0.1.27.tar.gz
Algorithm Hash digest
SHA256 aac236e6840590c08508844e8743aca1c286b4e6026abac48fefe3dccf96de26
MD5 63143646fe4b93e130f12f66e5ff8eed
BLAKE2b-256 59e473ae1a7da0578cdbf3c43a77594a274b70d211d15ecc7411e4953bf6106f

See more details on using hashes here.

File details

Details for the file mapply-0.1.27-py3-none-any.whl.

File metadata

  • Download URL: mapply-0.1.27-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for mapply-0.1.27-py3-none-any.whl
Algorithm Hash digest
SHA256 18366e2fc1bf442963386e00616411503526926716f9644399002452a8ab0a26
MD5 c2bcc041df6b7d0770292c95392d44df
BLAKE2b-256 751ab1f2917686cfc0c20a2957203cd89dd294afc0e7feaa09feb44da6457034

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page