I have the following date and I tried the following code,
df[\'start_date_time\'] = [\"2016-05-19 08:25:00\",\"2016-05-19 16:00:00\",\"2016-05-20 07:45:00\",\"20
pd.to_datetime
will return datetime
objects, which have second
as attribute : there's not much you can do about it. You can set second
to 0
, but the attribute will still be here and the standard representation will still include a trailing ':00'
.
You need to apply replace
on each element of df
:
import pandas as pd
df = pd.DataFrame({'start_date_time': ["2016-05-19 08:25:23","2016-05-19 16:00:45","2016-05-20 07:45:00","2016-05-24 12:50:00","2016-05-25 23:00:00","2016-05-26 19:45:00"]})
df['start_date_time'] = pd.to_datetime(df['start_date_time'])
df['start_date_time'] = df['start_date_time'].apply(lambda t: t.replace(second=0))
print(df)
# start_date_time
# 0 2016-05-19 08:25:00
# 1 2016-05-19 16:00:00
# 2 2016-05-20 07:45:00
# 3 2016-05-24 12:50:00
# 4 2016-05-25 23:00:00
# 5 2016-05-26 19:45:00
:23
and :45
from the first times have been replaced by :00
, but they are still printed.
':00'
from the stringsIf you just want a string representation of those times and only parse the strings to datetime
objects in order to remove ':00'
at the end of the string, you could just remove the last 3 characters :
>>> "2016-05-19 08:25:00"[:-3]
'2016-05-19 08:25'
You could apply this to every element in your list, before initializing df['start_date_time']
:
>>> start_date_time = ["2016-05-19 08:25:00","2016-05-19 16:00:00","2016-05-20 07:45:00","2016-05-24 12:50:00","2016-05-25 23:00:00","2016-05-26 19:45:00"]
>>> map(lambda s: s[:-3], start_date_time)
['2016-05-19 08:25', '2016-05-19 16:00', '2016-05-20 07:45', '2016-05-24 12:50', '2016-05-25 23:00', '2016-05-26 19:45']
If you want to work with datetime
objects but don't want to show seconds :
print(df['start_date_time'].apply(lambda t: t.strftime('%Y-%m-%d %H:%M')))
# 0 2016-05-19 08:25
# 1 2016-05-19 16:00
# 2 2016-05-20 07:45
# 3 2016-05-24 12:50
# 4 2016-05-25 23:00
# 5 2016-05-26 19:45
# Name: start_date_time, dtype: object