Skip to content

Commit

Permalink
new solution to mypy opinion on pandas.pct_change(fill_method). no lo…
Browse files Browse the repository at this point in the history
…nger explicitly required with new pandas_stubs
  • Loading branch information
karrmagadgeteer2 committed Sep 11, 2024
1 parent e5b810d commit ce39144
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 62 deletions.
70 changes: 23 additions & 47 deletions openseries/_common_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,7 @@ def worst_month(self: Self) -> float | Series[float]:
"""
wmdf = self.tsdf.copy()
wmdf.index = DatetimeIndex(wmdf.index)
result = wmdf.resample("BME").last().pct_change(
fill_method=None, # type: ignore[arg-type,unused-ignore]
).min()
result = wmdf.resample("BME").last().pct_change().min()

if self.tsdf.shape[1] == 1:
return float(result.iloc[0])
Expand Down Expand Up @@ -1023,8 +1021,7 @@ def arithmetic_ret_func(

result = (
self.tsdf.loc[cast(int, earlier) : cast(int, later)]
.pct_change(fill_method=None) # type: ignore[arg-type,unused-ignore]
.mean()
.pct_change().mean()
* time_factor
)

Expand Down Expand Up @@ -1082,9 +1079,7 @@ def vol_func(
time_factor = how_many / fraction

data = self.tsdf.loc[cast(int, earlier) : cast(int, later)]
result = data.pct_change(
fill_method=None, # type: ignore[arg-type,unused-ignore]
).std().mul(sqrt(time_factor))
result = data.pct_change().std().mul(sqrt(time_factor))

if self.tsdf.shape[1] == 1:
return float(cast(SupportsFloat, result.iloc[0]))
Expand Down Expand Up @@ -1280,22 +1275,20 @@ def _var_implied_vol_and_target_func(
if drift_adjust:
imp_vol = (-sqrt(time_factor) / norm.ppf(level)) * (
self.tsdf.loc[cast(int, earlier) : cast(int, later)]
.pct_change(fill_method=None) # type: ignore[arg-type,unused-ignore]
.pct_change()
.quantile(1 - level, interpolation=interpolation)
- self.tsdf.loc[cast(int, earlier) : cast(int, later)]
.pct_change(fill_method=None) # type: ignore[arg-type,unused-ignore]
.pct_change()
.sum()
/ len(
self.tsdf.loc[cast(int, earlier) : cast(int, later)].pct_change(
fill_method=None, # type: ignore[arg-type,unused-ignore]
),
self.tsdf.loc[cast(int, earlier) : cast(int, later)].pct_change(),
)
)
else:
imp_vol = (
-sqrt(time_factor)
* self.tsdf.loc[cast(int, earlier) : cast(int, later)]
.pct_change(fill_method=None) # type: ignore[arg-type,unused-ignore]
.pct_change()
.quantile(1 - level, interpolation=interpolation)
/ norm.ppf(level)
)
Expand Down Expand Up @@ -1358,16 +1351,14 @@ def cvar_down_func(
cvar_df = self.tsdf.loc[cast(int, earlier) : cast(int, later)].copy(deep=True)
result = [
cvar_df.loc[:, x] # type: ignore[call-overload,index]
.pct_change(fill_method=None) # type: ignore[arg-type,unused-ignore]
.pct_change()
.sort_values()
.iloc[
: int(
ceil(
(1 - level)
* cvar_df.loc[:, x] # type: ignore[index]
.pct_change(
fill_method=None, # type: ignore[arg-type,unused-ignore]
)
.pct_change()
.count(),
),
)
Expand Down Expand Up @@ -1427,7 +1418,7 @@ def downside_deviation_func(
)
how_many = (
self.tsdf.loc[cast(int, earlier) : cast(int, later)]
.pct_change(fill_method=None) # type: ignore[arg-type,unused-ignore]
.pct_change()
.count(numeric_only=True)
)
if periods_in_a_year_fixed:
Expand All @@ -1442,7 +1433,7 @@ def downside_deviation_func(

dddf = (
self.tsdf.loc[cast(int, earlier) : cast(int, later)]
.pct_change(fill_method=None) # type: ignore[arg-type,unused-ignore]
.pct_change()
.sub(min_accepted_return / time_factor)
)

Expand Down Expand Up @@ -1545,7 +1536,7 @@ def skew_func(
)
result: NDArray[float64] = skew(
a=self.tsdf.loc[cast(int, earlier) : cast(int, later)]
.pct_change(fill_method=None) # type: ignore[arg-type,unused-ignore]
.pct_change()
.to_numpy(),
bias=True,
nan_policy="omit",
Expand Down Expand Up @@ -1592,9 +1583,7 @@ def kurtosis_func(
to_dt=to_date,
)
result: NDArray[float64] = kurtosis(
self.tsdf.loc[cast(int, earlier) : cast(int, later)].pct_change(
fill_method=None, # type: ignore[arg-type,unused-ignore]
),
self.tsdf.loc[cast(int, earlier) : cast(int, later)].pct_change(),
fisher=True,
bias=True,
nan_policy="omit",
Expand Down Expand Up @@ -1690,18 +1679,15 @@ def positive_share_func(
)
pos = (
self.tsdf.loc[cast(int, earlier) : cast(int, later)]
.pct_change(fill_method=None)[1:][ # type: ignore[arg-type,unused-ignore]
self.tsdf.loc[cast(int, earlier) : cast(int, later)].pct_change(
fill_method=None, # type: ignore[arg-type,unused-ignore]
)[1:]
.pct_change()[1:][
self.tsdf.loc[cast(int, earlier) : cast(int, later)].pct_change()[1:]
> zero
]
.count()
)
tot = (
self.tsdf.loc[cast(int, earlier) : cast(int, later)]
.pct_change(fill_method=None)[1:] # type: ignore[arg-type,unused-ignore]
.count()
.pct_change().count()
)
share = pos / tot
if self.tsdf.shape[1] == 1:
Expand Down Expand Up @@ -1878,9 +1864,7 @@ def omega_ratio_func(
from_dt=from_date,
to_dt=to_date,
)
retdf = self.tsdf.loc[cast(int, earlier) : cast(int, later)].pct_change(
fill_method=None, # type: ignore[arg-type,unused-ignore]
)
retdf = self.tsdf.loc[cast(int, earlier) : cast(int, later)].pct_change()
pos = retdf[retdf > min_accepted_return].sub(min_accepted_return).sum()
neg = retdf[retdf < min_accepted_return].sub(min_accepted_return).sum()
ratio = pos / -neg
Expand Down Expand Up @@ -1968,9 +1952,7 @@ def value_ret_calendar_period(
period = "-".join([str(year), str(month).zfill(2)])
vrdf = self.tsdf.copy()
vrdf.index = DatetimeIndex(vrdf.index)
resultdf = DataFrame(vrdf.pct_change(
fill_method=None, # type: ignore[arg-type,unused-ignore]
))
resultdf = DataFrame(vrdf.pct_change())
result = resultdf.loc[period] + 1
cal_period = result.cumprod(axis="index").iloc[-1] - 1
if self.tsdf.shape[1] == 1:
Expand Down Expand Up @@ -2022,8 +2004,7 @@ def var_down_func(
)
result = (
self.tsdf.loc[cast(int, earlier) : cast(int, later)]
.pct_change(fill_method=None) # type: ignore[arg-type,unused-ignore]
.quantile(1 - level, interpolation=interpolation)
.pct_change().quantile(1 - level, interpolation=interpolation)
)

if self.tsdf.shape[1] == 1:
Expand Down Expand Up @@ -2070,7 +2051,7 @@ def worst_func(
)
result = (
self.tsdf.loc[cast(int, earlier) : cast(int, later)]
.pct_change(fill_method=None) # type: ignore[arg-type,unused-ignore]
.pct_change()
.rolling(observations, min_periods=observations)
.sum()
.min()
Expand Down Expand Up @@ -2116,9 +2097,7 @@ def z_score_func(
from_dt=from_date,
to_dt=to_date,
)
zscframe = self.tsdf.loc[cast(int, earlier) : cast(int, later)].pct_change(
fill_method=None, # type: ignore[arg-type,unused-ignore]
)
zscframe = self.tsdf.loc[cast(int, earlier) : cast(int, later)].pct_change()
result = (zscframe.iloc[-1] - zscframe.mean()) / zscframe.std()

if self.tsdf.shape[1] == 1:
Expand Down Expand Up @@ -2187,8 +2166,7 @@ def rolling_return(
ret_label = cast(tuple[str], self.tsdf.iloc[:, column].name)[0]
retseries = (
self.tsdf.iloc[:, column]
.pct_change(fill_method=None) # type: ignore[arg-type,unused-ignore]
.rolling(observations, min_periods=observations)
.pct_change().rolling(observations, min_periods=observations)
.sum()
)
retdf = retseries.dropna().to_frame()
Expand Down Expand Up @@ -2264,9 +2242,7 @@ def rolling_vol(
else:
time_factor = self.periods_in_a_year
vol_label = cast(tuple[str, ValueType], self.tsdf.iloc[:, column].name)[0]
dframe = self.tsdf.iloc[:, column].pct_change(
fill_method=None, # type: ignore[arg-type,unused-ignore]
)
dframe = self.tsdf.iloc[:, column].pct_change()
volseries = dframe.rolling(
observations,
min_periods=observations,
Expand Down
8 changes: 2 additions & 6 deletions openseries/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,7 @@ def value_to_ret(self: Self) -> Self:
The returns of the values in the series
"""
self.tsdf = self.tsdf.pct_change(
fill_method=None, # type: ignore[arg-type,unused-ignore]
)
self.tsdf = self.tsdf.pct_change()
self.tsdf.iloc[0] = 0
self.valuetype = ValueType.RTRN
self.tsdf.columns = MultiIndex.from_arrays(
Expand Down Expand Up @@ -698,9 +696,7 @@ def running_adjustment(
returns_input = True
else:
values = [cast(float, self.tsdf.iloc[0, 0])]
ra_df = self.tsdf.pct_change(
fill_method=None, # type: ignore[arg-type,unused-ignore]
)
ra_df = self.tsdf.pct_change()
returns_input = False
ra_df = ra_df.dropna()

Expand Down
8 changes: 2 additions & 6 deletions openseries/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ def realized_mean_return(self: Self) -> float:
return cast(
float,
(
self.results.pct_change(
fill_method=None, # type: ignore[arg-type,unused-ignore]
).mean()
self.results.pct_change().mean()
* self.trading_days_in_year
).iloc[0],
)
Expand All @@ -143,9 +141,7 @@ def realized_vol(self: Self) -> float:
return cast(
float,
(
self.results.pct_change(
fill_method=None, # type: ignore[arg-type,unused-ignore]
).std()
self.results.pct_change().std()
* sqrt(self.trading_days_in_year)
).iloc[0],
)
Expand Down
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ce39144

Please sign in to comment.