So this recipe is a short example on how to convert timezone of timeseries in python. Let's get started.
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.
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'
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.
Once we run the above code snippet, we will see:
Scroll down the ipython file to visualize the final output.