Lorenz
Lorenz

Reputation: 306

Setting time zone of a Pandas Timestamp to Boise

I want to read in a unix time into a timestamp and assign the timezone of Boise. Current timezone there is "MDT – Mountain Daylight Time (Daylight Saving Time)". It will switch to MST during winter time.

What is the right timezone to use, when I want want to have it not dependent on summer / winter time?

I want to have something like this

pd.Timestamp(1513393355, unit='s', tz='US/Pacific') 

Upvotes: 4

Views: 1144

Answers (2)

unutbu
unutbu

Reputation: 880637

This interprets 1513393355 as a timestamp in UTC and then converts the timestamp to the America/Boise timezone:

In [57]: pd.Timestamp(1513393355, unit='s', tz='UTC').tz_convert('America/Boise')
Out[57]: Timestamp('2017-12-15 20:02:35-0700', tz='America/Boise')

Pandas depends on pytz for timezone calculations. pytz depends on the Olson database. The Olson database contains a history of the utc offsets for each timezone. So you don't have to worry about whether Daylight Savings Time is in effect on a particular date. tz_convert will take care of that for you.

You can take a look at all the options using pytz.timezones.

There is a US/Mountain timezone, but it is deprecated. See Matt Johnson's answer for more authoritative information on the relevant options.

Upvotes: 2

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241808

A few things:

  • Generally speaking, one should use America/Denver for most of US Mountain Time, which currently switches between MST (UTC-7) and MDT (UTC-6).

  • Use America/Phoenix for the part of US Mountain Time that does not currently use DST, such as applies in most (but not all) of Arizona.

    • However, be aware that it used to have DST in the past, last in 1968.
  • Use Etc/GMT+7 if you need a fixed offset that is UTC-7 and never had DST. Be aware that the sign of the offset is inverted intentionally (Etc/GMT+7 = UTC-7).

    • If you are using this zone for anything other than times for ships at sea, then you should probably re-evaluate this decision. In other words, if your data is for a local time zone that does use DST, then you should not pick a zone that ignores it.
  • You can use America/Boise, but you should only use it for the areas of southern Idaho and eastern Oregon that started DST four weeks late in 1974 (1974-02-03 instead of 1974-01-06), as described in the commentary of the TZ Database sources:

    # Southern Idaho (Ada, Adams, Bannock, Bear Lake, Bingham, Blaine,
    # Boise, Bonneville, Butte, Camas, Canyon, Caribou, Cassia, Clark,
    # Custer, Elmore, Franklin, Fremont, Gem, Gooding, Jefferson, Jerome,
    # Lemhi, Lincoln, Madison, Minidoka, Oneida, Owyhee, Payette, Power,
    # Teton, Twin Falls, Valley, Washington counties, and the southern
    # quarter of Idaho county) and eastern Oregon (most of Malheur County)
    # switched four weeks late in 1974.
    
    • If you aren't working with timestamps from 1974, then you can ignore this zone and just use America/Denver.
  • You can use US/Mountain, but understand that it is linked as an alias of America/Denver and is considered deprecated. It exists for backwards compatibility.

    • It is generally recommended to use canonical identifiers of the Area/Locality form instead of these older representations. Refer to the list of TZ Database time zones on Wikipedia.

Upvotes: 4

Related Questions