question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"TypeError: data type not understood" with dtype: period[M]

See original GitHub issue

Thanks for a great project! With these versions:

  • altair: 4.0.1
  • pandas: 0.25.3
  • numpy: 1.18.1
  • Python: 3.8.1

I get the following error when trying to pass a dataframe with a period to altair: TypeError: data type not understood

The error disappears when the ‘Sale month’ column is not passed to altair.

sale['Sale month'] = pd.to_datetime(sale['Sale month'], format='%b-%y').dt.to_period('M')

sale['Sale month']
0        2011-07
1        2011-08
2        2011-09
3        2011-10
4        2011-11
          ...   
15845    2013-11
15846    2013-12
15847    2014-01
15848    2014-02
15849    2014-03
Name: Sale month, Length: 1173, dtype: period[M]

alt.Chart(sale[['Value', 'Sale month']].head()).mark_bar().encode(
)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/Documents/…/virtualenv/lib/python3.8/site-packages/altair/vegalite/v4/api.py in to_dict(self, *args, **kwargs)
    353         copy = self.copy(deep=False)
    354         original_data = getattr(copy, 'data', Undefined)
--> 355         copy.data = _prepare_data(original_data, context)
    356 
    357         if original_data is not Undefined:

~/Documents/…/virtualenv/lib/python3.8/site-packages/altair/vegalite/v4/api.py in _prepare_data(data, context)
     82     # convert dataframes  or objects with __geo_interface__ to dict
     83     if isinstance(data, pd.DataFrame) or hasattr(data, '__geo_interface__'):
---> 84         data = pipe(data, data_transformers.get())
     85 
     86     # convert string input to a URLData

~/Documents/…/virtualenv/lib/python3.8/site-packages/toolz/functoolz.py in pipe(data, *funcs)
    632     """
    633     for func in funcs:
--> 634         data = func(data)
    635     return data
    636 

~/Documents/…/virtualenv/lib/python3.8/site-packages/toolz/functoolz.py in __call__(self, *args, **kwargs)
    301     def __call__(self, *args, **kwargs):
    302         try:
--> 303             return self._partial(*args, **kwargs)
    304         except TypeError as exc:
    305             if self._should_curry(args, kwargs, exc):

~/Documents/…/virtualenv/lib/python3.8/site-packages/altair/vegalite/data.py in default_data_transformer(data, max_rows)
     11 @curry
     12 def default_data_transformer(data, max_rows=5000):
---> 13     return pipe(data, limit_rows(max_rows=max_rows), to_values)
     14 
     15 

~/Documents/…/virtualenv/lib/python3.8/site-packages/toolz/functoolz.py in pipe(data, *funcs)
    632     """
    633     for func in funcs:
--> 634         data = func(data)
    635     return data
    636 

~/Documents/…/virtualenv/lib/python3.8/site-packages/toolz/functoolz.py in __call__(self, *args, **kwargs)
    301     def __call__(self, *args, **kwargs):
    302         try:
--> 303             return self._partial(*args, **kwargs)
    304         except TypeError as exc:
    305             if self._should_curry(args, kwargs, exc):

~/Documents/…/virtualenv/lib/python3.8/site-packages/altair/utils/data.py in to_values(data)
    138         return {'values': data}
    139     elif isinstance(data, pd.DataFrame):
--> 140         data = sanitize_dataframe(data)
    141         return {'values': data.to_dict(orient='records')}
    142     elif isinstance(data, dict):

~/Documents/…/virtualenv/lib/python3.8/site-packages/altair/utils/core.py in sanitize_dataframe(df)
    221             # otherwise it will give an error on np.issubdtype(dtype, np.integer)
    222             continue
--> 223         elif np.issubdtype(dtype, np.integer):
    224             # convert integers to objects; np.int is not JSON serializable
    225             df[col_name] = df[col_name].astype(object)

~/Documents/…/virtualenv/lib/python3.8/site-packages/numpy/core/numerictypes.py in issubdtype(arg1, arg2)
    391     """
    392     if not issubclass_(arg1, generic):
--> 393         arg1 = dtype(arg1).type
    394     if not issubclass_(arg2, generic):
    395         arg2_orig = arg2

TypeError: data type not understood

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
jakevdpcommented, Jun 14, 2020

It will be resolved when someone fixes it. Are you interested?

0reactions
jakevdpcommented, Jun 15, 2020

We don’t have any mechanism at the moment for data sanitization to affect the encoding specification, and I think we should probably avoid making those kinds of assumptions for the user. Silent changes of defaultsthat are convenient in some situations often lead to a lot of confusion in others.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix TypeError: data type not understood with a datetime ...
It's working for the sample you shared, not sure where the issue is, are there any missing values in your month column?
Read more >
Keep getting "data type 'category' not understood"? - Reddit
Hi, So I'm trying to convert my categorical data into numbers and use it for a correlation matrix. However, when I run this:...
Read more >
TypeError: data type not understood" comparing dtype np ...
Coding example for the question "TypeError: data type not understood" comparing dtype np.datetime64-Pandas,Python.
Read more >
Why We Need to Use Pandas New String Dtype Instead of ...
Before pandas 1.0, only “object” datatype was used to store strings which cause some drawbacks because non-string data can also be stored ...
Read more >
Categorical Data — pandas 0.20.1 documentation
Categoricals are a pandas data type, which correspond to categorical variables in ... TypeError: data type "category" not understood In [217]: dtype =...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found