Skip to content

Commit 10e43c6

Browse files
Chang Shewesm
Chang She
authored andcommitted
BUG: ts plotting fix after nanoseconds change #1265
1 parent 8ce369a commit 10e43c6

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

pandas/src/datetime.pyx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ try:
4343
except NameError: # py3
4444
basestring = str
4545

46+
def ints_to_pydatetime(ndarray[int64_t] arr):
47+
cdef:
48+
Py_ssize_t i, n = len(arr)
49+
pandas_datetimestruct dts
50+
ndarray[object] result = np.empty(n, dtype=object)
51+
52+
for i in range(n):
53+
pandas_datetime_to_datetimestruct(arr[i], PANDAS_FR_ns, &dts)
54+
result[i] = datetime(dts.year, dts.month, dts.day,
55+
dts.hour, dts.min, dts.sec, dts.us)
56+
57+
return result
58+
4659

4760
# Python front end to C extension type _Timestamp
4861
# This serves as the box for datetime64

pandas/tseries/index.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ def _cached_range(cls, start=None, end=None, periods=None, offset=None,
408408

409409
def _mpl_repr(self):
410410
# how to represent ourselves to matplotlib
411-
return self.values.astype('O')
411+
return lib.ints_to_pydatetime(self.asi8)
412412

413413
def __repr__(self):
414414
if self.offset is not None:

pandas/tseries/plotting.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def tsplot(series, plotf, *args, **kwargs):
7171
freq = getattr(series.index, 'freq', None)
7272
if freq is None and hasattr(series.index, 'inferred_freq'):
7373
freq = series.index.inferred_freq
74+
7475
if isinstance(freq, DateOffset):
7576
freq = freq.rule_code
7677

@@ -80,7 +81,7 @@ def tsplot(series, plotf, *args, **kwargs):
8081
idx = series.index.to_period(freq=freq)
8182
series = Series(series.values, idx, name=series.name)
8283

83-
if not isinstance(series.index, PeriodIndex):
84+
if not isinstance(series.index, PeriodIndex): # business freq
8485
raise TypeError('series argument to tsplot must have DatetimeIndex or '
8586
'PeriodIndex')
8687

@@ -231,6 +232,16 @@ def _handle_period_index(curr, remaining, series, xdata, freq):
231232
if series is None:
232233
raise ValueError(noinfo_msg)
233234

235+
def infer_min_freq(series):
236+
"""
237+
To be used for irregular DatetimeIndex
238+
figure out minimum time span between points and map to some offset alias
239+
240+
Returns
241+
-------
242+
offset alias: str
243+
"""
244+
234245

235246
##### -------------------------------------------------------------------------
236247
#---- --- Locators ---

pandas/tseries/tests/test_plotting.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
from pandas import Index, Series, DataFrame, isnull, notnull
1111

1212
from pandas.tseries.index import date_range
13-
from pandas.tseries.offsets import Minute, bday
13+
from pandas.tseries.offsets import Minute, bday, DateOffset
1414
from pandas.tseries.period import period_range
1515
from pandas.tseries.resample import DatetimeIndex, TimeGrouper
1616
import pandas.tseries.offsets as offsets
17+
import pandas.tseries.frequencies as frequencies
1718

1819
from pandas.util.testing import assert_series_equal, assert_almost_equal
1920
import pandas.util.testing as tm
@@ -91,14 +92,28 @@ def test_line_plot_inferred_freq(self):
9192
_check_plot_works(ser.plot)
9293

9394
@slow
94-
def test_aplot_offset_freq(self):
95+
def test_plot_offset_freq(self):
9596
ser = tm.makeTimeSeries()
9697
_check_plot_works(ser.plot)
9798

9899
dr = date_range(ser.index[0], freq='BQS', periods=10)
99100
ser = Series(np.random.randn(len(dr)), dr)
100101
_check_plot_works(ser.plot)
101102

103+
@slow
104+
def test_irregular_datetime64_repr_bug(self):
105+
ser = tm.makeTimeSeries()
106+
ser = ser[[0,1,2,7]]
107+
import matplotlib.pyplot as plt
108+
109+
fig = plt.gcf()
110+
plt.clf()
111+
ax = fig.add_subplot(211)
112+
ret = ser.plot()
113+
assert(ret is not None)
114+
115+
for rs, xp in zip(ax.get_lines()[0].get_xdata(), ser.index):
116+
assert(rs == xp)
102117

103118
PNG_PATH = 'tmp.png'
104119
def _check_plot_works(f, freq=None, series=None, *args, **kwargs):
@@ -111,8 +126,12 @@ def _check_plot_works(f, freq=None, series=None, *args, **kwargs):
111126
assert(ret is not None) # do something more intelligent
112127

113128
orig_ax = kwargs.pop('ax', plt.gca())
114-
if series is not None:
115-
assert(orig_ax.freq == series.index.freq)
129+
if series is not None: # non-business
130+
dfreq = series.index.freq
131+
if isinstance(dfreq, DateOffset):
132+
dfreq = dfreq.rule_code
133+
#dfreq = frequencies.offset_to_period_alias(dfreq)
134+
assert(orig_ax.freq == dfreq)
116135

117136
if freq is not None:
118137
assert(orig_ax.freq == freq)

0 commit comments

Comments
 (0)