Convert Datetime Object To Local Time Zone In Pandas
Last Updated :
08 Feb, 2024
Pandas has established itself as one of the most powerful and versatile libraries in Python. When working with time series data, handling datetime objects efficiently becomes paramount. Oftentimes, datasets contain timestamps in various time zones, necessitating conversion to a consistent reference point, typically the local time zone
Convert Datetime Object To Local Time Zone
Importing necessary Libraries
Python3
import pandas as pd
from datetime import datetime
import pytz
Create Sample Data
We can convert a datetime object to the local time zone using the datetime
and pytz
modules
Python3
# Create a sample DataFrame with datetime columns
data = {
'timestamp_utc': [
'2024-02-07 08:00:00',
'2024-02-07 12:00:00',
'2024-02-07 16:00:00'
]
}
df = pd.DataFrame(data)
df['timestamp_utc'] = pd.to_datetime(df['timestamp_utc'])
# Display the DataFrame
print("DataFrame with UTC timestamps:")
print(df)
Output:
DataFrame with UTC timestamps:
timestamp_utc
0 2024-02-07 08:00:00
1 2024-02-07 12:00:00
2 2024-02-07 16:00:00
1. Converting to Local Time Zone using pytz.timezone()
The local time zone 'Asia/Kolkata' is assigned using the pytz.timezone()
function, representing the Indian Standard Time (IST),
Python3
# Convert UTC timestamps to local time zone
local_timezone = pytz.timezone('America/New_York') # Example: Convert to New York time zone
df['timestamp_local'] = df['timestamp_utc'].dt.tz_localize('UTC').dt.tz_convert(local_timezone)
# Display the DataFrame with local time zone
print("\nDataFrame with local time zone:")
print(df)
Output:
DataFrame with local time zone:
timestamp_utc timestamp_local
0 2024-02-07 08:00:00 2024-02-07 03:00:00-05:00
1 2024-02-07 12:00:00 2024-02-07 07:00:00-05:00
2 2024-02-07 16:00:00 2024-02-07 11:00:00-05:00
2. Converting to Local Time Zone using tz_convert()
We will implement conversion to Local Time using tz_convert().
Python3
# Localize UTC timestamps to UTC time zone
df['timestamp_local'] = df['timestamp_utc'].dt.tz_localize('UTC')
# Convert localized timestamps to local time zone (Asia/Kolkata)
df['timestamp_local'] = df['timestamp_local'].dt.tz_convert('Asia/Kolkata')
# Display the DataFrame with local time zone
print("\nDataFrame with local time zone (Asia/Kolkata):")
print(df)
Output:
DataFrame with local time zone (Asia/Kolkata):
timestamp_utc timestamp_local
0 2024-02-07 08:00:00 2024-02-07 13:30:00+05:30
1 2024-02-07 12:00:00 2024-02-07 17:30:00+05:30
2 2024-02-07 16:00:00 2024-02-07 21:30:00+05:30
Similar Reads
Pandas Series dt.tz_localize() | Convert tz-Naive DateTime to tz-Aware The dt.tz_localize() method converts the time zone (tz)-naive Datetime Series object into a tz-aware Datetime Series. It does not move the time to another time zone. Example: Python3 import pandas as pd sr = pd.Series(['2012-12-31', '2019-1-1 12:30', '2008-02-2 10:30', '2010-1-1 09:25', '2019-12-31
2 min read
Convert Datetime Object To Seconds, Minutes & Hours Using pandas We can convert a datetime object to seconds, minutes, and hours using pandas in Python by accessing the attributes of the datetime object and performing the necessary calculations. Convert Datetime Object To Seconds, Minutes & Hours Using pandasConvert a datetime Object to SecondsTo retrieve the
1 min read
How to Convert Datetime to Date in Pandas ? DateTime is a collection of dates and times in the format of "yyyy-mm-dd HH:MM:SS" where yyyy-mm-dd is referred to as the date and HH:MM:SS is referred to as Time. In this article, we are going to discuss converting DateTime to date in pandas. For that, we will extract the only date from DateTime us
4 min read
How to Change Pandas Dataframe Datetime to Time The DatetimeIndex contains datetime64[ns] data type, which represents timestamps with nanosecond precision. In many cases, we may just want to extract the time component from a Pandas Datetime column or index. Let's discuss easy ways to convert the Datetime to Time data while preserving all the time
2 min read
Convert string to datetime in Python with timezone Converting a string to a datetime in Python with timezone means parsing a date-time string and creating a timezone-aware datetime object. For example, a string like '2021-09-01 15:27:05.004573 +0530' can be converted to a Python datetime object that accurately represents the date, time and timezone.
2 min read