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's really being asked
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.
The full answer
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.
The mistakes people make
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.
What usually comes next
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.
A 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.
Interview question
Which code correctly and efficiently extracts weekday names like 'Monday' from a pandas DataFrame column of timestamp strings?
- a.pd.to_datetime(df['ts']).day_name()
- b.pd.to_datetime(df['ts']).dt.day_name()Correct
- c.df['ts'].apply(lambda x: pd.to_datetime(x).day_name())
- d.df['ts'].day_name()
Why? this is the answer
pd.to_datetime vectorizes parsing to datetime64 without Python loops, and the .dt accessor exposes day_name(); wrapping conversion in apply processes rows one by one and destroys performance, while calling day_name directly on a string or Series raises an AttributeError.
Just read this? Test yourself on what you have been reading.
Read the original → pandas.pydata.org
- #pandas
- #datetime
- #feature-engineering
- #time-series
- #data-manipulation
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles