【问题标题】:Deleting rows of daylight saving time from a time indexed pandas dataframe从时间索引的熊猫数据框中删除夏令时行
【发布时间】:2013-08-07 17:23:04
【问题描述】:

我编写了以下函数来删除 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

由于这似乎在删除单行时有效,因此在尝试使用此循环执行此操作时会出现以下错误消息:

exceptions.TypeError: 'Timestamp' 对象不可迭代

我也试过了

df =  df.ix[DSTvalue].drop

但这似乎并没有删除数据框中的行。 有没有人知道我做错了什么?

【问题讨论】:

标签: python pandas timestamp time-series


【解决方案1】:

问题在于drop takes an array-like argument labels,而您只是传递了一个时间戳。您也应该能够使用列表推导而不是循环:

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

【讨论】:

    【解决方案2】:

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

    df.drop(df.index[hrsDSTadjust])
    

    【讨论】:

    • 不幸的是,这似乎不起作用。我收到此错误消息:exceptions.IndexError: unsupported iterator index
    猜你喜欢
    • 2021-03-19
    • 2018-01-02
    • 2017-02-09
    • 1970-01-01
    • 2018-02-02
    • 2015-06-13
    • 2023-01-12
    • 2013-05-12
    • 1970-01-01
    相关资源
    最近更新 更多