Skip to content

Commit

Permalink
ENH: datetime.time converters for plotting
Browse files Browse the repository at this point in the history
  • Loading branch information
Chang She authored and wesm committed Jun 28, 2012
1 parent ce4ae93 commit 61aac3d
Show file tree
Hide file tree
Showing 6 changed files with 804 additions and 653 deletions.
1 change: 1 addition & 0 deletions pandas/src/datetime.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ cdef extern from "datetime.h":
int PyDateTime_TIME_GET_MICROSECOND(object o)
bint PyDateTime_Check(object o)
bint PyDate_Check(object o)
bint PyTime_Check(object o)
object PyDateTime_FromDateAndTime(int year, int month, int day, int hour,
int minute, int second, int us)

Expand Down
11 changes: 11 additions & 0 deletions pandas/src/inference.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ cdef inline bint is_datetime(object o):
cdef inline bint is_date(object o):
return PyDate_Check(o)

cdef inline bint is_time(object o):
return PyTime_Check(o)

def is_bool_array(ndarray values):
cdef:
Py_ssize_t i, n = len(values)
Expand Down Expand Up @@ -240,6 +243,14 @@ def is_date_array(ndarray[object] values):
return False
return True

def is_time_array(ndarray[object] values):
cdef int i, n = len(values)
if n == 0:
return False
for i in range(n):
if not is_time(values[i]):
return False
return True

def maybe_convert_numeric(ndarray[object] values, set na_values,
convert_empty=True):
Expand Down
46 changes: 10 additions & 36 deletions pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
import pandas.tseries.tools as datetools
import pandas.lib as lib

try: # mpl optional
import pandas.tseries.converter as conv
conv.register()
except ImportError:
pass

def _get_standard_kind(kind):
return {'density' : 'kde'}.get(kind, kind)

Expand Down Expand Up @@ -492,8 +498,9 @@ def plt(self):

def _get_xticks(self, convert_period=False):
index = self.data.index
is_datetype = index.inferred_type in ('datetime', 'date',
'datetime64')
is_datetype = (index.inferred_type in ('datetime', 'date',
'datetime64')
or lib.is_time_array(index))

if self.use_index:
if convert_period and isinstance(index, PeriodIndex):
Expand All @@ -508,6 +515,7 @@ def _get_xticks(self, convert_period=False):
"""
x = index._mpl_repr()
else:
foo
self._need_to_set_index = True
x = range(len(index))
else:
Expand Down Expand Up @@ -578,40 +586,6 @@ def _post_plot_logic(self):
if self.subplots and self.legend:
self.axes[0].legend(loc='best')

try: # matplotlib is optional dependency
import matplotlib.units as units
import matplotlib.dates as dates

class DatetimeConverter(dates.DateConverter):

@staticmethod
def convert(values, unit, axis):
def try_parse(values):
try:
return datetools.to_datetime(values).toordinal()
except Exception:
return values

if isinstance(values, (datetime.datetime, datetime.date)):
return values.toordinal()
elif isinstance(values, (datetime.time)):
return dates.date2num(values)
elif (com.is_integer(values) or com.is_float(values)):
return values
elif isinstance(values, str):
return try_parse(values)
elif isinstance(values, Index):
return values.map(try_parse)
elif isinstance(values, (list, tuple, np.ndarray)):
return [try_parse(x) for x in values]
return values

units.registry[lib.Timestamp] = DatetimeConverter()
units.registry[datetime.date] = DatetimeConverter()
units.registry[datetime.datetime] = DatetimeConverter()
except ImportError:
pass

class LinePlot(MPLPlot):

def __init__(self, data, **kwargs):
Expand Down
Loading

0 comments on commit 61aac3d

Please sign in to comment.