Skip to content

Commit

Permalink
DOC: Annotate lib.random_ohlc_data() as a Generator (#1162)
Browse files Browse the repository at this point in the history
* Update lib.py

Replacing pd.DataFrame with Generator[pd.DataFrame, None, None]
The reason for replacing pd.DataFrame with Generator[pd.DataFrame, None, None] is to better reflect the actual output type of the random_ohlc_data function. Here are the specific reasons and benefits:

Reasons:
Accuracy of Output Type: The original code declared that the function returns a pd.DataFrame, but in reality, the function is a generator that yields multiple pd.DataFrame objects. Using Generator more accurately describes the function's behavior.
Clarity of Type Hinting: Using Generator allows the code readers and users to more easily understand that the function returns a generator rather than a single DataFrame. This helps prevent potential misunderstandings and misuse.
Benefits:
Performance Improvement: Generators can generate data on-demand rather than generating all data at once, saving memory and improving performance, especially when dealing with large datasets.
Lazy Evaluation: Generators allow for lazy evaluation, meaning data frames are only generated when needed. This can improve the efficiency and responsiveness of the code.
Better Code Maintainability: Explicitly using generators makes the intent of the code clearer, enhancing readability and maintainability, making it easier for other developers to understand and maintain the code.

* Import typing.Generator
  • Loading branch information
gitctrlx authored Jan 22, 2025
1 parent 4b829b7 commit 5a59c4e
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions backtesting/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from inspect import currentframe
from itertools import compress
from numbers import Number
from typing import Callable, Optional, Sequence, Union
from typing import Callable, Generator, Optional, Sequence, Union

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -332,7 +332,7 @@ def wrap_func(resampled, *args, **kwargs):


def random_ohlc_data(example_data: pd.DataFrame, *,
frac=1., random_state: Optional[int] = None) -> pd.DataFrame:
frac=1., random_state: Optional[int] = None) -> Generator[pd.DataFrame, None, None]:
"""
OHLC data generator. The generated OHLC data has basic
[descriptive statistics](https://en.wikipedia.org/wiki/Descriptive_statistics)
Expand Down

0 comments on commit 5a59c4e

Please sign in to comment.