Skip to content

Commit 2e2eb78

Browse files
minor bug fix (.iloc[i] instead of .loc[i]) and optimization on portfolio (fmilthaler#32)
Using .iloc[i] instead of .loc[i], and calling pf._update only once, after portfolio was generated. This closes fmilthaler#31 Co-authored-by: Donin <[email protected]>
1 parent 40bef5b commit 2e2eb78

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

finquant/portfolio.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def add_stock(self, stock: Stock) -> None:
185185
self._add_stock_data(stock)
186186

187187
# update quantities of portfolio
188-
self._update()
188+
# self._update() # the update will be done at the end of building portfolio
189189

190190
def _add_stock_data(self, stock: Stock) -> None:
191191
# insert given data into portfolio stocks dataframe:
@@ -1023,11 +1023,13 @@ def _build_portfolio_from_df(
10231023
pf.market_index = Market(data=market_data)
10241024
for i in range(len(pf_allocation)):
10251025
# get name of stock
1026-
name = pf_allocation.loc[i].Name
1026+
name = pf_allocation.iloc[i].Name
10271027
# extract data column of said stock
10281028
stock_data = data.loc[:, [name]].copy(deep=True).squeeze()
10291029
# create Stock instance and add it to portfolio
1030-
pf.add_stock(Stock(pf_allocation.loc[i], data=stock_data))
1030+
pf.add_stock(Stock(pf_allocation.iloc[i], data=stock_data))
1031+
# update the portfolio
1032+
pf._update()
10311033
return pf
10321034

10331035

0 commit comments

Comments
 (0)