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.
This is related with https://github.com/matplotlib/matplotlib/issues/9577 and https://github.com/pydata/xarray/issues/1661
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.
cc @TomAugspurger
Yep, that's the fix. Hopefully this will eventually all be in matplotlib, but for now explicitly registering the converters is required.
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 ^_^
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.
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
Most helpful comment
Hey, coming across this issue in 1.0.0 Since tseries no longer has a
converter
I tried to usefrom 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.