Skip to content

Commit

Permalink
adjustments to align with new pandas-stubs version
Browse files Browse the repository at this point in the history
  • Loading branch information
karrmagadgeteer2 committed Sep 10, 2024
1 parent b27a7df commit e5b810d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 36 deletions.
60 changes: 33 additions & 27 deletions openseries/_common_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,9 @@ 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=cast(str, None)).min()
)
result = wmdf.resample("BME").last().pct_change(
fill_method=None, # type: ignore[arg-type,unused-ignore]
).min()

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

result = (
self.tsdf.loc[cast(int, earlier) : cast(int, later)]
.pct_change(fill_method=cast(str, None))
.pct_change(fill_method=None) # type: ignore[arg-type,unused-ignore]
.mean()
* time_factor
)
Expand Down Expand Up @@ -1082,9 +1082,9 @@ def vol_func(
time_factor = how_many / fraction

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

if self.tsdf.shape[1] == 1:
return float(cast(SupportsFloat, result.iloc[0]))
Expand Down Expand Up @@ -1280,22 +1280,22 @@ 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=cast(str, None))
.pct_change(fill_method=None) # type: ignore[arg-type,unused-ignore]
.quantile(1 - level, interpolation=interpolation)
- self.tsdf.loc[cast(int, earlier) : cast(int, later)]
.pct_change(fill_method=cast(str, None))
.pct_change(fill_method=None) # type: ignore[arg-type,unused-ignore]
.sum()
/ len(
self.tsdf.loc[cast(int, earlier) : cast(int, later)].pct_change(
fill_method=cast(str, None),
fill_method=None, # type: ignore[arg-type,unused-ignore]
),
)
)
else:
imp_vol = (
-sqrt(time_factor)
* self.tsdf.loc[cast(int, earlier) : cast(int, later)]
.pct_change(fill_method=cast(str, None))
.pct_change(fill_method=None) # type: ignore[arg-type,unused-ignore]
.quantile(1 - level, interpolation=interpolation)
/ norm.ppf(level)
)
Expand Down Expand Up @@ -1358,14 +1358,16 @@ 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=cast(str, None))
.pct_change(fill_method=None) # type: ignore[arg-type,unused-ignore]
.sort_values()
.iloc[
: int(
ceil(
(1 - level)
* cvar_df.loc[:, x] # type: ignore[index]
.pct_change(fill_method=cast(str, None))
.pct_change(
fill_method=None, # type: ignore[arg-type,unused-ignore]
)
.count(),
),
)
Expand Down Expand Up @@ -1425,7 +1427,7 @@ def downside_deviation_func(
)
how_many = (
self.tsdf.loc[cast(int, earlier) : cast(int, later)]
.pct_change(fill_method=cast(str, None))
.pct_change(fill_method=None) # type: ignore[arg-type,unused-ignore]
.count(numeric_only=True)
)
if periods_in_a_year_fixed:
Expand All @@ -1440,7 +1442,7 @@ def downside_deviation_func(

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

Expand Down Expand Up @@ -1543,7 +1545,7 @@ def skew_func(
)
result: NDArray[float64] = skew(
a=self.tsdf.loc[cast(int, earlier) : cast(int, later)]
.pct_change(fill_method=cast(str, None))
.pct_change(fill_method=None) # type: ignore[arg-type,unused-ignore]
.to_numpy(),
bias=True,
nan_policy="omit",
Expand Down Expand Up @@ -1591,7 +1593,7 @@ def kurtosis_func(
)
result: NDArray[float64] = kurtosis(
self.tsdf.loc[cast(int, earlier) : cast(int, later)].pct_change(
fill_method=cast(str, None),
fill_method=None, # type: ignore[arg-type,unused-ignore]
),
fisher=True,
bias=True,
Expand Down Expand Up @@ -1688,17 +1690,17 @@ def positive_share_func(
)
pos = (
self.tsdf.loc[cast(int, earlier) : cast(int, later)]
.pct_change(fill_method=cast(str, None))[1:][
.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=cast(str, None),
fill_method=None, # type: ignore[arg-type,unused-ignore]
)[1:]
> zero
]
.count()
)
tot = (
self.tsdf.loc[cast(int, earlier) : cast(int, later)]
.pct_change(fill_method=cast(str, None))[1:]
.pct_change(fill_method=None)[1:] # type: ignore[arg-type,unused-ignore]
.count()
)
share = pos / tot
Expand Down Expand Up @@ -1877,7 +1879,7 @@ def omega_ratio_func(
to_dt=to_date,
)
retdf = self.tsdf.loc[cast(int, earlier) : cast(int, later)].pct_change(
fill_method=cast(str, None),
fill_method=None, # type: ignore[arg-type,unused-ignore]
)
pos = retdf[retdf > min_accepted_return].sub(min_accepted_return).sum()
neg = retdf[retdf < min_accepted_return].sub(min_accepted_return).sum()
Expand Down Expand Up @@ -1966,7 +1968,9 @@ 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]
resultdf = DataFrame(vrdf.pct_change(
fill_method=None, # type: ignore[arg-type,unused-ignore]
))
result = resultdf.loc[period] + 1
cal_period = result.cumprod(axis="index").iloc[-1] - 1
if self.tsdf.shape[1] == 1:
Expand Down Expand Up @@ -2018,7 +2022,7 @@ def var_down_func(
)
result = (
self.tsdf.loc[cast(int, earlier) : cast(int, later)]
.pct_change(fill_method=cast(str, None))
.pct_change(fill_method=None) # type: ignore[arg-type,unused-ignore]
.quantile(1 - level, interpolation=interpolation)
)

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

Expand Down Expand Up @@ -2183,7 +2187,7 @@ def rolling_return(
ret_label = cast(tuple[str], self.tsdf.iloc[:, column].name)[0]
retseries = (
self.tsdf.iloc[:, column]
.pct_change(fill_method=cast(str, None))
.pct_change(fill_method=None) # type: ignore[arg-type,unused-ignore]
.rolling(observations, min_periods=observations)
.sum()
)
Expand Down Expand Up @@ -2260,7 +2264,9 @@ 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=cast(str, None))
dframe = self.tsdf.iloc[:, column].pct_change(
fill_method=None, # type: ignore[arg-type,unused-ignore]
)
volseries = dframe.rolling(
observations,
min_periods=observations,
Expand Down
8 changes: 6 additions & 2 deletions openseries/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,9 @@ 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]
self.tsdf = self.tsdf.pct_change(
fill_method=None, # type: ignore[arg-type,unused-ignore]
)
self.tsdf.iloc[0] = 0
self.valuetype = ValueType.RTRN
self.tsdf.columns = MultiIndex.from_arrays(
Expand Down Expand Up @@ -696,7 +698,9 @@ 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]
ra_df = self.tsdf.pct_change(
fill_method=None, # type: ignore[arg-type,unused-ignore]
)
returns_input = False
ra_df = ra_df.dropna()

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

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

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ genbadge = {version = ">=1.1.1,<2.0.0", extras = ["coverage"]}
mypy = "^1.11.2"
pandas-stubs = ">=2.1.2,<3.0.0"
pre-commit = ">=3.7.1,<4.0.0"
pytest = ">=8.2.2,<9.0.0"
pytest = "^8.3.3"
ruff = "^0.6.4"
types-openpyxl = ">=3.1.2,<4.0.0"
types-python-dateutil = ">=2.8.2,<3.0.0"
Expand Down

0 comments on commit e5b810d

Please sign in to comment.