Reputation: 435
I have the following code:
df["date"] = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
df['date'] = pd.to_datetime(df['date'])
It takes 2020-04-29 14:12:32.692683
convert it to String of 2020-04-29 14:12:32
and then set it up as TIMESTAMP type.
i remove the milliseconds as they are not needed.
It works well but the idea of timestamp -> string -> timestamp sounds very inefficient.
I must have the date column as TIMESTAMP type because it's being added to database and this is the only way that the database will recognise it as TIMESTAMP type rather than VARCHAR
Is there a better way to do this?
Upvotes: 0
Views: 159
Reputation: 25544
use replace()
to get the right format/dtype right away [ref]:
from datetime import datetime
import pandas as pd
df = pd.DataFrame({'date':[datetime.utcnow().replace(microsecond=0)]})
# df['date']
# 0 2020-04-29 15:05:06
# Name: t, dtype: datetime64[ns]
Upvotes: 1
Reputation: 816
A one liner for that:
df["date"] = datetime.strptime(str(datetime.utcnow())[:19], '%Y-%m-%d %H:%M:%S')
Possibly better?
Execution time for timeit: 0.1083306
print(timeit.timeit(stmt=myfunction, number=10000))
Upvotes: 1