0

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Markus W
  • 1,451
  • 5
  • 19
  • 32

2 Answers2

2

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)
Paulo Almeida
  • 7,803
  • 28
  • 36
0

you don't need a loop, try this one:

df.drop(df.index[hrsDSTadjust])
nick
  • 1,090
  • 1
  • 11
  • 24
erkinc
  • 34
  • 1
  • unfortunately this doesn't seem to work. Im getting this error message: exceptions.IndexError: unsupported iterator index – Markus W Aug 16 '13 at 12:21