pandas: extract date and time from timestamp

前端 未结 2 681
名媛妹妹
名媛妹妹 2020-12-24 08:20

I have a timestamp column where the timestamp is in the following format

2016-06-16T21:35:17.098+01:00

I want to extract date

相关标签:
2条回答
  • 2020-12-24 08:56

    Do this first:

    df['time'] = pd.to_datetime(df['timestamp'])
    

    Before you do your extraction as usual:

    df['dates'] = df['time'].dt.date
    
    0 讨论(0)
  • 2020-12-24 09:15

    If date is in string form then:

    import datetime
    
    # this line converts the string object in Timestamp object
    df['DateTime'] = [datetime.datetime.strptime(d, "%Y-%m-%d %H:%M") for d in df["DateTime"]]
    
    # extracting date from timestamp
    df['Date'] = [datetime.datetime.date(d) for d in df['DateTime']] 
    
    # extracting time from timestamp
    df['Time'] = [datetime.datetime.time(d) for d in df['DateTime']] 
    

    If the object is already in the Timestamp format then skip the first line of code.

    %Y-%m-%d %H:%M this means your timestamp object must be in the form like 2016-05-16 12:35:00.

    0 讨论(0)
提交回复
热议问题