8000 TST: centralize and standardize pandas imports by tacaswell · Pull Request #10124 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

TST: centralize and standardize pandas imports #10124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/matplotlib/testing/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,26 @@ def mpl_image_comparison_parameters(request, extension):
yield
finally:
delattr(func.__wrapped__, 'parameters')


@pytest.fixture
def pd(request):
'''fixture to import and configure pandas'''

pd = pytest.importorskip('pandas')
if pd:
try:
from pandas.plotting import (
register_matplotlib_converters as register)
except ImportError:
from pandas.tseries.converter import register
register()

try:
from pandas.plotting import (
deregister_matplotlib_converters as deregister)
request.addfinalizer(deregister)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a yield fixture (in the style of a context manager) seems mildly more pythonic (https://docs.pytest.org/en/latest/fixture.html#fixture-finalization-executing-teardown-code).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tacaswell let us know if you want to make the change or I can make it in a separate pr too

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anntzer sorry, been traveling, feel free to push to this branch.

except ImportError:
pass

return pd
3 changes: 2 additions & 1 deletion lib/matplotlib/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

from matplotlib.testing.conftest import (mpl_test_settings,
mpl_image_comparison_parameters,
pytest_configure, pytest_unconfigure)
pytest_configure, pytest_unconfigure,
pd)
36 changes: 8 additions & 28 deletions lib/matplotlib/tests/test_axes.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -5091,11 +5091,7 @@ def test_broken_barh_empty():
ax.broken_barh([], (.1, .5))


def test_pandas_pcolormesh():
pd = pytest.importorskip('pandas')
from pandas.tseries import converter
converter.register()

def test_pandas_pcolormesh(pd):
time = pd.date_range('2000-01-01', periods=10)
depth = np.arange(20)
data = np.random.rand(20, 10)
Expand All @@ -5104,11 +5100,7 @@ def test_pandas_pcolormesh():
ax.pcolormesh(time, depth, data)


def test_pandas_indexing_dates():
pd = pytest.importorskip('pandas')
from pandas.tseries import converter
converter.register()

def test_pandas_indexing_dates(pd):
dates = np.arange('2005-02', '2005-03', dtype='datetime64[D]')
values = np.sin(np.array(range(len(dates))))
df = pd.DataFrame({'dates': dates, 'values': values})
Expand All @@ -5119,42 +5111,30 @@ def test_pandas_indexing_dates():
ax.plot('dates', 'values', data=without_zero_index)


def test_pandas_errorbar_indexing():
pd = pytest.importorskip('pandas')
from pandas.tseries import converter
converter.register()

def test_pandas_errorbar_indexing(pd):
df = pd.DataFrame(np.random.uniform(size=(5, 4)),
columns=['x', 'y', 'xe', 'ye'],
index=[1, 2, 3, 4, 5])
fig, ax = plt.subplots()
ax.errorbar('x', 'y', xerr='xe', yerr='ye', data=df)


def test_pandas_indexing_hist():
pd = pytest.importorskip('pandas')
from pandas.tseries import converter
converter.register()

def test_pandas_indexing_hist(pd):
ser_1 = pd.Series(data=[1, 2, 2, 3, 3, 4, 4, 4, 4, 5])
ser_2 = ser_1.iloc[1:]
fig, axes = plt.subplots()
axes.hist(ser_2)


def test_pandas_bar_align_center():
def test_pandas_bar_align_center(pd):
# Tests fix for issue 8767
pd = pytest.importorskip('pandas')
from pandas.tseries import converter
converter.register()

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

fig, ax = plt.subplots(1)

rect = ax.bar(df.loc[df['a'] == 1, 'b'],
df.loc[df['a'] == 1, 'b'],
align='center')
ax.bar(df.loc[df['a'] == 1, 'b'],
df.loc[df['a'] == 1, 'b'],
align='center')

fig.canvas.draw()

Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,7 @@ def test_size_in_xy():
ax.set_ylim(0, 30)


def test_pandas_indexing():
pd = pytest.importorskip('pandas')
def test_pandas_indexing(pd):

# Should not fail break when faced with a
# non-zero indexed series
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,8 +604,7 @@ def _azimuth2math(azimuth, elevation):
return theta, phi


def test_pandas_iterable():
pd = pytest.importorskip('pandas')
def test_pandas_iterable(pd):
# Using a list or series yields equivalent
# color maps, i.e the series isn't seen as
# a single color
Expand Down
5 changes: 1 addition & 4 deletions lib/matplotlib/tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,9 @@ def tz_convert(dt_list, tzinfo):
_test_date2num_dst(date_range, tz_convert)


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

def tz_convert(*args):
return pd.DatetimeIndex.tz_convert(*args).astype(object)
Expand Down
4 changes: 1 addition & 3 deletions lib/matplotlib/tests/test_preprocess_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,8 @@ def test_function_call_with_dict_data_not_in_data(func):


@pytest.mark.parametrize('func', all_funcs, ids=all_func_ids)
def test_function_call_with_pandas_data(func):
def test_function_call_with_pandas_data(func, pd):
"""test with pandas dataframe -> label comes from data["col"].name """
pd = pytest.importorskip('pandas')

data = pd.DataFrame({"a": np.array([1, 2], dtype=np.int32),
"b": np.array([8, 9], dtype=np.int32),
"w": ["NOT", "NOT"]})
Expand Down
0