Time series data is everywhere in stock prices, weather data,server logs, sales records ,website traffic per hour,machine logs per second. Pandas has tools for parsing,indexing,resampling and analyzing datasets.
Note - The .dt accessor works for period and timedelta dtypes.
1.Converting to Datetime.
The first process convert the date column to date time before doing the time series analysis.
df['date'] = pd.to_datetime(df['date'])
2.Extracting day from a column date
a.Having the output as 0,1,2,3...
df['day_of_week_num'] = df['date'].dt.weekday
- df['day_of_week_num']: It extracts the numeric day of the week, where Monday is 0 and Sunday is 6.
b. Having the output as name Monday,tuesday,Wednesday,thursday
df['day_of_week_name'] = df['date'].dt.day_name()
- It returns the name of the day for each date in the Series or DataFrame, such as "Monday", "Tuesday"
c. Abbreviated day name (e.g., "Mon", "Tue", "Wed")
df['day_of_week_abbr'] = df['date'].dt.strftime('%a')
- This gives you a short form for the day name, which is often used when you want to save space or display compact labels.
3. Check for Weekend.
df['is_weekend'] = df['date'].dt.weekday >= 5
- This create a new column in your DataFrame that flags whether a date falls on a weekend (Saturday or Sunday).
4. Extracting time attributes .
Here we will extract Hours,Minutes and seconds.
.dt.hour: - This Extracts the hour from the datetime (0β23).
.dt.minute: - It Extracts the minute from the datetime (0β59)
.dt.second: It Extracts the second from the datetime (0β59).
5. Extract Month (Number and Name).
df['month'] = df['date'].dt.month
- Extracts the numeric month (1β12) from the date column.
df['month_name'] = df['date'].dt.month_name()
- Extracts the full month name (e.g., "January", "February").
C. Three-letter abbreviation
df['month_abbr'] = df['month_name'].str[:3]
6.To Extract the quarter of the year from a datetime column use .dt.quarter
df['quarter'] = df['date'].dt.quarter - 1
7.Half of the Year (0 = JanβJun, 1 = JulβDec)
df['half'] = np.where(df['month'] < 6, 0, 1)
It assumes your month column is zero-based (i.e., 0 = January, 11 = December).
- Months 0β5 β First Half (0)
- Months 6β11 β Second Half (1)
8 . Week of the Year (.dt.isocalendar().week)
df['week_of_year'] = df['date'].dt.isocalendar().week
Note: As of Pandas 1.1.0, .dt.week is deprecated and replaced by .dt.isocalendar().week.
Note.What is ISO Week?
- ISO weeks follow the ISO 8601 standard:
- Weeks start on Monday
- Week 1 is the week that contains the first Thursday of the year
- Can have 52 or 53 weeks.
9. Year.
df['year']=df['date'].dt.year
- Extracts the year as an interger.
- It's useful for grouping,filtering or feature engineering.
- Splits time series data by year.
- Groups data for annual trends.
- Creates Pivot tables or plots year-wise
date | year |
---|---|
2022-04-05 | 2022 |
2023-11-12 | 2023 |
2024-01-01 | 2024 |
10. π Day of the month.
- This extracts the day of the month (1-31)
df['day_of_month'=df['date'].dt.day
date | day |
---|---|
2024-01-05 | 5 |
2024-01-15 | 15 |
2024-01-31 | 31 |
11. How to handle holidays in pandas
You have to manually define your own list of public holidays and pass them to customBusinessDay.
from pandas.tseries.offsets import CustomBusinessDay
Kenya_holidays=pd.to_datetime([
'2025-01-01', # New Year's Day
'2025-03-29', # Good Friday
'2025-05-01', # Labour Day
'2025-06-01', # Madaraka Day
'2025-10-20', # Mashujaa Day
'2025-12-12', # Jamhuri Day
'2025-12-25', # Christmas
'2025-12-26', # Boxing Day
])
- Create a custom businessDay (Holidays=kenya_holidays)
kenya_bday=CustomBusinessDay(holidays=kenya_holidays)
- used to generate business days
dates=pd.date_range('2025-12-20','2025-12-31' freq=kenya_bday)
12. dt.period is an accessor in pandas used with datetime-like columns to convert them into period objects.
a.Calendar Frequencies.
- This is an example of calendar frequency. df['period'] = df['date'].dt.to_period('M')
Frequency Code | Description | Example Output |
---|---|---|
'D' |
Daily | 2023-05-11 |
'W' |
Weekly | 2023-05-08/2023-05-14 |
'M' |
Month End | 2023-05 |
'MS' |
Month Start | 2023-05 |
'Q' |
Quarter End | 2023Q2 |
'QS' |
Quarter Start | 2023Q2 |
'A' or 'Y'
|
Year End | 2023 |
'AS' or 'YS'
|
Year Start | 2023 |
b.Business Frequencies.
- They are special time intervals that consider only working days(Monday to Friday) and can exclude holidays if well defined.
- Finance - No trading on weekends or holidays.
- Payroll/HR - Payments and attendance only on working days.
- Operations - Delivery and logistics exclude weekends. The key difference from calendar frequencies include.
- Business Day Frequencies
- Business Month Frequencies.
Get last business day of each month in 2024
bm_end = pd.date_range(start='2024-01-01', end='2024-12-31', freq='BM')
print(bm_end)
Output >>> DatetimeIndex(['2024-01-31', '2024-02-29', '2024-03-29', '2024-04-30', ...], dtype='datetime64[ns]', freq='BM')
-Business Quarter Frequencies.
- Business Year Frequencies.
Top comments (0)