Skip to content

Commit 5fbffe4

Browse files
author
Chang She
committed
BUG: sub-second plotting was not working
1 parent 91afd54 commit 5fbffe4

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

pandas/tools/plotting.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pandas.core.series import Series
1111
from pandas.tseries.index import DatetimeIndex
1212
from pandas.tseries.period import PeriodIndex
13-
from pandas.tseries.frequencies import get_period_alias
13+
from pandas.tseries.frequencies import get_period_alias, get_base_alias
1414
from pandas.tseries.offsets import DateOffset
1515
import pandas.tseries.tools as datetools
1616

@@ -590,14 +590,19 @@ def __init__(self, data, **kwargs):
590590
def has_ts_index(self):
591591
from pandas.core.frame import DataFrame
592592
if isinstance(self.data, (Series, DataFrame)):
593-
if isinstance(self.data.index, (DatetimeIndex, PeriodIndex)):
594-
has_freq = (hasattr(self.data.index, 'freq') and
595-
self.data.index.freq is not None)
596-
has_inferred = (hasattr(self.data.index, 'inferred_freq') and
597-
self.data.index.inferred_freq is not None)
598-
return has_freq or has_inferred
593+
freq = (getattr(self.data.index, 'freq', None)
594+
or getattr(self.data.index, 'inferred_freq', None))
595+
return (freq is not None) and self._has_dynamic_index_freq(freq)
599596
return False
600597

598+
def _has_dynamic_index_freq(self, freq):
599+
if isinstance(freq, DateOffset):
600+
freq = freq.rule_code
601+
else:
602+
freq = get_base_alias(freq)
603+
freq = get_period_alias(freq)
604+
return freq is not None
605+
601606
def _make_plot(self):
602607
# this is slightly deceptive
603608
if self.use_index and self.has_ts_index:

pandas/tseries/frequencies.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,16 @@ def _get_freq_str(base, mult=1):
246246
'BA' : 'A',
247247
'AS' : 'A',
248248
'BAS' : 'A',
249-
'MS' : 'M'
249+
'MS' : 'M',
250+
'D' : 'D',
251+
'B' : 'B',
252+
'T' : 'T',
253+
'S' : 'S',
254+
'H' : 'H',
255+
'Q' : 'Q',
256+
'A' : 'A',
257+
'W' : 'W',
258+
'M' : 'M'
250259
}
251260

252261
need_suffix = ['QS', 'BQ', 'BQS', 'AS', 'BA', 'BAS']
@@ -257,9 +266,20 @@ def _get_freq_str(base, mult=1):
257266
_offset_to_period_map['%s-%s' % (prefix, m)] = \
258267
_offset_to_period_map[prefix]
259268

269+
months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP',
270+
'OCT', 'NOV', 'DEC']
271+
for prefix in ['A', 'Q']:
272+
for m in months:
273+
alias = '%s-%s' % (prefix, m)
274+
_offset_to_period_map[alias] = alias
275+
276+
_days = ['MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN']
277+
for d in _days:
278+
_offset_to_period_map['W-%s' % d] = 'W-%s' % d
279+
260280
def get_period_alias(offset_str):
261281
""" alias to closest period strings BQ->Q etc"""
262-
return _offset_to_period_map.get(offset_str, offset_str)
282+
return _offset_to_period_map.get(offset_str, None)
263283

264284
_rule_aliases = {
265285
# Legacy rules that will continue to map to their original values

pandas/tseries/plotting.py

-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ def _get_default_annual_spacing(nyears):
136136
(min_spacing, maj_spacing) = (factor * 20, factor * 100)
137137
return (min_spacing, maj_spacing)
138138

139-
140139
def period_break(dates, period):
141140
"""
142141
Returns the indices where the given period changes.

pandas/tseries/tests/test_plotting.py

+9
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ def test_tsplot(self):
6868
ax = ts.plot(style='k')
6969
self.assert_((0., 0., 0.) == ax.get_lines()[0].get_color())
7070

71+
@slow
72+
def test_high_freq(self):
73+
freaks = ['ms', 'us']
74+
for freq in freaks:
75+
rng = date_range('1/1/2012', periods=100000, freq=freq)
76+
ser = Series(np.random.randn(len(rng)), rng)
77+
_check_plot_works(ser.plot)
78+
7179
def test_get_datevalue(self):
7280
from pandas.tseries.plotting import get_datevalue
7381
self.assert_(get_datevalue(None, 'D') is None)
@@ -268,6 +276,7 @@ def test_finder_monthly(self):
268276
@slow
269277
def test_finder_annual(self):
270278
import matplotlib.pyplot as plt
279+
plt.close('all')
271280
xp = [1987, 1988, 1990, 1990, 1995, 2020, 2070, 2170]
272281
for i, nyears in enumerate([5, 10, 19, 49, 99, 199, 599, 1001]):
273282
rng = period_range('1987', periods=nyears, freq='A')

0 commit comments

Comments
 (0)