How to remove seconds from datetime?

2020-08-26 04:07发布

问题:

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'

回答1:

Solutions if need datetimes in output:

df = pd.DataFrame({'start_date_time': ["2016-05-19 08:25:23","2016-05-19 16:00:45"]})
df['start_date_time'] = pd.to_datetime(df['start_date_time'])
print (df)
       start_date_time
0  2016-05-19 08:25:23
1  2016-05-19 16:00:45

Use Series.dt.floor by minutes T or Min:

df['start_date_time'] = df['start_date_time'].dt.floor('T')

df['start_date_time'] = df['start_date_time'].dt.floor('Min')

You can use convert to numpy values first and then truncate seconds by cast to <M8[m], but this solution remove possible timezones:

df['start_date_time'] = df['start_date_time'].values.astype('<M8[m]')
print (df)
      start_date_time
0 2016-05-19 08:25:00
1 2016-05-19 16:00:00

Another solution is create timedelta Series from second and substract:

print (pd.to_timedelta(df['start_date_time'].dt.second, unit='s'))
0   00:00:23
1   00:00:45
Name: start_date_time, dtype: timedelta64[ns]

df['start_date_time'] = df['start_date_time'] - 
                        pd.to_timedelta(df['start_date_time'].dt.second, unit='s')
print (df)
      start_date_time
0 2016-05-19 08:25:00
1 2016-05-19 16:00:00

Timings:

df = pd.DataFrame({'start_date_time': ["2016-05-19 08:25:23","2016-05-19 16:00:45"]})
df['start_date_time'] = pd.to_datetime(df['start_date_time'])

#20000 rows
df = pd.concat([df]*10000).reset_index(drop=True)


In [28]: %timeit df['start_date_time'] = df['start_date_time'] - pd.to_timedelta(df['start_date_time'].dt.second, unit='s')
4.05 ms ± 130 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [29]: %timeit df['start_date_time1'] = df['start_date_time'].values.astype('<M8[m]')
1.73 ms ± 117 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [30]: %timeit df['start_date_time'] = df['start_date_time'].dt.floor('T')
1.07 ms ± 116 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [31]: %timeit df['start_date_time2'] = df['start_date_time'].apply(lambda t: t.replace(second=0))
183 ms ± 19.7 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

Solutions if need strings repr of datetimes in output

Use Series.dt.strftime:

print(df['start_date_time'].dt.strftime('%Y-%m-%d %H:%M'))
0    2016-05-19 08:25
1    2016-05-19 16:00
Name: start_date_time, dtype: object

And if necessary set :00 to seconds:

print(df['start_date_time'].dt.strftime('%Y-%m-%d %H:%M:00'))
0    2016-05-19 08:25:00
1    2016-05-19 16:00:00
Name: start_date_time, dtype: object


回答2:

Set seconds to 0

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.

Remove ':00' from the strings

If 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']

Display datetimes without seconds

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


回答3:

HTML Code:

< input type="time" class="form-control" name="meeting_time" required /> 

Python Django Code:

meeting_time = request.POST['meeting_time'] #Like your_time = "12:35:00"
get_time = meeting_time.strftime("%H:%M")

Result is:

get_time = "12:35"


回答4:

Give this a shot with:

df.index = df.index.map(lambda t: t.strftime('%Y-%m-%d %H:%M'))

As written in one of the comments, the above apply to the case where the dates are not strings. If they, however, are strings, you can simply slice the last three characters from each list in the list:

import pandas as pd

df = pd.DataFrame({'date': ["2016-05-19 08:25:00"]})

print(df['date'].map(lambda t: t[:-3]))

The above will output:

0    2016-05-19 08:25
Name: date, dtype: object


回答5:

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)


回答6:

Convert String to datetime object first, then you can use the replace method.

from _datetime import *


df = dict()
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"]

for dt in df['start_date_time']:
    cur_dt = datetime.strptime(dt, '%Y-%m-%d %H:%M:%S')
    cur_dt = cur_dt.replace(second=0)
    print(cur_dt)

    cur_dt_without_second = cur_dt.strftime('%Y-%m-%d %H:%M')
    print(cur_dt_without_second)

-------------------
2016-05-19 08:25:00
2016-05-19 08:25
2016-05-19 16:00:00
2016-05-19 16:00
2016-05-20 07:45:00
2016-05-20 07:45
2016-05-24 12:50:00
2016-05-24 12:50
2016-05-25 23:00:00
2016-05-25 23:00
2016-05-26 19:45:00
2016-05-26 19:45


回答7:

this is with the time module not datetime module but might be what your looking for...

import time
X = time.strftime("%H:%M")
print(X)

or you with seconds

import time
X = time.strftime("%H:%M:%S")
print(X)

If your gonna down vote please explain why



回答8:

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:

from dateutil import parser

datetime_variable = parser.parse(str_datetime_var)

datetime_variable.strftime("'%Y-%m-%d %H:%M'")



回答9:

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'>)