Convert numpy.datetime64 to string object in python

后端 未结 6 928
無奈伤痛
無奈伤痛 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:13

    I wanted an ISO 8601 formatted string without needing any extra dependencies. My numpy_array has a single element as a datetime64. With help from @Wirawan-Purwanto, I added just a bit:

    from datetime import datetime   
    
    ts = numpy_array.values.astype(datetime)/1000000000
    return datetime.utcfromtimestamp(ts).isoformat() # "2018-05-24T19:54:48"
    

提交回复
热议问题