How to Handle Time Zone in Python?

How to Handle Time Zone in Python?

Introduction

Handling time zones is an attractive disordered matter overall, then again language runtimes may have even bigger problems. There are thoughts beyond those that an operating system or distribution needs to handle. There are some lines to handling time zones, beginning with the fact that they change.

Description

At work with time zones is usually well-thought-out one of the most unfriendly parts of time series manipulation. Especially, daylight savings time (DST) transitions are a public source of the problem. For itself, several time series users select to work with time series in organized universal time or UTC that is the heir to Greenwich Mean Time. This is the existing international standard. Time zones are stated by way of offsets from UTC; e.g., New York is four hours late UTC in daylight savings time and 5 hours the rest of the year.

Time zone information comes in?Python?from the 3rd party pytz library. Those disclosures the Olson database, a gathering of world time zone information. This is particularly significant for historical data as the DST transition dates have been changed many times subject to the impulses of local governments. The DST transition times have been changed numerous times since 1900 in the United States.

pytz library

pytz takes the Olson tz database into Python. This library permits precise and cross-platform timezone calculations using Python 2.4 or higher. It similarly resolves the issue of uncertain times at the end of daylight saving time that we can read more about in the Python Library Reference, datetime.info.

Important to Note: This library is different from the documented Python API for tzinfo applications. We need to use the localize ( ) method documented in this document if we want to create local wallclock times. Furthermore, if we implement date arithmetic on local times that cross DST boundaries, the result can be in an incorrect timezone. A normalizes ( ) method is given to correct this.

How to install pytz library?

This package may be installed using pip or from a tarball using the standard?Python?distutils. We don’t need to download anything as the latest version will be downloaded for us from PyPI if we are installing using pip.

pip install pytz

Run the below command as an administrative user if we are installing from a tarball.

python setup.py install

pytz for Enterprise

This is accessible as part of the Tidelift Subscription.?The maintainers of pytz and many of the new packages are working with Tidelift to bring profitable support and maintenance for the open-source dependencies we use to build our applications.?Though paying the maintainers of the thorough needs we use save time, decrease risk, and improve code health.

We’ll need to look at that library’s documentation for detailed information about pytz library. We can ignore its API outside of the time zone names because pandas wrap pytz’s functionality.

Time zone names can be set up interactively and in the docs:

In [418]: import pytz

In [419]: pytz.common_timezones[-5:]

Out[419]: [‘US/Eastern’, ‘US/Hawaii’, ‘US/Mountain’, ‘US/Pacific’, ‘UTC’]

Use pytz.timezone to catch a time zone object from pytz:

In [420]: tz = pytz.timezone(‘US/Eastern’)

In [421]: tz

Out [421]:

Localization and Conversion

Time series, by default, in pandas are time zone naive. Study the below time series:

rng = pd.date_range(‘3/9/2012 9:30′, periods=6, freq=’D’)

ts = Series(np.random.randn(len(rng)), index=rng)

The index’s tz field is None:

In [423]: print (ts.index.tz)

None

Date ranges may be created with a time zone set:

In [424]: pd.date_range (‘3/9/2012 9:30′, periods=10, freq=’D’, tz=’UTC’)

Out [424]:

[2012-03-09 09:30:00,…, 2012-03-18 09:30:00]

Length: 10, Freq: D, Timezone: UTC

Change from naive to localized is handled by the tz_localize method:

In [425]: ts_utc = ts.tz_localize (‘UTC’)

In [426]: ts_utc

Out [426]:

2012-03-09 09:30:00+00:00 0.414615

2012-03-10 09:30:00+00:00 0.427185

2012-03-11 09:30:00+00:00 1.172557

2012-03-12 09:30:00+00:00 -0.351572

2012-03-13 09:30:00+00:00 1.454593

2012-03-14 09:30:00+00:00 2.043319

Freq: D

In [427]: ts_utc.index

Out [427]:

[2012-03-09 09:30:00,…, 2012-03-14 09:30:00]

Length: 6, Freq: D, Timezone: UTC

When a time series has been localized to a specific time zone, it may be converted to one more time zone using tz_convert:

In [428]: ts_utc.tz_convert (‘US/Eastern’)

Out [428]:

2012-03-09 04:30:00-05:00 0.414615

2012-03-10 04:30:00-05:00 0.427185

2012-03-11 05:30:00-04:00 1.172557

2012-03-12 05:30:00-04:00 -0.351572

2012-03-13 05:30:00-04:00 1.454593

2012-03-14 05:30:00-04:00 2.043319

Freq: D

We could localize to EST and convert to, say, UTC or Berlin time in the case of the above time series that straddles a DST transition in the US/Eastern time zone:

In [429]: ts_eastern = ts.tz_localize (‘US/Eastern’)

In [430]: ts_eastern.tz_convert(‘UTC’)

Out [430]:

2012-03-09 14:30:00+00:00 0.414615

2012-03-10 14:30:00+00:00 0.427185

2012-03-11 13:30:00+00:00 1.172557

2012-03-12 13:30:00+00:00 -0.351572

2012-03-13 13:30:00+00:00 1.454593

2012-03-14 13:30:00+00:00 2.043319

Freq: D

In [431]: ts_eastern.tz_convert (‘Europe/Berlin’)

Out [431]:

2012-03-09 15:30:00+01:00 0.414615

2012-03-10 15:30:00+01:00 0.427185

2012-03-11 14:30:00+01:00 1.172557

2012-03-12 14:30:00+01:00 -0.351572

2012-03-13 14:30:00+01:00 1.454593

2012-03-14 14:30:00+01:00 2.043319

Freq: D

tz_localize and tz_convert are likewise example methods on DatetimeIndex:

In [432]: ts.index.tz_localize(‘Asia/Shanghai’)

Out [432]:

[2012-03-09 09:30:00… 2012-03-14 09:30:00]

Length: 6, Freq: D, Timezone: Asia/Shanghai

For more details visit:https://www.technologiesinindustry4.com/2021/06/how-to-handle-time-zone-in-python.html

要查看或添加评论,请登录

Mansoor Ahmed的更多文章

社区洞察

其他会员也浏览了