【问题标题】:Python Dataframe: Remove timestamp from calculated Days fieldPython Dataframe:从计算的天数字段中删除时间戳
【发布时间】:2017-11-15 20:43:29
【问题描述】:

我使用以下代码计算了一列:

df_EVENT5_5['age'] = dt.datetime.now().date() - df_EVENT5_5['dt_old']
    df_EVENT5_5['age_no_days'] = df_EVENT5_5['age'].dt.total_seconds()/ (24 * 60 * 60)

出于某种原因,输出列包含时间戳。

如何删除时间戳?

我在下面试过但没有用:

remove_timestamp_col = ['COL_1', 'COL_2']

    for i in remove_timestamp_col:
        df_EVENT5_13[i] = df_EVENT5_13[i].age.days()

【问题讨论】:

    标签: python datetime timestamp difference


    【解决方案1】:

    我认为数据中可能有一些带有时间戳的日期,请尝试仅减去日期:

    df_EVENT5_5['age'] = dt.date.today() - df_EVENT5_5['dt_old'].apply(dt.datetime.date)
    #This gives you the days difference as a number
    df_EVENT5_5['age_no_days'] = df_EVENT5_5['age'].dt.days
    #If you want it to have 'Days' in the end, you can use concatenation:
    df_EVENT5_5['age_with_days'] = df_EVENT5_5['age_no_days'].astype('str') + ' Days'
    

    【讨论】:

    • 当我将表格写入 Excel 时,我仍然在单元格值中看到时间戳.....帮助?
    • 我对代码进行了一些修改,我调用了“days”属性而不是“total_seconds()”来为您提供天数的差异,然后您可以将其与单词 'days' 给出你想要的表示。
    • 啊,这太完美了!我错过了 .dt.days 代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多