Python | Pandas Series.tz_localize - GeeksforGeeks

30 Jan, 2019
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 series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.
Pandas Series.tz_localize() function is used to localize tz-naive index of a Series or DataFrame to target time zone. This operation localizes the Index. In order to localize the values in a timezone-naive Series, we can use Series.dt.tz_localize().
Syntax: Series.tz_localize(tz, axis=0, level=None, copy=True, ambiguous=’raise’, nonexistent=’raise’)
Parameter :tz : string or pytz.timezone objectaxis : the axis to localizelevel : If axis ia a MultiIndex, localize a specific level. Otherwise must be Nonecopy : Also make a copy of the underlying dataambiguous : ‘infer’, bool-ndarray, ‘NaT’, default ‘raise’nonexistent : str, default ‘raise’
Returns : Series or DataFrame
Example #1: Use Series.tz_localize() function to localize the time zone naive index of the given Series to the target time zone.
# importing pandas as pdimport pandas as pd  # Creating the Seriessr = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio', 'Moscow'])  # Create the Datetime Indexdidx = pd.DatetimeIndex(start ='2014-08-01 10:00', freq ='W',                                                  periods = 6)   # set the indexsr.index = didx  # Print the seriesprint(sr)
Output :
Now we will use Series.tz_localize() function to localize the given time zone naive index to time zone aware index. The target time zone is ‘US/Central’.
# Localize to 'US / Central'sr.tz_localize('US/Central')
Output :As we can see in the output, the Series.tz_localize() function has converted the given naive time zone index to a time aware index. Example #2: Use Series.tz_localize() function to localize the time zone naive index of the given Series to the target time zone.
# importing pandas as pdimport pandas as pd  # Creating the Seriessr = pd.Series([19.5, 16.8, 22.78, 20.124, 18.1002])  # Create the Datetime Indexdidx = pd.DatetimeIndex(start ='2014-08-01 10:00', freq ='W',                                                  periods = 5)   # set the indexsr.index = didx  # Print the seriesprint(sr)
Output :
Now we will use Series.tz_localize() function to localize the given time zone naive index to time zone aware index. The target time zone is ‘Asia/Calcutta’.
# Localize to 'Asia/Calcutta'sr.tz_localize('Asia/Calcutta')
Output :
Python pandas-series
Python pandas-series-methods
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()