v Handling Time Zones - Machine Learning

Handling Time Zones

Preliminaries

# Load libraries
import pandas as pd
from pytz import all_timezones

View Timezones

# Show ten time zones
all_timezones[0:10]
['Africa/Abidjan',
 'Africa/Accra',
 'Africa/Addis_Ababa',
 'Africa/Algiers',
 'Africa/Asmara',
 'Africa/Asmera',
 'Africa/Bamako',
 'Africa/Bangui',
 'Africa/Banjul',
 'Africa/Bissau']

Create Timestamp With Time Zone

# Create datetime
pd.Timestamp('2017-05-01 06:00:00', tz='Europe/London')
Timestamp('2017-05-01 06:00:00+0100', tz='Europe/London')

Create Timestamp Without Time Zone

# Create datetime
date = pd.Timestamp('2017-05-01 06:00:00')

Add Time Zone

# Set time zone
date_in_london = date.tz_localize('Europe/London')

Convert Time Zone

# Change time zone
date_in_london.tz_convert('Africa/Abidjan')
Timestamp('2017-05-01 05:00:00+0000', tz='Africa/Abidjan')