Skip to content

Commit 2ab6074

Browse files
committed
fix #3: replace .append (deprecated) with .concat
1 parent 430d5d3 commit 2ab6074

File tree

7 files changed

+423
-377
lines changed

7 files changed

+423
-377
lines changed

okama/asset_list.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def recovery_periods(self) -> pd.Series:
374374
# Max recovery period date should not be in the border (it's not recovered)
375375
max_period = s2.max() if s2.idxmax().to_timestamp() != self.last_date else np.NAN
376376
ser = pd.Series(max_period, index=[name])
377-
max_recovery_periods = max_recovery_periods.append(ser)
377+
max_recovery_periods = pd.concat([max_recovery_periods, ser])
378378
return max_recovery_periods
379379

380380
def get_cagr(self, period: Optional[int] = None, real: bool = False) -> pd.Series:
@@ -692,7 +692,7 @@ def describe(
692692
ytd_return = self.get_cumulative_return(period="YTD")
693693
row = ytd_return.to_dict()
694694
row.update(period="YTD", property="Compound return")
695-
description = description.append(row, ignore_index=True)
695+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
696696
# CAGR for a list of periods
697697
if self.pl.years >= 1:
698698
for i in years:
@@ -702,32 +702,32 @@ def describe(
702702
else:
703703
row = {x: None for x in df.columns}
704704
row.update(period=f"{i} years", property="CAGR")
705-
description = description.append(row, ignore_index=True)
705+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
706706
# CAGR for full period
707707
row = self.get_cagr(period=None).to_dict()
708708
row.update(period=self._pl_txt, property="CAGR")
709-
description = description.append(row, ignore_index=True)
709+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
710710
# Dividend Yield
711711
row = self.assets_dividend_yield.iloc[-1].to_dict()
712712
row.update(period="LTM", property="Dividend yield")
713-
description = description.append(row, ignore_index=True)
713+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
714714
# risk for full period
715715
row = self.risk_annual.to_dict()
716716
row.update(period=self._pl_txt, property="Risk")
717-
description = description.append(row, ignore_index=True)
717+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
718718
# CVAR
719719
if self.pl.years >= 1:
720720
row = self.get_cvar_historic().to_dict()
721721
row.update(period=self._pl_txt, property="CVAR")
722-
description = description.append(row, ignore_index=True)
722+
description = pd.concat([description,pd.DataFrame(row, index=[0])], ignore_index=True)
723723
# max drawdowns
724724
row = self.drawdowns.min().to_dict()
725725
row.update(period=self._pl_txt, property="Max drawdowns")
726-
description = description.append(row, ignore_index=True)
726+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
727727
# max drawdowns dates
728728
row = self.drawdowns.idxmin().to_dict()
729729
row.update(period=self._pl_txt, property="Max drawdowns dates")
730-
description = description.append(row, ignore_index=True)
730+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
731731
# inception dates
732732
row = {}
733733
for ti in self.symbols:
@@ -737,7 +737,7 @@ def describe(
737737
row.update(period=None, property="Inception date")
738738
if hasattr(self, "inflation"):
739739
row.update({self.inflation: self.inflation_first_date.strftime("%Y-%m")})
740-
description = description.append(row, ignore_index=True)
740+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
741741
# last asset date
742742
row = {}
743743
for ti in self.symbols:
@@ -747,11 +747,11 @@ def describe(
747747
row.update(period=None, property="Last asset date")
748748
if hasattr(self, "inflation"):
749749
row.update({self.inflation: self.inflation_last_date.strftime("%Y-%m")})
750-
description = description.append(row, ignore_index=True)
750+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
751751
# last data date
752752
row = {x: self.last_date.strftime("%Y-%m") for x in df.columns}
753753
row.update(period=None, property="Common last data date")
754-
description = description.append(row, ignore_index=True)
754+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
755755
# rename columns
756756
if hasattr(self, "inflation"):
757757
description.rename(columns={self.inflation: "inflation"}, inplace=True)
@@ -1015,7 +1015,7 @@ def tracking_difference_annual(self) -> pd.DataFrame:
10151015
df = x[1]
10161016
wealth_index = helpers.Frame.get_wealth_indexes(df)
10171017
row = helpers.Index.tracking_difference(wealth_index).iloc[[-1]]
1018-
result = result.append(row, ignore_index=False)
1018+
result = pd.concat([result, row], ignore_index=False)
10191019
result.index = result.index.asfreq('Y')
10201020
return result
10211021

okama/frontier/multi_period.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ def _get_ef_points(self):
686686
for i, target_cagr in enumerate(self._target_cagr_range_left):
687687
start_time = time.time()
688688
row = self.minimize_risk(target_cagr)
689-
df = df.append(row, ignore_index=True)
689+
df = pd.concat([df, pd.DataFrame(row, index=[0])], ignore_index=True)
690690
end_time = time.time()
691691
if self.verbose:
692692
print(f"left EF point #{i + 1}/{self.n_points} is done in {end_time - start_time:.2f} sec.")
@@ -697,7 +697,7 @@ def _get_ef_points(self):
697697
for i, target_cagr in enumerate(range_right):
698698
start_time = time.time()
699699
row = self._maximize_risk(target_cagr)
700-
df = df.append(row, ignore_index=True)
700+
df = pd.concat([df, pd.DataFrame(row, index=[0])], ignore_index=True)
701701
end_time = time.time()
702702
if self.verbose:
703703
print(f"right EF point #{i + 1}/{n} is done in {end_time - start_time:.2f} sec.")
@@ -777,5 +777,5 @@ def get_monte_carlo(self, n: int = 100) -> pd.DataFrame:
777777
'Risk': risk,
778778
'CAGR': cagr
779779
}
780-
random_portfolios = random_portfolios.append(row, ignore_index=True)
780+
random_portfolios = pd.concat([random_portfolios, pd.DataFrame(row, index=[0])], ignore_index=True)
781781
return random_portfolios

okama/frontier/single_period.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ def ef_points(self) -> pd.DataFrame:
703703
df = pd.DataFrame(dtype="float")
704704
for x in target_rs:
705705
row = self.minimize_risk(x, monthly_return=True)
706-
df = df.append(row, ignore_index=True)
706+
df = pd.concat([df, pd.DataFrame(row, index=[0])], ignore_index=True)
707707
df = helpers.Frame.change_columns_order(df, ["Risk", "Mean return", "CAGR"])
708708
self._ef_points = df
709709
return self._ef_points
@@ -774,7 +774,7 @@ def mdp_points(self) -> pd.DataFrame:
774774
df = pd.DataFrame(dtype="float")
775775
for x in target_rs:
776776
row = self.get_most_diversified_portfolio(target_return=x, monthly_return=True)
777-
df = df.append(row, ignore_index=True)
777+
df = pd.concat([df, pd.DataFrame(row, index=[0])], ignore_index=True)
778778
df = helpers.Frame.change_columns_order(df, ["Risk", "Mean return", "CAGR"])
779779
self._mdp_points = df
780780
return self._mdp_points
@@ -854,7 +854,7 @@ def get_monte_carlo(self, n: int = 100, kind: str = "mean") -> pd.DataFrame:
854854
row = dict(Risk=risk, Return=mean_return)
855855
else:
856856
raise ValueError('kind should be "mean" or "cagr"')
857-
random_portfolios = random_portfolios.append(row, ignore_index=True)
857+
random_portfolios = pd.concat([random_portfolios, pd.DataFrame(row, index=[0])], ignore_index=True)
858858
return random_portfolios
859859

860860
def plot_transition_map(self, cagr: bool = True, figsize: Optional[tuple] = None) -> plt.axes:

okama/macro.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ def describe(self, years: Tuple[int, ...] = (1, 5, 10)) -> pd.DataFrame:
122122

123123
row2 = {self.name: helpers.Float.get_purchasing_power(inflation)}
124124
row2.update(period="YTD", property="1000 purchasing power")
125-
126-
description = description.append([row1, row2], ignore_index=True)
125+
rows_df = pd.DataFrame.from_records([row1, row2], index=[0, 1])
126+
description = pd.concat([description, rows_df], ignore_index=True)
127127

128128
# inflation properties for a given list of periods
129129
for i in years:
@@ -161,33 +161,31 @@ def describe(self, years: Tuple[int, ...] = (1, 5, 10)) -> pd.DataFrame:
161161

162162
row4.update(period=f"{i} years", property="1000 purchasing power")
163163

164-
description = description.append(row1, ignore_index=True)
165-
description = description.append(row2, ignore_index=True)
166-
description = description.append(row3, ignore_index=True)
167-
description = description.append(row4, ignore_index=True)
164+
df_rows = pd.DataFrame.from_records([row1, row2, row3, row4], index=[0, 1, 2, 3])
165+
description = pd.concat([description, df_rows], ignore_index=True)
168166
# Annual inflation for full period available
169167
ts = df
170168
full_inflation = helpers.Frame.get_cagr(ts)
171169
row = {self.name: full_inflation}
172170
row.update(period=self._pl_txt, property="annual inflation")
173-
description = description.append(row, ignore_index=True)
171+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
174172
# compound inflation
175173
comp_inflation = helpers.Frame.get_cumulative_return(ts)
176174
row = {self.name: comp_inflation}
177175
row.update(period=self._pl_txt, property="compound inflation")
178-
description = description.append(row, ignore_index=True)
176+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
179177
# max inflation for full period available
180178
max_inflation = self.rolling_inflation.nlargest(n=1)
181179
row = {self.name: max_inflation.iloc[0]}
182180
row.update(
183181
period=max_inflation.index.values[0].strftime("%Y-%m"),
184182
property="max 12m inflation",
185183
)
186-
description = description.append(row, ignore_index=True)
184+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
187185
# purchase power
188186
row = {self.name: helpers.Float.get_purchasing_power(comp_inflation)}
189187
row.update(period=self._pl_txt, property="1000 purchasing power")
190-
description = description.append(row, ignore_index=True)
188+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
191189
return helpers.Frame.change_columns_order(
192190
description, ["property", "period"], position="first"
193191
)

okama/portfolio.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ def describe(self, years: Tuple[int] = (1, 5, 10)) -> pd.DataFrame:
11111111
ytd_return = self.get_cumulative_return(period="YTD")
11121112
row = ytd_return.to_dict()
11131113
row.update(period="YTD", property="compound return")
1114-
description = description.append(row, ignore_index=True)
1114+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
11151115
# CAGR for a list of periods
11161116
if self.pl.years >= 1:
11171117
for i in years:
@@ -1125,44 +1125,44 @@ def describe(self, years: Tuple[int] = (1, 5, 10)) -> pd.DataFrame:
11251125
else {self.symbol: None}
11261126
)
11271127
row.update(period=f"{i} years", property="CAGR")
1128-
description = description.append(row, ignore_index=True)
1128+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
11291129
# CAGR for full period
11301130
row = self.get_cagr(period=None).to_dict()
11311131
row.update(period=self._pl_txt, property="CAGR",)
1132-
description = description.append(row, ignore_index=True)
1132+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
11331133
# Dividend Yield
11341134
value = self.dividend_yield.iloc[-1]
11351135
row = {self.symbol: value}
11361136
row.update(period="LTM", property=f"Dividend yield",)
1137-
description = description.append(row, ignore_index=True)
1137+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
11381138
# risk (standard deviation)
11391139
row = {self.symbol: self.risk_annual}
11401140
row.update(
11411141
period=self._pl_txt, property="Risk"
11421142
)
1143-
description = description.append(row, ignore_index=True)
1143+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
11441144
# CVAR
11451145
if self.pl.years >= 1:
11461146
row = {self.symbol: self.get_cvar_historic()}
11471147
row.update(
11481148
period=self._pl_txt,
11491149
property="CVAR",
11501150
)
1151-
description = description.append(row, ignore_index=True)
1151+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
11521152
# max drawdowns
11531153
row = {self.symbol: self.drawdowns.min()}
11541154
row.update(
11551155
period=self._pl_txt,
11561156
property="Max drawdown",
11571157
)
1158-
description = description.append(row, ignore_index=True)
1158+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
11591159
# max drawdowns dates
11601160
row = {self.symbol: self.drawdowns.idxmin()}
11611161
row.update(
11621162
period=self._pl_txt,
11631163
property="Max drawdown date",
11641164
)
1165-
description = description.append(row, ignore_index=True)
1165+
description = pd.concat([description, pd.DataFrame(row, index=[0])], ignore_index=True)
11661166
if hasattr(self, "inflation"):
11671167
description.rename(columns={self.inflation: "inflation"}, inplace=True)
11681168
description = helpers.Frame.change_columns_order(

0 commit comments

Comments
 (0)