Skip to content

Instantly share code, notes, and snippets.

@yassineAlouini
Last active November 28, 2017 09:06
Show Gist options
  • Save yassineAlouini/831a56ae0133251dc9e7a5248dc6aa6a to your computer and use it in GitHub Desktop.
Save yassineAlouini/831a56ae0133251dc9e7a5248dc6aa6a to your computer and use it in GitHub Desktop.
A utility function to localize a UTC timestamp DataFrame column
import pytz
def localize_datetime(input_df, timezone, tms_col):
"""
Convert datetime column from UTC to another timezone.
"""
tmz = pytz.timezone(timezone)
df = input_df.copy()
return (df.set_index(tms_col)
.tz_localize(pytz.utc) # UTC time
.tz_convert(tmz) # Timezone time
.reset_index())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment