Skip to content

Commit 0573c27

Browse files
author
torext
committed
Silence Period[B] warnings in plotting code
1 parent 8352331 commit 0573c27

File tree

3 files changed

+55
-18
lines changed

3 files changed

+55
-18
lines changed

pandas/plotting/_matplotlib/converter.py

+43-18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
Any,
1414
cast,
1515
)
16+
import warnings
1617

1718
import matplotlib.dates as mdates
1819
from matplotlib.ticker import (
@@ -239,18 +240,29 @@ def _convert_1d(values, units, axis):
239240
if not hasattr(axis, "freq"):
240241
raise TypeError("Axis must have `freq` set to convert to Periods")
241242
valid_types = (str, datetime, Period, pydt.date, pydt.time, np.datetime64)
242-
if isinstance(values, valid_types) or is_integer(values) or is_float(values):
243-
return get_datevalue(values, axis.freq)
244-
elif isinstance(values, PeriodIndex):
245-
return values.asfreq(axis.freq).asi8
246-
elif isinstance(values, Index):
247-
return values.map(lambda x: get_datevalue(x, axis.freq))
248-
elif lib.infer_dtype(values, skipna=False) == "period":
249-
# https://github.com/pandas-dev/pandas/issues/24304
250-
# convert ndarray[period] -> PeriodIndex
251-
return PeriodIndex(values, freq=axis.freq).asi8
252-
elif isinstance(values, (list, tuple, np.ndarray, Index)):
253-
return [get_datevalue(x, axis.freq) for x in values]
243+
with warnings.catch_warnings():
244+
warnings.filterwarnings(
245+
"ignore", "Period with BDay freq is deprecated", category=FutureWarning
246+
)
247+
warnings.filterwarnings(
248+
"ignore", r"PeriodDtype\[B\] is deprecated", category=FutureWarning
249+
)
250+
if (
251+
isinstance(values, valid_types)
252+
or is_integer(values)
253+
or is_float(values)
254+
):
255+
return get_datevalue(values, axis.freq)
256+
elif isinstance(values, PeriodIndex):
257+
return values.asfreq(axis.freq).asi8
258+
elif isinstance(values, Index):
259+
return values.map(lambda x: get_datevalue(x, axis.freq))
260+
elif lib.infer_dtype(values, skipna=False) == "period":
261+
# https://github.com/pandas-dev/pandas/issues/24304
262+
# convert ndarray[period] -> PeriodIndex
263+
return PeriodIndex(values, freq=axis.freq).asi8
264+
elif isinstance(values, (list, tuple, np.ndarray, Index)):
265+
return [get_datevalue(x, axis.freq) for x in values]
254266
return values
255267

256268

@@ -575,11 +587,18 @@ def _daily_finder(vmin, vmax, freq: BaseOffset) -> np.ndarray:
575587
(vmin, vmax) = (int(vmin), int(vmax))
576588
span = vmax - vmin + 1
577589

578-
dates_ = period_range(
579-
start=Period(ordinal=vmin, freq=freq),
580-
end=Period(ordinal=vmax, freq=freq),
581-
freq=freq,
582-
)
590+
with warnings.catch_warnings():
591+
warnings.filterwarnings(
592+
"ignore", "Period with BDay freq is deprecated", category=FutureWarning
593+
)
594+
warnings.filterwarnings(
595+
"ignore", r"PeriodDtype\[B\] is deprecated", category=FutureWarning
596+
)
597+
dates_ = period_range(
598+
start=Period(ordinal=vmin, freq=freq),
599+
end=Period(ordinal=vmax, freq=freq),
600+
freq=freq,
601+
)
583602

584603
# Initialize the output
585604
info = np.zeros(
@@ -1072,7 +1091,13 @@ def __call__(self, x, pos: int = 0) -> str:
10721091
fmt = self.formatdict.pop(x, "")
10731092
if isinstance(fmt, np.bytes_):
10741093
fmt = fmt.decode("utf-8")
1075-
period = Period(ordinal=int(x), freq=self.freq)
1094+
with warnings.catch_warnings():
1095+
warnings.filterwarnings(
1096+
"ignore",
1097+
"Period with BDay freq is deprecated",
1098+
category=FutureWarning,
1099+
)
1100+
period = Period(ordinal=int(x), freq=self.freq)
10761101
assert isinstance(period, Period)
10771102
return period.strftime(fmt)
10781103

pandas/tests/plotting/frame/test_frame.py

+7
Original file line numberDiff line numberDiff line change
@@ -2487,6 +2487,13 @@ def test_secondary_y(self, secondary_y):
24872487
assert ax.get_ylim() == (0, 100)
24882488
assert ax.get_yticks()[0] == 99
24892489

2490+
@pytest.mark.slow
2491+
def test_plot_no_warning(self):
2492+
df = tm.makeTimeDataFrame()
2493+
with tm.assert_produces_warning(False):
2494+
_ = df.plot()
2495+
_ = df.T.plot()
2496+
24902497

24912498
def _generate_4_axes_via_gridspec():
24922499
import matplotlib.pyplot as plt

pandas/tests/plotting/test_series.py

+5
Original file line numberDiff line numberDiff line change
@@ -973,3 +973,8 @@ def test_series_none_color(self):
973973
ax = series.plot(color=None)
974974
expected = _unpack_cycler(mpl.pyplot.rcParams)[:1]
975975
_check_colors(ax.get_lines(), linecolors=expected)
976+
977+
@pytest.mark.slow
978+
def test_plot_no_warning(self, ts):
979+
with tm.assert_produces_warning(False):
980+
_ = ts.plot()

0 commit comments

Comments
 (0)