Get MM-DD-YYYY from pandas Timestamp

后端 未结 4 773
庸人自扰
庸人自扰 2020-12-05 08:02

dates seem to be a tricky thing in python, and I am having a lot of trouble simply stripping the date out of the pandas TimeStamp. I would like to get from 2013-09-29

4条回答
  •  一个人的身影
    2020-12-05 08:39

    You can try using .dt.date on datetime64[ns] of the dataframe.

    For e.g. df['Created_date'] = df['Created_date'].dt.date

    Input dataframe named as test_df:

    print(test_df)
    

    Result:

             Created_date
    0     2015-03-04 15:39:16
    1     2015-03-22 17:36:49
    2     2015-03-25 22:08:45
    3     2015-03-16 13:45:20
    4     2015-03-19 18:53:50
    

    Checking dtypes:

    print(test_df.dtypes)
    

    Result:

    Created_date    datetime64[ns]
    dtype: object
    

    Extracting date and updating Created_date column:

    test_df['Created_date'] = test_df['Created_date'].dt.date
    print(test_df)
    

    Result:

      Created_date
    0   2015-03-04
    1   2015-03-22
    2   2015-03-25
    3   2015-03-16
    4   2015-03-19
    

提交回复
热议问题