pythonintermediate
Timezone-Aware Timestamps in pandas
Convert naive timestamps to timezone-aware, handle DST transitions, and localise to UTC.
pythonPress ⌘/Ctrl + Shift + C to copy
import pandas as pd
df = pd.DataFrame({'ts_utc': pd.to_datetime(['2024-03-09 06:00','2024-11-03 07:00']).tz_localize('UTC')})
df['ts_ny'] = df['ts_utc'].dt.tz_convert('America/New_York')
df['ts_lon'] = df['ts_utc'].dt.tz_convert('Europe/London')
df['ts_naive']= df['ts_utc'].dt.tz_localize(None)
print(df[['ts_utc','ts_ny','ts_lon']])Use Cases
- global event logs
- DST handling
- multi-timezone analytics
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
pythonintermediate
Pandas Business Day Offsets
Compute business-day-adjusted dates using pandas offsets for financial and SLA calculations.
Best for: financial calendars
#pandas#datetime
pythonbeginner
Pandas Datetime Component Extraction
Extract year, month, day, hour, day-of-week and other components from a datetime column.
Best for: time-based features
#pandas#datetime
pythonbeginner
Pandas DataFrame Transformations
Common pandas DataFrame transformations including column operations, type casting, and string methods.
Best for: Cleaning raw data files for analysis
#pandas#dataframe
pythonbeginner
Pandas DataFrame Filtering Techniques
Filter DataFrames using boolean masks, query syntax, isin, between, and string matching methods.
Best for: Extracting subsets of data for reporting
#pandas#filtering