Skip to content

Commit 5a59c4e

Browse files
authored
DOC: Annotate lib.random_ohlc_data() as a Generator (#1162)
* 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
1 parent 4b829b7 commit 5a59c4e

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

backtesting/lib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from inspect import currentframe
1818
from itertools import compress
1919
from numbers import Number
20-
from typing import Callable, Optional, Sequence, Union
20+
from typing import Callable, Generator, Optional, Sequence, Union
2121

2222
import numpy as np
2323
import pandas as pd
@@ -332,7 +332,7 @@ def wrap_func(resampled, *args, **kwargs):
332332

333333

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

0 commit comments

Comments
 (0)