A decorator for applying a moving window to a function that consumes and returns a Pandas DataFrame.
Project description
pd-window-decorators
Easily apply windowing to functions that mutate Pandas DataFrames. Useful for data science projects where you want to apply a function for a DataFrame using smaller chunks of the DataFrame automatically. A moving window can be easily applied to the function using Python decorators.
Sliding Window
To apply a sliding window, import df_sliding_window from pd_window_decorators and apply it to your function. The decorator takes a timedelta object as a required argument to define the slice size. By default, the decorator will look for a Pandas DataFrame named df in the function arguments. The DataFrame must also have a time column. The column name is ds by default. The decorator also expects the function to return a DataFrame.
Example
Using the decorator with default arguments:
@df_sliding_window(window_size=timedelta(days=2))
def sum_all(df):
df.loc[:, 'sum'] = df['y'].sum()
return df
Using the decorator with custom arguments:
@df_sliding_window(window_size=timedelta(days=2), df_name='my_df', time_column='my_time')
def sum_all(my_df):
df.loc[:, 'sum'] = df['y'].sum()
return df
Note that in the second example, the DataFrame is named my_df and the df_name argument is set to my_df, so they match.
Arguments
| Argument | Type | Optional | Description |
|---|---|---|---|
window_size |
timedelta |
True |
The size of the window to apply to the function. |
df_name |
str |
False |
The name of the DataFrame variable to pass to the function as an argument. Defaults to df. |
time_column |
str |
False |
The name of the column in the DataFrame that contains the time information. Defaults to ds. |
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 pd-window-decorators-0.0.1.tar.gz.
File metadata
- Download URL: pd-window-decorators-0.0.1.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dbeb221723c27ede5ac48c353a3655239abafd981875ada97325a6e1f3a516ff
|
|
| MD5 |
929c5fd944f953bd0fa97973c5de07cf
|
|
| BLAKE2b-256 |
04c2fcac5a0db166ed466f475020e580522cef5c17ff09e34471035650a818df
|
File details
Details for the file pd_window_decorators-0.0.1-py3-none-any.whl.
File metadata
- Download URL: pd_window_decorators-0.0.1-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1f340d8c3451fe5a863f353a2435a5000ac161989c7f99c5068277528bbe51df
|
|
| MD5 |
3e6ad7c1e5c0e4c56aa2afa624b7e190
|
|
| BLAKE2b-256 |
5db0c2299f80cbc714562f182bb0abab8827d24296bb6dca56c0807d084f8ec6
|