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
You can subtract the seconds using a timedelta:
import datetime
d = datetime.datetime.now() #datetime including seconds
without_seconds = d - datetime.timedelta(seconds=d.second)
Here is a very simple way to remove seconds from datetime:
from datetime import datetime
print(str(datetime.today())[:16])
Output:
2021-02-14 21:30
It effectively transforms the timestamp into text and leaves only the first 16 symbols. Just don't lose yourself in all those brackets ;)
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
Convert the string to a datetime object and then manipulate that
>>> x = ["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"]
>>> for i in x:
... y = datetime.datetime.strptime(i, '%Y-%m-%d %H:%M:%S')
... z = datetime.datetime.strftime(y, '%Y-%m-%d %H:%M')
... print (y, type(y))
... print (z, type(z))
...
(datetime.datetime(2016, 5, 19, 8, 25), <type 'datetime.datetime'>)
('2016-05-19 08:25', <type 'str'>)
(datetime.datetime(2016, 5, 19, 16, 0), <type 'datetime.datetime'>)
('2016-05-19 16:00', <type 'str'>)
(datetime.datetime(2016, 5, 20, 7, 45), <type 'datetime.datetime'>)
('2016-05-20 07:45', <type 'str'>)
(datetime.datetime(2016, 5, 24, 12, 50), <type 'datetime.datetime'>)
('2016-05-24 12:50', <type 'str'>)
(datetime.datetime(2016, 5, 25, 23, 0), <type 'datetime.datetime'>)
('2016-05-25 23:00', <type 'str'>)
(datetime.datetime(2016, 5, 26, 19, 45), <type 'datetime.datetime'>)
('2016-05-26 19:45', <type 'str'>)