How to convert timezone of timeseries in python?

This recipe helps you convert timezone of timeseries in python

Recipe Objective

While operating with over timeseries, we might have time data of any country. Now we wish to change timeseries data with respect to other country. We can do it by add/subtracting specific time but it can be done in one go using pytz library.

So this recipe is a short example on how to convert timezone of timeseries in python. Let's get started.

Get Access to Time Series Analysis Real World Projects in Python

Step 1 - Import the library

import pandas as pd import pytz

Let's pause and look at these imports. Pandas is generally used for performing mathematical operation and preferably over arrays. pytz library allows accurate and cross platform timezone calculations.

Step 2 - Setup the Data and timezone

index = pd.date_range('20210101 00:00', freq='45S', periods=5) df = pd.DataFrame(1, index=index, columns=['X']) df.index = df.index.tz_localize('GMT') print(df)

Here we have first set the index as 2021/01/01 00:00 and took 5 period with gap interval of 45 seconds. Nowe we have created a dataframe with given index and a column 'X' with values equal to 1. Finally using pytz library, we have set the timezone to 'GMT'

Step 3 - Coverting timezone to India

Indian = pytz.timezone('Asia/Kolkata') df.index = df.index.tz_convert(Indian) print(df)

Here, we have created a variable containing timezone of Kolkata (City in India) and finally resetting the index.

Step 4 - Let's look at our dataset now

Once we run the above code snippet, we will see:

Scroll down the ipython file to visualize the final output.

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

Deep Learning Project for Beginners with Source Code Part 1
Learn to implement deep neural networks in Python .

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.

Multi-Class Text Classification with Deep Learning using BERT
In this deep learning project, you will implement one of the most popular state of the art Transformer models, BERT for Multi-Class Text Classification