From bfb14e1a53f65f0857cc08b09ab52f979ee3fda6 Mon Sep 17 00:00:00 2001 From: Jalil F Date: Mon, 5 Sep 2022 09:13:06 -0400 Subject: [PATCH] Adding optional columns before compute with columns. (#2445) * Adding optional columns before compute with columns. * Improve logic flow of optional portfolio fields. Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com> Co-authored-by: hjoaquim Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com> --- openbb_terminal/portfolio/portfolio_model.py | 55 ++++++++++---------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/openbb_terminal/portfolio/portfolio_model.py b/openbb_terminal/portfolio/portfolio_model.py index ce74fe97530..260401d35ec 100644 --- a/openbb_terminal/portfolio/portfolio_model.py +++ b/openbb_terminal/portfolio/portfolio_model.py @@ -1014,6 +1014,33 @@ class PortfolioModel: try: console.print(" Preprocessing orderbook: ", end="") + + # if optional fields not in the orderbook add missing + optional_fields = [ + "Sector", + "Industry", + "Country", + "Region", + "Fees", + "Premium", + ] + if not set(optional_fields).issubset(set(self.__orderbook.columns)): + for field in optional_fields: + if field not in self.__orderbook.columns: + self.__orderbook[field] = np.nan + + # if optional fields for stock data has missing datapoints, fill them + if ( + self.__orderbook.loc[ + self.__orderbook["Type"] == "STOCK", + optional_fields, + ] + .isnull() + .values.any() + ): + # if any fields is empty for Stocks (overwrites any info there) + self.load_company_data() + # Convert Date to datetime self.__orderbook["Date"] = pd.to_datetime(self.__orderbook["Date"]) console.print(".", end="") @@ -1079,34 +1106,6 @@ class PortfolioModel: self.inception_date = self.__orderbook["Date"][0] console.print(".", end="") - # Populate fields Sector, Industry and Country - if not ( - {"Sector", "Industry", "Country", "Region"}.issubset( - set(self.__orderbook.columns) - ) - ): - # if fields not in the orderbook add missing - if "Sector" not in self.__orderbook.columns: - self.__orderbook["Sector"] = np.nan - if "Industry" not in self.__orderbook.columns: - self.__orderbook["Industry"] = np.nan - if "Country" not in self.__orderbook.columns: - self.__orderbook["Country"] = np.nan - if "Region" not in self.__orderbook.columns: - self.__orderbook["Region"] = np.nan - - self.load_company_data() - elif ( - self.__orderbook.loc[ - self.__orderbook["Type"] == "STOCK", - ["Sector", "Industry", "Country", "Region"], - ] - .isnull() - .values.any() - ): - # if any fields is empty for Stocks (overwrites any info there) - self.load_company_data() - except Exception: console.print("\nCould not preprocess orderbook.")