【问题标题】:AttributeError: module 'pandas.tseries.offsets' has no attribute 'timedelta'AttributeError:模块“pandas.tseries.offsets”没有属性“timedelta”
【发布时间】:2021-10-26 04:44:02
【问题描述】:

我的熊猫版本是 1.1.3,我正在尝试运行从this question 接受的答案中获得的代码,但是

offset = pd.offsets.timedelta(days=-6)

给出了我标题中提到的错误,timedelta 已导入但没有运气。我怎样才能让它正常运行?

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    因为有警告:

    FutureWarning:.resample() 和 Grouper() 中的“loffset”已弃用。

    您可以在resample 天后用减法6 更改解决方案:

    from pandas.tseries.frequencies import to_offset
    
    df = df.resample('W').apply(logic)
    df.index -= to_offset("6D")
    #alternative
    #df.index = df.index - to_offset("6D")
    
    print (df)
                     Open       High        Low      Close   Volume
    Date                                                           
    2010-01-04  38.660000  40.700001  38.509998  40.290001  5925600
    2010-01-11  40.209999  40.970001  39.279999  40.450001  6234600
    

    【讨论】:

    • 首先我必须将 pd.offsets.timedelta 更改为 pd.timedelta 才能收到警告。解决方案,谢谢!
    猜你喜欢
    • 2020-07-07
    • 2018-04-14
    • 2019-02-18
    • 1970-01-01
    • 2020-01-01
    • 2019-07-20
    • 2021-11-05
    • 2021-11-04
    相关资源
    最近更新 更多