Remove 'seconds' and 'minutes' from a Pandas dataframe column

前端 未结 1 548
长发绾君心
长发绾君心 2021-01-02 20:17

Given a dataframe like:

import numpy as np
import pandas as pd

df = pd.DataFrame(
{\'Date\' : pd.date_range(\'1/1/2011\', periods=5, freq=\'3675S\'),
 \'Num         


        
相关标签:
1条回答
  • 2021-01-02 20:52

    dt.round

    This is how it should be done... use dt.round

    df.assign(Date=df.Date.dt.round('H'))
    
                     Date       Num
    0 2011-01-01 00:00:00  0.577957
    1 2011-01-01 01:00:00  0.995748
    2 2011-01-01 02:00:00  0.864013
    3 2011-01-01 03:00:00  0.468762
    4 2011-01-01 04:00:00  0.866827
    

    OLD ANSWER

    One approach is to set the index and use resample

    df.set_index('Date').resample('H').last().reset_index()
    
                     Date       Num
    0 2011-01-01 00:00:00  0.577957
    1 2011-01-01 01:00:00  0.995748
    2 2011-01-01 02:00:00  0.864013
    3 2011-01-01 03:00:00  0.468762
    4 2011-01-01 04:00:00  0.866827
    

    Another alternative is to strip the date and hour components

    df.assign(
        Date=pd.to_datetime(df.Date.dt.date) +
             pd.to_timedelta(df.Date.dt.hour, unit='H'))
    
                     Date       Num
    0 2011-01-01 00:00:00  0.577957
    1 2011-01-01 01:00:00  0.995748
    2 2011-01-01 02:00:00  0.864013
    3 2011-01-01 03:00:00  0.468762
    4 2011-01-01 04:00:00  0.866827
    
    0 讨论(0)
提交回复
热议问题