Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c5ed696

Browse files
authored
Merge pull request #10124 from tacaswell/tst_supress_pandas_warnings
TST: centralize and standardize pandas imports
2 parents 3b3c16d + 34ad36c commit c5ed696

File tree

7 files changed

+37
-40
lines changed

7 files changed

+37
-40
lines changed

lib/matplotlib/testing/conftest.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,26 @@ def mpl_image_comparison_parameters(request, extension):
7676
yield
7777
finally:
7878
delattr(func.__wrapped__, 'parameters')
79+
80+
81+
@pytest.fixture
82+
def pd(request):
83+
'''fixture to import and configure pandas'''
84+
85+
pd = pytest.importorskip('pandas')
86+
if pd:
87+
try:
88+
from pandas.plotting import (
89+
register_matplotlib_converters as register)
90+
except ImportError:
91+
from pandas.tseries.converter import register
92+
register()
93+
94+
try:
95+
from pandas.plotting import (
96+
deregister_matplotlib_converters as deregister)
97+
request.addfinalizer(deregister)
98+
except ImportError:
99+
pass
100+
101+
return pd

lib/matplotlib/tests/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
from matplotlib.testing.conftest import (mpl_test_settings,
44
mpl_image_comparison_parameters,
5-
pytest_configure, pytest_unconfigure)
5+
pytest_configure, pytest_unconfigure,
6+
pd)

lib/matplotlib/tests/test_axes.py

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5091,11 +5091,7 @@ def test_broken_barh_empty():
50915091
ax.broken_barh([], (.1, .5))
50925092

50935093

5094-
def test_pandas_pcolormesh():
5095-
pd = pytest.importorskip('pandas')
5096-
from pandas.tseries import converter
5097-
converter.register()
5098-
5094+
def test_pandas_pcolormesh(pd):
50995095
time = pd.date_range('2000-01-01', periods=10)
51005096
depth = np.arange(20)
51015097
data = np.random.rand(20, 10)
@@ -5104,11 +5100,7 @@ def test_pandas_pcolormesh():
51045100
ax.pcolormesh(time, depth, data)
51055101

51065102

5107-
def test_pandas_indexing_dates():
5108-
pd = pytest.importorskip('pandas')
5109-
from pandas.tseries import converter
5110-
converter.register()
5111-
5103+
def test_pandas_indexing_dates(pd):
51125104
dates = np.arange('2005-02', '2005-03', dtype='datetime64[D]')
51135105
values = np.sin(np.array(range(len(dates))))
51145106
df = pd.DataFrame({'dates': dates, 'values': values})
@@ -5119,42 +5111,30 @@ def test_pandas_indexing_dates():
51195111
ax.plot('dates', 'values', data=without_zero_index)
51205112

51215113

5122-
def test_pandas_errorbar_indexing():
5123-
pd = pytest.importorskip('pandas')
5124-
from pandas.tseries import converter
5125-
converter.register()
5126-
5114+
def test_pandas_errorbar_indexing(pd):
51275115
df = pd.DataFrame(np.random.uniform(size=(5, 4)),
51285116
columns=['x', 'y', 'xe', 'ye'],
51295117
index=[1, 2, 3, 4, 5])
51305118
fig, ax = plt.subplots()
51315119
ax.errorbar('x', 'y', xerr='xe', yerr='ye', data=df)
51325120

51335121

5134-
def test_pandas_indexing_hist():
5135-
pd = pytest.importorskip('pandas')
5136-
from pandas.tseries import converter
5137-
converter.register()
5138-
5122+
def test_pandas_indexing_hist(pd):
51395123
ser_1 = pd.Series(data=[1, 2, 2, 3, 3, 4, 4, 4, 4, 5])
51405124
ser_2 = ser_1.iloc[1:]
51415125
fig, axes = plt.subplots()
51425126
axes.hist(ser_2)
51435127

51445128

5145-
def test_pandas_bar_align_center():
5129+
def test_pandas_bar_align_center(pd):
51465130
# Tests fix for issue 8767
5147-
pd = pytest.importorskip('pandas')
5148-
from pandas.tseries import converter
5149-
converter.register()
5150-
51515131
df = pd.DataFrame({'a': range(2), 'b': range(2)})
51525132

51535133
fig, ax = plt.subplots(1)
51545134

5155-
rect = ax.bar(df.loc[df['a'] == 1, 'b'],
5156-
df.loc[df['a'] == 1, 'b'],
5157-
align='center')
5135+
ax.bar(df.loc[df['a'] == 1, 'b'],
5136+
df.loc[df['a'] == 1, 'b'],
5137+
align='center')
51585138

51595139
fig.canvas.draw()
51605140

lib/matplotlib/tests/test_collections.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,7 @@ def test_size_in_xy():
593593
ax.set_ylim(0, 30)
594594

595595

596-
def test_pandas_indexing():
597-
pd = pytest.importorskip('pandas')
596+
def test_pandas_indexing(pd):
598597

599598
# Should not fail break when faced with a
600599
# non-zero indexed series

lib/matplotlib/tests/test_colors.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,7 @@ def _azimuth2math(azimuth, elevation):
616616
return theta, phi
617617

618618

619-
def test_pandas_iterable():
620-
pd = pytest.importorskip('pandas')
619+
def test_pandas_iterable(pd):
621620
# Using a list or series yields equivalent
622621
# color maps, i.e the series isn't seen as
623622
# a single color

lib/matplotlib/tests/test_dates.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,12 +552,9 @@ def tz_convert(dt_list, tzinfo):
552552
_test_date2num_dst(date_range, tz_convert)
553553

554554

555-
def test_date2num_dst_pandas():
555+
def test_date2num_dst_pandas(pd):
556556
# Test for github issue #3896, but in date2num around DST transitions
557557
# with a timezone-aware pandas date_range object.
558-
pd = pytest.importorskip('pandas')
559-
from pandas.tseries import converter
560-
converter.register()
561558

562559
def tz_convert(*args):
563560
return pd.DatetimeIndex.tz_convert(*args).astype(object)

lib/matplotlib/tests/test_preprocess_data.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,8 @@ def test_function_call_with_dict_data_not_in_data(func):
162162

163163

164164
@pytest.mark.parametrize('func', all_funcs, ids=all_func_ids)
165-
def test_function_call_with_pandas_data(func):
165+
def test_function_call_with_pandas_data(func, pd):
166166
"""test with pandas dataframe -> label comes from data["col"].name """
167-
pd = pytest.importorskip('pandas')
168-
169167
data = pd.DataFrame({"a": np.array([1, 2], dtype=np.int32),
170168
"b": np.array([8, 9], dtype=np.int32),
171169
"w": ["NOT", "NOT"]})

0 commit comments

Comments
 (0)