[BUG] apply_categorical_cmap TypeError when passing pandas categories
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:
- imported lonboard into a new notebook
- used pandas to bin a continuous variable into categories
- created an instance of a map
- added a path layer of the binned categorical data, with
apply_categorical_cmapas theget_colorargument
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).
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.
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],
...
}
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