DEV Community

Cover image for πŸ“ Mastering Date/Time Extraction in Pandas: From Year to Weekend Check.
Kaira Kelvin.
Kaira Kelvin.

Posted on

2 1

πŸ“ Mastering Date/Time Extraction in Pandas: From Year to Weekend Check.

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'])
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Image description

  • 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()
Enter fullscreen mode Exit fullscreen mode
  • 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')

Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • Extracts the numeric month (1–12) from the date column.
df['month_name'] = df['date'].dt.month_name() 
Enter fullscreen mode Exit fullscreen mode
  • Extracts the full month name (e.g., "January", "February").

C. Three-letter abbreviation

df['month_abbr'] = df['month_name'].str[:3]
Enter fullscreen mode Exit fullscreen mode

6.To Extract the quarter of the year from a datetime column use .dt.quarter

 df['quarter'] = df['date'].dt.quarter - 1
Enter fullscreen mode Exit fullscreen mode

Image description

7.Half of the Year (0 = Jan–Jun, 1 = Jul–Dec)

df['half'] = np.where(df['month'] < 6, 0, 1)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
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
])
Enter fullscreen mode Exit fullscreen mode
  • Create a custom businessDay (Holidays=kenya_holidays)
kenya_bday=CustomBusinessDay(holidays=kenya_holidays)
Enter fullscreen mode Exit fullscreen mode
  • used to generate business days
dates=pd.date_range('2025-12-20','2025-12-31' freq=kenya_bday)
Enter fullscreen mode Exit fullscreen mode

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.

Image description.

  • Business Day Frequencies

Image description

  • Business Month Frequencies.

Image description

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)
Enter fullscreen mode Exit fullscreen mode

Output >>> DatetimeIndex(['2024-01-31', '2024-02-29', '2024-03-29', '2024-04-30', ...], dtype='datetime64[ns]', freq='BM')

-Business Quarter Frequencies.

Image description

  • Business Year Frequencies.

Image description

tutorial image

Next.js Tutorial 2025 - Build a Full Stack Social App

In this 4-hour hands-on tutorial, Codesistency walks you through the process of building a social platform from scratch with Next.js (App Router), React, Prisma ORM, Clerk for authentication, Neon for PostgreSQL hosting, Tailwind CSS, Shadcn UI, and UploadThing for image uploads.

Watch the video β†’

Top comments (0)

DevCycle image

Ship Faster, Stay Flexible.

DevCycle is the first feature flag platform with OpenFeature built-in to every open source SDK, designed to help developers ship faster while avoiding vendor-lock in.

Start shipping