Converting between datetime, Timestamp and datetime64

前端 未结 12 1442
傲寒
傲寒 2020-11-22 01:43

How do I convert a numpy.datetime64 object to a datetime.datetime (or Timestamp)?

In the following code, I create a datetime,

12条回答
  •  离开以前
    2020-11-22 02:06

    Welcome to hell.

    You can just pass a datetime64 object to pandas.Timestamp:

    In [16]: Timestamp(numpy.datetime64('2012-05-01T01:00:00.000000'))
    Out[16]: 
    

    I noticed that this doesn't work right though in NumPy 1.6.1:

    numpy.datetime64('2012-05-01T01:00:00.000000+0100')
    

    Also, pandas.to_datetime can be used (this is off of the dev version, haven't checked v0.9.1):

    In [24]: pandas.to_datetime('2012-05-01T01:00:00.000000+0100')
    Out[24]: datetime.datetime(2012, 5, 1, 1, 0, tzinfo=tzoffset(None, 3600))
    

提交回复
热议问题