What are the .str and .dt accessors and when would you use them?

Quick Overview

Explain the pandas .str and .dt accessors and when to use them on text and datetime columns. Tests vectorized string and date operations, missing value handling, dtype requirements, and how the accessors compare with apply and loops.

What are the .str and .dt accessors and when would you use them?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

What are the `.str` and `.dt` accessors on a pandas Series, and when would you use them? ```hint Picture the alternative Think about how you would lower-case a whole text column or pull the weekday out of a timestamp column without writing a Python loop or calling `.apply`. ``` ### What a Strong Answer Covers - What an accessor is and which column dtypes each one works on - Typical operations for text columns and for date and time columns - How missing values are handled - How the accessors compare with `.apply` or a loop in clarity and speed, including the dtype caveat - Common failure cases, such as calling an accessor on a column of the wrong dtype ### Follow-up Questions - `df[df["name"].str.contains("a.b")]` returns rows you did not expect, and on another column it raises an error. Why, and how do you fix both? - How would you compute the number of whole days between two datetime columns, and which accessor do you use on the result? - How do you convert a column of UTC timestamps to local time in another time zone? - Why can `.str` methods on an object column be barely faster than `.apply`, and what changes with an Arrow-backed string dtype?

Overview: Explain the pandas .str and .dt accessors and when to use them on text and datetime columns. Tests vectorized string and date operations, missing value handling, dtype requirements, and how the accessors compare with apply and loops.

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

What are the .str and .dt accessors on a pandas Series, and when would you use them?

What a Strong Answer Covers Guidance

  • What an accessor is and which column dtypes each one works on
  • Typical operations for text columns and for date and time columns
  • How missing values are handled
  • How the accessors compare with .apply or a loop in clarity and speed, including the dtype caveat
  • Common failure cases, such as calling an accessor on a column of the wrong dtype

Follow-up Questions Guidance

  • df[df["name"].str.contains("a.b")] returns rows you did not expect, and on another column it raises an error. Why, and how do you fix both?
  • How would you compute the number of whole days between two datetime columns, and which accessor do you use on the result?
  • How do you convert a column of UTC timestamps to local time in another time zone?
  • Why can .str methods on an object column be barely faster than .apply , and what changes with an Arrow-backed string dtype?