A Python package to dynamically create Pandas Series accessors for any classes.
Project description
DynPaAc - Dynamic Pandas Accessors
A Python package to dynamically create Pandas Series accessors for any classes.
This package allows you to dynamically access methods and properties of a target class on a Pandas Series. By registering the accessor, you can apply the methods and properties of the class to any Series element. The return values of these methods and properties are given back as a Pandas Series. The module includes validation to ensure that the Series elements are compatible with the class, and allows the exclusion of certain attributes. It also supports custom methods that can be defined and applied to the Series alongside the methods and properties of the target class.
The project is currently in an early stage of development and changes in its behaviour might be introduced.
Installation
Install the latest release of UnPaAc from PyPI
via pip:
pip install dynpaac
The development version can be installed from
the Git repository using pip:
# Via https
pip install git+https://codeberg.org/Cs137/DynPaAc.git
# Via ssh
pip install git+ssh://git@codeberg.org:Cs137/DynPaAc.git
Usage
The package provides the DynamicSeriesAccessor class, a wrapper to access methods
and properties of a target class from a pandas Series. The target class must be
initialisable with not more than one mandatory argument, and the arguments type
must correspond to the type of the series values.
Since the DynamicSeriesAccessor class is initialised by pandas, there is the
create_dynamic_series_accessor() function, which allows to preconfigure an
accessor for a certain target class and register it as pandas series accessor.
A name for the accessor and the desired target_class have to be defined.
The accessors can be called via the name attribute of series instances with
values of valid input parameter types.
Moreover, the exclusion of certain attributes is possible and the declaration of custom methods that can be applied to the series alongside the methods and properties of the target class is supported.
The creation function is all you need to dynamically create a series accessor for a certain target class, simply import it as follows:
from dynpaac.dynamic_series_accessor import create_dynamic_series_accessor
If you have any questions or need assistance, feel free to open an issue on the repository.
Examples
Series Accessor - Basics
import pandas as pd
from dynpaac.dynamic_series_accessor import create_dynamic_series_accessor
# Define an example class
class MyClass:
def __init__(self, value: str):
self.value = value
@property
def length(self) -> int:
return len(self.value)
def add(self, string: str) -> str:
return self.value + string
# Create an accessor for the example class and define its name
create_dynamic_series_accessor("mycls", MyClass)
# Create a pandas Series with strings
data = pd.Series(["hello", "world"])
# Use the dynamic accessor to apply methods and properties of the example class
data.mycls.length # Access the 'length' property
data.mycls.add("!!!") # Access the 'add' method
Output
0 5
1 5
dtype: int64
0 hello!!!
1 world!!!
dtype: object
Series Accessor - Custom Methods
You can add your own methods to extend the functionality of the target class.
On the condition that they take the value of the series as the first argument.
Declare a dictionary of method names and callables as custom_methods when you create the accessor.
The custom methods are available through the declared names on the accessor.
# Define an example custom method
def custom_method(value: str, prefix: str) -> str:
return prefix + value
custom_methods={"custom": custom_method}
# Recreate the accessor and include the custom method
create_dynamic_series_accessor("mycls", MyClass, custom_methods=custom_methods)
# Use the dynamic accessor to apply the custom method
data.mycls.custom(">>> ") # Access the 'custom_method'
Output
0 >>> hello
1 >>> world
dtype: object
Changes
All notable changes to this project are documented in the file
CHANGELOG.md.
Contributing
Contributions to the DynPaAc package are very welcomed. Feel free to submit a
pull request, if you would like to contribute to the project. In case you are
unfamiliar with the process, consult the
forgejo documentation
and follow the steps using this repository instead of the example repository.
Create your pull request (PR) to
inform that you start working on a contribution. Provide a clear description
of your envisaged changes and the motivation behind them, prefix the PR's title
with WIP: until your changes are finalised.
All kind of contributions are appreciated, whether they are bug fixes, new features, or improvements to the documentation.
Development
Installing for development
To install the package in development mode, clone the Git repository and install the package using Poetry, as shown in the code block underneath. To install Poetry, which is required for virtual environment and dependency management, follow the instructions on the Poetry website.
git clone https://codeberg.org/Cs137/DynPaAc.git
cd dynpaac
poetry install
This will create a virtual environment and install the package dependencies and
the package itself in editable mode, allowing you to make changes to the code and
see the effects immediately in the corresponding virtual environment. Alternatively,
you can install it via pip install -e in an existing virtual environment.
License
DynPaAc is open source software released under the MIT License. See LICENSE file for details.
This package was created and is maintained by Christian Schreinemachers, (C) 2025.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dynpaac-0.1.1.tar.gz.
File metadata
- Download URL: dynpaac-0.1.1.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.13.1 Linux/6.13.1-arch2-1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
94b53142b205a8e8b6f98a53db49c859492ef76f6736ca134594ddfb5a496ff2
|
|
| MD5 |
56cd7cb82d037363732f723a1c59c568
|
|
| BLAKE2b-256 |
efdd4ad7a314668aa1c2f587f0032b7963101ed7cf745c4e89e71019b200da7f
|
File details
Details for the file dynpaac-0.1.1-py3-none-any.whl.
File metadata
- Download URL: dynpaac-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.5 CPython/3.13.1 Linux/6.13.1-arch2-1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e88d5125f38b06fc0bed69a032ac55682ee8834753ffbe2da07ea3bbae921a61
|
|
| MD5 |
575e1530e3044687205bd0c124f1ae19
|
|
| BLAKE2b-256 |
b6470f82d3a9ce2c92740802eb857f35d56a6412675917951e9eea1d08b9115f
|