4

May be I'm to blind or narrow minded to find a smart solution, but I'd like to extract the time of an numpy.datetime64 object and can't find an obvious solution.

Of course I can do this:

import numpy as np
a = np.datetime64('2015-03-23T22:58')
print(a.tolist().time())

But that still leaves me with handling timezone problems.

Additionally it just doesn't make sense to me to use numpy.datetime64 just to convert it to datetime and use these methods. Or is numpy.datetime64 lacking some major features/I'm not use it as intended and should better fall back to datetime.datetime?

MichaelA
  • 1,866
  • 2
  • 23
  • 38
  • The help indicates that you can specify Zulu or local time in the string...haven't played with it, but it is worth a look. http://docs.scipy.org/doc/numpy/reference/arrays.datetime.html –  Apr 23 '15 at 21:19
  • 2
    I posted an incorrect answer. However, does `Pandas` work for you? In `pd.to_datetime('2015-03-23T22:58').hour` and `pd.to_datetime('2015-03-23T22:58').minute` – Zero Apr 23 '15 at 21:22
  • Does this answer your question? [How to extract hours/minutes/seconds from np.datetime64](https://stackoverflow.com/questions/61406911/how-to-extract-hours-minutes-seconds-from-np-datetime64) – Niko Föhr Feb 04 '22 at 10:02

1 Answers1

1

In addition to my comment, I experimented with a simple example to see what the formats had to offer from the documentation reference

>>> import numpy as np
>>> a = np.datetime64('2015-03-23T17:00')  # 17hr local time
>>> b = np.datetime64('2015-03-23T17:00Z') # 17hr Zulu
>>> a
numpy.datetime64('2015-03-23T17:00-0400')
>>> b
numpy.datetime64('2015-03-23T13:00-0400')
>>> 
>>> a.tolist().time()
datetime.time(21, 0)
>>> b.tolist().time()
datetime.time(17, 0)
>>> 

which is the appropriate time difference for my locale.