Skip to content

Commit

Permalink
Improved handling for datashading empty objects
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Feb 15, 2018
1 parent c0a6fce commit e13a25c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
9 changes: 6 additions & 3 deletions holoviews/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1569,14 +1569,17 @@ def dt_to_int(value, time_unit='us'):
"""
Converts a datetime type to an integer with the supplied time unit.
"""
if time_unit == 'ns':
tscale = 1
if isinstance(value, np.datetime64):
value = np.datetime64(value, 'ns')
tscale = np.timedelta64(1, time_unit)/np.timedelta64(1, 'ns') * 1000
elif time_unit == 'ns':
tscale = 1000.
else:
tscale = 1./np.timedelta64(1, time_unit).tolist().total_seconds()
if pd and isinstance(value, pd.Timestamp):
value = value.to_pydatetime()
elif isinstance(value, np.datetime64):
value = np.datetime64(value, 'ns').tolist()
value = value.tolist()
if isinstance(value, (int, long)):
# Handle special case of nanosecond precision which cannot be
# represented by python datetime
Expand Down
6 changes: 5 additions & 1 deletion holoviews/operation/datashader.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def get_agg_data(cls, obj, category=None):
paths = [p.compute() if isinstance(p, dd.DataFrame) else p for p in paths]
df = pd.concat(paths)
else:
df = paths[0]
df = paths[0] if paths else pd.DataFrame([], columns=[x.name, y.name])
if category and df[category].dtype.name != 'category':
df[category] = df[category].astype('category')

Expand Down Expand Up @@ -360,6 +360,10 @@ def _process(self, element, key=None):
xarray = xr.DataArray(np.full((height, width), np.NaN, dtype=np.float32),
dims=['y', 'x'], coords={'x': xs, 'y': ys})
return self.p.element_type(xarray)
elif not len(data):
xarray = xr.DataArray(np.full((height, width), np.NaN, dtype=np.float32),
dims=[y.name, x.name], coords={x.name: xs, y.name: ys})
return self.p.element_type(xarray)

cvs = ds.Canvas(plot_width=width, plot_height=height,
x_range=x_range, y_range=y_range)
Expand Down
18 changes: 17 additions & 1 deletion tests/testcoreutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,10 +562,18 @@ def test_datetime64_to_us_int(self):
dt = np.datetime64(datetime.datetime(2017, 1, 1))
self.assertEqual(dt_to_int(dt), 1483228800000000.0)

def test_datetime64_us_to_us_int(self):
dt = np.datetime64(datetime.datetime(2017, 1, 1), 'us')
self.assertEqual(dt_to_int(dt), 1483228800000000.0)

def test_datetime64_s_to_us_int(self):
dt = np.datetime64(datetime.datetime(2017, 1, 1), 's')
self.assertEqual(dt_to_int(dt), 1483228800000000.0)

def test_timestamp_to_us_int(self):
dt = pd.Timestamp(datetime.datetime(2017, 1, 1))
self.assertEqual(dt_to_int(dt), 1483228800000000.0)

def test_datetime_to_s_int(self):
dt = datetime.datetime(2017, 1, 1)
self.assertEqual(dt_to_int(dt, 's'), 1483228800.0)
Expand All @@ -574,6 +582,14 @@ def test_datetime64_to_s_int(self):
dt = np.datetime64(datetime.datetime(2017, 1, 1))
self.assertEqual(dt_to_int(dt, 's'), 1483228800.0)

def test_datetime64_us_to_s_int(self):
dt = np.datetime64(datetime.datetime(2017, 1, 1), 'us')
self.assertEqual(dt_to_int(dt, 's'), 1483228800.0)

def test_datetime64_s_to_s_int(self):
dt = np.datetime64(datetime.datetime(2017, 1, 1), 's')
self.assertEqual(dt_to_int(dt, 's'), 1483228800.0)

def test_timestamp_to_s_int(self):
dt = pd.Timestamp(datetime.datetime(2017, 1, 1))
self.assertEqual(dt_to_int(dt, 's'), 1483228800.0)
Expand Down

0 comments on commit e13a25c

Please sign in to comment.