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

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

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.

AWS MLOps Project to Deploy Multiple Linear Regression Model
Build and Deploy a Multiple Linear Regression Model in Python on AWS

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.