【问题标题】:Python localtime,TypeError: cannot convert the series to <class 'int'>Python localtime,TypeError:无法将系列转换为 <class 'int'>
【发布时间】:2020-08-09 03:42:10
【问题描述】:

我有一个数据框: enter image description here

我想在当地时间显示“时间戳”,但不运行。

time.localtime(data_user['TimeStamps'].astype(int))

错误:

TypeError: cannot convert the series to <class 'int'>

我怎样才能正确地做到这一点?

【问题讨论】:

    标签: python typeerror localtime


    【解决方案1】:

    使用箭头包

    from datetime import datetime
    import arrow
    
    now = datetime.utcnow()
    
    print(arrow.get(now).to('local').format())
    # 2020-04-25 15:55:31+01:00
    

    类似这样的:

    def to_local(x):
        return arrow.get(x).to('local').format() 
    
    data_user['TimeStamps']= data_user['TimeStamps'].apply(to_local)
    

    【讨论】:

    • 谢谢。我有一个和你类似的答案。 data_user['time'] = data_user['TimeStamps'].apply(lambda t: time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(t)))
    猜你喜欢
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多