Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datetime64 fixes #816

Merged
merged 13 commits into from
Aug 18, 2016
Merged
2 changes: 1 addition & 1 deletion holoviews/core/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ def get_dimension_type(self, dim):
dim_obj = self.get_dimension(dim)
if dim_obj and dim_obj.type is not None:
return dim_obj.type
return self.interface.dimension_type(self, dim)
return self.interface.dimension_type(self, dim_obj)


def dframe(self, dimensions=None):
Expand Down
6 changes: 2 additions & 4 deletions holoviews/core/data/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,8 @@ def validate(cls, dataset):

@classmethod
def dimension_type(cls, dataset, dim):
if dim in dataset.kdims:
arr = dataset.data[dim.name]
elif dim in dataset.vdims:
arr = dataset.data[dim.name]
if dim in dataset.dimensions():
arr = cls.values(dataset, dim, False, False)
else:
return None
return arr.dtype.type
Expand Down
5 changes: 4 additions & 1 deletion holoviews/core/data/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ def range(cls, dataset, dimension):
dim = dataset.get_dimension(dimension).name
if dim in dataset.data:
data = dataset.data[dim]
return data.min().item(), data.max().item()
dmin, dmax = data.min().data, data.max().data
dmin = dmin if np.isscalar(dmin) else dmin.item()
dmax = dmax if np.isscalar(dmax) else dmax.item()
return dmin, dmax
else:
return np.NaN, np.NaN

Expand Down
12 changes: 11 additions & 1 deletion holoviews/plotting/mpl/chart.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
from __future__ import unicode_literals

from itertools import product

import numpy as np
from matplotlib import cm
from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.dates import date2num

import param

from ...core import OrderedDict
from ...core.util import (match_spec, unique_iterator, safe_unicode,
basestring, max_range, unicode)
from ...element import Points, Raster, Polygons, HeatMap
from ..util import compute_sizes, get_sideplot_ranges
from ..util import compute_sizes, get_sideplot_ranges, dt64_to_dt
from .element import ElementPlot, ColorbarPlot, LegendPlot
from .path import PathPlot
from .plot import AdjoinedPlot
Expand Down Expand Up @@ -61,6 +63,14 @@ def get_data(self, element, ranges, style):
ys = element.dimension_values(1)
return (xs, ys), style, {}

def init_artists(self, ax, plot_args, plot_kwargs):
xs, ys = plot_args
if xs.dtype.kind == 'M':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding a comment mentioning what the 'M' code means. I assume it is the numpy datetime64 type?

xs = np.array([date2num(dt64_to_dt(x)) for x in xs])
artist = ax.plot_date(xs, ys, '-', **plot_kwargs)[0]
else:
artist = ax.plot(xs, ys, **plot_kwargs)[0]
return {'artist': artist}

def update_handles(self, key, axis, element, ranges, style):
artist = self.handles['artist']
Expand Down
3 changes: 3 additions & 0 deletions holoviews/plotting/mpl/element.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import math
import datetime as dt

from matplotlib import ticker
from matplotlib import colors
Expand Down Expand Up @@ -302,6 +303,8 @@ def _set_axis_limits(self, axis, view, subplots, ranges):
extents = self.get_extents(view, ranges)
if extents and not self.overlaid:
coords = [coord if np.isreal(coord) else np.NaN for coord in extents]
coords = [c.astype(dt.datetime) if isinstance(c, np.datetime64) else c
for c in coords]
valid_lim = lambda c: util.isnumeric(c) and not np.isnan(c)
if self.projection == '3d' or len(extents) == 6:
l, b, zmin, r, t, zmax = coords
Expand Down
9 changes: 9 additions & 0 deletions holoviews/plotting/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import unicode_literals

from datetime import datetime as dt

import numpy as np
import param

Expand Down Expand Up @@ -270,3 +272,10 @@ def dim_axis_label(dimensions, separator=', '):
if not isinstance(dimensions, list): dimensions = [dimensions]
return separator.join([safe_unicode(d.pprint_label)
for d in dimensions])

def dt64_to_dt(dt64):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a useful utility!

"""
Safely converts NumPy datetime64 to a datetime object.
"""
ts = (dt64 - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's')
return dt.utcfromtimestamp(ts)