XSAM assistant
Project description
XSAM
XSAM is a Python package designed to assist with management and analysis of financial data.
Installation
To install the package, use the following command:
pip install xsam
If you want to run the package's tests, install dev dependencies for pytest
pip install xsam[dev]
pytest
Usage
Entry Point
Run the main program like so
xsam
Or run the main module
python -m xsam.main
Saving Data
You can save a DataFrame, Series, dictionary, or Figure to a file using the save function:
from pathlib import Path
import pandas as pd
from xsam.output import save
# Create a DataFrame
df = pd.DataFrame({
'A': [1, 2, 3],
'B': [4, 5, 6]
})
# Save the DataFrame to a CSV filev
save(df, 'data', 'csv', Path('data'))
Additional Examples
-
Save a Series to a Pickle File:
series = pd.Series([1, 2, 3, 4, 5]) save(series, 'series_data', 'pickle', Path('data'))
-
Save a Dictionary of DataFrames to an Excel File:
data_dict = { 'Sheet1': pd.DataFrame({'A': [1, 2], 'B': [3, 4]}), 'Sheet2': pd.DataFrame({'C': [5, 6], 'D': [7, 8]}) } save(data_dict, 'data_dict', 'xlsx', Path('data'))
-
Save a Matplotlib Figure to a SVG File:
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot([1, 2, 3], [4, 5, 6]) save(fig, 'figure', 'svg', Path('data'))
Loading Data
You can load a DataFrame, Series, or dictionary from a file using the load function:
from xsam.input import load
# Load the latest file in the log
loaded_df = load(file_name='data')
# Print the loaded DataFrame
print(loaded_df)
Arguments for load
file_name(str): The name of the file to load. If not provided, the function will search the log for the latest file.file_format(str): The format of the file to load. Supported formats are 'csv', 'xlsx', and 'pickle'.full_file_path(Path | str): The path to the file. If not provided, the function will search the log for the latest file.log_id(str): The unique file ID from the log file. If provided, the function will use this to locate the file.
Examples
-
Load by Name:
loaded_df = load(file_name='data')
-
Load by Name and Format:
loaded_df = load(file_name='data', file_format='csv')
-
Load by Full File Path:
loaded_df = load(full_file_path='data/data.csv')
-
Load by Log ID:
loaded_df = load(log_id='unique-log-id')
Aggregating Fields by Label
The aggregate_fields_by_label function aggregates values in a DataFrame by label, with optional regex matching and multipliers.
Parameters
df(pd.DataFrame): DataFrame containing the data to aggregate.id_column(str): Column containing IDs to be associated with groups.weight_column(str, optional): Column to use for weighted averages. Defaults to None.field_columns(list[str], optional): List of field columns to be aggregated. Defaults to None.label_column(str, optional): Column containing labels to use for grouping. Defaults to None.label_regex(dict, optional): Dictionary of regex patterns and multipliers to use for grouping. Defaults to None.method(str, optional): Method to use for aggregation. Options are "sum", "wsum", "avg", "wavg". Defaults to "sum".preliminary(bool, optional): Whether to return the DataFrame before summing the values for each group. Defaults to False.
Returns
pd.DataFrame: DataFrame with aggregated values for each group.
Example
import pandas as pd
from xsam.aggregation import aggregate_fields_by_label
data = {
"id": ["A0", "B0", "C0", "A1", "B1", "C1", "A2", "B2", "C2"],
"value1": [10, 20, 30, 40, 50, 60, 70, 80, 90],
"value2": [15, 25, 35, 45, 55, 65, 75, 85, 95],
"weight": [1, 2, 3, 4, 5, 6, 7, 8, 9],
}
df = pd.DataFrame(data)
regex_dict = {
"A_group": {"long": ("A.*", 1), "short": ("A1", -1), "other": ("A1", 1)},
"B_group": {"long": ("B.*", 1), "short": ("B1", -1)},
"C_group": {"long": ("C.*", 1), "short": ("C1", -1)},
}
result = aggregate_fields_by_label(df, "id", "weight", ["value1", "value2"], label_regex=regex_dict, method="wavg")
print(result)
License
This project is licensed under the MIT License. See the LICENSE file for details.
Authors
- Stabile Frisur - stabilefrisur@proton.me
Contributing
No need to contribute at this point. Thank you!
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 xsam-0.1.2.tar.gz.
File metadata
- Download URL: xsam-0.1.2.tar.gz
- Upload date:
- Size: 68.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ae396248e55d8165ca30283127d4d189600490628564ee30c092e872ba2dab39
|
|
| MD5 |
6ca1ccf6ab31d4c5c8890e791c1e4126
|
|
| BLAKE2b-256 |
f74cdecb141b08d052280c010340ad826d553f6693df36b37e77a42474fb5ff9
|
File details
Details for the file xsam-0.1.2-py3-none-any.whl.
File metadata
- Download URL: xsam-0.1.2-py3-none-any.whl
- Upload date:
- Size: 25.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c97a3ddf0ff7e6bb4ac9cd28efe0e9502e6c9905b38374a67650436e5501609f
|
|
| MD5 |
55359fd7449457d38a88c91c22397bd8
|
|
| BLAKE2b-256 |
3914e821551db261ccd96851729212c79ef39034e0ef96dcaef327e2841694e4
|