What are the pandas .str and .dt accessors for, and how do you use them on string and datetime columns?

Quick Overview

This pandas interview question asks what the .str and .dt accessors are for and how to use them on string and datetime columns. It tests whether you know the common methods, the dtype each accessor needs, and how missing values and performance behave.

What are the pandas .str and .dt accessors for, and how do you use them on string and datetime columns?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

In pandas, what are the purpose and usage of the `.str` accessor for string operations and the `.dt` accessor for datetime operations? Show a short example of cleaning a text column and extracting parts of a date. ```hint Try a missing value Compare `s.str.lower()` with `s.apply(lambda x: x.lower())` on a column that contains one missing value. ``` ```hint Check the dtype Ask what dtype a column must have before `.dt` works, and what dtype a date column read from a CSV file usually has. ``` ### What a Strong Answer Covers - What an accessor is and which Series dtypes each one works on - Common `.str` methods, including regex and element access - Common `.dt` properties and methods, including the conversion step before using them - How missing values and mixed types behave - How the speed of `.str` and `.dt` compares with `apply`, and what changes it ### Follow-up Questions - `s.str.contains(".")` returns True for every non-missing row. Why, and how do you fix it? - How do you filter rows with `.str.contains` when the column has missing values? - How would you convert naive UTC timestamps to a local time zone and then group by local calendar day? - How would you pull the number out of strings like "order-1042" into a new integer column?

Overview: This pandas interview question asks what the .str and .dt accessors are for and how to use them on string and datetime columns. It tests whether you know the common methods, the dtype each accessor needs, and how missing values and performance behave.

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

In pandas, what are the purpose and usage of the .str accessor for string operations and the .dt accessor for datetime operations? Show a short example of cleaning a text column and extracting parts of a date.

What a Strong Answer Covers Guidance

  • What an accessor is and which Series dtypes each one works on
  • Common .str methods, including regex and element access
  • Common .dt properties and methods, including the conversion step before using them
  • How missing values and mixed types behave
  • How the speed of .str and .dt compares with apply , and what changes it

Follow-up Questions Guidance

  • s.str.contains(".") returns True for every non-missing row. Why, and how do you fix it?
  • How do you filter rows with .str.contains when the column has missing values?
  • How would you convert naive UTC timestamps to a local time zone and then group by local calendar day?
  • How would you pull the number out of strings like "order-1042" into a new integer column?