Create a naive datetime, localize it to US/Eastern timezone, then convert it to Europe/London timezone.
Execution Table
Step
Variable
Value/Action
Result/Output
1
naive
pd.Timestamp('2024-06-01 12:00:00')
2024-06-01 12:00:00 (naive)
2
localized
naive.tz_localize('US/Eastern')
2024-06-01 12:00:00-04:00 (US/Eastern)
3
converted
localized.tz_convert('Europe/London')
2024-06-01 17:00:00+01:00 (Europe/London)
4
print(converted)
Output the converted datetime
2024-06-01 17:00:00+01:00
💡 All steps complete, datetime converted from naive to localized to another timezone.
Variable Tracker
Variable
Start
After Step 1
After Step 2
After Step 3
Final
naive
None
2024-06-01 12:00:00 (naive)
2024-06-01 12:00:00 (naive)
2024-06-01 12:00:00 (naive)
2024-06-01 12:00:00 (naive)
localized
None
None
2024-06-01 12:00:00-04:00 (US/Eastern)
2024-06-01 12:00:00-04:00 (US/Eastern)
2024-06-01 12:00:00-04:00 (US/Eastern)
converted
None
None
None
2024-06-01 17:00:00+01:00 (Europe/London)
2024-06-01 17:00:00+01:00 (Europe/London)
Key Moments - 3 Insights
Why do we need to localize a naive datetime before converting timezones?
Because a naive datetime has no timezone info, tz_localize assigns the correct timezone so pandas knows how to convert it properly, as shown in step 2 of the execution_table.
What happens if we try to convert a naive datetime directly without localizing?
Pandas will raise an error because it cannot convert timezones without knowing the original timezone. This is why step 2 (localize) must happen before step 3 (convert).
Why does the time change from 12:00 to 17:00 when converting from US/Eastern to Europe/London?
Because Europe/London is 5 hours ahead of US/Eastern at that date, so the time shifts accordingly, as shown in step 3 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the timezone of the 'localized' variable after step 2?
AUS/Eastern
BEurope/London
CUTC
DNo timezone (naive)
💡 Hint
Check the 'Value/Action' and 'Result/Output' columns for step 2 in the execution_table.
At which step does the datetime become timezone-aware?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Refer to the 'Result/Output' column in the execution_table where timezone info first appears.
If we skip tz_localize and try to convert a naive datetime, what will happen?
AIt will convert correctly
BIt will raise an error
CIt will ignore timezone and keep time same
DIt will localize automatically
💡 Hint
Recall the key moment about converting naive datetime without localization.
Concept Snapshot
Timezone handling in pandas:
- Start with naive datetime (no timezone).
- Use tz_localize() to assign a timezone.
- Use tz_convert() to change to another timezone.
- Time shifts according to timezone differences.
- Always localize before converting.
Full Transcript
This example shows how to handle timezones in pandas. We start with a naive datetime without timezone info. Then we localize it to US/Eastern timezone using tz_localize. After that, we convert it to Europe/London timezone using tz_convert. The time changes from 12:00 to 17:00 because London is 5 hours ahead of Eastern time. This process ensures correct timezone-aware datetime handling for analysis.