我有一个问题已经在其他stackoverflow页面上以不同的方式进行了讨论,但是我找不到任何可以解决我的奇怪问题的解决方案。我有一个Excel文件(.xlsx),可以读取并存储在数据框中。一列称为“时间”,其数据格式为“ 2018/10/13 14:29:00”。 在我的python脚本中,我需要将此列从对象类型(查询python时可以看到的类型)转换为datetime,并将此新格式保存在名为“ date_time_fmt”的新列中。
这实际上是我的脚本,在这里我尝试了在stackoverflow中找到的所有可能的解决方案:
try:
df_accounts_followed['date_time_fmt'] = df_accounts_followed['time'].astype('datetime64[ns]')
except Exception as e:
print("Error 1")
exception_short_name = type(e).__name__
print("Error Type: {}".format( exception_short_name))
print("Error description: {}".format( e))
try:
df_accounts_followed['time'] = df_accounts_followed['time'].astype('|S')
name_dtype = df_accounts_followed['time'].dtype.name
print("df_accounts_followed['time'] is now of type: {}".format(name_dtype))
df_accounts_followed['date_time_fmt'] = df_accounts_followed['time'].astype('datetime64[s]')
except Exception as e:
try:
print("Error 2")
exception_short_name = type(e).__name__
print("Error Type: {}".format( exception_short_name))
print("Error description: {}".format( e))
df_accounts_followed['date_time_fmt'] = pd.to_datetime(df_accounts_followed['time'], format = '%Y/%m/%d %H:%M:%S')
except Exception as e:
print("Error 3")
exception_short_name = type(e).__name__
print("Error Type: {}".format( exception_short_name))
print("Error description: {}".format( e))
try:
df_accounts_followed['date_time_fmt'] = df_accounts_followed['time'].apply(lambda x: datetime.strptime(x,'%Y/%m/%d %H:%M:%S'))
except Exception as e:
print("Error 4")
print("Error line num: {}".format(sys.exc_info()[-1].tb_lineno))
exception_short_name = type(e).__name__
print("Error Type: {}".format( exception_short_name))
print("Error description: {}".format( e))
input("Enough !")
现在,当我运行脚本时,会发生这种情况。第一次尝试会出现此错误:
Error when filtering df_excel_actions_report dataframe.
Error Type: ValueError
Error description: Error parsing datetime string "2018/10/13 14:29:00" at position 4
这是第二次尝试后出现的错误:
Error Type: ValueError
Error description: Error parsing datetime string "2018/10/13 14:29:00" at position 4
这是第三次尝试后出现的错误:
Error Type: TypeError
Error description: <class 'bytes'> is not convertible to datetime
这是我上次尝试时遇到的错误:
Error Type: TypeError
Error description: strptime() argument 1 must be str, not bytes
这时我的绝望程度很高。我希望有人能帮助我。这些是我阅读的一些页面:time.strptime() - argument 0 must be str, not bytes,Convert Pandas Column to DateTime II,pandas convert string columns to datetime, allowing missing but not invalid。 谢谢
答案 0 :(得分:0)
不确定这是否对您有帮助,但是最近我偶然发现了<class 'bytes'> is not convertible to datetime
错误,这就是我的解决方法。
我的数据框看起来像这样,请注意索引值是字节字符串:
Times
b'2018-07-01_00:00:00' 0.006036
b'2018-07-01_01:00:00' 0.000000
b'2018-07-01_02:00:00' 0.000000
b'2018-07-01_03:00:00' 0.000000
解决方案是解码Times
索引,然后使用自定义格式将其转换为日期时间。
pandas.to_datetime(df.index.astype('str'),
format="%Y-%m-%d_%H:%M:%S")
在上面的代码中(尝试3),它看起来像:
df_accounts_followed['date_time_fmt'] = pd.to_datetime(df_accounts_followed['time'].astype('str'),
format = '%Y/%m/%d %H:%M:%S')