How to remove seconds from datetime?

前端 未结 10 1973
终归单人心
终归单人心 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:37

    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
    

提交回复
热议问题