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
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)
df['Time']=df['Time'].apply(lambda dt: dt.replace(year=2018, day=21, month=11))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With