1

I'm a bit confused how numpy handles timezones. If I create a datetime-object just with a date, it seems it uses Zulu-Timezone. If I use an additional timestep, it uses my current timezone. If I then manipulate these objects, e.g. add a timedelta, the results are different:

import numpy as np
a = np.datetime64('2015-04-22')
b = np.datetime64('2015-04-22T00:00')
delta = np.timedelta64(1,'h')
print(a+delta,b+delta)

I must ensure that all values are in the same timezone, so my question is, how can I ensure that a user, who initializes these date doesn't mix dates and dates with time.

MichaelA
  • 1,866
  • 2
  • 23
  • 38

1 Answers1

0

If you specify Zulu in datetime with timestep you'll get uniform data.

In [30]: b = np.datetime64('2015-04-22T00:00Z')

In [31]: b + delta
Out[31]: numpy.datetime64('2015-04-22T03:00+0200')

In [32]: a + delta
Out[32]: numpy.datetime64('2015-04-22T03:00+0200','h')

http://docs.scipy.org/doc/numpy/reference/arrays.datetime.html#basic-datetimes

Pawel Miech
  • 7,742
  • 4
  • 36
  • 57
  • I know, but the function relies on user input, so I have to check, what the user did. That is my main problem. If the user uses sometimes only a date, and sometimes a full datetime, I will encounter a problem with the running code. – MichaelA Apr 23 '15 at 08:47
  • 1
    in this case you should probably specify what user should put into, and validate input, throw error if user inputs incorrect format – Pawel Miech Apr 23 '15 at 09:41