1. Home
  2. Data Science
  3. Pandas
  4. df.resample('M').sum() is called on a DataFrame whose date…

Added 30 Nov -0001

df.resample('M').sum() is called on a DataFrame whose date lives in an ordinary string column rather than the index. What happens?

  1. The call groups by calendar month names, treating the strings as categorical data
  2. An empty frame is returned, because no rows fall on the month-end timestamps
  3. A TypeError about the index, because resample needs a DatetimeIndex on the axis Correct answer
  4. The string column is parsed automatically and the monthly totals are returned

Correct answer

C. A TypeError about the index, because resample needs a DatetimeIndex on the axis

14 views

MCQSEXAM

Explanation

resample operates on a time-based index, so the datetime column must first be made the index or passed with on='date' after pd.to_datetime. The 'M' rule labels each bin by month end, controlled by the label and closed arguments. Use .dt.to_period('M') when calendar-month grouping rather than timestamp binning is wanted.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More Pandas MCQs

Practise all Pandas MCQs

Similar MCQs