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

TypeError: invalid type promotion #18212

Closed
kdebrab opened this issue Nov 10, 2017 · 7 comments
Closed

TypeError: invalid type promotion #18212

kdebrab opened this issue Nov 10, 2017 · 7 comments
Labels
Milestone

Comments

@kdebrab
Copy link
Contributor

kdebrab commented Nov 10, 2017

I'm not sure where to post this, as it involves pandas, seaborn, matplotlib and numpy.

As it works with pandas 0.20.3, but throws an error in pandas 0.21.0, it seems to be a pandas issue. Below, an easily reproducible example:

import pandas as pd
import seaborn as sns

index = pd.date_range('2017-06-01', periods=16)
df = pd.DataFrame(index=index, columns=['a', 'b', 'c'])
df.iloc[:2, 0] = '1000'
df.iloc[[2, 3, 6, 8, 10, 12, 14, 15], 1] = '1000'
df.iloc[[4, 5, 9, 13], 2] = '1000'
df.iloc[[7, 11], 1] = '1001'
df.index.name = 'Datetime'
df.columns.name = 'Element'
df = df.stack().rename('Event').reset_index()
df['Event / Element'] = df['Event'] + ' / ' + df['Element']
sns.stripplot(x='Datetime', y='Event / Element', hue='Element', data=df, orient='h')

Throws following error in pandas 0.21.0:

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-a21ce0484562>", line 14, in <module>
    sns.stripplot(x='Datetime', y='Event / Element', hue='Element', data=df, orient='h')
  File "C:\Python27\lib\site-packages\seaborn\categorical.py", line 2603, in stripplot
    plotter.plot(ax, kwargs)
  File "C:\Python27\lib\site-packages\seaborn\categorical.py", line 1191, in plot
    self.draw_stripplot(ax, kws)
  File "C:\Python27\lib\site-packages\seaborn\categorical.py", line 1171, in draw_stripplot
    ax.scatter(strip_data, cat_pos, **kws)
  File "C:\Python27\lib\site-packages\matplotlib\__init__.py", line 1710, in inner
    return func(ax, *args, **kwargs)
  File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 4087, in scatter
    offsets = np.column_stack([x, y])
  File "C:\Python27\lib\site-packages\numpy\lib\shape_base.py", line 353, in column_stack
    return _nx.concatenate(arrays, 1)
TypeError: invalid type promotion

numpy==1.13.3
seaborn==0.8.1
matplotlib==2.1.0

The above code does work fine in pandas 0.20.3, with same versions of numpy, seaborn and matplotlib.

@kdebrab
Copy link
Contributor Author

kdebrab commented Nov 10, 2017

This is related with matplotlib/matplotlib#9577 and pydata/xarray#1661

@kdebrab
Copy link
Contributor Author

kdebrab commented Nov 10, 2017

https://github.com/pydata/xarray/pull/1669/files provides a hint to the solution/workaround.

First execute:

from pandas.tseries import converter
converter.register()

and then execute above example and everything works fine in pandas==0.21.0.

@gfyoung
Copy link
Member

gfyoung commented Nov 10, 2017

cc @TomAugspurger

@TomAugspurger
Copy link
Contributor

Yep, that's the fix. Hopefully this will eventually all be in matplotlib, but for now explicitly registering the converters is required.

@tstw414
Copy link

tstw414 commented Nov 25, 2017

kdebrab i've been struggling with this for 3 days, too bad i didn't find it sooner. Thanks so much for the answers! Much appreciated 10/10 ^_^

@jorisvandenbossche jorisvandenbossche added this to the 0.21.1 milestone Feb 19, 2018
@lordgrenville
Copy link
Contributor

Hey, coming across this issue in 1.0.0 Since tseries no longer has a converter I tried to use

from pandas.plotting._matplotlib import converter
converter.register()

But I'm still getting invalid type promotion. Using a dataframe with a DateTimeIndex and 2 columns of floats.

@IanFLee
Copy link

IanFLee commented Nov 30, 2020

But I'm still getting invalid type promotion.

Same here, but with sklearn.preprocessing's StandardScaler(). Everything I can tell seems to point to the datetime, I think i saw @TomAugspurger on another seemingly-related issue, too. I have just dropped the datetime column for the time being but will need it back later so...

Related to the converter issue, I have seen someone on another issue relate the converter registration to schrodinger's cat, in that it must be either registered or deregistered, so they suggested manually adding either converter.register() or converter.deregister() or something like that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants