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

Skip to content

Commit db394cb

Browse files
committed
Fix datetime mess
1 parent 95e4e52 commit db394cb

File tree

4 files changed

+29
-21
lines changed

4 files changed

+29
-21
lines changed

lib/matplotlib/dates.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -411,19 +411,24 @@ def date2num(d):
411411
For details see the module docstring.
412412
"""
413413

414-
if hasattr(d, "values"):
415-
# this unpacks pandas series or dataframes...
416-
d = d.values
417-
418-
if ((isinstance(d, np.ndarray) and np.issubdtype(d.dtype, np.datetime64))
419-
or isinstance(d, np.datetime64)):
420-
return _dt64_to_ordinalf(d)
421414
if not cbook.iterable(d):
415+
if hasattr(d, 'dtype'):
416+
if (np.issubdtype(d.dtype, np.datetime64) or
417+
isinstance(d, np.datetime64)):
418+
return _dt64_to_ordinalf(d)
422419
return _to_ordinalf(d)
423420
else:
424421
d = np.asarray(d)
422+
# note that this unpacks anything that has a numpy array in it
423+
# and just returns the array. So this strips the array out of
424+
# a pandas series object, for instance.
425425
if not d.size:
426426
return d
427+
if hasattr(d, 'dtype'):
428+
if ((isinstance(d, np.ndarray) and
429+
np.issubdtype(d.dtype, np.datetime64)) or
430+
isinstance(d, np.datetime64)):
431+
return _dt64_to_ordinalf(d)
427432
return _to_ordinalf_np_vectorized(d)
428433

429434

lib/matplotlib/testing/conftest.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,8 @@ def pd():
8282
pd = pytest.importorskip('pandas')
8383
try:
8484
from pandas.plotting import (
85-
register_matplotlib_converters as register)
85+
deregister_matplotlib_converters as deregister)
86+
deregister()
8687
except ImportError:
87-
from pandas.tseries.converter import register
88-
register()
89-
try:
90-
yield pd
91-
finally:
92-
try:
93-
from pandas.plotting import (
94-
deregister_matplotlib_converters as deregister)
95-
except ImportError:
96-
pass
97-
else:
98-
deregister()
88+
pass
89+
return pd

lib/matplotlib/tests/test_axes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5257,6 +5257,18 @@ def test_pandas_bar_align_center(pd):
52575257
fig.canvas.draw()
52585258

52595259

5260+
def test_pandas_data_unpack(pd):
5261+
# smoke test for all these different calling methods.
5262+
dates = [datetime.datetime(2018, 7, i) for i in range(1, 5)]
5263+
values = np.cumsum(np.random.rand(len(dates)))
5264+
df = pd.DataFrame({"dates": dates, "values": values})
5265+
plt.plot(df["dates"].values, df["values"])
5266+
plt.scatter(df["dates"], df["values"])
5267+
plt.plot("dates", "values", data=df)
5268+
plt.scatter(x="dates", y="values", data=df)
5269+
plt.draw()
5270+
5271+
52605272
def test_axis_set_tick_params_labelsize_labelcolor():
52615273
# Tests fix for issue 4346
52625274
axis_1 = plt.subplot()

lib/matplotlib/tests/test_dates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ def tz_convert(*args):
585585

586586
def test_dateboxplot_pandas(pd):
587587
# smoke test that this doesn't fail.
588-
data = np.random.rand(5,2)
588+
data = np.random.rand(5, 2)
589589
years = pd.date_range('1/1/2000',
590590
periods=2, freq=pd.DateOffset(years=1)).year
591591
# Does not work

0 commit comments

Comments
 (0)