From ce39144dbf25da7e3c38c68816cdd814b11b2df7 Mon Sep 17 00:00:00 2001 From: Martin Karrin Date: Wed, 11 Sep 2024 07:35:12 +0200 Subject: [PATCH] new solution to mypy opinion on pandas.pct_change(fill_method). no longer explicitly required with new pandas_stubs --- openseries/_common_model.py | 70 ++++++++++++------------------------- openseries/series.py | 8 ++--- openseries/simulation.py | 8 ++--- poetry.lock | 6 ++-- 4 files changed, 30 insertions(+), 62 deletions(-) diff --git a/openseries/_common_model.py b/openseries/_common_model.py index 4cc39a46..077da48d 100644 --- a/openseries/_common_model.py +++ b/openseries/_common_model.py @@ -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]) @@ -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 ) @@ -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])) @@ -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) ) @@ -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(), ), ) @@ -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: @@ -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) ) @@ -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", @@ -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", @@ -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: @@ -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 @@ -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: @@ -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: @@ -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() @@ -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: @@ -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() @@ -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, diff --git a/openseries/series.py b/openseries/series.py index 40703c0f..ed1199d8 100644 --- a/openseries/series.py +++ b/openseries/series.py @@ -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( @@ -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() diff --git a/openseries/simulation.py b/openseries/simulation.py index 1e687dcc..84383dbe 100644 --- a/openseries/simulation.py +++ b/openseries/simulation.py @@ -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], ) @@ -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], ) diff --git a/poetry.lock b/poetry.lock index 380fe528..262bd9e1 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1053,13 +1053,13 @@ six = ">=1.5" [[package]] name = "pytz" -version = "2024.1" +version = "2024.2" description = "World timezone definitions, modern and historical" optional = false python-versions = "*" files = [ - {file = "pytz-2024.1-py2.py3-none-any.whl", hash = "sha256:328171f4e3623139da4983451950b28e95ac706e13f3f2630a879749e7a8b319"}, - {file = "pytz-2024.1.tar.gz", hash = "sha256:2a29735ea9c18baf14b448846bde5a48030ed267578472d8955cd0e7443a9812"}, + {file = "pytz-2024.2-py2.py3-none-any.whl", hash = "sha256:31c7c1817eb7fae7ca4b8c7ee50c72f93aa2dd863de768e1ef4245d426aa0725"}, + {file = "pytz-2024.2.tar.gz", hash = "sha256:2aa355083c50a0f93fa581709deac0c9ad65cca8a9e9beac660adcbd493c798a"}, ] [[package]]