Finding the Weekday of a Date in Python

Dates And Time Mar 19, 2026

Finding the Weekday of a Date in Python

Determining the weekday (Monday, Tuesday, etc.) of a date is one of the most common operations in data science. It is used for creating day-of-week features, analyzing weekly seasonality, building business calendars, and generating insightful reports. In 2026, Python offers several clean and efficient ways to get the weekday of any date.

TL;DR — Recommended Methods

  • date.weekday() → 0 = Monday, 6 = Sunday
  • date.isoweekday() → 1 = Monday, 7 = Sunday
  • date.strftime("%A") → Full day name ("Monday")
  • pandas .dt.day_name() and .dt.day_of_week → vectorized for DataFrames

1. Pure Python – datetime.date

from datetime import date

d = date(2026, 3, 19)

print(d.weekday())          # 3 → Thursday (0=Monday)
print(d.isoweekday())       # 4 → Thursday (1=Monday)
print(d.strftime("%A"))     # "Thursday"
print(d.strftime("%a"))     # "Thu"

2. pandas – Vectorized Weekday Extraction

import pandas as pd

# Create a small demo file so this example runs as-is
# (in your project, sales_data.csv is your real export)
import numpy as np
rng = np.random.default_rng(42)
n = 60
pd.DataFrame({
    "order_id": range(1001, 1001 + n),
    "order_date": pd.date_range("2026-01-01", periods=n, freq="3D"),
    "customer_id": rng.integers(101, 121, n),
    "customer_name": rng.choice([" ana", "ben ", "chen", "dana"], n),
    "product_id": rng.integers(1, 9, n),
    "region": rng.choice(["North", "South", "East", "West"], n),
    "category": rng.choice(["Electronics", "Computers", "Books", "Home"], n),
    "payment_method": rng.choice(["card", "cash", "wallet"], n),
    "segment": rng.choice(["consumer", "business"], n),
    "quantity": rng.integers(1, 6, n),
    "amount": rng.uniform(50, 2500, n).round(2),
    "discount": rng.choice([0.0, 0.05, 0.1, 0.2], n),
}).assign(
    price=lambda d: (d["amount"] / d["quantity"]).round(2),
    profit=lambda d: (d["amount"] * rng.uniform(0.05, 0.3, n)).round(2),
).assign(
    ship_date=lambda d: d["order_date"] + pd.Timedelta(days=2),
    status=lambda d: np.where(d["amount"] > 2000, "priority", "standard"),
).to_csv("sales_data.csv", index=False)

df = pd.read_csv("sales_data.csv", parse_dates=["order_date"])

# Add weekday features
df["weekday_number"] = df["order_date"].dt.day_of_week          # 0 = Monday
df["day_name"] = df["order_date"].dt.day_name()             # "Thursday"
df["day_name_short"] = df["order_date"].dt.day_name().str[:3]
df["is_weekend"] = df["order_date"].dt.day_of_week.isin([5, 6])

print(df[["order_date", "day_name", "is_weekend"]].head())

3. Real-World Data Science Examples

# Example 1: Weekly seasonality analysis
weekly_sales = df.groupby(df["order_date"].dt.day_name())["amount"].mean()

# Example 2: Business-day vs weekend filtering
business_days = df[~df["order_date"].dt.day_of_week.isin([5, 6])]
weekend_sales = df[df["order_date"].dt.day_of_week.isin([5, 6])]

4. Best Practices in 2026

  • Use pandas .dt.day_name() and .dt.day_of_week for large DataFrames
  • Prefer .weekday() (0=Monday) for most calculations
  • Use strftime("%A") only when you need the full name as a string
  • Always work with timezone-aware datetimes before extracting weekday
  • Create is_weekend feature early in your pipeline

Weekday Numbering Conventions

Python offers two numbering systems. weekday() returns 0 for Monday through 6 for Sunday, while isoweekday() returns 1 for Monday through 7 for Sunday. pandas .dt.day_of_week follows the first convention.

Day names from strftime("%A") and .dt.day_name() depend on the language setting, so numbers are safer for logic and names are better for display.

Weekend flags are simply a check for 5 and 6 with the zero-based convention.

Worked Example: Weekend Flags and Ordered Day Names

We flag weekends and order a summary by weekday rather than alphabetically.

import pandas as pd

days = pd.Series(pd.date_range("2026-03-16", periods=7, freq="D"))
df = pd.DataFrame({"date": days, "orders": [12, 15, 11, 18, 22, 30, 25]})
df["dow"] = df["date"].dt.day_of_week
df["is_weekend"] = df["dow"] >= 5
df["day"] = df["date"].dt.day_name()

print(df[["day", "dow", "is_weekend"]].head(3))
summary = df.groupby(["dow", "day"])["orders"].sum().reset_index("dow", drop=True)
print(summary.index.tolist()[:3], int(df.loc[df["is_weekend"], "orders"].sum()))

16 March 2026 is a Monday, so the week starts at 0. Grouping by the number before the name keeps the days in calendar order, and the weekend orders add up to 55.

Practical Tips

For business-day logic, pandas offers bdate_range and the BDay offset, which skip weekends automatically. Custom holiday calendars can be added when national holidays also need to be excluded.

For reports read by people in different countries, prefer ISO dates such as 2026-03-16 over names of days and months, because numbers need no translation and sort correctly.

Include the weekday name only where it helps people plan, such as schedules and delivery dates.

Common Mistakes

  • Mixing weekday() and isoweekday() numbers.
  • Sorting day names alphabetically.
  • Relying on day names for logic.
  • Hard-coding locale-specific names.

Quick Summary

  • weekday() starts at 0 for Monday.
  • isoweekday() starts at 1.
  • Use numbers for logic, names for display.
  • Group by number to keep calendar order.

Conclusion

Finding the weekday of a date is simple yet extremely valuable in data science. In 2026, combine Python’s date.weekday() / strftime() with pandas’ vectorized .dt accessor to create powerful day-of-week features, analyze weekly patterns, and build smarter time-based models.

Next steps:

  • Add weekday and weekend features to one of your current datasets using the patterns above

Related Tutorials

Share:
Last updated: March 2026
Built-in Functions in Python 2026 str() in Python 2026: String Conversion + Modern Formatting & Best Practices memoryview with JAX in Python 2026: Zero-Copy NumPy → JAX Array Interop + Efficient ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + GPU Pinning & ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + ML Examples memoryview with NumPy & PyTorch in Python 2026: Zero-Copy Views, Efficient Slicing & ML Interop Examples memoryview with NumPy in Python 2026: Zero-Copy Views, Efficient Slicing & Real ML Examples memoryview() in Python 2026: Zero-Copy Magic for Large Binary Data + Real Examples import() in Python 2026: How It Works, Security Risks & Modern Alternatives (importlib) zip() in Python 2026: How to Use It, strict=True Behavior & Real-World Examples vars() in Python 2026: Accessing Object Namespace + Modern Introspection Patterns type() in Python 2026: Dynamic Type Inspection & Object Creation + Modern Patterns tuple() in Python 2026: Immutable Sequences + Modern Patterns & Best Practices super() in Python 2026: Method Resolution & Modern Inheritance Patterns sum() in Python 2026: Summing Iterables + Modern Numeric Patterns & Best Practices staticmethod() in Python 2026: Static Methods, Utility Functions & Modern Best Practices sorted() in Python 2026: Sorting Iterables + Modern Patterns & Best Practices slice() in Python 2026: Creating Slice Objects + Modern Patterns & Best Practices setattr() in Python 2026: Dynamic Attribute Setting + Modern Patterns & Safety set() in Python 2026: Mutable Sets Creation + Modern Patterns & Best Practices