8000 TST: centralize and standardize pandas imports · matplotlib/matplotlib@e5f7982 · GitHub
[go: up one dir, main page]

Skip to content

Commit e5f7982

Browse files
committed
TST: centralize and standardize pandas imports
Added a decorator to handle: - importorskip - using the correct unit-handler registration function to deal with: - pandas not auto-registering - pandas moving an renaming the registration function
1 parent 45ebc85 commit e5f7982

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

lib/matplotlib/tests/test_axes.py

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5091,11 +5091,27 @@ 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()
5094+
def _pandas_wrapper(func):
5095+
'''Decorator to centralize pandas setup / conditional import'''
5096+
import functools
5097+
5098+
@functools.wraps(func)
5099+
def wrapped_for_pandas(*args, **kwargs):
5100+
pytest.importorskip('pandas')
5101+
try:
5102+
from pandas.plotting import (
5103+
register_matplotlib_converters as register)
5104+
except ImportError:
5105+
from pandas.tseries.converter import register
5106+
register()
5107+
return func(*args, **kwargs)
5108+
5109+
return wrapped_for_pandas
50985110

5111+
5112+
@_pandas_wrapper
5113+
def test_pandas_pcolormesh():
5114+
import pandas as pd
50995115
time = pd.date_range('2000-01-01', periods=10)
51005116
depth = np.arange(20)
51015117
data = np.random.rand(20, 10)
@@ -5104,10 +5120,9 @@ def test_pandas_pcolormesh():
51045120
ax.pcolormesh(time, depth, data)
51055121

51065122

5123+
@_pandas_wrapper
51075124
def test_pandas_indexing_dates():
5108-
pd = pytest.importorskip('pandas')
5109-
from pandas.tseries import converter
5110-
converter.register()
5125+
import pandas as pd
51115126

51125127
dates = np.arange('2005-02', '2005-03', dtype='datetime64[D]')
51135128
values = np.sin(np.array(range(len(dates))))
@@ -5119,10 +5134,9 @@ def test_pandas_indexing_dates():
51195134
ax.plot('dates', 'values', data=without_zero_index)
51205135

51215136

5137+
@_pandas_wrapper
51225138
def test_pandas_errorbar_indexing():
5123-
pd = pytest.importorskip('pandas')
5124-
from pandas.tseries import converter
5125-
converter.register()
5139+
import pandas as pd
51265140

51275141
df = pd.DataFrame(np.random.uniform(size=(5, 4)),
51285142
columns=['x', 'y', 'xe', 'ye'],
@@ -5131,30 +5145,28 @@ def test_pandas_errorbar_indexing():
51315145
ax.errorbar('x', 'y', xerr='xe', yerr='ye', data=df)
51325146

51335147

5148+
@_pandas_wrapper
51345149
def test_pandas_indexing_hist():
5135-
pd = pytest.importorskip('pandas')
5136-
from pandas.tseries import converter
5137-
converter.register()
5150+
import pandas as pd
51385151

51395152
ser_1 = pd.Series(data=[1, 2, 2, 3, 3, 4, 4, 4, 4, 5])
51405153
ser_2 = ser_1.iloc[1:]
51415154
fig, axes = plt.subplots()
51425155
axes.hist(ser_2)
51435156

51445157

5158+
@_pandas_wrapper
51455159
def test_pandas_bar_align_center():
51465160
# Tests fix for issue 8767
5147-
pd = pytest.importorskip('pandas')
5148-
from pandas.tseries import converter
5149-
converter.register()
5161+
import pandas as pd
51505162

51515163
df = pd.DataFrame({'a': range(2), 'b': range(2)})
51525164

51535165
fig, ax = plt.subplots(1)
51545166

5155-
rect = ax.bar(df.loc[df['a'] == 1, 'b'],
5156-
df.loc[df['a'] == 1, 'b'],
5157-
align='center')
5167+
ax.bar(df.loc[df['a'] == 1, 'b'],
5168+
df.loc[df['a'] == 1, 'b'],
5169+
align='center')
51585170

51595171
fig.canvas.draw()
51605172

0 commit comments

Comments
 (0)
0