【问题标题】:Parsing datetime64 and datetime.time Python 3.6.5解析 datetime64 和 datetime.time Python 3.6.5
【发布时间】:2018-08-20 20:52:22
【问题描述】:

我有两列,一列的类型为 datetime64 和 datetime.time。这 第一列有日期,第二列有小时和分钟。我 无法解析它们:

Leistung_0011

       ActStartDateExecution ActStartTimeExecution
0             2016-02-17              11:00:00
10            2016-04-15              07:15:00
20            2016-06-10              10:30:00

Leistung_0011['Start_datetime'] = pd.to_datetime(Leistung_0011['ActStartDateExecution'].astype(str) + ' ' + Leistung_0011['ActStartTimeExecution'].astype(str))

ValueError: ('Unknown string format:', 'NaT 00:00:00')

【问题讨论】:

  • 如果没有您的数据框示例 (Leistung_0011),我们将无法为您提供帮助。您还应该包含完整的回溯。
  • 请编辑您的问题以包含MCVE
  • 你能发布你的数据吗?
  • 是的,对不起,这是我在 stackoverflow 中的第一篇文章。

标签: python python-3.x pandas datetime


【解决方案1】:

您可以转换为str 并在传递给pd.to_datetime 之前加入空格:

df['datetime'] = pd.to_datetime(df['day'].astype(str) + ' ' + df['time'].astype(str))

print(df, df.dtypes, sep='\n')

#          day      time            datetime
# 0 2018-01-01  15:00:00 2018-01-01 15:00:00
# 1 2015-12-30  05:00:00 2015-12-30 05:00:00
# day         datetime64[ns]
# time                object
# datetime    datetime64[ns]
# dtype: object

设置

from datetime import datetime

df = pd.DataFrame({'day': ['2018-01-01', '2015-12-30'],
                   'time': ['15:00', '05:00']})

df['day'] = pd.to_datetime(df['day'])
df['time'] = df['time'].apply(lambda x: datetime.strptime(x, '%H:%M').time())

print(df['day'].dtype, type(df['time'].iloc[0]), sep='\n')

# datetime64[ns]
# <class 'datetime.time'>

包含秒数的完整示例:

import pandas as pd
from io import StringIO

x = StringIO("""       ActStartDateExecution ActStartTimeExecution
0             2016-02-17              11:00:00
10            2016-04-15              07:15:00
20            2016-06-10              10:30:00""")

df = pd.read_csv(x, delim_whitespace=True)

df['ActStartDateExecution'] = pd.to_datetime(df['ActStartDateExecution'])
df['ActStartTimeExecution'] = df['ActStartTimeExecution'].apply(lambda x: datetime.strptime(x, '%H:%M:%S').time())
df['datetime'] = pd.to_datetime(df['ActStartDateExecution'].astype(str) + ' ' + df['ActStartTimeExecution'].astype(str))

print(df.dtypes)

ActStartDateExecution    datetime64[ns]
ActStartTimeExecution            object
datetime                 datetime64[ns]
dtype: object

【讨论】:

  • 我尝试将其转换为 str,但仍然出现此错误: Leistung_0011['Start_datetime'] = pd.to_datetime(Leistung_0011['ActStartDateExecution'].astype(str) + ' ' + Leistung_0011 ['ActStartTimeExecution'].astype(str)) ValueError: ('Unknown string format:', 'NaT 00:00:00')
  • @DiegoLainfiesta,这不足以解决您的问题。您应该提供minimal reproducible example
  • 对不起……我是第一次。这是数据集的副本: ActStartDateExecution ActStartTimeExecution 0 2016-02-17 11:00:00 10 2016-04-15 07:15:00 20 20 2016-06-10 10:30:00 Leistung_0011['Start_datetime'] = pd.to_datetime(Leistung_0011['ActStartDateExecution'].astype(str) + '' + Leistung_0011['ActStartTimeExecution'].astype(str)) ValueError: ('Unknown string format:', 'NaT 00:00:00' )
  • 没问题。但是请edit your question 使用格式正确的代码,不要将代码放在 cmets 中,它不可读。
  • 完成...在原帖中... :)
猜你喜欢
  • 1970-01-01
  • 2018-11-21
  • 1970-01-01
  • 2017-11-09
  • 2020-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多