Python: reduce precision pandas timestamp dataframe

前端 未结 3 1529
长情又很酷
长情又很酷 2020-12-17 09:33

Hello I have the following dataframe

df = 

       Record_ID       Time
        94704   2014-03-10 07:19:19.647342
        94705   2014-03-10 07:21:44.479363         


        
3条回答
  •  -上瘾入骨i
    2020-12-17 09:56

    If you really must remove the microsecond part of the datetime, you can use the Timestamp.replace method along with Series.apply method to apply it across the series , to replace the microsecond part with 0. Example -

    df['Time'] = df['Time'].apply(lambda x: x.replace(microsecond=0))
    

    Demo -

    In [25]: df
    Out[25]:
       Record_ID                       Time
    0      94704 2014-03-10 07:19:19.647342
    1      94705 2014-03-10 07:21:44.479363
    2      94706 2014-03-10 07:21:45.479581
    3      94707 2014-03-10 07:21:54.481588
    4      94708 2014-03-10 07:21:55.481804
    
    In [26]: type(df['Time'][0])
    Out[26]: pandas.tslib.Timestamp
    
    In [27]: df['Time'] = df['Time'].apply(lambda x: x.replace(microsecond=0))
    
    In [28]: df
    Out[28]:
       Record_ID                Time
    0      94704 2014-03-10 07:19:19
    1      94705 2014-03-10 07:21:44
    2      94706 2014-03-10 07:21:45
    3      94707 2014-03-10 07:21:54
    4      94708 2014-03-10 07:21:55
    

提交回复
热议问题