1. Home
  2. Data Science
  3. Pandas
  4. A DatetimeIndex holds naive timestamps that are known to…

Added 30 Nov -0001

A DatetimeIndex holds naive timestamps that are known to be UTC. Which call turns it into a timezone-aware index?

  1. tz_convert on the index, because it attaches a zone to a naive index
  2. astype to a datetime64 value with the UTC unit, which attaches the zone
  3. tz_localize(None), which labels the index as UTC without adding a zone
  4. tz_localize on the index, because tz_convert needs an aware index first Correct answer

Correct answer

D. tz_localize on the index, because tz_convert needs an aware index first

14 views

MCQSEXAM

Explanation

tz_localize attaches a timezone to naive timestamps, asserting that the wall-clock values belong to that zone, while tz_convert translates an already-aware index to another zone. Calling tz_convert on a naive index raises TypeError, and tz_localize on an aware index raises unless the existing zone is passed. Getting the two backwards shifts every timestamp by the UTC offset.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? 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