Splitting timestamp column into separate date and time columns

后端 未结 7 760
执念已碎
执念已碎 2020-11-27 06:29

I have a pandas dataframe with over 1000 timestamps (below) that I would like to loop through:

2016-02-22 14:59:44.561776

I\'m having a har

7条回答
  •  孤城傲影
    2020-11-27 06:41

    I think the most easiest way is to use dt attribute of pandas Series. For your case you need to use dt.date and dt.time:

    df = pd.DataFrame({'full_date': pd.date_range('2016-1-1 10:00:00.123', periods=10, freq='5H')})
    df['date'] = df['full_date'].dt.date
    df['time'] = df['full_date'].dt.time
    
    In [166]: df
    Out[166]:
                    full_date        date             time
    0 2016-01-01 10:00:00.123  2016-01-01  10:00:00.123000
    1 2016-01-01 15:00:00.123  2016-01-01  15:00:00.123000
    2 2016-01-01 20:00:00.123  2016-01-01  20:00:00.123000
    3 2016-01-02 01:00:00.123  2016-01-02  01:00:00.123000
    4 2016-01-02 06:00:00.123  2016-01-02  06:00:00.123000
    5 2016-01-02 11:00:00.123  2016-01-02  11:00:00.123000
    6 2016-01-02 16:00:00.123  2016-01-02  16:00:00.123000
    7 2016-01-02 21:00:00.123  2016-01-02  21:00:00.123000
    8 2016-01-03 02:00:00.123  2016-01-03  02:00:00.123000
    9 2016-01-03 07:00:00.123  2016-01-03  07:00:00.123000
    

提交回复
热议问题