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

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

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.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.