Skip to content

Commit 9425d7c

Browse files
authored
FIX: fix cleanup warnings for errorbar timeseries (#36982)
1 parent aa92172 commit 9425d7c

File tree

1 file changed

+101
-80
lines changed

1 file changed

+101
-80
lines changed

pandas/tests/plotting/test_frame.py

+101-80
Original file line numberDiff line numberDiff line change
@@ -2658,67 +2658,84 @@ def test_pie_df_nan(self):
26582658

26592659
@pytest.mark.slow
26602660
def test_errorbar_plot(self):
2661-
with warnings.catch_warnings():
2662-
d = {"x": np.arange(12), "y": np.arange(12, 0, -1)}
2663-
df = DataFrame(d)
2664-
d_err = {"x": np.ones(12) * 0.2, "y": np.ones(12) * 0.4}
2665-
df_err = DataFrame(d_err)
2661+
d = {"x": np.arange(12), "y": np.arange(12, 0, -1)}
2662+
df = DataFrame(d)
2663+
d_err = {"x": np.ones(12) * 0.2, "y": np.ones(12) * 0.4}
2664+
df_err = DataFrame(d_err)
26662665

2667-
# check line plots
2668-
ax = _check_plot_works(df.plot, yerr=df_err, logy=True)
2669-
self._check_has_errorbars(ax, xerr=0, yerr=2)
2670-
ax = _check_plot_works(df.plot, yerr=df_err, logx=True, logy=True)
2671-
self._check_has_errorbars(ax, xerr=0, yerr=2)
2672-
ax = _check_plot_works(df.plot, yerr=df_err, loglog=True)
2673-
self._check_has_errorbars(ax, xerr=0, yerr=2)
2666+
# check line plots
2667+
ax = _check_plot_works(df.plot, yerr=df_err, logy=True)
2668+
self._check_has_errorbars(ax, xerr=0, yerr=2)
26742669

2675-
kinds = ["line", "bar", "barh"]
2676-
for kind in kinds:
2677-
ax = _check_plot_works(df.plot, yerr=df_err["x"], kind=kind)
2678-
self._check_has_errorbars(ax, xerr=0, yerr=2)
2679-
ax = _check_plot_works(df.plot, yerr=d_err, kind=kind)
2680-
self._check_has_errorbars(ax, xerr=0, yerr=2)
2681-
ax = _check_plot_works(df.plot, yerr=df_err, xerr=df_err, kind=kind)
2682-
self._check_has_errorbars(ax, xerr=2, yerr=2)
2683-
ax = _check_plot_works(
2684-
df.plot, yerr=df_err["x"], xerr=df_err["x"], kind=kind
2685-
)
2686-
self._check_has_errorbars(ax, xerr=2, yerr=2)
2687-
ax = _check_plot_works(df.plot, xerr=0.2, yerr=0.2, kind=kind)
2688-
self._check_has_errorbars(ax, xerr=2, yerr=2)
2689-
2690-
# _check_plot_works adds an ax so catch warning. see GH #13188
2691-
axes = _check_plot_works(
2692-
df.plot, yerr=df_err, xerr=df_err, subplots=True, kind=kind
2693-
)
2694-
self._check_has_errorbars(axes, xerr=1, yerr=1)
2695-
2696-
ax = _check_plot_works(
2697-
(df + 1).plot, yerr=df_err, xerr=df_err, kind="bar", log=True
2698-
)
2699-
self._check_has_errorbars(ax, xerr=2, yerr=2)
2670+
ax = _check_plot_works(df.plot, yerr=df_err, logx=True, logy=True)
2671+
self._check_has_errorbars(ax, xerr=0, yerr=2)
27002672

2701-
# yerr is raw error values
2702-
ax = _check_plot_works(df["y"].plot, yerr=np.ones(12) * 0.4)
2703-
self._check_has_errorbars(ax, xerr=0, yerr=1)
2704-
ax = _check_plot_works(df.plot, yerr=np.ones((2, 12)) * 0.4)
2673+
ax = _check_plot_works(df.plot, yerr=df_err, loglog=True)
2674+
self._check_has_errorbars(ax, xerr=0, yerr=2)
2675+
2676+
ax = _check_plot_works(
2677+
(df + 1).plot, yerr=df_err, xerr=df_err, kind="bar", log=True
2678+
)
2679+
self._check_has_errorbars(ax, xerr=2, yerr=2)
2680+
2681+
# yerr is raw error values
2682+
ax = _check_plot_works(df["y"].plot, yerr=np.ones(12) * 0.4)
2683+
self._check_has_errorbars(ax, xerr=0, yerr=1)
2684+
2685+
ax = _check_plot_works(df.plot, yerr=np.ones((2, 12)) * 0.4)
2686+
self._check_has_errorbars(ax, xerr=0, yerr=2)
2687+
2688+
# yerr is column name
2689+
for yerr in ["yerr", "誤差"]:
2690+
s_df = df.copy()
2691+
s_df[yerr] = np.ones(12) * 0.2
2692+
2693+
ax = _check_plot_works(s_df.plot, yerr=yerr)
27052694
self._check_has_errorbars(ax, xerr=0, yerr=2)
27062695

2707-
# yerr is column name
2708-
for yerr in ["yerr", "誤差"]:
2709-
s_df = df.copy()
2710-
s_df[yerr] = np.ones(12) * 0.2
2711-
ax = _check_plot_works(s_df.plot, yerr=yerr)
2712-
self._check_has_errorbars(ax, xerr=0, yerr=2)
2713-
ax = _check_plot_works(s_df.plot, y="y", x="x", yerr=yerr)
2714-
self._check_has_errorbars(ax, xerr=0, yerr=1)
2696+
ax = _check_plot_works(s_df.plot, y="y", x="x", yerr=yerr)
2697+
self._check_has_errorbars(ax, xerr=0, yerr=1)
27152698

2716-
with pytest.raises(ValueError):
2717-
df.plot(yerr=np.random.randn(11))
2699+
with pytest.raises(ValueError):
2700+
df.plot(yerr=np.random.randn(11))
27182701

2719-
df_err = DataFrame({"x": ["zzz"] * 12, "y": ["zzz"] * 12})
2720-
with pytest.raises((ValueError, TypeError)):
2721-
df.plot(yerr=df_err)
2702+
df_err = DataFrame({"x": ["zzz"] * 12, "y": ["zzz"] * 12})
2703+
with pytest.raises((ValueError, TypeError)):
2704+
df.plot(yerr=df_err)
2705+
2706+
@pytest.mark.slow
2707+
@pytest.mark.parametrize("kind", ["line", "bar", "barh"])
2708+
def test_errorbar_plot_different_kinds(self, kind):
2709+
d = {"x": np.arange(12), "y": np.arange(12, 0, -1)}
2710+
df = DataFrame(d)
2711+
d_err = {"x": np.ones(12) * 0.2, "y": np.ones(12) * 0.4}
2712+
df_err = DataFrame(d_err)
2713+
2714+
ax = _check_plot_works(df.plot, yerr=df_err["x"], kind=kind)
2715+
self._check_has_errorbars(ax, xerr=0, yerr=2)
2716+
2717+
ax = _check_plot_works(df.plot, yerr=d_err, kind=kind)
2718+
self._check_has_errorbars(ax, xerr=0, yerr=2)
2719+
2720+
ax = _check_plot_works(df.plot, yerr=df_err, xerr=df_err, kind=kind)
2721+
self._check_has_errorbars(ax, xerr=2, yerr=2)
2722+
2723+
ax = _check_plot_works(df.plot, yerr=df_err["x"], xerr=df_err["x"], kind=kind)
2724+
self._check_has_errorbars(ax, xerr=2, yerr=2)
2725+
2726+
ax = _check_plot_works(df.plot, xerr=0.2, yerr=0.2, kind=kind)
2727+
self._check_has_errorbars(ax, xerr=2, yerr=2)
2728+
2729+
with tm.assert_produces_warning(UserWarning):
2730+
# _check_plot_works creates subplots inside,
2731+
# which leads to warnings like this:
2732+
# UserWarning: To output multiple subplots,
2733+
# the figure containing the passed axes is being cleared
2734+
# Similar warnings were observed in GH #13188
2735+
axes = _check_plot_works(
2736+
df.plot, yerr=df_err, xerr=df_err, subplots=True, kind=kind
2737+
)
2738+
self._check_has_errorbars(axes, xerr=1, yerr=1)
27222739

27232740
@pytest.mark.xfail(reason="Iterator is consumed", raises=ValueError)
27242741
@pytest.mark.slow
@@ -2765,35 +2782,39 @@ def test_errorbar_with_partial_columns(self):
27652782
self._check_has_errorbars(ax, xerr=0, yerr=1)
27662783

27672784
@pytest.mark.slow
2768-
def test_errorbar_timeseries(self):
2785+
@pytest.mark.parametrize("kind", ["line", "bar", "barh"])
2786+
def test_errorbar_timeseries(self, kind):
2787+
d = {"x": np.arange(12), "y": np.arange(12, 0, -1)}
2788+
d_err = {"x": np.ones(12) * 0.2, "y": np.ones(12) * 0.4}
27692789

2770-
with warnings.catch_warnings():
2771-
d = {"x": np.arange(12), "y": np.arange(12, 0, -1)}
2772-
d_err = {"x": np.ones(12) * 0.2, "y": np.ones(12) * 0.4}
2790+
# check time-series plots
2791+
ix = date_range("1/1/2000", "1/1/2001", freq="M")
2792+
tdf = DataFrame(d, index=ix)
2793+
tdf_err = DataFrame(d_err, index=ix)
27732794

2774-
# check time-series plots
2775-
ix = date_range("1/1/2000", "1/1/2001", freq="M")
2776-
tdf = DataFrame(d, index=ix)
2777-
tdf_err = DataFrame(d_err, index=ix)
2795+
ax = _check_plot_works(tdf.plot, yerr=tdf_err, kind=kind)
2796+
self._check_has_errorbars(ax, xerr=0, yerr=2)
27782797

2779-
kinds = ["line", "bar", "barh"]
2780-
for kind in kinds:
2781-
ax = _check_plot_works(tdf.plot, yerr=tdf_err, kind=kind)
2782-
self._check_has_errorbars(ax, xerr=0, yerr=2)
2783-
ax = _check_plot_works(tdf.plot, yerr=d_err, kind=kind)
2784-
self._check_has_errorbars(ax, xerr=0, yerr=2)
2785-
ax = _check_plot_works(tdf.plot, y="y", yerr=tdf_err["x"], kind=kind)
2786-
self._check_has_errorbars(ax, xerr=0, yerr=1)
2787-
ax = _check_plot_works(tdf.plot, y="y", yerr="x", kind=kind)
2788-
self._check_has_errorbars(ax, xerr=0, yerr=1)
2789-
ax = _check_plot_works(tdf.plot, yerr=tdf_err, kind=kind)
2790-
self._check_has_errorbars(ax, xerr=0, yerr=2)
2791-
2792-
# _check_plot_works adds an ax so catch warning. see GH #13188
2793-
axes = _check_plot_works(
2794-
tdf.plot, kind=kind, yerr=tdf_err, subplots=True
2795-
)
2796-
self._check_has_errorbars(axes, xerr=0, yerr=1)
2798+
ax = _check_plot_works(tdf.plot, yerr=d_err, kind=kind)
2799+
self._check_has_errorbars(ax, xerr=0, yerr=2)
2800+
2801+
ax = _check_plot_works(tdf.plot, y="y", yerr=tdf_err["x"], kind=kind)
2802+
self._check_has_errorbars(ax, xerr=0, yerr=1)
2803+
2804+
ax = _check_plot_works(tdf.plot, y="y", yerr="x", kind=kind)
2805+
self._check_has_errorbars(ax, xerr=0, yerr=1)
2806+
2807+
ax = _check_plot_works(tdf.plot, yerr=tdf_err, kind=kind)
2808+
self._check_has_errorbars(ax, xerr=0, yerr=2)
2809+
2810+
with tm.assert_produces_warning(UserWarning):
2811+
# _check_plot_works creates subplots inside,
2812+
# which leads to warnings like this:
2813+
# UserWarning: To output multiple subplots,
2814+
# the figure containing the passed axes is being cleared
2815+
# Similar warnings were observed in GH #13188
2816+
axes = _check_plot_works(tdf.plot, kind=kind, yerr=tdf_err, subplots=True)
2817+
self._check_has_errorbars(axes, xerr=0, yerr=1)
27972818

27982819
def test_errorbar_asymmetrical(self):
27992820

0 commit comments

Comments
 (0)