Bprodz Bprodz - 4 months ago 39
Python Question

Locate first and last non NaN values in a Pandas DataFrame

I have a Pandas

DataFrame
indexed by date. There a number of columns but many columns are only populated for part of the time series. I'd like to find where the first and last values non-
NaN
values are located so that I can extracts the dates and see how long the time series is for a particular column.

Could somebody point me in the right direction as to how I could go about doing something like this? Thanks in advance.

Answer

@behzad.nouri's solution worked perfectly to return the first and last non-NaN values using Series.first_valid_index and Series.last_valid_index, respectively.