## Pandas - Find longest stretch without Nan values

Question

I have a pandas dataframe "df", a sample of which is below:

```
time x
0 1 1
1 2 Nan
2 3 3
3 4 Nan
4 5 8
5 6 7
6 7 5
7 8 Nan
```

The real frame is much bigger. I am trying to find the longest stretch of non NaN values in the "x" series, and print out the starting and ending index for this frame. Is this possible?

Thank You

Show source

## Answers to Pandas - Find longest stretch without Nan values ( 4 )

So you can get the index values of the

`NaN`

's in the following way:Then one solution would be to see the largest difference between subsequent index values and that would give you the longest stretch of non

`NaN`

values.Maybe a faster way would be the following (given that you say you have a long dataframe, speed matters):

Here's a vectorized approach with NumPy tools -

Sample run -

The intervals are set such that the difference between each start and stop would give us the length of each interval. So, by

`ending index`

if you meant to get the last index of non-zero element, we need to subtract one from`stop`

.pandasnumpydemo