mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-11 20:01:16 +00:00
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 <h.joaquim@campus.fct.unl.pt> Co-authored-by: Colin Delahunty <72827203+colin99d@users.noreply.github.com>
This commit is contained in:
@@ -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.")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user