I have a pandas column which contain time data and column datatype is object.
time
Aug 23, 2019 16:30:00
Aug 24, 2019 13:50:00
Aug 16, 2019 08:20:00
Aug 18, 2019 09:00:00
How to convert the column to Datatime
I have a pandas column which contain time data and column datatype is object.
time
Aug 23, 2019 16:30:00
Aug 24, 2019 13:50:00
Aug 16, 2019 08:20:00
Aug 18, 2019 09:00:00
How to convert the column to Datatime
For speedup, better to specify the date format,
df['DATE'] = pd.to_datetime(df['DATE'], format="%b %d, %Y %H:%M:%S")
you can try the following
df['DATE'] = pd.to_datetime(df['DATE'])
do not do
df['Date'] = pd.to_datetime(df['time']).strftime('%d_%m_%Y')
as series objects has no attribute 'strftime'
Convert any string pattern into datetime object using this
1. Write a function which takes in the string object and convertes it into datetime object
2. Use .apply() to use the function to all rows of the Series object.
Check the below code
import datetime
def splitdates(string):
month, date, year, time = string.split()
return datetime.date(int(year), month, date[:len(date)-1])
# Assuming time is a Series object
# Do something like this
time.apply(lambda x: splitdates(x))