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","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']).replace(second = 0)
I get the following error:
TypeError: replace() got an unexpected keyword argument 'second'
As you mentioned removed so I assumed you don't want the seconds or microsecond in the result.If this is the case then following might help:
datetime_variable.strftime("'%Y-%m-%d %H:%M'")
If you have datetime in string then you can convert it in datetime obj:
Convert String to datetime object first, then you can use the replace method.
Convert the string to a datetime object and then manipulate that
HTML Code:
Python Django Code:
Result is:
Solutions if need datetimes in output:
Use
Series.dt.floor
by minutesT
orMin
:You can use convert to
numpy values
first and then truncateseconds
by cast to<M8[m]
, but this solution remove possible timezones:Another solution is create
timedelta
Series fromsecond
and substract:Timings:
Solutions if need strings repr of datetimes in output
Use
Series.dt.strftime
:And if necessary set
:00
to seconds:Set seconds to 0
pd.to_datetime
will returndatetime
objects, which havesecond
as attribute : there's not much you can do about it. You can setsecond
to0
, 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 ofdf
::23
and:45
from the first times have been replaced by:00
, but they are still printed.Remove
':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 :You could apply this to every element in your list, before initializing
df['start_date_time']
:Display datetimes without seconds
If you want to work with
datetime
objects but don't want to show seconds :