Add integer year and month columns from a datetime column in pandas

Quick Overview

Add integer year and month columns to a pandas DataFrame from an existing datetime column without changing the input frame. It tests the .dt accessor, copying a DataFrame before adding columns, integer dtypes and what happens when some dates are missing.

Add integer year and month columns from a datetime column in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `extract_year_month(df: pd.DataFrame) -> pd.DataFrame`. The input has a `datetime64[ns]` column `'date'`. Return a new DataFrame with all the original columns plus integer `'year'` and `'month'` columns. The function must: - Take `'year'` from `df['date'].dt.year` and `'month'` from `df['date'].dt.month`. - Give both new columns an integer dtype (`int32` or `int64`). - Keep all original columns and the original index alignment. - Leave the input DataFrame unchanged. Example: ```python df = pd.DataFrame({'date': pd.to_datetime(['2023-01-15', '2024-07-03'])}) extract_year_month(df) # date year month # 0 2023-01-15 2023 1 # 1 2024-07-03 2024 7 ``` ```hint Mutation Check whether adding a column to the frame you were given changes the caller's DataFrame, and what you need to do so it does not. ``` ### Constraints & Assumptions - `'date'` is already `datetime64[ns]`, so no parsing is needed. - The frame may have other columns besides `'date'`, and they must come through unchanged. ### What a Strong Answer Covers - Getting calendar parts from the datetime column with the `.dt` accessor - Returning a new frame and leaving the input untouched - Integer dtypes for the new columns and alignment with the original index - How missing dates and time zones change the result ### Follow-up Questions - What if `'date'` contains `NaT` values? - If the dates are timezone-aware, which year and month do you get, and how would you report them in UTC? - How would you add a single year-month key, for example to group sales by month?

Overview: Add integer year and month columns to a pandas DataFrame from an existing datetime column without changing the input frame. It tests the .dt accessor, copying a DataFrame before adding columns, integer dtypes and what happens when some dates are missing.

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

Implement extract_year_month(df: pd.DataFrame) -> pd.DataFrame. The input has a datetime64[ns] column 'date'. Return a new DataFrame with all the original columns plus integer 'year' and 'month' columns.

The function must:

  • Take 'year' from df['date'].dt.year and 'month' from df['date'].dt.month .
  • Give both new columns an integer dtype ( int32 or int64 ).
  • Keep all original columns and the original index alignment.
  • Leave the input DataFrame unchanged.

Example:

df = pd.DataFrame({'date': pd.to_datetime(['2023-01-15', '2024-07-03'])})
extract_year_month(df)
#         date  year  month
# 0 2023-01-15  2023      1
# 1 2024-07-03  2024      7

Constraints & Assumptions

  • 'date' is already datetime64[ns] , so no parsing is needed.
  • The frame may have other columns besides 'date' , and they must come through unchanged.

What a Strong Answer Covers Guidance

  • Getting calendar parts from the datetime column with the .dt accessor
  • Returning a new frame and leaving the input untouched
  • Integer dtypes for the new columns and alignment with the original index
  • How missing dates and time zones change the result

Follow-up Questions Guidance

  • What if 'date' contains NaT values?
  • If the dates are timezone-aware, which year and month do you get, and how would you report them in UTC?
  • How would you add a single year-month key, for example to group sales by month?