简体   繁体   English

从时间索引的pandas数据帧中删除夏令时行

[英]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: 我编写了以下函数来删除pandas数据帧中夏令时的行,因为并非每列都有切换小时的数据:

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 exceptions.TypeError:'Timestamp'对象不可迭代

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. 问题是drop接受一个类似数组的参数labels ,而你只是传递一个时间戳。 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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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