Search code examples
pythonpandasmilliseconds

Hot to use pd.datetime() on milliseconds timestamp?


I am trying to convert my time from an object (as I read it from csv file) to a datetime format. my time format is 07:00:00.16 (hour:minutes:seconds.milliseconds)

import pandas as pd 
df=pd.read_csv('Copy.txt')
df.columns = df.columns.str.strip()
df['Time']=pd.to_datetime(df.Time, errors = 'coerce')

My results is the the Time column is all "NaT"

If I use: df['Time']=pd.to_datetime(df.Time)

than I get this error: OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-01-01 07:00:00

Pls advice how to change the column type and to keep the full milliseconds data. thanks!

the data format is:

0       07:00:00.0
1       07:00:00.1
2       07:00:00.2
3       07:00:00.3
4       07:00:00.4
5       07:00:00.5
6       07:00:00.6
7       07:00:00.7
8       07:00:00.8
9       07:00:00.9
10     07:00:00.10
11     07:00:00.11
12     07:00:00.12
13     07:00:00.13
14     07:00:00.14
15     07:00:00.15
16     07:00:00.16
Name: Time, dtype: object

Solution

  • If your csv has time stored in string format like '07:00:00.16' you can simply specify the format and extract the time part to convert your column into datetime object:

    df['Time'] = pd.to_datetime(df['Time'], format='%H:%M:%S.%f').dt.time