Truncate `TimeStamp` column to hour precision in pandas `DataFrame`

后端 未结 2 1344
轻奢々
轻奢々 2020-12-02 12:58

I have a pandas.DataFrame called df which has an automatically generated index, with a column dt:

df[\'dt\'].dtype, df         


        
2条回答
  •  甜味超标
    2020-12-02 13:40

    In pandas 0.18.0 and later, there are datetime floor, ceil and round methods to round timestamps to a given fixed precision/frequency. To round down to hour precision, you can use:

    >>> df['dt2'] = df['dt'].dt.floor('h')
    >>> df
                          dt                     dt2
    0    2014-10-01 10:02:45     2014-10-01 10:00:00
    1    2014-10-01 13:08:17     2014-10-01 13:00:00
    2    2014-10-01 17:39:24     2014-10-01 17:00:00
    

    Here's another alternative to truncate the timestamps. Unlike floor, it supports truncating to a precision such as year or month.

    You can temporarily adjust the precision unit of the underlying NumPy datetime64 datatype, changing it from [ns] to [h]:

    df['dt'].values.astype('

    This truncates everything to hour precision. For example:

    >>> df
                           dt
    0     2014-10-01 10:02:45
    1     2014-10-01 13:08:17
    2     2014-10-01 17:39:24
    
    >>> df['dt2'] = df['dt'].values.astype('>> df
                          dt                     dt2
    0    2014-10-01 10:02:45     2014-10-01 10:00:00
    1    2014-10-01 13:08:17     2014-10-01 13:00:00
    2    2014-10-01 17:39:24     2014-10-01 17:00:00
    
    >>> df.dtypes
    dt     datetime64[ns]
    dt2    datetime64[ns]
    

    The same method should work for any other unit: months 'M', minutes 'm', and so on:

    • Keep up to year: '
    • Keep up to month: '
    • Keep up to day: '
    • Keep up to minute: '
    • Keep up to second: '

提交回复
热议问题