Skip to content

Commit

Permalink
fix: datashade don't update the dtype of the input DataFrame (#6433)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxbro authored Nov 1, 2024
1 parent 9400f82 commit d3aafd6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
12 changes: 8 additions & 4 deletions holoviews/operation/datashader.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,17 @@ def get_agg_data(cls, obj, category=None):
df = pd.concat(paths)
else:
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')

is_custom = lazy_isinstance(df, "dask.dataframe:DataFrame") or cuDFInterface.applies(df)
if any((not is_custom and len(df[d.name]) and isinstance(df[d.name].values[0], cftime_types)) or
df[d.name].dtype.kind in ["M", "u"] for d in (x, y)):
category_check = category and df[category].dtype.name != 'category'
if (
category_check or
any((not is_custom and len(df[d.name]) and isinstance(df[d.name].values[0], cftime_types)) or
df[d.name].dtype.kind in ["M", "u"] for d in (x, y))
):
df = df.copy()
if category_check:
df[category] = df[category].astype('category')

for d in (x, y):
vals = df[d.name]
Expand Down
9 changes: 9 additions & 0 deletions holoviews/tests/operation/test_datashader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1646,3 +1646,12 @@ def test_imagestack_dynspread():
points = Points(df, ['x','y'], ['language'])
op = dynspread(rasterize(points, aggregator=ds.by('language', ds.count())))
render(op) # should not error out

def test_datashade_count_cat_no_change_inplace():
# Test for https://github.com/holoviz/holoviews/issues/6324
df = pd.DataFrame({"x": range(3), "y": range(3), "c": list(map(str, range(3)))})
assert df["c"].dtype == "object"
op = datashade(Points(df), aggregator=ds.count_cat("c"))
render(op)
# Should not convert to category dtype
assert df["c"].dtype == "object"

0 comments on commit d3aafd6

Please sign in to comment.