Skip to content

Commit 16ff906

Browse files
ranaroussiclaude
andcommitted
Fix FutureWarning for deprecated fill_method in pandas pct_change()
Updated all pct_change() calls to explicitly use fill_method=None to prevent the deprecation warning: "The default fill_method='pad' in Series.pct_change is deprecated and will be removed in a future version." Changes: - utils.py: Fixed pct_change() in _prepare_returns, download_returns, and _prepare_benchmark - _plotting/wrappers.py: Fixed pct_change() in portfolio calculations - All instances now use fill_method=None with explicit fillna(0) where needed This ensures compatibility with pandas 2.x and future versions. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent c89404b commit 16ff906

3 files changed

Lines changed: 11 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ Changelog
1616
- Data is converted to UTC then made timezone-naive for consistent comparisons
1717
- Fixes "Cannot compare dtypes datetime64[ns] and datetime64[ns, UTC]" errors
1818

19+
- Fixed FutureWarning for pandas pct_change():
20+
- Updated all pct_change() calls to use fill_method=None parameter
21+
- Prevents "fill_method='pad' is deprecated" warnings in pandas 2.x
22+
- Ensures compatibility with future pandas versions
23+
1924
0.0.75
2025
------
2126

quantstats/_plotting/wrappers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def snapshot(
161161
# Select color scheme based on grayscale preference
162162
colors = _GRAYSCALE_COLORS if grayscale else _FLATUI_COLORS
163163
# Convert to portfolio format and calculate percentage changes
164-
returns = _utils.make_portfolio(returns.dropna(), 1, mode).pct_change().fillna(0)
164+
returns = _utils.make_portfolio(returns.dropna(), 1, mode).pct_change(fill_method=None).fillna(0)
165165

166166
# Use current figure size if not specified
167167
if figsize is None:

quantstats/utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -610,10 +610,10 @@ def _prepare_returns(data, rf=0.0, nperiods=None):
610610
col_clean = data[col].dropna()
611611
# Check if data looks like prices (positive values > 1)
612612
if col_clean.min() >= 0 and col_clean.max() > 1:
613-
data[col] = data[col].pct_change()
613+
data[col] = data[col].pct_change(fill_method=None)
614614
# Process Series data
615615
elif data.min() >= 0 and data.max() > 1:
616-
data = data.pct_change()
616+
data = data.pct_change(fill_method=None)
617617

618618
# cleanup data - replace infinite values with NaN
619619
data = data.replace([_np.inf, -_np.inf], float("NaN"))
@@ -688,8 +688,8 @@ def download_returns(ticker, period="max", proxy=None):
688688
params["period"] = period
689689

690690
# Download data and calculate returns
691-
df = safe_yfinance_download(proxy=proxy, **params)["Close"].pct_change() # type: ignore
692-
df = df.tz_localize(None)
691+
df = safe_yfinance_download(proxy=proxy, **params)["Close"].pct_change(fill_method=None) # type: ignore
692+
df = df.fillna(0).tz_localize(None)
693693
return df
694694

695695

@@ -720,7 +720,7 @@ def _prepare_benchmark(benchmark=None, period="max", rf=0.0, prepare_returns=Tru
720720
benchmark = (
721721
benchmark_prices.reindex(new_index, method="bfill")
722722
.reindex(period)
723-
.pct_change()
723+
.pct_change(fill_method=None)
724724
.fillna(0)
725725
)
726726
benchmark = benchmark[benchmark.index.isin(period)]

0 commit comments

Comments
 (0)