Status
Done
TABLE OF CONTENT
Time Series Handling
1. DateTime Conversion
df['datetime_series'].to_period(freq) → Convert to Period (e.g., 'M' for monthly)df['period_series'].to_timestamp() → Convert Period back to Timestamp
2. Date Component Accessors (via .dt)
- Basic Components:
.dt.year → Extract year.dt.month → Extract month (1-12).dt.day → Extract day (1-31).dt.hour → Extract hour (0-23).dt.minute → Extract minute (0-59).dt.second → Extract second (0-59)- Advanced Components:
.dt.dayofweek → Day of week (0=Monday, alias:Â.weekday).dt.dayofyear → Day of year (1-366).dt.quarter → Quarter (1-4).dt.is_month_end → Bool for month-end dates.dt.is_quarter_end → Bool for quarter-end dates.dt.is_year_end → Bool for year-end dates
3. DateTime Formatting & Rounding
.dt.strftime(format) → Format as string (e.g., "%Y-%m-%d").dt.round(freq) → Round to nearest frequency (e.g., 'H' for hour).dt.floor(freq) → Round down to frequency.dt.ceil(freq) → Round up to frequency.dt.normalize() → Set time to midnight (00:00:00)
4. TimeDelta Operations (for timedelta Series)
.dt.total_seconds() → Convert entire duration to seconds
Example Usage:
# Create datetime Series
dates = pd.Series(pd.date_range('2023-01-01', periods=3, freq='M'))
# Access components
dates.dt.month # → [1, 2, 3]
dates.dt.is_quarter_end # → [False, False, True] (March end)
# Formatting
dates.dt.strftime("%Y-%m") # → ['2023-01', '2023-02', '2023-03']
# Rounding
pd.Series([pd.Timestamp('2023-01-01 14:30:45')]).dt.round('H')
# → 2023-01-01 15:00:00Key Notes:
- TheÂ
.dt accessor only works with datetime-like Series - For timezone handling, use:
tz_localize()Â to assign timezonestz_convert()Â to convert timezones- Frequency strings (for rounding/periods):
- 'D' = day, 'M' = month, 'Q' = quarter
- 'H' = hour, 'T'/'min' = minute, 'S' = second
Common Use Cases:
- Extracting month/year for grouping
- Flagging period-end dates
- Creating formatted date strings for reports
- Normalizing timestamps to compare dates without times
Time Series Handling in Pandas Series
‣
1. DateTime Conversion Fundamentals
‣
2. Date Component Extraction
‣
3. DateTime Formatting & Rounding
‣
4. TimeDelta Operations
‣
5. Timezone Handling
‣
Practical Applications
‣
Performance Optimization
‣
Common Pitfalls & Solutions
‣