has2k1 / plotnine

A Grammar of Graphics for Python
https://plotnine.org
MIT License
4k stars 213 forks source link

facet_* with date columns behaving inconsistently #629

Closed zykezero closed 1 year ago

zykezero commented 1 year ago

Hello,

I'll be honest I'm new to python + plotnine so I'm not sure if this is a real issue or not.

example_pd = pd.DataFrame(
    {
        "col": [1,2,3],
        "group": ['a', 'b', 'c'],
        "date": ['2020-01-01', '2020-02-01', '2020-03-01']
    }
)
example_pd['date'] = pd.to_datetime(example_pd['date'])

facet_grid returns a 0 index error IndexError: index 0 is out of bounds for axis 0 with size 0

(
    ggplot(x, aes(
        y = 'col',
        x = 'group'
    )) +
    facet_grid('group ~ date') +
    geom_col()
)

facet_wrap seems to put everything in one facet

(
    ggplot(x, aes(
        y = 'col',
        x = 'group'
    )) +
    facet_wrap('date') + 
    geom_col()
)

But if you are to go and do this all without converting the date column to date format and not just a character everything works as expected.

has2k1 commented 1 year ago

Yes this is a bug. A workaround is to convert the date column to a pd.Categorical.

zykezero commented 1 year ago

great! new to it all and thought I was doing something wrong. Great work I appreciate it greatly coming from R to python.