Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set missing Year, Month and Day for Panda's Datetime

I have timestamp data in the format %H:%M:%S:%f. When I read it and pass it into a dataframe with pd.to_datetime it will automatically fill in 01/01/1900 in addition to the properly passed %H:%M:%S:%f How can I set it to a specific year, month, day while reading it and passing it the hour, minute, second, millisecond data?

Code I'm using:

import pandas as pd
df = pd.DataFrame({'Time': ["00:00:00:001", "00:00:00:005","00:00:00:009"]})
df['Time'] = pd.to_datetime(df['Time'], format='%H:%M:%S:%f')
print(df)

Output:

                     Time
0 1900-01-01 00:00:00.001
1 1900-01-01 00:00:00.005
2 1900-01-01 00:00:00.009

Desired output:

A set date, so for instance:

                     Time
0 2018-11-21 00:00:00.001
1 2018-11-21 00:00:00.005
2 2018-11-21 00:00:00.009
like image 913
Chrisvdberge Avatar asked Sep 15 '25 09:09

Chrisvdberge


2 Answers

There's a few ways you could do this, perhaps the easiest (at least that springs to my mind) is to append the string value of the date you want and then convert to datetime afterwards

import pandas as pd
df = pd.DataFrame({'Time': ["00:00:00:001", "00:00:00:005","00:00:00:009"]})
df["Time"] = "2018-11-21 "+ df["Time"]
df['Time'] = pd.to_datetime(df['Time'], format='%Y-%m-%d %H:%M:%S:%f')
print(df)
like image 102
Sven Harris Avatar answered Sep 16 '25 22:09

Sven Harris


df['Time']=df['Time'].apply(lambda dt: dt.replace(year=2018, day=21, month=11)) 
like image 38
Tom Ron Avatar answered Sep 16 '25 22:09

Tom Ron