How to remove seconds from datetime?

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

    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 , but this solution remove possible timezones:

    df['start_date_time'] = df['start_date_time'].values.astype('

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

    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
    

提交回复
热议问题