Date and Time Manipulation

Learn common operations that we need to perform when dealing with date data.

pandas allows us to create Series with date and time information in them. Let’s talk about dates in brief.

Date theory

Coordinated Universal Time (UTC) is the time standard at 0 degrees longitude. It has an excellent property, namely that it’s monotonically increasing. If someone lives in Salt Lake City, Utah, they follow the America/Denver timezone, which is six or seven hours offset of UTC depending on the time of the year.

In short, a timezone may contain one or more offsets (depending on whether they observe daylight savings time). There is a standardized format, ISO 8601, for representing dates. It does not include the timezone information but the option to include an offset.

Note: The public domain timezone database (also known as the Olsen database) from iana.org provides code and data regarding timezones and their history. Their documentation states: "Timezones are typically identified by continent or ocean and then by the name of the largest city within the region containing the clocks. For example, America/New_York represents most of the US Eastern Time Zone; America/Phoenix represents most of Arizona, which uses mountain time without daylight savings time (DST); America/Detroit represents most of Michigan, which uses eastern time but with different DST rules in 1975; and other entries represent smaller regions like Starke County, Indiana, which switched from Central Time to Eastern Time in 1991 and switched back in 2006.

Getting the correct timezone name is important and might be confusing or difficult. If we search for “Timezone for Salt Lake City,” we get “Mountain Daylight Time” or “GMT-6,” neither of which is a timezone. We might also see “US/Mountain,” “MST,” or “MDT.” These are not timezones either. These are deprecated names or offsets. The correct timezone name is “America/Denver.” However, many applications support erroneous names.

It’s recommended to preface our search with “IANA” (i.e., “IANA Timezone for Salt Lake City”).

It’s important to have the offset information as well. Timezones that have daylight savings time can have “ambiguous time” in the fall when the time goes back. For example, in Salt Lake on Nov 1, 2015, after 1:59 AM (MDT), the clock went to 1:00 AM (MST). On that date there were two 1:30 AMs—one at MDT and another an hour later at MST.

Get hands-on with 1400+ tech skills courses.