Skip to content

Facet_grid(drop=True)) + nan in categorical -> IndexErorr #1086

Description

@TyberiusPrime

Probably something trivial when dealing with Nan/None in Categoricals:

Reproducer:

import pandas as pd
import plotnine as p9

print("plotnine", p9.__version__)

c = pd.Categorical(["x", "y", None, "x", "y", None], categories=["x", "y"])
df = pd.DataFrame(
    {
        "x": [0.0] * 6,
        "y": [0.0] * 6,
        "R": ["a", "a", "a", "b", "b", "b"],
        "C": c,
    }
)

p = p9.ggplot(df, p9.aes("x", "y")) + p9.geom_point() + p9.facet_grid("R ~ C", drop=False)
p.save("debug_wo_drop.png") # passes

p = p9.ggplot(df, p9.aes("x", "y")) + p9.geom_point() + p9.facet_grid("R ~ C")
p.save("debug.png") # IndexError

Error:

plotnine 0.15.7
/nix/store/nqm1pim5gv72cfi1pff5x9qnzq5i2csf-anysnake2-venv/lib/python3.12/site-packages/plotnine/ggplot.py:623: PlotnineWarning: Saving 6.4 x 4.8 in image.
/nix/store/nqm1pim5gv72cfi1pff5x9qnzq5i2csf-anysnake2-venv/lib/python3.12/site-packages/plotnine/ggplot.py:624: PlotnineWarning: Filename: debug_wo_drop.png
/nix/store/nqm1pim5gv72cfi1pff5x9qnzq5i2csf-anysnake2-venv/lib/python3.12/site-packages/plotnine/ggplot.py:623: PlotnineWarning: Saving 6.4 x 4.8 in image.
/nix/store/nqm1pim5gv72cfi1pff5x9qnzq5i2csf-anysnake2-venv/lib/python3.12/site-packages/plotnine/ggplot.py:624: PlotnineWarning: Filename: debug.png
Traceback (most recent call last):
  File "/home/user/upstream/python/expose_plotnine.py", line 22, in <module>
    p.save("debug.png") # IndexError
    ^^^^^^^^^^^^^^^^^^^
  File "/nix/store/nqm1pim5gv72cfi1pff5x9qnzq5i2csf-anysnake2-venv/lib/python3.12/site-packages/plotnine/ggplot.py", line 681, in save
    sv = self.save_helper(
         ^^^^^^^^^^^^^^^^^
  File "/nix/store/nqm1pim5gv72cfi1pff5x9qnzq5i2csf-anysnake2-venv/lib/python3.12/site-packages/plotnine/ggplot.py", line 629, in save_helper
    figure = self.draw(show=False)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/nqm1pim5gv72cfi1pff5x9qnzq5i2csf-anysnake2-venv/lib/python3.12/site-packages/plotnine/ggplot.py", line 309, in draw
    self.axs = self.facet.setup(self)
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/nqm1pim5gv72cfi1pff5x9qnzq5i2csf-anysnake2-venv/lib/python3.12/site-packages/plotnine/facets/facet.py", line 154, in setup
    self.strips = Strips.from_facet(self)
                  ^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/nqm1pim5gv72cfi1pff5x9qnzq5i2csf-anysnake2-venv/lib/python3.12/site-packages/plotnine/facets/strips.py", line 151, in from_facet
    new.setup()
  File "/nix/store/nqm1pim5gv72cfi1pff5x9qnzq5i2csf-anysnake2-venv/lib/python3.12/site-packages/plotnine/facets/strips.py", line 185, in setup
    ax = self.axs[layout_info.panel_index]
         ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
IndexError: list index out of range

Disappears if drop=False.
Still preset int 0.16.0a11
Doesn't seem to be an issue in facet_wrap.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions