0
0
Pandasdata~10 mins

Timezone handling basics in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Timezone handling basics
Create naive datetime
Localize to timezone
Convert to another timezone
Use timezone-aware datetime for analysis
Start with a datetime without timezone, add timezone info, then convert to another timezone for correct time representation.
Execution Sample
Pandas
import pandas as pd
naive = pd.Timestamp('2024-06-01 12:00:00')
localized = naive.tz_localize('US/Eastern')
converted = localized.tz_convert('Europe/London')
print(converted)
Create a naive datetime, localize it to US/Eastern timezone, then convert it to Europe/London timezone.
Execution Table
StepVariableValue/ActionResult/Output
1naivepd.Timestamp('2024-06-01 12:00:00')2024-06-01 12:00:00 (naive)
2localizednaive.tz_localize('US/Eastern')2024-06-01 12:00:00-04:00 (US/Eastern)
3convertedlocalized.tz_convert('Europe/London')2024-06-01 17:00:00+01:00 (Europe/London)
4print(converted)Output the converted datetime2024-06-01 17:00:00+01:00
💡 All steps complete, datetime converted from naive to localized to another timezone.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
naiveNone2024-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)
localizedNoneNone2024-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)
convertedNoneNoneNone2024-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.