Seaborn: sns.kdeplot() || TypeError: slice indices must be integers or None or have an __index__ method

Created on 25 Jan 2017  路  8Comments  路  Source: mwaskom/seaborn

I can't get kdeplot or distplot to work with my pd.Series (or np.array)

u = np.array([ 3.41959 , 1.79315 , 1.17229 , 1.59909 , 1.27337 , 1.21917 , 2.60591 , 2.0571 , 1.83865 , 1.94869 , 1.65421 , 1.67777 , 1.23781 , 1.46352 , 1.41791 , 2.00387 , 2.51076 , 1.59734 , 1.32982 , 1.89372 , 1.40614 , 1.41747 , 1.34109 , 1.38647 , 1.49432 , 1.05335 , 1.03805 , 1.18631 , 1.06402 , 1.06929 , 1.12031 , 1.07605 , 1.25702 , 1.25158 , 1.06541 , 1.07368 , 1.00559 , 1.00555 , 1.54561 , 1.01144 , 1.79598 , 1.02094 , 1.36236 , 1.6214 , 1.09803 , 1.25866 , 0.987888, 0.993576, 1.25805 , 5.67273 , 1.20665 , 0.985643, 1.07809 , 1.22087 , 1.1201 , 1.0367 , 1.26575 , 1.0295 , 0.993422, 1.08648 , 1.32767 , 1.35376 , 0.98585 , 0.991962, 1.49533 , 1.04997 , 0.995653, 1.13044 , 1.12311 , 1.29017 , 1.6424 , 1.1599 , 1.26453 , 1.0633 , 1.00454 , 1.35529 , 0.990889, 1.19621 , 1.30717 , 1.32321 , 1.5471 , 1.13225 , 1.97847 , 1.17142 , 1.36377 , 2.14062 , 0.996708, 1.03417 , 1.03212 , 1.0082 , 1.06432 , 3.49213 , 4.28245 , 1.00274 , 1.09338 , 1.0156 , 1.13566 , 1.10697 , 1.56438 , 0.96706 ], dtype=float)
Se_u = pd.Series(u)
sns.kdeplot(Se_u)

This is the error I get:

TypeError                                 Traceback (most recent call last)
<ipython-input-27-70aaa51b2d69> in <module>()
      4 u = np.array([ 3.41959 , 1.79315 , 1.17229 , 1.59909 , 1.27337 , 1.21917 , 2.60591 , 2.0571 , 1.83865 , 1.94869 , 1.65421 , 1.67777 , 1.23781 , 1.46352 , 1.41791 , 2.00387 , 2.51076 , 1.59734 , 1.32982 , 1.89372 , 1.40614 , 1.41747 , 1.34109 , 1.38647 , 1.49432 , 1.05335 , 1.03805 , 1.18631 , 1.06402 , 1.06929 , 1.12031 , 1.07605 , 1.25702 , 1.25158 , 1.06541 , 1.07368 , 1.00559 , 1.00555 , 1.54561 , 1.01144 , 1.79598 , 1.02094 , 1.36236 , 1.6214 , 1.09803 , 1.25866 , 0.987888, 0.993576, 1.25805 , 5.67273 , 1.20665 , 0.985643, 1.07809 , 1.22087 , 1.1201 , 1.0367 , 1.26575 , 1.0295 , 0.993422, 1.08648 , 1.32767 , 1.35376 , 0.98585 , 0.991962, 1.49533 , 1.04997 , 0.995653, 1.13044 , 1.12311 , 1.29017 , 1.6424 , 1.1599 , 1.26453 , 1.0633 , 1.00454 , 1.35529 , 0.990889, 1.19621 , 1.30717 , 1.32321 , 1.5471 , 1.13225 , 1.97847 , 1.17142 , 1.36377 , 2.14062 , 0.996708, 1.03417 , 1.03212 , 1.0082 , 1.06432 , 3.49213 , 4.28245 , 1.00274 , 1.09338 , 1.0156 , 1.13566 , 1.10697 , 1.56438 , 0.96706 ], dtype=float)
      5 Se_u = pd.Series(u)
----> 6 sns.kdeplot(Se_u)

/Users/jespinoz/anaconda/lib/python3.5/site-packages/seaborn/distributions.py in kdeplot(data, data2, shade, vertical, kernel, bw, gridsize, cut, clip, legend, cumulative, shade_lowest, ax, **kwargs)
    602         ax = _univariate_kdeplot(data, shade, vertical, kernel, bw,
    603                                  gridsize, cut, clip, legend, ax,
--> 604                                  cumulative=cumulative, **kwargs)
    605 
    606     return ax

/Users/jespinoz/anaconda/lib/python3.5/site-packages/seaborn/distributions.py in _univariate_kdeplot(data, shade, vertical, kernel, bw, gridsize, cut, clip, legend, ax, cumulative, **kwargs)
    268         x, y = _statsmodels_univariate_kde(data, kernel, bw,
    269                                            gridsize, cut, clip,
--> 270                                            cumulative=cumulative)
    271     else:
    272         # Fall back to scipy if missing statsmodels

/Users/jespinoz/anaconda/lib/python3.5/site-packages/seaborn/distributions.py in _statsmodels_univariate_kde(data, kernel, bw, gridsize, cut, clip, cumulative)
    326     fft = kernel == "gau"
    327     kde = smnp.KDEUnivariate(data)
--> 328     kde.fit(kernel, bw, fft, gridsize=gridsize, cut=cut, clip=clip)
    329     if cumulative:
    330         grid, y = kde.support, kde.cdf

/Users/jespinoz/anaconda/lib/python3.5/site-packages/statsmodels/nonparametric/kde.py in fit(self, kernel, bw, fft, weights, gridsize, adjust, cut, clip)
    144             density, grid, bw = kdensityfft(endog, kernel=kernel, bw=bw,
    145                     adjust=adjust, weights=weights, gridsize=gridsize,
--> 146                     clip=clip, cut=cut)
    147         else:
    148             density, grid, bw = kdensity(endog, kernel=kernel, bw=bw,

/Users/jespinoz/anaconda/lib/python3.5/site-packages/statsmodels/nonparametric/kde.py in kdensityfft(X, kernel, bw, weights, gridsize, adjust, clip, cut, retgrid)
    504     zstar = silverman_transform(bw, gridsize, RANGE)*y # 3.49 in Silverman
    505                                                    # 3.50 w Gaussian kernel
--> 506     f = revrt(zstar)
    507     if retgrid:
    508         return f, grid, bw

/Users/jespinoz/anaconda/lib/python3.5/site-packages/statsmodels/nonparametric/kdetools.py in revrt(X, m)
     18     if m is None:
     19         m = len(X)
---> 20     y = X[:m/2+1] + np.r_[0,X[m/2+1:],0]*1j
     21     return np.fft.irfft(y)*m
     22 

TypeError: slice indices must be integers or None or have an __index__ method

but this works:

Se_u.plot(kind="kde")

image

Most helpful comment

Sorry about that @mwaskom I searched around but I didn't see it.
conda uninstall statsmodels --yes
conda install -c taugspurger statsmodels=0.8.0
This fixed everything.

All 8 comments

Duplicate of #1092

Sorry about that @mwaskom I searched around but I didn't see it.
conda uninstall statsmodels --yes
conda install -c taugspurger statsmodels=0.8.0
This fixed everything.

Nice; that's a helpful recipe that it might be good to add to the other issue in case others end up there.

This doesn't help:
conda uninstall statsmodels --yes
conda install -c taugspurger statsmodels=0.8.0
There must be some other solution...

statsmodels 0.8.0 is on conda-forge. Install instructions are here, but conda install -c conda-forge statsmodels

Upgrading to statsmodels 0.8.0 solved my problem.

Thanks @TomAugspurger , your solution solved my problem as well.

@mwaskom wouldn't it make sense to have seaborn depend statsmodels>=0.8.0 to avoid this?
In addition to the issue referenced above I just had another one (https://github.com/wdecoster/NanoPlot/issues/23) with the same problem reported on a tool I wrote.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amelio-vazquez-reina picture amelio-vazquez-reina  路  3Comments

chanshing picture chanshing  路  3Comments

vinay-jayaram picture vinay-jayaram  路  3Comments

Bercio picture Bercio  路  3Comments

JanHomann picture JanHomann  路  3Comments