Pandas Series.tz_localize - localize timezone
Last Updated :
29 Nov, 2024
In Pandas, Series.tz_localize() function is used to localize a timezone-naive index to a target timezone. This operation converts a datetime index, which does not have timezone information (naive), into a timezone-aware index. It helps standardize time data for analysis in a specific time zone.
Syntax: Series.tz_localize(tz, axis=0, level=None, copy=True, ambiguous='raise', nonexistent='raise')
Parameters:
- tz: A string or pytz.timezone object specifying the target timezone (e.g., 'US/Central', 'Asia/Calcutta').
- axis: The axis to localize (default is 0, meaning rows).
- level: If the index is a MultiIndex, this specifies the level to localize.
- copy: Whether to make a copy of the underlying data (default is True).
- ambiguous: Determines how ambiguous time indices are handled. Options include 'infer', boolean ndarray, and 'NaT'.
- nonexistent: Specifies how to handle nonexistent times (default is 'raise').
The function returns a series or dataframe with a timezone-aware index.
Localizing Time Zone in a Series using Series.tz_localize()
In this example, we'll localize a timezone-naive index to the 'US/Central' timezone using Series.tz_localize().
Python
# Importing pandas
import pandas as pd
# Creating a Series
sr = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio', 'Moscow'])
# Create a DatetimeIndex using pd.date_range
didx = pd.date_range(start='2014-08-01 10:00', freq='W', periods=6)
# Setting the index for the Series
sr.index = didx
# Print the original Series
print("Original Series:")
display(sr)
# Localizing the time zone from naive to 'US/Central'
sr_localized = sr.tz_localize('US/Central')
# Print the localized Series
print("\nLocalized Series:")
display(sr_localized)
Output:
As seen in the output, the tz_localize() function successfully converted the timezone-naive datetime index to a timezone-aware index in 'US/Central'.
Localizing Time Zone in a Series with Numeric Values using tz_localize()
In this example, we will localize a timezone-naive datetime index of numeric values in the Series to the 'Asia/Calcutta' timezone.
Python
# Importing pandas
import pandas as pd
# Creating the Series
sr = pd.Series([19.5, 16.8, 22.78, 20.124, 18.1002])
# Create a DatetimeIndex
didx = pd.date_range(start='2014-08-01 10:00', freq='W', periods=5)
# Setting the index for the Series
sr.index = didx
# Print the original Series
print("Original Series:")
display(sr)
# Localizing the time zone from naive to 'Asia/Calcutta'
sr_localized = sr.tz_localize('Asia/Calcutta')
# Print the localized Series
print("\nLocalized Series:")
display(sr_localized)
Output:
As shown, the tz_localize() method converts the datetime index to a timezone-aware index with the timezone set to 'Asia/Calcutta'.
The Series.tz_localize() function is a powerful tool for handling timezone-naive datetime indices in Pandas. It allows you to localize time zones, ensuring that your time series data is accurately represented in the correct time zone for analysis.
Similar Reads
Pandas Series dt.tz_convert | Change Timezone in DateTime Series The dt.tz_convert() method converts tz-aware DateTime Series object from one time zone to another. Example Python3 import pandas as pd sr = pd.Series(pd.date_range('2012-12-31 00:00', periods = 5, freq = 'D', tz = 'US / Central')) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] sr.index = idx re
2 min read
Pandas Series dt.tz_convert | Change Timezone in DateTime Series The dt.tz_convert() method converts tz-aware DateTime Series object from one time zone to another. Example Python3 import pandas as pd sr = pd.Series(pd.date_range('2012-12-31 00:00', periods = 5, freq = 'D', tz = 'US / Central')) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] sr.index = idx re
2 min read
Python | Pandas DataFrame.tz_localize Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Arithmetic operations align on both row and column labels. It can be thought of as a dict-like container for Series objects. This is the primary data structure o
3 min read
Python | Pandas DatetimeIndex.tz_localize() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas DatetimeIndex.tz_localize() function localize tz-naive DatetimeIndex to tz-awar
2 min read
Pandas Series dt.normalize() | Normalize Time in Pandas Series When working with DateTime data in Pandas, sometimes the time doesn't matter and you just want to focus on the date. The dt.normalize() method in Pandas is used for this as it resets the time component of each DateTime entry to midnight (00:00:00) while leaving the date and time zone unchanged. For
2 min read
Pandas Series dt.normalize() | Normalize Time in Pandas Series When working with DateTime data in Pandas, sometimes the time doesn't matter and you just want to focus on the date. The dt.normalize() method in Pandas is used for this as it resets the time component of each DateTime entry to midnight (00:00:00) while leaving the date and time zone unchanged. For
2 min read