Skip to content

Conversation

xw393
Copy link

@xw393 xw393 commented Nov 20, 2018

There is an error when secondary_y and y are both lists.

sample code can be:

df = pd.DataFrame(np.random.randn(100, 4), columns=['var1', 'var2', 'var3', 'var4'])
df['x'] = list(range(df.shape[0]))
df.iplot(x='x', y=['var1', 'var2'], secondary_y=['var3'])

error:
TypeError: unhashable type: 'list'

Add more conditions to make it more complete. Now, y and secondary_y can be either list and str.

@xw393
Copy link
Author

xw393 commented Nov 20, 2018

The main change starts at line 819 in plotlytools.py

@xw393 xw393 mentioned this pull request Jan 3, 2019
@pjandrews
Copy link

@xw393 , would you like me to test your fix?

@xw393
Copy link
Author

xw393 commented Jan 11, 2019 via email

@pjandrews
Copy link

Hi @xw393 , I'm seeing this error after changing plotlytools.py using the example that you gave above.

I'm using: plotly 3.4.2 and cufflinks 0.14.5.


TypeError Traceback (most recent call last)
in ()
1 df = pd.DataFrame(np.random.randn(100, 4), columns=['var1', 'var2', 'var3', 'var4'])
2 df['x'] = list(range(df.shape[0]))
----> 3 df.iplot(x='x', y=['var1', 'var2'], secondary_y=['var3'])

C:\ProgramData\Anaconda3\lib\site-packages\cufflinks\plotlytools.py in _iplot(self, kind, data, layout, filename, sharing, title, xTitle, yTitle, zTitle, theme, colors, colorscale, fill, width, dash, mode, interpolation, symbol, size, barmode, sortbars, bargap, bargroupgap, bins, histnorm, histfunc, orientation, boxpoints, annotations, keys, bestfit, bestfit_colors, mean, mean_colors, categories, x, y, z, text, gridcolor, zerolinecolor, margin, labels, values, secondary_y, secondary_y_title, subplots, shape, error_x, error_y, error_type, locations, lon, lat, asFrame, asDates, asFigure, asImage, dimensions, asPlot, asUrl, online, **kwargs)
817 cols = [y] + [secondary_y]
818 elif isinstance(y, str) and isinstance(secondary_y, list):
--> 819 cols = [y] + secondary_y
820 elif isinstance(y, list) and isinstance(secondary_y, str):
821 cols = y + [secondary_y]

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in getitem(self, key)
2680 if isinstance(key, (Series, np.ndarray, Index, list)):
2681 # either boolean or fancy integer index
-> 2682 return self._getitem_array(key)
2683 elif isinstance(key, DataFrame):
2684 return self._getitem_frame(key)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_array(self, key)
2724 return self._take(indexer, axis=0)
2725 else:
-> 2726 indexer = self.loc._convert_to_indexer(key, axis=1)
2727 return self._take(indexer, axis=1)
2728

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py in _convert_to_indexer(self, obj, axis, is_setter)
1312 # unique index
1313 if labels.is_unique:
-> 1314 indexer = check = labels.get_indexer(objarr)
1315
1316 # non-unique (dups)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_indexer(self, target, method, limit, tolerance)
3257 'backfill or nearest reindexing')
3258
-> 3259 indexer = self._engine.get_indexer(target._ndarray_values)
3260
3261 return _ensure_platform_int(indexer)

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_indexer()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.lookup()

TypeError: unhashable type: 'list'

@xw393
Copy link
Author

xw393 commented Jan 12, 2019

Hi @pjandrews, thanks for testing. Based on your error log, it looks like the error is due to line 818- 819

818 elif isinstance(y, str) and isinstance(secondary_y, list):
--> 819 cols = [y] + secondary_y

However, in my test code example df.iplot(x='x', y=['var1', 'var2'], secondary_y=['var3']), y is a list rather than str, the code should not hit line 819.

I created a test env with

plotly==3.4.2
cufflinks==0.14.5
Python==3.5.6

same as yours, there is no error. BTW, I'm using OS X.

Here is a screenshot of my test in the Jupyter Notebook:
image

Can you please check your codes and test again? Thanks!

@pjandrews
Copy link

Thanks @xw393 , my mistake, I imported the libraries again but didn't restart the kernel which is why I was seeing the strange traceback error. It's also working for me now. Thank you so much for the fix!

@pjandrews
Copy link

pjandrews commented Jan 14, 2019

HI @xw393 , it looks like there is still a problem. If I try to create separate figures so that I can include two different types of charts all the columns are included (even the ones that are not specified) in the first figure. Here is an example:

df = pd.DataFrame([['group1', 'cats',3,4,500,600], ['group2', 'dogs',4,5,600,700], ['group3', 'skunks',6,9,1000,1100]], columns=['A', 'B','C','D','E','F'])

fig1 = df.iplot(kind='scatter', mode='lines+markers', x=['A', 'B'],y=['C', 'D',],asFigure=True).to_plotly_json()
fig2 = df.iplot(kind='bar', x=['A', 'B'],secondary_y=['E','F'],asFigure=True).to_plotly_json()
#fig2 = df.iplot(kind='bar', x=['A', 'B'],secondary_y=['E','F'],asFigure=True).to_plotly_json()
fig2['data'].extend(fig1['data'])
plot(fig2, filename=r'c:\temp\myfile.html',auto_open=True)

@xw393
Copy link
Author

xw393 commented Jan 14, 2019

@pjandrews Can you change your code to:

import pandas as pd
import cufflinks as cf

df = pd.DataFrame([['group1', 'cats',3,4,500,600], ['group2', 'dogs',4,5,600,700], ['group3', 'skunks',6,9,1000,1100]], columns=['A', 'B','C','D','E','F'])

fig1 = df.iplot(kind='scatter', mode='lines+markers', x=['A', 'B'],y=['C', 'D',],asFigure=True).to_plotly_json()
fig2 = df.iplot(kind='bar', x=['A', 'B'],secondary_y=['E','F'],asFigure=True).to_plotly_json()
fig2['data'].extend(fig1['data'])

# use cf.iplot to generate the figure.
cf.iplot(fig2)

@pjandrews
Copy link

Hi @xw393 I tried with the code that you suggested but am still seeing the same issue. Columns C and D are plotted as Bar and Line plots. Columns C and D should only be represented as line plots.

image

@pjandrews
Copy link

@xw393 @timkpaine Just a friendly reminder in case you have a chance to look at this one. Thanks!

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

Successfully merging this pull request may close these issues.

2 participants