Extract Year, Month, and Weekday
Using the dt accessor for features.
Extract Year, Month, and Weekday is a free Data Science Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
A Timestamp Holds a Lot
One datetime value packs a year, month, day, hour, and weekday inside it. The skill is pulling each piece out as its own column for analysis. 🧩
Meet the dt Accessor
On a datetime column, the dt accessor is your gateway. It exposes every date part as a simple attribute you can read or assign.
df["date"].dt # gateway to date partsPull Out the Year
Grabbing the year is one line. It is perfect for grouping sales or events into annual buckets.
df["year"] = df["date"].dt.yearMonth as a Number
The month attribute returns 1 through 12. Use it to compare the same month across different years.
df["month"] = df["date"].dt.monthMonth by Name
Numbers are fine for sorting, but charts read better with names. Use month_name to get January, February, and friends.
df["date"].dt.month_name() # 'March'Day of the Week
The dayofweek attribute numbers days from Monday=0 to Sunday=6. It is the backbone of any weekday analysis.
df["dow"] = df["date"].dt.dayofweekWeekday by Name
Want readable labels instead? Use day_name to turn each date into Monday, Tuesday, and so on for clean grouping.
df["date"].dt.day_name() # 'Friday'Spot the Weekend
Combine dayofweek with a comparison to flag weekends. This boolean column instantly splits work days from rest days.
df["weekend"] = df["date"].dt.dayofweek >= 5Quarter and Day of Year
Two more handy parts: quarter for Q1 to Q4 reporting, and dayofyear for tracking position within a single year.
df["date"].dt.quarter
df["date"].dt.dayofyearHours and Minutes Too
If your data has time, the dt accessor also exposes hour and minute. That makes it easy to study peak activity windows.
df["hour"] = df["date"].dt.hourFeatures Power Analysis
Each extracted part becomes a feature you can group by or feed to a model. This is how raw timestamps turn into insight.
df.groupby(df["date"].dt.month).size()Quick Check
You want to flag rows that fall on Saturday or Sunday.
Recap: Unpack Any Date
You used the dt accessor to pull year, month, weekday, quarter, and hour from timestamps, turning each into a feature ready for grouping. 🗓️
Frequently asked questions
Is the “Extract Year, Month, and Weekday” lesson free?
Yes — the full text of “Extract Year, Month, and Weekday” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.
What will I learn in “Extract Year, Month, and Weekday”?
Using the dt accessor for features. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Data Science Academy?
No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Extract Year, Month, and Weekday” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Data Science Academy lesson?
Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Parse Strings Into Datetimes
- Extract Year, Month, and Weekday
- Resample to Daily or Monthly
- Time Zones and Date Ranges