【问题标题】:How to add a date for a specific time如何为特定时间添加日期
【发布时间】:2020-05-23 19:09:17
【问题描述】:

对于像下面这样的数据框,我只想在 0:00 时区的日期中添加一天。 有什么办法吗?

df.head(50)

【问题讨论】:

  • 您没有在 DF 中包含时区,因此不确定如何在逻辑中使用时区...您是否要求在所有恰好是午夜的时间中添加一天?
  • 这正是我想说的。我英语不好,对不起。
  • 请在文本代码模式下添加您的数据框示例。谢谢!

标签: python pandas date datetime


【解决方案1】:
# create a mask for all entries with midnight as their time
row_mask = (df['timezone'].dt.hour == 0) & (df['timezone'].dt.minute == 0)
#now apply it
df.loc[row_mask,'timezone'] = df[row_mask]['timezone'] + datetime.timedelta(days=1)

【讨论】:

    猜你喜欢
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 2021-07-24
    • 2017-09-20
    • 1970-01-01
    • 2016-11-17
    相关资源
    最近更新 更多