Skip to main content

finlab-guard

This is an unofficial, third-party implementation

A lightweight package for managing a local finlab data cache with versioning and time-context features.

Python versions Windows Linux macOS License: MIT build lint coverage

Installation

pip install finlab-guard

Usage examples

Three short examples showing the most common flows.

1) Monkey-patch finlab.data.get (installing FinlabGuard)

This project can monkey-patch finlab.data.get so reads go through the guarded cache. Example:

from finlab import data
from finlab_guard import FinlabGuard

# Create a FinlabGuard instance and install the monkey-patch
guard = FinlabGuard()
guard.install_patch()

# Use data.get as normal; FinlabGuard will intercept and use cache
result = data.get('price:收盤價')

# When done, remove the monkey-patch
guard.remove_patch()

2) Set a time context and get historical data

FinlabGuard supports a time context so you can query data "as-of" a past time.

from finlab import data
from finlab_guard import FinlabGuard
from datetime import datetime, timedelta

guard = FinlabGuard()
guard.install_patch()

# Set time context to 7 days ago
query_time = datetime.now() - timedelta(days=7)
guard.set_time_context(query_time)

# Now call data.get normally; the guard will return historical data
result = data.get('price:收盤價')

# Clear the time context and remove the monkey-patch when done
guard.clear_time_context()
guard.remove_patch()

3) Parameter precedence for allow_historical_changes

FinlabGuard uses an effective_allow_changes logic with parameter precedence:

from finlab import data
from finlab_guard import FinlabGuard

# Set global setting via install_patch
guard = FinlabGuard()
guard.install_patch(allow_historical_changes=False)  # Global setting

# Method parameter overrides global setting
result1 = data.get('price:收盤價', allow_historical_changes=True)  # Uses True (method override)
result2 = data.get('volume:成交量')  # Uses False (global setting)

# Precedence order: method parameter > global setting > default (True)

Parameter Precedence:

  1. Method parameter (highest priority): get(dataset, allow_historical_changes=True/False)
  2. Global setting: Set via install_patch(allow_historical_changes=True/False)
  3. Default value (lowest priority): True - allows historical changes by default

This allows fine-grained control where you can set a global policy but override it for specific datasets when needed.

What's New in v0.4.0

🔧 Breaking Changes

  • Default allow_historical_changes changed to True: Historical data modifications are now allowed by default. Set to False if you need strict change detection.

🐛 Critical Bug Fixes

  • Row/column lifecycle filtering: Fixed stale cell_changes incorrectly affecting re-added rows/columns after deletion.

Performance

finlab-guard delivers significant performance improvements through its DuckDB + Polars architecture:

🚀 Cache Performance: Up to 96% faster with hash optimization

Version Reconstruction Time Hash Match Time Improvement
v0.1.0 (pandas.stack) 17.9s N/A baseline
v0.2.0 (DuckDB+Polars) 12.4s N/A -30.6% ⚡
v0.3.0 (Hash + orjson) 11.2s 0.74s -37.5% / -96% 🚀

Benchmark: etl:adj_close cache retrieval (4,533 × 2,645 DataFrame) - average of 10 runs

Key Optimizations

  • DataFrame hash optimization (v0.3.0): Fast data comparison using SHA256 hashes to avoid expensive reconstruction when data is unchanged
  • orjson acceleration (v0.3.0): Faster JSON parsing with vectorized operations and reduced memory overhead for reconstruction scenarios
  • Eliminated pandas.stack() bottleneck: Replaced with vectorized Polars operations
  • Cell-level change tracking: Only stores actual differences, not full datasets
  • DuckDB storage engine: High-performance indexed storage with time-based reconstruction
  • Intelligent thresholding: Large row changes stored efficiently as JSON objects

These improvements make finlab-guard ideal for:

  • Large datasets with frequent updates
  • Historical data analysis and backtesting
  • Production environments requiring consistent performance

Disclaimer

This project is not affiliated with, endorsed by, or officially supported by finlab. It is an independent implementation designed to work alongside the finlab package for enhanced data caching and version control.

License

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

Metadata

Release files for finlab-guard 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for finlab-guard 0.4.1
File Size Uploaded Provenance
finlab_guard-0.4.1.tar.gz 229.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for finlab-guard 0.4.1
File Interpreter ABI Platform
finlab_guard-0.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 262.9 kB

Release files / finlab_guard-0.4.1.tar.gz

Download URL finlab_guard-0.4.1.tar.gz
Size 229.5 kB
Tags Source
SHA-256 checksum
How to use checksums
8637fea887ea41b06f02991c9539db5be0e53121cecff559fe5d224015365531
BLAKE2b-256 checksum
How to use checksums
e60429955349b3e994722800dac949f41e0b799d6ffd4adb248e4d13d144949d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / finlab_guard-0.4.1-py3-none-any.whl

Download URL finlab_guard-0.4.1-py3-none-any.whl
Size 33.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2f44499c46824fbec5e81ec3eb9e448c9732cadc53718eb5fc0b7cea987c5a6c
BLAKE2b-256 checksum
How to use checksums
b39ba6a184e028ff7413d2b5fc653fc0d5f94d8bb8eee133977e329988a05365
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Security for finlab-guard 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page