vincent icon indicating copy to clipboard operation
vincent copied to clipboard

DataFrame from pandas.concat w/ keys argument leads to vincent error

Open flamby opened this issue 10 years ago • 0 comments

Hello,

I'm using pandas.concat for merging several dataframes per indexes. The concat df works great, except if I'm giving it a keys argument for prefixing my legends.

This is the error :

/Users/[..]/lib/python2.7/site-packages/vincent/charts.pyc in data_type(data, grouped, columns, key_on, iter_idx)
     33         if isinstance(data, (pd.Series, pd.DataFrame)):
     34             return Data.from_pandas(data, grouped=grouped, columns=columns,
---> 35                                     key_on=key_on)
     36     if isinstance(data, (list, tuple, dict)):
     37             return Data.from_iter(data)

/Users/[..]/lib/python2.7/site-packages/vincent/data.pyc in from_pandas(cls, data, columns, key_on, name, series_key, grouped, records, **kwargs)
    224                     value = {}
    225                     value['idx'] = cls.serialize(i)
--> 226                     value['col'] = cls.serialize(k)
    227                     value['val'] = cls.serialize(v)
    228                     if grouped:

/Users/[..]/lib/python2.7/site-packages/vincent/data.pyc in serialize(obj)
    149         else:
    150             raise LoadError('cannot serialize index of type '
--> 151                             + type(obj).__name__)
    152 
    153     @classmethod

LoadError: cannot serialize index of type tuple

For the sake of simplicity, here is a iPython notebook sample to reproduce the issue (not w/ my real data, just to get the idea)

import pandas
df1=pandas.DataFrame({"Apple":[1,2,3],"Orange":[9,8,7]},index=['2014/05','2014/06','2014/07'])
df2=pandas.DataFrame({"Shiitake":[4,5,6],"Pleurotus eryngii":[9,10,11]},index=['2014/05','2014/06','2014/07'])
df3=pandas.DataFrame({"Celery":[7,8,9],"Carrot":[12,13,14]},index=['2014/05','2014/06','2014/07'])
mydict={"Fruits":df1,"Mushrooms":df2,"Vegetables":df3}
merged_df = pandas.concat(mydict.values(), axis=1, 
                          keys=mydict.keys()
                          )
import vincent
vincent.core.initialize_notebook()
graph = vincent.GroupedBar(merged_df)
graph.legend(title='Category')

Of course, when I comment the keys=mydict.keys() line, vincent's chart works like a charm.

merged_df.head() w/ keys enabled is ok of course :

    Mushrooms   Vegetables  Fruits
Pleurotus eryngii   Shiitake    Carrot  Celery  Apple   Orange
2014/05 9   4   12  7   1   9
2014/06 10  5   13  8   2   8
2014/07 11  6   14  9   3   7

Thanks.

flamby avatar Jul 13 '14 14:07 flamby