Extract day and month from a datetime object

后端 未结 4 1522
臣服心动
臣服心动 2021-01-06 01:26

I have a column with dates in string format \'2017-01-01\'. Is there a way to extract day and month from it using pandas?

I have converted the column to

4条回答
  •  不思量自难忘°
    2021-01-06 02:01

    With dt.day and dt.month --- Series.dt

    df = pd.DataFrame({'date':pd.date_range(start='2017-01-01',periods=5)})
    df.date.dt.month
    Out[164]: 
    0    1
    1    1
    2    1
    3    1
    4    1
    Name: date, dtype: int64
    
    df.date.dt.day
    Out[165]: 
    0    1
    1    2
    2    3
    3    4
    4    5
    Name: date, dtype: int64
    

    Also can do with dt.strftime

    df.date.dt.strftime('%m')
    Out[166]: 
    0    01
    1    01
    2    01
    3    01
    4    01
    Name: date, dtype: object
    

提交回复
热议问题