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

Altair unable to chart pd.Series and integer column names #1189

Closed
HarvsG opened this issue Nov 6, 2018 · 1 comment
Closed

Altair unable to chart pd.Series and integer column names #1189

HarvsG opened this issue Nov 6, 2018 · 1 comment

Comments

@HarvsG
Copy link

HarvsG commented Nov 6, 2018

#matplot/pandas code
%matplotlib inline
star_wars.iloc[:,9:15].mean().plot.bar()

#equivalent altair code
means = star_wars.iloc[:,9:15].mean().to_frame().reset_index() 
#this line is needed as altair needs an index and cannot take pd.Series

means = sums.rename(columns={0:'mean_ranking'}) 
# since df.reset_index() given unamed columns int headers, which altair cant handle, the column must be renamed

alt.Chart(means).mark_bar().encode(
    x = 'index:N',
    y = 'mean_ranking' #not '0:Q'
)

If one does not rename the column the following error occurs

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/altair/vegalite/v2/api.py in to_dict(self, *args, **kwargs)
    396         copy = self.copy()
    397         original_data = getattr(copy, 'data', Undefined)
--> 398         copy.data = _prepare_data(original_data, context)
    399 
    400         if original_data is not Undefined:

~/anaconda3/lib/python3.6/site-packages/altair/vegalite/v2/api.py in _prepare_data(data, context)
     90     # consolidate inline data to top-level datasets
     91     if data_transformers.consolidate_datasets:
---> 92         data = _consolidate_data(data, context)
     93 
     94     # if data is still not a recognized type, then return

~/anaconda3/lib/python3.6/site-packages/altair/vegalite/v2/api.py in _consolidate_data(data, context)
     57 
     58     if values is not Undefined:
---> 59         name = _dataset_name(values)
     60         data = core.NamedData(name=name, **kwds)
     61         context.setdefault('datasets', {})[name] = values

~/anaconda3/lib/python3.6/site-packages/altair/vegalite/v2/api.py in _dataset_name(values)
     33     if isinstance(values, core.InlineDataset):
     34         values = values.to_dict()
---> 35     values_json = json.dumps(values, sort_keys=True)
     36     hsh = hashlib.md5(values_json.encode()).hexdigest()
     37     return 'data-' + hsh

~/anaconda3/lib/python3.6/json/__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
    236         check_circular=check_circular, allow_nan=allow_nan, indent=indent,
    237         separators=separators, default=default, sort_keys=sort_keys,
--> 238         **kw).encode(obj)
    239 
    240 

~/anaconda3/lib/python3.6/json/encoder.py in encode(self, o)
    197         # exceptions aren't as detailed.  The list call should be roughly
    198         # equivalent to the PySequence_Fast that ''.join() would do.
--> 199         chunks = self.iterencode(o, _one_shot=True)
    200         if not isinstance(chunks, (list, tuple)):
    201             chunks = list(chunks)

~/anaconda3/lib/python3.6/json/encoder.py in iterencode(self, o, _one_shot)
    255                 self.key_separator, self.item_separator, self.sort_keys,
    256                 self.skipkeys, _one_shot)
--> 257         return _iterencode(o, 0)
    258 
    259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,

TypeError: '<' not supported between instances of 'int' and 'str'

it seems this might be the wide-form, long-form problem rearing it's head?

@jakevdp
Copy link
Collaborator

jakevdp commented Nov 6, 2018

This has been addressed in master in #1107

@jakevdp jakevdp closed this as completed Nov 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants