pandas: Delivery times in days

Model (df["delivered"] - df["ordered"]).dt.days and summarise the delivery times.

What to build

A program that computes delivery times and how many were late.

Requirements

  • For each order print ID: N days, or ID: NaT when either date is missing or invalid.
  • Then print mean M over the valid durations, to two decimals, or mean NaN if there are none, and late K, the number of orders that took more than 5 days.

Input

Line 1: n. Then n lines ID ORDERED DELIVERED, dates as YYYY-MM-DD or NA.

Output

n + 2 lines.

Constraints

1 ≤ n ≤ 1000; when both dates are valid, delivery is not before ordering.

Example

Input:

3
o1 2024-01-01 2024-01-04
o2 2024-01-30 2024-02-08
o3 2024-02-28 2024-03-01

Output:

o1: 3 days
o2: 9 days
o3: 2 days
mean 4.67
late 1

Why it works

Subtracting real dates counts calendar days correctly across month ends and leap days, and skipping NaT in the mean matches how pandas ignores missing values.

Examples

Example 1 · Input
3
o1 2024-01-01 2024-01-04
o2 2024-01-30 2024-02-08
o3 2024-02-28 2024-03-01
Expected output
o1: 3 days
o2: 9 days
o3: 2 days
mean 4.67
late 1
Example 2 · Input
1
o1 2023-12-31 2024-01-01
Expected output
o1: 1 days
mean 1.00
late 0

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

Hints

Hint 1

Parse both dates; if either fails, the duration is NaT.

Hint 2

(end - start).days gives the whole days.

Hint 3

Average only the valid durations; with none, the mean is NaN.

Solution

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


def parse(text):
    try:
        return date.fromisoformat(text)
    except ValueError:
        return None


n = int(input())
days = []
for _ in range(n):
    oid, a, b = input().split()
    start, end = parse(a), parse(b)
    if start is None or end is None:
        print(f"{oid}: NaT")
        continue
    d = (end - start).days
    days.append(d)
    print(f"{oid}: {d} days")
print(f"mean {sum(days) / len(days):.2f}" if days else "mean NaN")
print("late", sum(1 for d in days if d > 5))

Lesson: Dates and times in pandas →