Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] apply_categorical_cmap TypeError when passing pandas categories #715

Open
z-clement opened this issue Dec 3, 2024 · 2 comments
Open
Labels
bug Something isn't working

Comments

@z-clement
Copy link

z-clement commented Dec 3, 2024

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 z-clement added the bug Something isn't working label Dec 3, 2024
@z-clement
Copy link
Author

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).

@kylebarron
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants