Convert string timestamps to datetime and extract day of week
This tests pandas datetime parsing and accessor fluency. A strong answer uses pd.to_datetime, assigns the result, then extracts the day via .dt.day_name() or .dt.dayofweek. Red flag: manual string splitting or Python loops instead of vectorized ops.
WHAT THIS TESTS: This question probes whether you know the idiomatic pandas path for time-series feature engineering. Interviewers want to see that you understand vectorized parsing, the datetime64 dtype, and the datetime accessor namespace. At the senior level, they also care if you mention performance nuances like format specification, timezone handling, or the difference between weekday integers and day_name labels.
A GOOD ANSWER COVERS: First, vectorized parsing with pd.to_datetime. Explain that passing the string column to pd.to_datetime converts the object dtype to datetime64 without Python loops. Second, assignment strategy. Mention assigning the parsed result back to the DataFrame, either replacing the original column or creating a new one like df['parsed_ts']. Third, extraction via the datetime accessor. State that you use df['parsed_ts'].dt.day_name() for string labels like Monday, or .dt.dayofweek for integers where Monday is 0 and Sunday is 6. Fourth, optional but strong extras. Mention specifying format to avoid inference overhead and noting that datetime accessors are only available on Series with datetime dtype.
COMMON WRONG ANSWERS: A major red flag is suggesting manual string splitting such as s.split('-') or regular expressions to pull out date components. Another is using df['timestamp'].apply(lambda x: datetime.strptime(x, ...)) which forces row-wise Python execution and destroys performance on large frames. Some candidates forget the accessor and try df['timestamp'].day_name() which raises an AttributeError because the column is still strings. Failing to mention dtype verification is also weak; a senior candidate should note checking df.dtypes to confirm the conversion.
LIKELY FOLLOW-UPS: The interviewer might ask how to handle mixed or ambiguous formats, in which case you discuss dayfirst or format arguments. They might ask about timezones, so you should know tz_localize and tz_convert. Another follow-up is resampling: after creating the datetime column, how would you aggregate metrics by week? Finally, they might ask memory implications of datetime64 versus storing strings, or how to convert to a PeriodIndex for fiscal calendars.
ONE CONCRETE EXAMPLE: Suppose you have a DataFrame with a timestamp column containing 2023-10-27 14:30:00. You would write df['parsed'] = pd.to_datetime(df['timestamp'], format='%Y-%m-%d %H:%M:%S') to parse exactly, then df['weekday'] = df['parsed'].dt.day_name() to get Friday. If you needed an integer for modeling, you would use df['parsed'].dt.dayofweek which returns 4 for Friday. You could then drop the original string column to save memory.
Read the original → pandas.pydata.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.