Skip to content

Commit 9f4dc15

Browse files
authored
Filter FutureWarning for idxmin()
See: pandas-dev/pandas#54226 (comment)
1 parent 9f846c9 commit 9f4dc15

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/barrier_method/barrier_method.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,14 @@ def _identify_barrier_hit(self, barrier: pd.DataFrame) -> pd.Series:
122122
Returns:
123123
pandas.DataFrame: Identified barrier hits.
124124
"""
125-
# .idxmin() returns np.nan if no barriers have been hit
126-
# => .fillna(0) labels the "no barrier hit" condition
127-
barrier = barrier.idxmin(axis=1, skipna=True).fillna(0).astype(int)
125+
# pd.Series([None]).idxmax(skipna=True) now raises a FutureWarning and will throw a ValueError eventually.
126+
# The solution is overly verbose, and we hope for something better.
127+
# See: https://github.com/pandas-dev/pandas/pull/54226#issuecomment-1794973298
128+
with warnings.catch_warnings():
129+
warnings.simplefilter("ignore", FutureWarning)
130+
# .idxmin() returns np.nan if no barriers have been hit
131+
# => .fillna(0) labels the "no barrier hit" condition
132+
barrier = barrier.idxmin(axis=1, skipna=True).fillna(0).astype(int)
128133
# Correct for a possible look-ahead-bias & errors introduced by .fillna()
129134
barrier.iloc[-self.n:] = np.nan
130135
return barrier
@@ -280,4 +285,3 @@ def plot_at_date(self, date: str, months=3, figsize=(12, 3)) -> None:
280285
ax.set_xlabel(None)
281286
plt.xlim(start_date, end_date)
282287
plt.show()
283-

0 commit comments

Comments
 (0)