How to replace by NaN a time delta object in a pandas serie?

Viewed 523

I would like to calculate a mean of a time delta serie excluding 00:00:00 values.

Then this is my time serie:

1    00:28:00
3    01:57:00
5    00:00:00
7    01:27:00
9    00:00:00
11   01:30:00

I try to replace 5 and 9 row per NaN and then apply .mean() to the serie. mean() doesn´t include NaN values and I get the desired value.

How can I do that stuff?

I´am trying:

`df["time_column"].replace('0 days 00:00:00', np.NaN).mean()`

but no values are replaced

1 Answers

One idea is use 0 Timedelta object:

out = df["time_column"].replace(pd.Timedelta(0), np.NaN).mean()
print (out)
0 days 01:20:30
Related