0
0
Pandasdata~20 mins

Timezone handling basics in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Timezone Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this timezone conversion code?

Consider the following code that creates a timezone-naive datetime and then localizes it to 'UTC'. What will be the output?

Pandas
import pandas as pd
naive_dt = pd.Timestamp('2024-01-01 12:00:00')
localized_dt = naive_dt.tz_localize('UTC')
print(localized_dt)
A2024-01-01 12:00:00+00:00
B2024-01-01 12:00:00
C2024-01-01 07:00:00-05:00
DError: Cannot localize already tz-aware datetime
Attempts:
2 left
💡 Hint

Think about what tz_localize does to a naive datetime.

data_output
intermediate
2:00remaining
What is the result of converting timezone from UTC to US/Eastern?

Given a timezone-aware datetime in UTC, what will be the result after converting it to US/Eastern timezone?

Pandas
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)
A2024-06-01 10:00:00-05:00
B2024-06-01 15:00:00-04:00
C2024-06-01 11:00:00-04:00
D2024-06-01 15:00:00+00:00
Attempts:
2 left
💡 Hint

Remember that US/Eastern is 4 hours behind UTC during daylight saving time in June.

🔧 Debug
advanced
2:00remaining
Why does this code raise an error when localizing a timezone-aware datetime?

Examine the code below. Why does it raise an error?

Pandas
import pandas as pd
dt = pd.Timestamp('2024-01-01 12:00:00', tz='UTC')
dt_localized = dt.tz_localize('US/Eastern')
ABecause <code>tz_localize</code> cannot be used on timezone-aware datetimes
BBecause <code>tz_localize</code> requires a naive datetime with no timezone
CBecause the datetime string format is incorrect
DBecause 'US/Eastern' is not a valid timezone
Attempts:
2 left
💡 Hint

Check the difference between tz_localize and tz_convert.

visualization
advanced
3:00remaining
Which plot correctly shows hourly data converted from UTC to Asia/Tokyo timezone?

You have hourly data timestamps in UTC. After converting to Asia/Tokyo timezone, which plot correctly shows the shifted time on the x-axis?

Pandas
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()
APlot with x-axis labels: 2023-12-31 15:00, 16:00, 17:00
BPlot with x-axis labels: 2024-01-01 00:00, 01:00, 02:00
CPlot with x-axis labels: 2024-01-01 03:00, 04:00, 05:00
DPlot with x-axis labels: 2024-01-01 09:00, 10:00, 11:00
Attempts:
2 left
💡 Hint

Asia/Tokyo is UTC+9 hours. Check how the timestamps shift.

🚀 Application
expert
3:00remaining
How many unique timezone-aware timestamps are in this DataFrame after conversion?

Given a DataFrame with timestamps localized to UTC, then converted to Europe/London timezone, how many unique timestamps remain?

Pandas
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)
A1
B3
C2
DRaises an error due to ambiguous times
Attempts:
2 left
💡 Hint

Consider daylight saving time change on 2024-03-31 in Europe/London.