How to convert timezone of timeseries in python?

How to convert timezone of timeseries in python?

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.

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.

Relevant Projects

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

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.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.