From 1137cd164fa6bf8d346d871f24c280d5d63f2492 Mon Sep 17 00:00:00 2001 From: Maxim Ivanov Date: Sat, 17 Oct 2020 21:15:57 +0700 Subject: [PATCH 1/2] BUG: record warnings related to DatetimeIndex --- pandas/tests/plotting/test_datetimelike.py | 27 +++++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/pandas/tests/plotting/test_datetimelike.py b/pandas/tests/plotting/test_datetimelike.py index 78aa1887f5611..4bb1323c4024e 100644 --- a/pandas/tests/plotting/test_datetimelike.py +++ b/pandas/tests/plotting/test_datetimelike.py @@ -2,6 +2,7 @@ from datetime import date, datetime, time, timedelta import pickle import sys +import warnings import numpy as np import pytest @@ -291,9 +292,16 @@ def test_irreg_hf(self): _, ax = self.plt.subplots() df2 = df.copy() df2.index = df.index.astype(object) - df2.plot(ax=ax) - diffs = Series(ax.get_lines()[0].get_xydata()[:, 0]).diff() - assert (np.fabs(diffs[1:] - sec) < 1e-8).all() + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always", FutureWarning) + df2.plot(ax=ax) + diffs = Series(ax.get_lines()[0].get_xydata()[:, 0]).diff() + assert (np.fabs(diffs[1:] - sec) < 1e-8).all() + match = ( + "Automatically casting object-dtype Index of datetimes " + "to DatetimeIndex is deprecated" + ) + assert match in str(w[0].message) def test_irregular_datetime64_repr_bug(self): ser = tm.makeTimeSeries() @@ -1028,9 +1036,16 @@ def test_irreg_dtypes(self): # np.datetime64 idx = date_range("1/1/2000", periods=10) idx = idx[[0, 2, 5, 9]].astype(object) - df = DataFrame(np.random.randn(len(idx), 3), idx) - _, ax = self.plt.subplots() - _check_plot_works(df.plot, ax=ax) + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter("always", FutureWarning) + df = DataFrame(np.random.randn(len(idx), 3), idx) + _, ax = self.plt.subplots() + _check_plot_works(df.plot, ax=ax) + match = ( + "Automatically casting object-dtype Index of datetimes " + "to DatetimeIndex is deprecated" + ) + assert match in str(w[0].message) @pytest.mark.slow def test_time(self): From 951dec15c52b68430426d232df49037deec26201 Mon Sep 17 00:00:00 2001 From: Maxim Ivanov Date: Sun, 18 Oct 2020 11:15:32 +0700 Subject: [PATCH 2/2] CLN: use tm.assert_produces_warning --- pandas/tests/plotting/test_datetimelike.py | 29 +++++++++++----------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/pandas/tests/plotting/test_datetimelike.py b/pandas/tests/plotting/test_datetimelike.py index 4bb1323c4024e..66463a4a2358a 100644 --- a/pandas/tests/plotting/test_datetimelike.py +++ b/pandas/tests/plotting/test_datetimelike.py @@ -2,7 +2,6 @@ from datetime import date, datetime, time, timedelta import pickle import sys -import warnings import numpy as np import pytest @@ -292,16 +291,16 @@ def test_irreg_hf(self): _, ax = self.plt.subplots() df2 = df.copy() df2.index = df.index.astype(object) - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always", FutureWarning) + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + # This warning will be emitted + # pandas/core/frame.py:3216: + # FutureWarning: Automatically casting object-dtype Index of datetimes + # to DatetimeIndex is deprecated and will be removed in a future version. + # Explicitly cast to DatetimeIndex instead. + # return klass(values, index=self.index, name=name, fastpath=True) df2.plot(ax=ax) diffs = Series(ax.get_lines()[0].get_xydata()[:, 0]).diff() assert (np.fabs(diffs[1:] - sec) < 1e-8).all() - match = ( - "Automatically casting object-dtype Index of datetimes " - "to DatetimeIndex is deprecated" - ) - assert match in str(w[0].message) def test_irregular_datetime64_repr_bug(self): ser = tm.makeTimeSeries() @@ -1036,16 +1035,16 @@ def test_irreg_dtypes(self): # np.datetime64 idx = date_range("1/1/2000", periods=10) idx = idx[[0, 2, 5, 9]].astype(object) - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always", FutureWarning) + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + # This warning will be emitted + # pandas/core/frame.py:3216: + # FutureWarning: Automatically casting object-dtype Index of datetimes + # to DatetimeIndex is deprecated and will be removed in a future version. + # Explicitly cast to DatetimeIndex instead. + # return klass(values, index=self.index, name=name, fastpath=True) df = DataFrame(np.random.randn(len(idx), 3), idx) _, ax = self.plt.subplots() _check_plot_works(df.plot, ax=ax) - match = ( - "Automatically casting object-dtype Index of datetimes " - "to DatetimeIndex is deprecated" - ) - assert match in str(w[0].message) @pytest.mark.slow def test_time(self):