[英]OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-01-01 00:00:34
I have a column in a csv file that says start time
.我在 csv 文件中有一个列,上面写着start time
。 And the values are like this:值是这样的:
0:10:52
4:33:34
1:41:06
21:19:40
0:30:55
22:27:23
I wrote the following piece of code to change the datatype of this column to "time":我编写了以下代码将该列的数据类型更改为“时间”:
log_file['start time'] = pd.to_datetime(log_file['start time']).dt.time
When I run this piece of code, it gives me the following error:当我运行这段代码时,它给了我以下错误:
OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-01-01 00:00:34 OutOfBoundsDatetime:越界纳秒时间戳:1-01-01 00:00:34
Can someone please help me understand this error and how to troubleshoot it.有人可以帮我理解这个错误以及如何解决它。 I went through the other outofboundsdatetime posts but I couldn't really understand the logic.我浏览了其他 outofboundsdatetime 帖子,但我无法真正理解逻辑。
Thanks!谢谢!
Problem is that some time values are not zero padding, you can pad column value with Series.str.zfill
问题是某些时间值不是零填充,您可以使用Series.str.zfill
填充列值
log_file['end time'] = (pd.to_datetime(
(log_file['start time']
.str.split(':', expand=True)
.apply(lambda col: col.str.zfill(2))
.fillna('00')
.agg(':'.join, axis=1))
).dt.time)
print(log_file)
start time end time
0 0:10:52 00:10:52
1 4:33:34 04:33:34
2 1:41:06 01:41:06
3 21:19:40 21:19:40
4 0:30:55 00:30:55
5 22:27:23 22:27:23
6 0:0:34 00:00:34
7 0::34 00:00:34
8 ::4 00:00:04
9 :4 00:04:00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.