edgartools icon indicating copy to clipboard operation
edgartools copied to clipboard

[FEATURE] Public Quarterization API for TTMCalculator

Open amcamc92 opened this issue 8 months ago • 2 comments

Feature Category

  • [x] New API functionality
  • [ ] Performance improvement
  • [x] Developer experience improvement
  • [ ] Documentation enhancement
  • [ ] Tool/utility addition

Problem Statement

Is your feature request related to a problem? Please describe. TTMCalculator._quarterize_facts() is a private method (underscore prefix) that converts YTD/annual cumulative facts into discrete quarterly facts. This is extremely useful functionality that many users need independently of TTM calculations — for example, computing YoY quarterly growth, scoring individual quarters, or building quarterly time series. Currently, users must call a private method, which violates the API contract and may break without notice.

Who would benefit from this feature?

  • [ ] Beginner Python users working with SEC filings
  • [x] Financial analysts and researchers
  • [x] Advanced developers building financial applications
  • [x] Data scientists working with financial datasets

Proposed Solution

Describe the solution you'd like Expose the existing _quarterize_facts() as a stable public method quarterize() on TTMCalculator. The implementation is trivial — it's a wrapper with a docstring. The hard work of deriving Q2/Q3/Q4 from cumulative filings is already done.

Describe alternatives you've considered

  1. Standalone function: A quarterize_facts(entity_facts, concept) top-level function. Less discoverable than a method on TTMCalculator.
  2. New class: A QuarterlyCalculator class. Unnecessary overhead — TTMCalculator already has the logic.
  3. Status quo: Users keep calling _quarterize_facts(). Fragile and poor API design.

Use Case Example

How would you use this feature?

from edgar import Company
from edgar.ttm import TTMCalculator

company = Company("AAPL")
entity_facts = company.get_facts()
calc = TTMCalculator(entity_facts)

# Current behavior (private method, may break)
quarterly_facts = calc._quarterize_facts()  # Works but bad practice

# Proposed behavior (stable public API)
quarterly_facts = calc.quarterize()

# Use cases:
# 1. YoY quarterly growth
q4_2024 = [f for f in quarterly_facts if f.fiscal_period == "Q4" and f.fiscal_year == 2024]
q4_2023 = [f for f in quarterly_facts if f.fiscal_period == "Q4" and f.fiscal_year == 2023]

# 2. Quarterly time series for charting
for fact in sorted(quarterly_facts, key=lambda f: f.period_end):
    print(f"FY{fact.fiscal_year} {fact.fiscal_period}: {fact.numeric_value}")

# 3. Derived facts are clearly marked
derived = [f for f in quarterly_facts if f.calculation_context and "derived" in str(f.calculation_context)]
print(f"{len(derived)} quarters were derived from cumulative data")

Implementation Considerations

Complexity Level:

  • [x] Simple (minor API addition)
  • [ ] Moderate (new functionality with existing patterns)
  • [ ] Complex (significant architectural changes)

Backwards Compatibility:

  • [x] This feature maintains backwards compatibility
  • [ ] This feature might break existing code (please explain below)
  • [ ] Unsure about compatibility impact

Suggested Implementation

class TTMCalculator:
    # ... existing code ...

    def quarterize(self) -> list[FinancialFact]:
        """Convert YTD/annual facts into discrete quarterly facts.

        Derives Q2, Q3, Q4 from cumulative filings:
          Q2 = YTD_6M - Q1
          Q3 = YTD_9M - YTD_6M
          Q4 = FY - YTD_9M

        Derived facts have `calculation_context` set to the derivation method
        (e.g., 'derived_q4_fy_minus_ytd9').

        Returns:
            Quarterly facts sorted by period_end descending.
        """
        return self._quarterize_facts()

Additional Context

The private _quarterize_facts() method already:

  • Handles the Q2/Q3/Q4 derivation from cumulative data correctly
  • Sets calculation_context on derived facts for transparency
  • Returns list[FinancialFact] — consistent with the rest of the API
  • Works across all concept types (Revenue, Net Income, etc.)

This is essentially a one-line change + docstring + tests. The value is in making this functionality officially supported so users don't have to rely on private internals.

Related Issues/Features:

  • TTMCalculator is already public and well-documented
  • _quarterize_facts() is used internally by the TTM computation — making it public doesn't change internal behavior

Feature requests are evaluated based on EdgarTools' core principles: Simple yet powerful, accurate financials, beginner-friendly, and joyful UX.

amcamc92 avatar Mar 12 '26 10:03 amcamc92

Good suggestion — exposing existing private functionality as a stable public API is exactly the kind of improvement we like.

Classification: P3 feature — scheduled for v5.24.0

This is essentially a one-liner + docstring + tests. The _quarterize_facts() method is already well-tested internally, so the risk is minimal.

Tracked as edgartools-0s8x.

dgunning avatar Mar 12 '26 12:03 dgunning

Shipped in v5.23.1 — public quarterize() API on TTMCalculator. Now on PyPI.

dgunning avatar Mar 12 '26 23:03 dgunning