python - 转换为“日期时间”类型的问题:“小时必须在 0..23 中”
问题描述
这些是我的 csv 文件中的一些示例行:
10/10/1949 20:30,san marcos,tx,us,cylinder,2700,45 minutes,"This event took place in early fall around 1949-50. It occurred after a Boy Scout meeting in the Baptist Church. The Baptist Church sit",4/27/2004,29.8830556,-97.9411111
10/10/1949 21:00,lackland afb,tx,,light,7200,1-2 hrs,"1949 Lackland AFB, TX. Lights racing across the sky & making 90 degree turns on a dime.",12/16/2005,29.38421,-98.581082
10/10/1955 17:00,chester (uk/england),,gb,circle,20,20 seconds,"Green/Orange circular disc over Chester, England",1/21/2008,53.2,-2.916667
10/10/1956 21:00,edna,tx,us,circle,20,1/2 hour,"My older brother and twin sister were leaving the only Edna theater at about 9 PM,...we had our bikes and I took a different route home",1/17/2004,28.9783333,-96.6458333
完整的 csv 文件在这里。
我将它加载到数据框中。在列名'datetime'
中,我有格式'object'
。我试图将类型转换为这样的'object'
类型'datetime'
:
df['datetime'] = pd.to_datetime(df.datetime)
结果我得到这个错误:
ValueError: hour must be in 0..23
任何帮助将不胜感激!
解决方案
显然问题是24:00
, 解决方案是Series.str.split
, date
s 通过to_datetime
and time
by转换to_timedelta
并加起来:
print (df)
datetime
0 10/10/1949 20:30
1 10/10/1949 21:00
2 10/10/1955 17:00
3 10/10/1956 24:00
df[['date','time']] = df['datetime'].str.split(expand=True)
df['datetime'] = (pd.to_datetime(df.pop('date'), format='%d/%m/%Y') +
pd.to_timedelta(df.pop('time') + ':00'))
print (df)
datetime
0 1949-10-10 20:30:00
1 1949-10-10 21:00:00
2 1955-10-10 17:00:00
3 1956-10-11 00:00:00
推荐阅读
- java - 如何关闭收集用户输入的 ArrayList 的 while 循环
- python-3.x - 寻找一种更 Pythonic 的方式将列表列表转换为字符串
- api - Block.io 代币提现
- sql-server - SQL Server 归档表分区
- c++ - 为什么在逗号分隔符上下文中将预增量的结果转换为 void?
- java - java中的汇编代码内联以及如何区分java中的32位和64位操作系统
- python - Django 套接字连接超时
- r - 错误:“usethis”的包或命名空间加载失败:
- java - For-loop 仅在整个 Excel 文件中打印 Array 的最后一个值
- python - 用python和bs4抓取后的不同数据