From 6a79d126403cb82c8adeafef6f68b91f5eef0583 Mon Sep 17 00:00:00 2001 From: Danglewood <85772166+deeleeramone@users.noreply.github.com> Date: Sun, 4 May 2025 22:25:19 -0700 Subject: [PATCH] [BugFix] Fix `to_df` in `OptionsChainsData` (#7109) * fix optionschains to_df handling * lint --- .../core/openbb_core/app/model/obbject.py | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/openbb_platform/core/openbb_core/app/model/obbject.py b/openbb_platform/core/openbb_core/app/model/obbject.py index 38f760138e4..b38eba914ab 100644 --- a/openbb_platform/core/openbb_core/app/model/obbject.py +++ b/openbb_platform/core/openbb_core/app/model/obbject.py @@ -1,5 +1,8 @@ """The OBBject.""" +# pylint: disable=too-many-branches, too-many-locals, too-many-statements + +from collections.abc import Hashable from typing import ( TYPE_CHECKING, Any, @@ -7,7 +10,6 @@ from typing import ( ClassVar, Dict, Generic, - Hashable, List, Literal, Optional, @@ -122,7 +124,7 @@ class OBBject(Tagged, Generic[T]): """ return self.to_dataframe(index=index, sort_by=sort_by, ascending=ascending) - def to_dataframe( + def to_dataframe( # noqa: PLR0912 self, index: Optional[Union[str, None]] = "date", sort_by: Optional[str] = None, @@ -182,10 +184,25 @@ class OBBject(Tagged, Generic[T]): # BaseModel if isinstance(res, BaseModel): - res_dict = res.model_dump(exclude_unset=True, exclude_none=True) - series = Series(res_dict, name=res.__class__.__name__) - df = series.to_frame().reset_index() - sort_columns = False + res_dict = res.model_dump( # pylint: disable=no-member + exclude_unset=True, exclude_none=True + ) + # Model is serialized as a dict[str, list] or list[dict] + if ( + ( + isinstance(res_dict, dict) + and res_dict + and all(isinstance(v, list) for v in res_dict.values()) + ) + or isinstance(res_dict, list) + and all(isinstance(item, dict) for item in res_dict) + ): + df = DataFrame(res_dict) + sort_columns = False + else: + series = Series(res_dict, name=res.__class__.__name__) + df = series.to_frame().reset_index() + sort_columns = False # Dict[str, Any] elif isinstance(res, dict):