Convert numpy.datetime64 to string object in python

后端 未结 6 955
無奈伤痛
無奈伤痛 2020-11-30 03:45

I am having trouble converting a python datetime64 object into a string. For example:

t = numpy.datetime64(\'2012-06-30T20:00:00.000000000-0400\         


        
6条回答
  •  清歌不尽
    2020-11-30 04:19

    If you don't want to do that conversion gobbledygook and are ok with just one date format, this was the best solution for me

    str(t)[:10]
    Out[11]: '2012-07-01'
    

    As noted this works for pandas too

    df['d'].astype(str).str[:10]
    df['d'].dt.strftime('%Y-%m-%d') # equivalent
    

提交回复
热议问题