简体   繁体   中英

Deleting rows of daylight saving time from a time indexed pandas dataframe

I have written the following function to delete the rows for Daylight Saving Time in a pandas dataframe as not every column has data for the hour of switching:

def hrsDSTdelete (df):
    import pandas as pd    
    hrsDSTadjust = ['2000-03-26 02:00:00', ...  '2012-03-25 02:00:00', '2013-03-31 02:00:00']

    for DSTvalue in hrsDSTadjust:                          
        if DSTvalue in df.index :  
            df = df.drop(pd.Timestamp(DSTvalue)) 
            print 'DST hour: ', DSTvalue, " deleted!"         
    return df   
pass

As this seems to work when deleting single rows, the following error message occurs when trying to do it with this loop:

exceptions.TypeError: 'Timestamp' object is not iterable

I have tried also with

df =  df.ix[DSTvalue].drop

but this does not seem to delete the row in the dataframe. Has anyone got an idea what I am doing wrong?

The problem is that drop takes an array-like argument labels , and you are only passing it a timestamp. You should be able to use a list comprehension instead of your loop too:

indices = [pd.Timestamp(DSTvalue) for DSTValue  in hrsDSTadjust if DSTvalue in df.index]
df = df.drop(indices)

你不需要循环,试试这个:

df.drop(df.index[hrsDSTadjust])

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