Consider the following code that creates a timezone-naive datetime and then localizes it to 'UTC'. What will be the output?
import pandas as pd naive_dt = pd.Timestamp('2024-01-01 12:00:00') localized_dt = naive_dt.tz_localize('UTC') print(localized_dt)
Think about what tz_localize does to a naive datetime.
The tz_localize('UTC') method assigns the UTC timezone to the naive datetime without changing the clock time, so the output shows the original time with +00:00 offset.
Given a timezone-aware datetime in UTC, what will be the result after converting it to US/Eastern timezone?
import pandas as pd utc_dt = pd.Timestamp('2024-06-01 15:00:00', tz='UTC') est_dt = utc_dt.tz_convert('US/Eastern') print(est_dt)
Remember that US/Eastern is 4 hours behind UTC during daylight saving time in June.
The tz_convert changes the timezone while adjusting the clock time. Since June is daylight saving time, US/Eastern is UTC-4, so 15:00 UTC becomes 11:00 US/Eastern.
Examine the code below. Why does it raise an error?
import pandas as pd dt = pd.Timestamp('2024-01-01 12:00:00', tz='UTC') dt_localized = dt.tz_localize('US/Eastern')
Check the difference between tz_localize and tz_convert.
tz_localize assigns a timezone to a naive datetime. If the datetime is already timezone-aware, it raises an error. To change timezone, use tz_convert.
You have hourly data timestamps in UTC. After converting to Asia/Tokyo timezone, which plot correctly shows the shifted time on the x-axis?
import pandas as pd import matplotlib.pyplot as plt idx = pd.date_range('2024-01-01 00:00', periods=3, freq='H', tz='UTC') data = [1, 2, 3] df = pd.DataFrame({'value': data}, index=idx) df_tokyo = df.tz_convert('Asia/Tokyo') df_tokyo.plot() plt.show()
Asia/Tokyo is UTC+9 hours. Check how the timestamps shift.
Converting from UTC to Asia/Tokyo adds 9 hours, so midnight UTC becomes 9 AM Tokyo time.
Given a DataFrame with timestamps localized to UTC, then converted to Europe/London timezone, how many unique timestamps remain?
import pandas as pd idx = pd.to_datetime(['2024-03-31 00:30', '2024-03-31 01:30', '2024-03-31 02:30']) idx_utc = idx.tz_localize('UTC') idx_london = idx_utc.tz_convert('Europe/London') df = pd.DataFrame({'value': [10, 20, 30]}, index=idx_london) unique_count = df.index.nunique() print(unique_count)
Consider daylight saving time change on 2024-03-31 in Europe/London.
On 2024-03-31, Europe/London moves clocks forward at 1 AM UTC, but the given UTC times convert to distinct local times, so all timestamps remain unique.