
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Write a program in Python to localize Asian timezone for a given dataframe
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')
- Related Articles
- Write a Python program to reshape a given dataframe in different ways
- Write a program in Python to convert a given dataframe to a LaTex document
- Write a program in Python to remove first duplicate rows in a given dataframe
- Write a program in Python to transpose the index and columns in a given DataFrame
- Write a Python program to sort a given DataFrame by name column in descending order
- Write a program in Python to modify the diagonal of a given DataFrame by 1
- Write a program in Python to select any random odd index rows in a given DataFrame
- Write a Python code to rename the given axis in a dataframe
- Write a Python code to filter palindrome names in a given dataframe
- Write a program in Python to count the total number of leap years in a given DataFrame
- Write a program in Python to count the records based on the designation in a given DataFrame
- Write a program in Python to remove one or more than one columns in a given DataFrame
- Write a program in Python to caluculate the adjusted and non-adjusted EWM in a given dataframe
- Write a Python code to swap last two rows in a given dataframe
- Write a program in Python to perform average of rolling window size 3 calculation in a given dataframe

Advertisements