lonboard icon indicating copy to clipboard operation
lonboard copied to clipboard

[BUG] apply_categorical_cmap TypeError when passing pandas categories

Open z-clement opened this issue 1 year ago • 2 comments

Context

What results were you expecting?
Expected results are for apply_categorical_cmap() to return a color object that I could pass to PathLayer.from_geopandas().

In version 0.9.2 the apply_categorical_cmap() function worked with the following call: apply_categorical_cmap(df[attribute_to_map], cmap) where df[attribute_to_map] is a categorical data column in a pandas dataframe, and cmap is a custom defined dictionary:

cmap = {
            "<20": colorbrewer.sequential.YlGnBu_8.colors[0],
            "20 to 30": colorbrewer.sequential.YlGnBu_8.colors[1],
            "30 to 40": colorbrewer.sequential.YlGnBu_8.colors[2],
            "40 to 50": colorbrewer.sequential.YlGnBu_8.colors[3],
            "50 to 60": colorbrewer.sequential.YlGnBu_8.colors[4],
            "60 to 70": colorbrewer.sequential.YlGnBu_8.colors[5],
            "70 to 80": colorbrewer.sequential.YlGnBu_8.colors[6],
            ">80": colorbrewer.sequential.YlGnBu_8.colors[7],
}

The keys of this cmap dictionary are the labels of the categorical bins, created using pd.cut(). The values are RGB colors from the colorbrewer module in paletteable.

Resulting behaviour, error message or logs

Describe what happened: An error is thrown:

File \.venv\Lib\site-packages\lonboard\colormap.py:193, in apply_categorical_cmap(values, cmap, alpha)
    [190](/.venv/Lib/site-packages/lonboard/colormap.py:190)     import pandas as pd
    [192](/.venv/Lib/site-packages/lonboard/colormap.py:192)     if isinstance(values, pd.Series):
--> [193](/.venv/Lib/site-packages/lonboard/colormap.py:193)         values = Array.from_numpy(values)
    [194](/.venv/Lib/site-packages/lonboard/colormap.py:194) except ImportError:
    [195](/.venv/Lib/site-packages/lonboard/colormap.py:195)     pass

ValueError: Unsupported data type object

Include relevant screenshots, error messages, and logs:

Environment

  • OS: Windows 11
  • Browser: VSCode
  • Python 3.12.3
  • Lonboard Version: 0.10.3

Steps to reproduce the bug

Describe the actions that led you to encounter the bug. Example:

  1. imported lonboard into a new notebook
  2. used pandas to bin a continuous variable into categories
  3. created an instance of a map
  4. added a path layer of the binned categorical data, with apply_categorical_cmap as the get_color argument

z-clement avatar Dec 03 '24 18:12 z-clement

Casting the dataframe column to a pyarrow array when passing it to the apply_categorical_cmap function makes it work. Going to leave this open in case it's unintended for that to have broken in between versions, but wanted to note a workaround.

Updated code is just: apply_categorical_cmap(pa.array(df[attribute_to_map]), cmap).

z-clement avatar Dec 03 '24 19:12 z-clement

Thanks for the issue. Indeed I'd say this is a bug. I don't have time to look into this right now, but I'm glad to see there's an easy workaround.

kylebarron avatar Dec 05 '24 16:12 kylebarron

Looks like it's caused by this issue in arro3: https://github.com/kylebarron/arro3/issues/256

Another simple workaround (I didn't want to import pyarrow just to typecast) when you have categorical data is to use the integer category codes.

You would then have to change your cmap keys to match the codes.


values = df["categories"].cat.codes

cmap = {
    0: [127, 60, 141],
    1: [57, 105, 172], 
    2: [17, 165, 121], 
    ... 
}

jwardbond avatar May 02 '25 19:05 jwardbond

This is fixed with the latest version of arro3-core, to be released imminently as arro3-core v0.6.

See upstream https://github.com/kylebarron/arro3/issues/256

kylebarron avatar Aug 22 '25 00:08 kylebarron