-
Notifications
You must be signed in to change notification settings - Fork 38
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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")) | ||
|
@@ -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): | ||
|
@@ -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)] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 usepct_change()
to allow customization of thefill_method
when necessary. This would provide more flexibility while still addressing the deprecation warning.