Convert String to Datetime

Pandas data engineering interview problem. Difficulty: beginner. Pattern: Date Functions. About 12 minutes. Free to practice.

Parse DD/MM/YYYY and extract year, month, day. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

Dates in files often arrive as text, and different countries write them in different orders. In this table the dates are written as day first, like 15/02/2024, which means 15 February 2024. From the DataFrame df (with date as text and value), turn the date column into real dates, reading them in the day/month/year order (%d/%m/%Y). Then add three whole-number columns: year, month and day. Keep the columns date, value, year, month and day, and sort by date. Save your final table in result.

Requirements

  • Convert date to a real datetime with the format %d/%m/%Y (day first).
  • Add integer columns year, month and day.
  • Keep date and value too.
  • Sort by date.

Constraints

  • Use pd.to_datetime(df['date'], format='%d/%m/%Y').
  • Use .dt.year, .dt.month and .dt.day to get the parts.
  • year, month and day must be integers.

Examples

Input: string dates Output: date | value | year | month | day 2024-01-01 | 1 | 2024 | 1 | 1 2024-02-15 | 2 | 2024 | 2 | 15 2024-03-31 | 3 | 2024 | 3 | 31 2024-04-10 | 4 | 2024 | 4 | 10 Day-first parsing avoids US month/day swaps.

Topics: lakebench, pandas, to_datetime.

More interview problems · All interview problems · Learn data engineering

beginner

Convert String to Datetime

Interview-style drill: Parse DD/MM/YYYY and extract year, month, day.

Parse `date` with format `%d/%m/%Y`, then add integer `year`, `month`, `day` columns. Keep `date`, `value`, and the parts. Sort by `date`. Assign `result`.