Add an integer day-of-week column to a pandas DataFrame

Quick Overview

Write a pandas function that adds an integer day-of-week column, with Monday as 0, to a copy of a DataFrame. Tests the .dt datetime accessor, leaving the input unchanged, integer dtypes, and how missing dates affect the result.

Add an integer day-of-week column to a pandas DataFrame

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `add_day_of_week(df: pd.DataFrame) -> pd.DataFrame` that takes a DataFrame with a `datetime64[ns]` column `'date'` and returns a new DataFrame with an added integer column `'dow'` (Monday=0 ... Sunday=6). The function must: - Compute `'dow'` from `df['date']` with the vectorized `.dt` accessor. - Keep all original columns and the index. - Return a copy and leave the input `df` unchanged. - Give `'dow'` an integer dtype. ```python df = pd.DataFrame({'date': pd.to_datetime(['2026-01-05', '2026-01-11'])}) out = add_day_of_week(df) # out['dow'] -> [0, 6] # Monday, Sunday ``` ```hint Numbering conventions Libraries disagree on day-of-week numbering: some start the week on Sunday and some count from 1. Check which `.dt` attribute uses Monday=0, and what dtype it returns. ``` ### What a Strong Answer Covers - Copying the frame before adding the column, so the caller's DataFrame is unchanged. - The `.dt` attribute that numbers Monday as 0 and Sunday as 6, applied to the whole column at once. - How the index and the original columns come through unchanged. - An explicit integer dtype, and what missing dates (`NaT`) do to it. ### Follow-up Questions - If `'date'` can contain `NaT`, what does the accessor return, and how would you keep `'dow'` an integer column? - The timestamps are stored in UTC, but users live in different time zones. How does that change the day of week you report? - How would you add the weekday name or a weekend flag, and make a groupby by weekday come out in calendar order?

Overview: Write a pandas function that adds an integer day-of-week column, with Monday as 0, to a copy of a DataFrame. Tests the .dt datetime accessor, leaving the input unchanged, integer dtypes, and how missing dates affect the result.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Write add_day_of_week(df: pd.DataFrame) -> pd.DataFrame that takes a DataFrame with a datetime64[ns] column 'date' and returns a new DataFrame with an added integer column 'dow' (Monday=0 ... Sunday=6).

The function must:

  • Compute 'dow' from df['date'] with the vectorized .dt accessor.
  • Keep all original columns and the index.
  • Return a copy and leave the input df unchanged.
  • Give 'dow' an integer dtype.
df = pd.DataFrame({'date': pd.to_datetime(['2026-01-05', '2026-01-11'])})
out = add_day_of_week(df)
# out['dow'] -> [0, 6]  # Monday, Sunday

What a Strong Answer Covers Guidance

  • Copying the frame before adding the column, so the caller's DataFrame is unchanged.
  • The .dt attribute that numbers Monday as 0 and Sunday as 6, applied to the whole column at once.
  • How the index and the original columns come through unchanged.
  • An explicit integer dtype, and what missing dates ( NaT ) do to it.

Follow-up Questions Guidance

  • If 'date' can contain NaT , what does the accessor return, and how would you keep 'dow' an integer column?
  • The timestamps are stored in UTC, but users live in different time zones. How does that change the day of week you report?
  • How would you add the weekday name or a weekend flag, and make a groupby by weekday come out in calendar order?