user3527975
user3527975

Reputation: 1773

Get rid of NaT values from pandas dataframe

I have a dataframe that looks like shown below

                               mean
comp_name  date                      
Appdynamics 2012-05-01 00:18:15.910000
            2012-05-01             NaT
            2012-05-01             NaT
            2012-05-02 00:20:12.145200
            2012-05-02             NaT
            2012-05-02             NaT

Here the comp_name and date form multiindex. I want to get rid of the NaT values and obtain only those rows where the mean(timedelta64) is not NaT.

                               mean
comp_name  date                      
Appdynamics 2012-05-01 00:18:15.910000
            2012-05-02 00:20:12.145200

Any ideas on this?

Upvotes: 19

Views: 37259

Answers (2)

user171780
user171780

Reputation: 3095

In Pandas 1.4.1 dropna gets rid of NaT values. Source: documentation and I am using it. So now it is as simple as

df = df.dropna()

Upvotes: 2

exp1orer
exp1orer

Reputation: 12039

pandas.notnull() takes a series and returns a Boolean series which is True where the input series is not null (None, np.NaN, np.NaT). Then you can slice a dataframe by the Boolean series:

df[pandas.notnull(df['mean'])]

Upvotes: 25

Related Questions