1

I am currently using cufflinks for the first time and there is an error that I believe is impossible to fix (without reverting pandas to an earlier version, I suppose). I just want to make sure that I am not missing something obvious.

When I try to create a spread graph, I get the error:

AttributeError: module 'pandas' has no attribute 'np'

And it indicates that the issue is located within the cufflinks source code, specifically at pd.np:

if kind=='spread':
    trace=self.apply(lambda x:x[0]-x[1],axis=1)
    positive=trace.apply(lambda x:x if x>=0 else pd.np.nan)
    negative=trace.apply(lambda x:x if x<0 else pd.np.nan)
    trace=pd.DataFrame({'positive':positive,'negative':negative})

Apparently that used to be valid pandas syntax, but no longer.

So does this mean I simply cannot use cufflinks for spread graphs? Everything else works fine.

I've tried updating each package individually as well as conda update --all. The code causing the error is as basic as it gets and comes straight from the source docs:

# Plot pre-generated DataFrame
import cufflinks as cf
cf.datagen.lines(2).iplot(kind='spread')

Expected output is a spread graph. cf.datagen.lines(2) was tested independently and works as expected, and iplot works for all other graph types.

ferreiradev
  • 327
  • 5
  • 16

1 Answers1

0

I found this same issue while doing a certain Udemy course on Data Science.

Short answer:

Yes, the release available through pip install (at pypi) is broken. Thought, it can be fixed and it was already fixed in the cufflinks master branch, but it has not been released in +3 years.

Long answer / Fix:

Step 1. Go to the install location of cufflinks and find the following file. It could be in either. It will be in the first if you are using a virtual environment.

  • .venv\Lib\site-packages\cufflinks\plotlytools.py
  • (If not using venv) C:\users\{username}\appdata\roaming\python\Python311\site-packages

Step 2. At the top of said file, add import numpy as np.

Step 3. Replace in L850, and L851 (Highlighted in the image below) pd.np.nan with np.nan

enter image description here

Step 4. Whenever you install a new venv, redo these steps.

If you did things right, it should be working. Note that this only works for Jupyter notebooks.


This was tested with:

  • pandas 2.0.3,
  • numpy 1.25.0,
  • cufflinks 0.17.3,
  • chart-studio 1.1.0
  • python 3.11

Demo

    import pandas as pd
    import numpy as np
    %matplotlib inline
    import chart_studio.plotly as py
    import plotly.figure_factory as ff
    import plotly.graph_objects as go
    
    from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
    import cufflinks as cf
    
    # For Notebooks
    init_notebook_mode(connected=True)
    
    # For offline use
    cf.go_offline()
    df = pd.DataFrame(np.random.randn(100,4),columns='A B C D'.split())
    df[['A','B']].iplot(kind='spread')

enter image description here

Bonus

You can have a similar workflow using only plotly.

    # https://plotly.com/python/pandas-backend/
    
    import pandas as pd
    
    df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv')
    # using Plotly Express via the Pandas backend
    pd.options.plotting.backend = "plotly"
    fig1 = df.plot.bar(x='country', y='gdpPercap')
    fig1.show()
ferreiradev
  • 327
  • 5
  • 16