Skip to content
Advertisement

Dropping rows at specific minutes

I am trying to drop rows at specific minutes ( 05,10, 20 ) I have datetime as an index

df5['Year'] = df5.index.year
df5['Month'] = df5.index.month
df5['Day']= df5.index.day
df5['Day_of_Week']= df5.index.day_name()
df5['hour']= df5.index.strftime('%H')
df5['Min']= df5.index.strftime('%M')
df5

enter image description here

Then I run below

def clean(df5):
for i in range(len(df5)):
    hour = pd.Timestamp(df5.index[i]).hour
    minute = pd.Timestamp(df5.index[i]).minute
    if df5 = df5[(df5.index.minute ==5) | (df5.index.minute == 10)| (df5.index.minute == 20)]
        df.drop(axis=1, index=i, inplace=True)

it returnes invalid syntax error.

Advertisement

Answer

You can just do it using boolean indexing, assuming that the index is already parsed as datetime.

df5 = df5[~((df5.index.minute == 5) | (df5.index.minute == 10) | (df5.index.minute == 20))]

Or the opposite of the same answer:

df5 = df5[(df5.index.minute != 5) | (df5.index.minute != 10) | (df5.index.minute != 20)]
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement