# scanpy.pp.highly\_variable\_genes and “raise KeyError”

**URL:** https://discourse.scverse.org/t/scanpy-pp-highly-variable-genes-and-raise-keyerror/578
**Category:** scanpy
**Created:** [June 28, 2022, 4:20pm UTC](https://discourse.scverse.org/t/scanpy-pp-highly-variable-genes-and-raise-keyerror/578 "2022-06-28T16:20:28Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![grimwoo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/grimwoo/32/290_2.png) [@grimwoo](https://discourse.scverse.org/u/grimwoo)
#### Post date: [June 28, 2022, 4:20pm UTC](https://discourse.scverse.org/t/scanpy-pp-highly-variable-genes-and-raise-keyerror/578/1 "2022-06-28T16:20:28Z")

</div>

Hi, I am using the data that was transformed from Seurat to Scanpy following the [official guidence](https://satijalab.org/seurat/archive/v3.0/conversion_vignette.html). Everything works fine. For example, I could plot a PAGA layout in Scanpy. However, one thing that I cannot is to run “scanpy.pp.highly\_variable\_genes”.

It resulted in error no matter in python 3.6 or python 3.8. Could anyone help me with this issue? I think that may be a problem of my data. Because the example data runs correctly without any error information .

in python 3.6：

**\>\>\> sc.pp.highly\_variable\_genes(adata)**  
/mnt/c/Users/grimw/CELLECT/enter/envs/stagewiseNN\_old/lib/python3.6/site-packages/scanpy/preprocessing/\_highly\_variable\_genes.py:208: RuntimeWarning: invalid value encountered in log  
dispersion = np.log(dispersion)  
Traceback (most recent call last):  
File “”, line 1, in   
File “/mnt/c/Users/grimw/CELLECT/enter/envs/stagewiseNN\_old/lib/python3.6/site-packages/scanpy/preprocessing/\_highly\_variable\_genes.py”, line 440, in highly\_variable\_genes  
flavor=flavor,  
File “/mnt/c/Users/grimw/CELLECT/enter/envs/stagewiseNN\_old/lib/python3.6/site-packages/scanpy/preprocessing/\_highly\_variable\_genes.py”, line 223, in \_highly\_variable\_genes\_single\_batch  
gen\_indices = np.where(one\_gene\_per\_bin[df[‘mean\_bin’].values])[0].tolist()  
File “/mnt/c/Users/grimw/CELLECT/enter/envs/stagewiseNN\_old/lib/python3.6/site-packages/pandas/core/series.py”, line 906, in **getitem**  
return self.\_get\_with(key)  
File “/mnt/c/Users/grimw/CELLECT/enter/envs/stagewiseNN\_old/lib/python3.6/site-packages/pandas/core/series.py”, line 946, in \_get\_with  
return self.loc[key]  
File “/mnt/c/Users/grimw/CELLECT/enter/envs/stagewiseNN\_old/lib/python3.6/site-packages/pandas/core/indexing.py”, line 879, in **getitem**  
return self.\_getitem\_axis(maybe\_callable, axis=axis)  
File “/mnt/c/Users/grimw/CELLECT/enter/envs/stagewiseNN\_old/lib/python3.6/site-packages/pandas/core/indexing.py”, line 1099, in \_getitem\_axis  
return self.\_getitem\_iterable(key, axis=axis)  
File “/mnt/c/Users/grimw/CELLECT/enter/envs/stagewiseNN\_old/lib/python3.6/site-packages/pandas/core/indexing.py”, line 1037, in \_getitem\_iterable  
keyarr, indexer = self.\_get\_listlike\_indexer(key, axis, raise\_missing=False)  
File “/mnt/c/Users/grimw/CELLECT/enter/envs/stagewiseNN\_old/lib/python3.6/site-packages/pandas/core/indexing.py”, line 1240, in \_get\_listlike\_indexer  
indexer, keyarr = ax.\_convert\_listlike\_indexer(key)  
File “/mnt/c/Users/grimw/CELLECT/enter/envs/stagewiseNN\_old/lib/python3.6/site-packages/pandas/core/indexes/base.py”, line 3216, in \_convert\_listlike\_indexer  
indexer = self.\_convert\_list\_indexer(keyarr)  
File “/mnt/c/Users/grimw/CELLECT/enter/envs/stagewiseNN\_old/lib/python3.6/site-packages/pandas/core/indexes/category.py”, line 598, in \_convert\_list\_indexer  
indexer = self.categories.\_convert\_list\_indexer(keyarr)  
File “/mnt/c/Users/grimw/CELLECT/enter/envs/stagewiseNN\_old/lib/python3.6/site-packages/pandas/core/indexes/interval.py”, line 533, in \_convert\_list\_indexer  
raise KeyError  
KeyError

in python 3.8：

**\>\>\> sc.pp.highly\_variable\_genes(adata)**  
/mnt/c/Users/grimw/CELLECT/enter/envs/stagewiseNN/lib/python3.8/site-packages/scanpy/preprocessing/\_highly\_variable\_genes.py:200: RuntimeWarning: invalid value encountered in expm1  
X = np.expm1(X)  
Traceback (most recent call last):  
File “”, line 1, in   
File “/mnt/c/Users/grimw/CELLECT/enter/envs/stagewiseNN/lib/python3.8/site-packages/scanpy/preprocessing/\_highly\_variable\_genes.py”, line 434, in highly\_variable\_genes  
df = \_highly\_variable\_genes\_single\_batch(  
File “/mnt/c/Users/grimw/CELLECT/enter/envs/stagewiseNN/lib/python3.8/site-packages/scanpy/preprocessing/\_highly\_variable\_genes.py”, line 215, in \_highly\_variable\_genes\_single\_batch  
df[‘mean\_bin’] = pd.cut(df[‘means’], bins=n\_bins)  
File “/mnt/c/Users/grimw/CELLECT/enter/envs/stagewiseNN/lib/python3.8/site-packages/pandas/core/reshape/tile.py”, line 292, in cut  
fac, bins = \_bins\_to\_cuts(  
File “/mnt/c/Users/grimw/CELLECT/enter/envs/stagewiseNN/lib/python3.8/site-packages/pandas/core/reshape/tile.py”, line 419, in \_bins\_to\_cuts  
raise ValueError(  
ValueError: Bin edges must be unique: array([nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,  
nan, nan, nan, nan, nan, nan, nan, nan]).  
You can drop duplicate edges by setting the ‘duplicates’ kwarg

---

<div class="post-metadata">

### Author: ![karenlawwc](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.scverse.org/karenlawwc/32/536_2.png) [@karenlawwc](https://discourse.scverse.org/u/karenlawwc)
#### Post date: [February 24, 2023, 5:37pm UTC](https://discourse.scverse.org/t/scanpy-pp-highly-variable-genes-and-raise-keyerror/578/2 "2023-02-24T17:37:19Z")

</div>

Were you able to figure this out? I am encountering the same error and suspects something wrong with my input. Thank you!
