From 90b8923018e134dcbe2450b00f858e63ea7e5ace Mon Sep 17 00:00:00 2001 From: CtrlX <76150709+gitctrlx@users.noreply.github.com> Date: Thu, 1 Aug 2024 18:52:59 +0800 Subject: [PATCH] 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. --- backtesting/lib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtesting/lib.py b/backtesting/lib.py index 14c01efd..e40e57be 100644 --- a/backtesting/lib.py +++ b/backtesting/lib.py @@ -329,7 +329,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)