Localize Asian Timezone for a DataFrame in Python



Assume, you have a time series and the result for localize asian time zone as,

Index is:
DatetimeIndex(['2020-01-05 00:30:00+05:30', '2020-01-12 00:30:00+05:30',
               '2020-01-19 00:30:00+05:30', '2020-01-26 00:30:00+05:30',
               '2020-02-02 00:30:00+05:30'],
               dtype='datetime64[ns, Asia/Calcutta]', freq='W-SUN')

Solution

  • Define a dataframe

  • Create time series using pd.date_range() function with start as ‘2020-01-01 00:30’, periods=5 and tz = ‘Asia/Calcutta’ then store it as time_index.

time_index = pd.date_range('2020-01-01 00:30', periods = 5, freq ='W',tz = 'Asia/Calcutta')
  • Set df.index to store localized time zone from time_index

df.index = time_index
  • Finally print the localized timezone

Example

Let’s check the following code to get a better understanding −

import pandas as pd
df = pd.DataFrame({'Id':[1,2,3,4,5],
                     'City':['Mumbai','Pune','Delhi','Chennai','Kolkata']})
time_index = pd.date_range('2020-01-01 00:30', periods = 5, freq ='W', tz = 'Asia/Calcutta')
df.index = time_index
print("DataFrame is:\n",df)
print("Index is:\n",df.index)

Output

DataFrame is:
                          Id City
2020-01-05 00:30:00+05:30 1 Mumbai
2020-01-12 00:30:00+05:30 2 Pune
2020-01-19 00:30:00+05:30 3 Delhi
2020-01-26 00:30:00+05:30 4 Chennai
2020-02-02 00:30:00+05:30 5 Kolkata
Index is:
DatetimeIndex(['2020-01-05 00:30:00+05:30', '2020-01-12 00:30:00+05:30',
               '2020-01-19 00:30:00+05:30', '2020-01-26 00:30:00+05:30',
               '2020-02-02 00:30:00+05:30'],
               dtype='datetime64[ns, Asia/Calcutta]', freq='W-SUN')
Updated on: 2021-02-25T07:06:49+05:30

244 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements