Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] OptionsChains Properties #6564

Merged
merged 30 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
207c7c2
stashing
deeleeramone Jul 4, 2024
b414e76
Merge branch 'develop' of https://github.com/OpenBB-finance/OpenBBTer…
deeleeramone Jul 5, 2024
6bba117
update providers
deeleeramone Jul 8, 2024
e02db6d
edge cases
deeleeramone Jul 8, 2024
902cffb
linter
deeleeramone Jul 8, 2024
e1290c9
grammar police
deeleeramone Jul 8, 2024
64eeca5
yfinance tests?
deeleeramone Jul 8, 2024
9c8f2c8
linter
deeleeramone Jul 8, 2024
a1639d1
more linting
deeleeramone Jul 8, 2024
faca306
one test update
deeleeramone Jul 8, 2024
ed1fc64
Merge branch 'develop' into feature/options-chains-properties
deeleeramone Jul 9, 2024
c6037e2
Merge branch 'develop' into feature/options-chains-properties
deeleeramone Jul 9, 2024
6d019bc
merge branch develop
deeleeramone Jul 14, 2024
cbe71c1
move properties code to separate file
deeleeramone Jul 14, 2024
1ee702f
to_df
deeleeramone Jul 14, 2024
ac1d61f
tmx return type
deeleeramone Jul 14, 2024
e2fd1c6
tradier tests
deeleeramone Jul 14, 2024
0c54199
cboe cassettes
deeleeramone Jul 14, 2024
5444b79
Merge branch 'develop' into feature/options-chains-properties
deeleeramone Jul 17, 2024
096ff88
Merge branch 'develop' of https://github.com/OpenBB-finance/OpenBBTer…
deeleeramone Jul 18, 2024
750f40b
elif instead of if
deeleeramone Jul 18, 2024
cccbf49
sort expirations and strikes
deeleeramone Jul 18, 2024
8402ca1
Merge branch 'develop' into feature/options-chains-properties
deeleeramone Jul 21, 2024
92000d9
test cassettes
deeleeramone Jul 22, 2024
a0e367f
Merge branch 'develop' into feature/options-chains-properties
IgorWounds Jul 24, 2024
cf0988e
Merge branch 'develop' into feature/options-chains-properties
jmaslek Jul 24, 2024
62e75aa
Merge branch 'develop' into feature/options-chains-properties
IgorWounds Jul 29, 2024
c5ae3dc
Merge branch 'develop' into feature/options-chains-properties
IgorWounds Jul 29, 2024
9bb786f
change tests
deeleeramone Jul 30, 2024
f3e9957
Merge branch 'develop' into feature/options-chains-properties
deeleeramone Jul 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 64 additions & 17 deletions openbb_platform/core/openbb_core/app/model/obbject.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,54 @@ def __repr__(self) -> str:
return f"{self.__class__.__name__}\n\n" + "\n".join(items)

def to_df(
self, index: Optional[Union[str, None]] = "date", sort_by: Optional[str] = None
self,
index: Optional[Union[str, None]] = "date",
sort_by: Optional[str] = None,
ascending: Optional[bool] = None,
) -> "DataFrame":
"""Alias for `to_dataframe`."""
return self.to_dataframe(index=index, sort_by=sort_by)
"""Alias for `to_dataframe`.

Supports converting creating Pandas DataFrames from the following
serializable data formats:

- List[BaseModel]
- List[Dict]
- List[List]
- List[str]
- List[int]
- List[float]
- Dict[str, Dict]
- Dict[str, List]
- Dict[str, BaseModel]

Other supported formats:
- str

Parameters
----------
index : Optional[str]
Column name to use as index.
sort_by : Optional[str]
Column name to sort by.
ascending: Optional[bool]
Sort by ascending for each column specified in `sort_by`.

Returns
-------
DataFrame
Pandas DataFrame.
"""
return self.to_dataframe(index=index, sort_by=sort_by, ascending=ascending)

def to_dataframe(
self, index: Optional[Union[str, None]] = "date", sort_by: Optional[str] = None
self,
index: Optional[Union[str, None]] = "date",
sort_by: Optional[str] = None,
ascending: Optional[bool] = None,
) -> "DataFrame":
"""Convert results field to pandas dataframe.
"""Convert results field to Pandas DataFrame.

Supports converting creating pandas DataFrames from the following
Supports converting creating Pandas DataFrames from the following
serializable data formats:

- List[BaseModel]
Expand All @@ -116,11 +153,13 @@ def to_dataframe(
Column name to use as index.
sort_by : Optional[str]
Column name to sort by.
ascending: Optional[bool]
Sort by ascending for each column specified in `sort_by`.

Returns
-------
DataFrame
Pandas dataframe.
Pandas DataFrame.
"""
# pylint: disable=import-outside-toplevel
from pandas import DataFrame, concat # noqa
Expand All @@ -142,8 +181,13 @@ def is_list_of_basemodel(items: Union[List[T], T]) -> bool:
df = None
sort_columns = True

# BaseModel
if isinstance(res, BaseModel):
df = DataFrame(res.model_dump(exclude_unset=True, exclude_none=True))
sort_columns = False

# List[Dict]
if isinstance(res, list) and len(res) == 1 and isinstance(res[0], dict):
elif isinstance(res, list) and len(res) == 1 and isinstance(res[0], dict):
r = res[0]
dict_of_df = {}

Expand Down Expand Up @@ -178,25 +222,29 @@ def is_list_of_basemodel(items: Union[List[T], T]) -> bool:
else:
try:
df = DataFrame(res) # type: ignore[call-overload]
# Set index, if any
if df is not None and index is not None and index in df.columns:
df.set_index(index, inplace=True)

except ValueError:
if isinstance(res, dict):
df = DataFrame([res])

if df is None:
raise OpenBBError("Unsupported data format.")

# Set index, if any
if index is not None and index in df.columns:
df.set_index(index, inplace=True)

# Drop columns that are all NaN, but don't rearrange columns
if sort_columns:
df.sort_index(axis=1, inplace=True)
df = df.dropna(axis=1, how="all")

# Sort by specified column
if sort_by:
df.sort_values(by=sort_by, inplace=True)
df.sort_values(
by=sort_by,
ascending=ascending if ascending is not None else True,
inplace=True,
)

except OpenBBError as e:
raise e
Expand All @@ -213,7 +261,7 @@ def is_list_of_basemodel(items: Union[List[T], T]) -> bool:

return df

def to_polars(self) -> "PolarsDataFrame":
def to_polars(self) -> "PolarsDataFrame": # type: ignore
"""Convert results field to polars dataframe."""
try:
from polars import from_pandas # type: ignore # pylint: disable=import-outside-toplevel
Expand All @@ -234,14 +282,13 @@ def to_dict(
"dict", "list", "series", "split", "tight", "records", "index"
] = "list",
) -> Union[Dict[Hashable, Any], List[Dict[Hashable, Any]]]:
"""Convert results field to a dictionary using any of pandas to_dict options.
"""Convert results field to a dictionary using any of Pandas `to_dict` options.

Parameters
----------
orient : Literal["dict", "list", "series", "split", "tight", "records", "index"]
Value to pass to `.to_dict()` method


Returns
-------
Union[Dict[Hashable, Any], List[Dict[Hashable, Any]]]
Expand Down Expand Up @@ -278,7 +325,7 @@ def to_llm(self) -> Union[Dict[Hashable, Any], List[Dict[Hashable, Any]]]:
date_unit="s",
)

return results
return results # type: ignore

def show(self, **kwargs: Any) -> None:
"""Display chart."""
Expand Down
Loading
Loading