0
0
Pandasdata~10 mins

Timezone handling basics in Pandas - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to convert a naive datetime to UTC timezone.

Pandas
import pandas as pd
naive_dt = pd.Timestamp('2024-01-01 12:00:00')
aware_dt = naive_dt.tz_localize([1])
print(aware_dt)
Drag options to blanks, or click blank then click option'
A'UTC'
B'local'
C'GMT+5'
D'EST'
Attempts:
3 left
💡 Hint
Common Mistakes
Using tz_localize with a non-standard string like 'local' causes errors.
Confusing tz_localize with tz_convert.
2fill in blank
medium

Complete the code to convert a datetime from UTC to US/Eastern timezone.

Pandas
import pandas as pd
utc_dt = pd.Timestamp('2024-01-01 12:00:00', tz='UTC')
est_dt = utc_dt.tz_convert([1])
print(est_dt)
Drag options to blanks, or click blank then click option'
A'UTC'
B'Europe/London'
C'US/Eastern'
D'Asia/Tokyo'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to tz_convert a naive datetime causes errors.
Using tz_localize instead of tz_convert on aware datetime.
3fill in blank
hard

Fix the error in the code to localize a naive datetime to 'Europe/London'.

Pandas
import pandas as pd
naive_dt = pd.Timestamp('2024-06-01 15:00:00')
aware_dt = naive_dt.[1]('Europe/London')
print(aware_dt)
Drag options to blanks, or click blank then click option'
Atz_convert
Btz_set
Ctz_change
Dtz_localize
Attempts:
3 left
💡 Hint
Common Mistakes
Using tz_convert on naive datetime causes AttributeError.
Using non-existent methods like tz_set or tz_change.
4fill in blank
hard

Fill both blanks to create a dictionary of datetimes localized to UTC and converted to Asia/Tokyo.

Pandas
import pandas as pd
naive_dt = pd.Timestamp('2024-03-10 08:00:00')
localized = naive_dt.[1]('UTC')
tokyo_dt = localized.[2]('Asia/Tokyo')
result = {'UTC': localized, 'Tokyo': tokyo_dt}
print(result)
Drag options to blanks, or click blank then click option'
Atz_localize
Btz_convert
Attempts:
3 left
💡 Hint
Common Mistakes
Using tz_convert on naive datetime causes errors.
Swapping the order of localization and conversion.
5fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps timezone names to converted datetimes if the name length is greater than 5.

Pandas
import pandas as pd
naive_dt = pd.Timestamp('2024-01-01 12:00:00')
tz_names = ['UTC', 'US/Eastern', 'Europe/London', 'Asia/Tokyo']
localized = naive_dt.tz_localize('UTC')
dt_dict = {name: localized.tz_convert(name) for name in tz_names if len(name) [1] [2] }
print(dt_dict)
Drag options to blanks, or click blank then click option'
A>
B5
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '==' instead of '>' causes wrong filtering.
Incorrect operator or value in the condition.