AssertionError: Gaps in blk ref_locs when attempting to drop rows with `np.nan`

Viewed 23

I have a pandas DataFrame of shape 12k * 150. One of the columns is Lat.

import pandas as pd

df = pd.DataFrame({
                   'id': [0, 1, 2, 3],
                   'Lat': [83.21, np.nan, np.nan, 83.42],
                   'Lon' [-19.21, np.nan, np.nan, -20.56]
                 })

This is for illustration not a reproducible example, as my DataFrame is fairly large and I don't exactly know what's causing the issue.

I am trying to drop the rows with np.nan and get this error when I tried, drop.na, mask = (df['Lat'].isna()) and other methods.

/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/core/internals/managers.py in _consolidate_inplace(self)
   1686             self._is_consolidated = True
   1687             self._known_consolidated = True
-> 1688             self._rebuild_blknos_and_blklocs()
   1689 
   1690 

/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/_libs/internals.pyx in pandas._libs.internals.BlockManager._rebuild_blknos_and_blklocs()

AssertionError: Gaps in blk ref_locs
1 Answers

use dropna with a subset attribute

if you need to include other columns to consider, add those in the subset list.

df.dropna(subset=['Lat'])

alternately,

df.loc[df['Lat'].notna()]
    id  Lat     Lon
0   0   83.21   -19.21
3   3   83.42   -20.56
Related