mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-04 00:12:29 +00:00
add concat param
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from typing import Any, Dict, Generic, List, Optional, TypeVar
|
||||
from typing import Any, Dict, Generic, List, Optional, TypeVar, Union
|
||||
|
||||
import pandas as pd
|
||||
from pydantic import Field
|
||||
@@ -49,13 +49,18 @@ class CommandOutput(GenericModel, Generic[T], Tagged):
|
||||
+ "\n".join([f"{k}: {v}" for k, v in self.dict().items()])
|
||||
)
|
||||
|
||||
def to_dataframe(self) -> pd.DataFrame:
|
||||
def to_dataframe(self, concat: bool = True) -> Union[pd.DataFrame, Dict[str, pd.DataFrame]]:
|
||||
"""Converts results field to pandas dataframe.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
concat : bool, optional
|
||||
If True, it concatenates the dataframes, by default True.
|
||||
|
||||
Returns
|
||||
-------
|
||||
pd.DataFrame
|
||||
Pandas dataframe.
|
||||
Union[pd.DataFrame, Dict[str, pd.DataFrame]]
|
||||
Pandas dataframe or dictionary of dataframes.
|
||||
"""
|
||||
if self.results is None:
|
||||
raise OpenBBError("Results not found.")
|
||||
@@ -67,13 +72,14 @@ class CommandOutput(GenericModel, Generic[T], Tagged):
|
||||
dict_of_df = {
|
||||
k: basemodel_to_df(v, "date") for k, v in self.results.items()
|
||||
}
|
||||
df = pd.concat(dict_of_df, axis=1)
|
||||
df = pd.concat(dict_of_df, axis=1) if concat else dict_of_df
|
||||
|
||||
elif isinstance(self.results, list):
|
||||
df = basemodel_to_df(self.results, "date")
|
||||
else:
|
||||
df = basemodel_to_df(self.results, "date")
|
||||
except Exception as e:
|
||||
raise OpenBBError("Failed to convert results to dataframe.") from e
|
||||
raise OpenBBError("Failed to convert results to DataFrame.") from e
|
||||
|
||||
return df
|
||||
|
||||
|
||||
Reference in New Issue
Block a user