Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed FutureWarning in Series.pct_change by specifying fill_method=None to avoid deprecated default behavior. #54

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added quantstats_lumi/.DS_Store
Binary file not shown.
6 changes: 5 additions & 1 deletion quantstats_lumi/_plotting/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ def snapshot(
returns.columns = strategy_colname

colors = _GRAYSCALE_COLORS if grayscale else _FLATUI_COLORS
returns = _utils.make_portfolio(returns.dropna(), 1, mode).pct_change().fillna(0)
returns = (
_utils.make_portfolio(returns.dropna(), 1, mode)
.pct_change(fill_method=None)
.fillna(0)
)

if figsize is None:
size = list(_plt.gcf().get_size_inches())
Expand Down
17 changes: 9 additions & 8 deletions quantstats_lumi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import io as _io
import datetime as _dt
import pandas as _pd
import inspect
import io as _io

import numpy as _np
import pandas as _pd
import yfinance as _yf

from . import stats as _stats
import inspect


def _mtd(df):
Expand Down Expand Up @@ -208,9 +210,9 @@ def _prepare_returns(data, rf=0.0, nperiods=None):
if isinstance(data, _pd.DataFrame):
for col in data.columns:
if data[col].dropna().min() >= 0 and data[col].dropna().max() > 1:
data[col] = data[col].pct_change()
data[col] = data[col].pct_change(fill_method=None)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

category Functionality

The change to use pct_change(fill_method=None) addresses the FutureWarning, but we should consider if this is the optimal approach for all scenarios. In some cases, we might want to fill NaN values differently. Consider adding a parameter to the functions that use pct_change() to allow customization of the fill_method when necessary. This would provide more flexibility while still addressing the deprecation warning.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

category Tests

The changes in this pull request involve the use of the 'fill_method' parameter in the 'pct_change()' function. It's important to ensure that the function behaves as expected when different arguments are passed to this parameter. I recommend adding parameterized tests to verify this. These tests should cover all possible values that could be passed to the 'fill_method' parameter.

elif data.min() >= 0 and data.max() > 1:
data = data.pct_change()
data = data.pct_change(fill_method=None)

# cleanup data
data = data.replace([_np.inf, -_np.inf], float("NaN"))
Expand Down Expand Up @@ -239,7 +241,7 @@ def download_returns(ticker, period="max", proxy=None):
params["start"] = period[0]
else:
params["period"] = period
return _yf.download(**params)["Close"].pct_change()
return _yf.download(**params)["Close"].pct_change(fill_method=None)


def _prepare_benchmark(benchmark=None, period="max", rf=0.0, prepare_returns=True):
Expand All @@ -259,14 +261,13 @@ def _prepare_benchmark(benchmark=None, period="max", rf=0.0, prepare_returns=Tru
benchmark = benchmark[benchmark.columns[0]].copy()

if isinstance(period, _pd.DatetimeIndex) and set(period) != set(benchmark.index):

# Adjust Benchmark to Strategy frequency
benchmark_prices = to_prices(benchmark, base=1)
new_index = _pd.date_range(start=period[0], end=period[-1], freq="D")
benchmark = (
benchmark_prices.reindex(new_index, method="bfill")
.reindex(period)
.pct_change()
.pct_change(fill_method=None)
.fillna(0)
)
benchmark = benchmark[benchmark.index.isin(period)]
Expand Down