Home > Back-end >  OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-01-01 00:00:34
OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-01-01 00:00:34

Time:08-21

I have a column in a csv file that says 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

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.

Thanks!

CodePudding user response:

Problem is that some time values are not zero padding, you can pad column value with 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
  • Related