Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force python print numpy datetime64 with specified timezone?

I want to see numpy datetime64 objects by my specified timezone.

>>> import numpy as np
>>> np.datetime64('2013-03-10T01:30:54')
numpy.datetime64('2013-03-10T01:30:54+0400')
>>> np.datetime64('2013-03-10T01:30:54+0300')
numpy.datetime64('2013-03-10T02:30:54+0400')

Python prints datetime objects always in UTC+0400 (it is my local timezone) even if I specify another timezone >>> np.datetime64('2013-03-10T01:30:54+0300'). Is there a way to force python print by UTC+0000 timezone?

I am using numpy 1.8.1 .

like image 568
Samvel Hovsepyan Avatar asked Aug 05 '14 08:08

Samvel Hovsepyan


People also ask

How do you convert UTC time to other timezone in Python?

Get the current time using the datetime. now() function(returns the current local date and time) and pass the timezone() function(gets the time zone of a specific location) with the timezone as an argument to it say 'UTC'. Format the above DateTime using the strftime() and print it.

How do I convert datetime64 to datetime?

After that, You can create datetime64 format using the numpy. datetime64() format. To convert it to datetime format then you have to use astype() method and just pass the datetime as an argument.

What is Numpy datetime64?

datetime64() method, we can get the date in a numpy array in a particular format i.e year-month-day by using numpy. datetime64() method. Syntax : numpy.datetime64(date) Return : Return the date in a format 'yyyy-mm-dd'.


1 Answers

Mentioned a few times in the numpy documentation:

The datetime object represents a single moment in time.

...

Datetimes are always stored based on POSIX time ...

So, internally a datetime64 is tracking a single integer, which represents a moment in time as a value since the UNIX epoch (1970-01-01) - not counting leap seaconds.

Therefore, time zones are not preserved. If you pass in a time zone offset, it will apply it to determine the correct UTC time. If you don't pass one, it will use the local machine's time zone. Regardless of input, on output it uses the local machine's time zone to project the UTC time to a local time with offset.

like image 160
Matt Johnson-Pint Avatar answered Oct 06 '22 00:10

Matt Johnson-Pint