Skip to content

Commit

Permalink
Fix portfolio and benchmark length matching (#4218)
Browse files Browse the repository at this point in the history
* Fix portfolio and benchmark length matching

* Fix linting

* Fix make_equal_length

* Handling pd.Series input

* Fix alloc command

* Fix metrics, yret and summary errors

---------

Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
Co-authored-by: James Maslek <jmaslek11@gmail.com>
  • Loading branch information
3 people authored Mar 9, 2023
1 parent ac34a41 commit 03c18f5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
10 changes: 10 additions & 0 deletions openbb_terminal/portfolio/allocation_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ def get_assets_allocation(
benchmark_assets_allocation = stockanalysis_model.get_etf_holdings(
benchmark_ticker
).reset_index()
if benchmark_assets_allocation.empty: # if not an etf or no data is available
benchmark_assets_allocation = pd.DataFrame(
data={
"symbol": [benchmark_ticker],
"Name": [benchmark_ticker],
"% Of Etf": ["100%"],
"Shares": [1],
},
index=[0],
)
benchmark_assets_allocation.rename(
columns={"symbol": "Symbol", "% Of Etf": "Benchmark"}, inplace=True
)
Expand Down
18 changes: 13 additions & 5 deletions openbb_terminal/portfolio/portfolio_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,20 @@ def make_equal_length(df1: pd.DataFrame, df2: pd.DataFrame):
Both DataFrames returned
"""
# Match the DataFrames so they share a similar length
if len(df1.index) > len(df2.index):
df1 = df1.loc[df2.index]
elif len(df2.index) > len(df1.index):
df2 = df2.loc[df1.index]
if isinstance(df1, pd.Series):
df1 = df1.to_frame()
if isinstance(df2, pd.Series):
df2 = df2.to_frame()
df2.columns = [str(i) + "2" for i in df2.columns]
df_merged = df1.join(df2, how="outer")
df_merged = df_merged.fillna(0)

return df1, df2
df1 = df_merged[df1.columns]
df2 = df_merged[df2.columns]

df2.columns = [i[:-1] for i in df2.columns]

return df1.iloc[:, 0], df2.iloc[:, 0]


def get_region_from_country(country: str) -> str:
Expand Down

0 comments on commit 03c18f5

Please sign in to comment.