holoviz / datashader

Quickly and accurately render even the largest data.
http://datashader.org
BSD 3-Clause "New" or "Revised" License
3.26k stars 363 forks source link

Reduction ds.by() with one category now fails on 0.15.0 #1230

Closed desean1625 closed 1 year ago

desean1625 commented 1 year ago

The below code worked on 0.14.2 and on 0.15.0 it doesn't get the correct dshape in the category_codes reducer. It wasn't obvious to me why adding a second category "fixed it"

NotImplementedError: Unexpected dshape <function dshape at 0x7f0dfed1da20>

import pandas as pd
import numpy as np
from collections import OrderedDict as odict
import datashader as ds
num=100
np.random.seed(1)

dists = {cat: pd.DataFrame(odict([('x',np.random.normal(x,s,num)), 
                                  ('y',np.random.normal(y,s,num)), 
                                  ('c',np.random.randint(0,val,num)), 
                                  ('t',cat)]))      
         for x,  y,  s,  val, cat in 
         [(  2,  2, 0.03, 10, "d1")] }

df = pd.concat(dists,ignore_index=True)
categories = ['d1']
cat_dtype = pd.api.types.CategoricalDtype(categories=categories, ordered=True)
df["t"] = df["t"].astype(cat_dtype)

agg = ds.Canvas(
    plot_height=256, 
    plot_width=256, 
    x_range=(-8,8),
    y_range=(-8,8)
    ).points(df, "x", "y", agg=ds.by("t", ds.sum("c")))

a "fix" is to add a second category if the length is 1

categories=['d1']
if len(categories) == 1:
  categories.append('')
ianthomas23 commented 1 year ago

Thanks for the report @desean1625. Evidently we don't have any tests for reductions using a single category.

Problem was caused by #1142. There will be a fix shortly.