How to remove seconds from datetime?

前端 未结 10 1976
终归单人心
终归单人心 2021-02-20 13:02

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         


        
相关标签:
10条回答
  • 2021-02-20 13:54

    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)
    
    0 讨论(0)
  • 2021-02-20 13:59

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

    0 讨论(0)
  • 2021-02-20 14:02

    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
    
    0 讨论(0)
  • 2021-02-20 14:02

    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'>)
    
    0 讨论(0)
提交回复
热议问题