I have a series of timedeltas like this:
import pandas as pd
deltas = pd.Series([
pd.Timedelta(hours=8),
pd.Timedelta(hours=9),
pd.Timedelta(hours=9, minutes=15),
])
I want to drop all entries that do not correspond to a full hour. I did:
idx_valid = deltas.dt.total_seconds() % 3600 == 0
which fails spectacularly:
0 True
1 False
2 False
Obviously there is some float precision problem going on behind the scene. (Not sure if the result is the same on all machines.) An easy way to fix this would be:
idx_valid = deltas.dt.total_seconds().astype(int) % 3600 == 0
But this fails if I have fractions of a second. I came up with:
idx_valid = deltas.dt.floor('h') == deltas
This works for the given values. But is it safe in every case? What is the right way to do this kind of comparison?