pandas: Year, month and day name

Model pd.to_datetime(s, errors="coerce") followed by .dt.year, .dt.month and .dt.day_name().

What to build

A program that parses dates and prints their parts.

Requirements

  • For a valid YYYY-MM-DD date print DATE: year Y, month M, NAME, with the month as a number without a leading zero and NAME the day of the week.
  • For anything else print TEXT: NaT.

Input

Line 1: n. Then n lines, each a date or other text.

Output

n lines.

Constraints

1 ≤ n ≤ 1000; dates are between the years 1900 and 2100.

Example

Input:

2
2024-03-15
2000-01-01

Output:

2024-03-15: year 2024, month 3, Friday
2000-01-01: year 2000, month 1, Saturday

Why it works

Parsing turns the text into a real calendar date, which knows month lengths and leap years, so impossible dates such as 30 February are rejected and the weekday is computed correctly.

Examples

Example 1 · Input
2
2024-03-15
2000-01-01
Expected output
2024-03-15: year 2024, month 3, Friday
2000-01-01: year 2000, month 1, Saturday
Example 2 · Input
1
2024-02-30
Expected output
2024-02-30: NaT

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

date.fromisoformat raises ValueError for an invalid date.

Hint 2

d.strftime("%A") gives the English day name.

Solution

Show one correct solution (try it yourself first)
from datetime import date

n = int(input())
for _ in range(n):
    text = input()
    try:
        d = date.fromisoformat(text)
    except ValueError:
        print(f"{text}: NaT")
        continue
    print(f"{text}: year {d.year}, month {d.month}, {d.strftime('%A')}")

Lesson: Dates and times in pandas →