简体   繁体   中英

Droping rows in DataFrame based on Timestamp Hour

I'm attempting to drop rows in a DataFrame that has a datetime index column. I'm getting an error for comparing a str to an int using <.

The code I'm running is below.

def clean(df):
    for i in range(len(df)):
        hour = pd.Timestamp(df.index[i]).hour
        minute = pd.Timestamp(df.index[i]).minute
        if hour < 8 and minute < 45:
            df.drop(axis=1, index=i, inplace=True)

Which results in the error: TypeError: '<' not supported between instances of 'str' and 'int'

If I write a separate line: type(pd.Timestamp(df.index[i]).hour) it returns <class 'int'>

I can perform math like hour += 1 but when comparing the hour or minute the if statement returns the error. Changing the code to hour = int(pd.Timestamp(df.index[i]).hour) also doesn't help.

Thank you

您可以制作一个掩码指定要保留的行,而不是循环遍历行(这会很慢),然后让pandas给您一个(更快的)答案:

df = df[(df.index.hour >=8) | (df.index.minute >= 45)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM