Skip to content

geom_sina fails when index doesn't contain 0 #888

@redst4r

Description

@redst4r

As suggested in #221 , geom_sina fails (as of plotnine="0.14.1") when called on a dataframe whose index does not contain the 0 element:

# make up some random data
df_fails = pd.DataFrame({
    'x': ['a','a','a','b','b','b'],
    'y': [1, 2 ,3 ,4 ,5 ,6]
}, index=[1,2,3,4,5,6])    # note that 0 is NOT in the index
pn.ggplot(df_fails) + pn.aes(x='x', y='y') + pn.geom_sina()

yields

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File /opt/conda/lib/python3.11/site-packages/pandas/core/indexes/base.py:3805, in Index.get_loc(self, key)
   3804 try:
-> 3805     return self._engine.get_loc(casted_key)
   3806 except KeyError as err:

File index.pyx:167, in pandas._libs.index.IndexEngine.get_loc()

File index.pyx:196, in pandas._libs.index.IndexEngine.get_loc()

File pandas /_libs/hashtable_class_helper.pxi:2606] in pandas._libs.hashtable.Int64HashTable.get_item()

File pandas/_libs/hashtable_class_helper.pxi:2630, in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 0

It traces back to this line

File /opt/conda/lib/python3.11/site-packages/plotnine/mapping/aes.py:647 in has_groups(data)
    632 """
    633 Check if data is grouped
    634 
   (...)
    643     If True, the data has groups.
    644 """
    645 # If any row in the group column is equal to NO_GROUP, then
    646 # the data all of them are and the data has no groups
--> 647 return data.loc[0, "group"] != NO_GROUP

Unfortunately I don't have time to look at the code more closely, but clearly this hard indexing via data.loc[0, "group"] will fail whenever data doesn't have 0 in its index.

Current workaround

Just reset the index via df_fails = df_fails.reset_index(drop=True) as pointed out by @idavi-bcs in #221 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions