【问题标题】:Pandas datetime - keep time only as dtype datetimePandas datetime - 仅将时间保留为 dtype datetime
【发布时间】:2021-07-13 13:25:28
【问题描述】:

我想要 Pandas 中没有日期的时间。 我想将时间保留为 dtype datetime64[ns] 而不是对象,以便我可以确定时间之间的时间段。

我得到的最接近如下,但它在新列中返回日期而不是所需的时间作为 dtype datetime。

df_pres_mf['time'] = pd.to_datetime(df_pres_mf['time'], format ='%H:%M', errors = 'coerce')   # returns date (1900-01-01) and actual time as a dtype datetime64[ns] format

df_pres_mf['just_time'] = df_pres_mf['time'].dt.date
df_pres_mf['normalised_time'] = df_pres_mf['time'].dt.normalize()
df_pres_mf.head()

将日期返回为 1900-01-01,而不是所需的时间。

编辑:数据

               time
1900-01-01 11:16:00
1900-01-01 15:20:00
1900-01-01 09:55:00
1900-01-01 12:01:00

【问题讨论】:

  • format='%H:%M' 不对。请在此处输入完整格式,例如format='%Y-%m-%d %H:%M',然后使用df_pres_mf['time'].dt.time
  • 请在您的帖子@db2020 中添加一些示例数据
  • 很遗憾,我无法分享任何数据。
  • 我们不想要任何机密信息。创建一个与您的数据相似的代表性示例数据框。
  • 1900-01-01 11:16:00、1900-01-01 15:20:00、1900-01-01 09:55:00、1900-01-01 12:01: 00

标签: python pandas datetime dtype


【解决方案1】:

你可以像 Vishnudev 建议的那样做,但是你会得到 dtype: object(甚至是字符串,在使用 dt.strftime 之后),你说你不想要。

您正在寻找的东西不存在,但我能得到的最接近您的东西是转换为 timedeltas。起初这似乎不是一个解决方案,但实际上非常有用。

像这样转换它:

# sample df
df
>>
                 time
0 2021-02-07 09:22:00
1 2021-05-10 19:45:00
2 2021-01-14 06:53:00
3 2021-05-27 13:42:00
4 2021-01-18 17:28:00

df["timed"] = df.time - df.time.dt.normalize() 
df
>>
 
                 time           timed
0 2021-02-07 09:22:00 0 days 09:22:00  # this is just the time difference
1 2021-05-10 19:45:00 0 days 19:45:00  # since midnight, which is essentially the 
2 2021-01-14 06:53:00 0 days 06:53:00  # same thing as regular time, except
3 2021-05-27 13:42:00 0 days 13:42:00  # that you can go over 24 hours
4 2021-01-18 17:28:00 0 days 17:28:00

这允许您像这样计算时间之间的周期:

# subtract the last time from the current
df["difference"] = df.timed - df.timed.shift() 
df
Out[48]: 
                 time           timed        difference
0 2021-02-07 09:22:00 0 days 09:22:00               NaT
1 2021-05-10 19:45:00 0 days 19:45:00   0 days 10:23:00
2 2021-01-14 06:53:00 0 days 06:53:00 -1 days +11:08:00  # <-- this is because the last
3 2021-05-27 13:42:00 0 days 13:42:00   0 days 06:49:00  # time was later than the current
4 2021-01-18 17:28:00 0 days 17:28:00   0 days 03:46:00  # (see below)

为了消除奇怪的差异,让它变得绝对:

df["abs_difference"] = df.difference.abs()
df
>>
                 time           timed        difference  abs_difference
0 2021-02-07 09:22:00 0 days 09:22:00               NaT             NaT
1 2021-05-10 19:45:00 0 days 19:45:00   0 days 10:23:00 0 days 10:23:00
2 2021-01-14 06:53:00 0 days 06:53:00 -1 days +11:08:00 0 days 12:52:00  ### <<--
3 2021-05-27 13:42:00 0 days 13:42:00   0 days 06:49:00 0 days 06:49:00
4 2021-01-18 17:28:00 0 days 17:28:00   0 days 03:46:00 0 days 03:46:00

【讨论】:

    【解决方案2】:

    根据您的日期格式使用适当的格式并转换为日期时间

    df['time'] = pd.to_datetime(df['time'], format='%Y-%m-%d %H:%M:%S')
    

    根据首选格式格式化

    df['time'].dt.strftime('%H:%M')
    

    输出

    0    11:16
    1    15:20
    2    09:55
    3    12:01
    Name: time, dtype: object
    

    【讨论】:

    • 你能解释一下为什么他应该选择 dtype 对象,以及 dt.strftime 如何让他计算时间间隔?从问题的第一句话开始:“[...] 而不是作为对象,以便我可以确定时间之间的时间段”
    • 我的理解是OP希望时间列作为日期时间而不是对象。您可以查看他代码第一行中的注释。 @Stryder 我认为这就是为什么会出现混乱。
    • 我明白你的意思
    猜你喜欢
    • 2012-11-19
    • 2015-02-01
    • 2023-04-07
    • 1970-01-01
    • 2019-05-30
    • 2013-04-25
    • 2020-01-06
    • 1970-01-01
    • 2018-07-04
    相关资源
    最近更新 更多