Skip to main content

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

Usage

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 aa_data_handler import save

# Create a DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})

# Save the DataFrame to a CSV file
save(df, 'data', 'csv', Path('data'))

Additional Examples

  1. Save a Series to a Pickle File:

    series = pd.Series([1, 2, 3, 4, 5])
    save(series, 'series_data', 'pickle', Path('data'))
    
  2. 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'))
    
  3. 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 aa_data_handler import load

# Load the latest file in the log
loaded_df = load(name='data')

# Print the loaded DataFrame
print(loaded_df)

Arguments for load

  • 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'.
  • 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

  1. Load by Name:

    loaded_df = load(name='data')
    
  2. Load by Name and Format:

    loaded_df = load(name='data', file_format='csv')
    
  3. Load by Name, Format, and Path:

    loaded_df = load(name='data', file_format='csv', file_path='data')
    
  4. 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): Column to use for weighted averages.
  • field_columns (list[str]): List of field columns to be aggregated as weighted average by group.
  • 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.

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)
print(result)

License

This project is licensed under the MIT License. See the LICENSE file for details.

Authors

Contributing

No need to contribute at this point. Thank you!

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

xsam-0.1.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xsam-0.1.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file xsam-0.1.0.tar.gz.

File metadata

  • Download URL: xsam-0.1.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.2

File hashes

Hashes for xsam-0.1.0.tar.gz
Algorithm Hash digest
SHA256 984fdd678340eb82d6c6d91a89ec41c8ec52f7ec0478c63ee0ea93d93804bdd0
MD5 4d706fc68fe3f4fa788b2b6023c58d57
BLAKE2b-256 ded073713c72fa1947eb60c67da354864053047c8d8d78943cbf3bf390dde950

See more details on using hashes here.

File details

Details for the file xsam-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: xsam-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.2

File hashes

Hashes for xsam-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c579599a85b7e58542355328c321250dbea240b2fe5b435a101e35e5137a3152
MD5 6a826fb7962e3af05220fcaa0523b457
BLAKE2b-256 956cf3659177c45dcef329f906cba245ac41fca002beda3be185ae2ccedcc050

See more details on using hashes here.

Supported by

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