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

Skip to content

Commit 3359f30

Browse files
committed
Add new test for pandas time series date formatter
1 parent 18b2655 commit 3359f30

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

plotly/tests/test_optional/test_matplotlylib/test_date_times.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
from __future__ import absolute_import
2+
3+
import random
24
import matplotlib
35
# Force matplotlib to not use any Xwindows backend.
46
matplotlib.use('Agg')
57
import matplotlib.pyplot as plt
68
import datetime
79
from matplotlib.dates import date2num
10+
import pandas as pd
811
import plotly.tools as tls
912
from unittest import TestCase
1013

@@ -41,3 +44,24 @@ def test_normal_mpl_dates(self):
4144
fig.axes[0].lines[0].get_xydata()[0][0], 7.33776000e+05
4245
)
4346
self.assertEqual(pfig['data'][0]['x'], date_strings)
47+
48+
def test_pandas_time_series_date_formatter(self):
49+
ndays = 3
50+
x = pd.date_range('1/1/2001', periods=ndays, freq='D')
51+
y = [random.randint(0, 10) for i in range(ndays)]
52+
s = pd.DataFrame(y, columns=['a'])
53+
54+
s['Date'] = x
55+
s.plot(x='Date')
56+
57+
fig = plt.gcf()
58+
pfig = tls.mpl_to_plotly(fig)
59+
60+
expected_x = ['2001-01-01 00:00:00',
61+
'2001-01-02 00:00:00',
62+
'2001-01-03 00:00:00']
63+
expected_x0 = 11323.0 # this is floating point days since epoch
64+
65+
x0 = fig.axes[0].lines[0].get_xydata()[0][0]
66+
self.assertEqual(x0, expected_x0)
67+
self.assertListEqual(pfig['data'][0]['x'], expected_x)

0 commit comments

Comments
 (0)