python/cpython · error · TypeError

date argument must be a date instance

Error message

date argument must be a date instance

What it means

Raised by datetime.combine() when the first argument is not a date instance. combine(date, time) assembles a datetime by reading date.year/month/day from the first parameter and clock fields from the second; it explicitly isinstance-checks both against _date_class and _time_class, so a string, tuple, or timestamp number for the date part is rejected.

Source

Thrown at Lib/_pydatetime.py:1944

    @classmethod
    def utcnow(cls):
        "Construct a UTC datetime from time.time()."
        import warnings
        warnings.warn("datetime.datetime.utcnow() is deprecated and scheduled for "
                      "removal in a future version. Use timezone-aware "
                      "objects to represent datetimes in UTC: "
                      "datetime.datetime.now(datetime.UTC).",
                      DeprecationWarning,
                      stacklevel=2)
        t = _time.time()
        return cls._fromtimestamp(t, True, None)

    @classmethod
    def combine(cls, date, time, tzinfo=True):
        "Construct a datetime from a given date and a given time."
        if not isinstance(date, _date_class):
            raise TypeError("date argument must be a date instance")
        if not isinstance(time, _time_class):
            raise TypeError("time argument must be a time instance")
        if tzinfo is True:
            tzinfo = time.tzinfo
        return cls(date.year, date.month, date.day,
                   time.hour, time.minute, time.second, time.microsecond,
                   tzinfo, fold=time.fold)

    @classmethod
    def fromisoformat(cls, date_string):
        """Construct a datetime from a string in one of the ISO 8601 formats."""
        if not isinstance(date_string, str):
            raise TypeError('fromisoformat: argument must be str')

        if len(date_string) < 7:
            raise ValueError(f'Invalid isoformat string: {date_string!r}')

        # Split this at the separator

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Parse the date first: datetime.combine(date.fromisoformat('2024-06-01'), t)
  2. Use datetime.strptime('2024-06-01 09:00', '%Y-%m-%d %H:%M') to build both parts in one step
  3. If you already have a datetime, call .replace(hour=..., minute=...) instead of combine

Example fix

# before
dt = datetime.combine('2024-06-01', time(9, 0))

# after
from datetime import date
dt = datetime.combine(date.fromisoformat('2024-06-01'), time(9, 0))
Defensive patterns

Strategy: type-guard

Validate before calling

from datetime import date as _date, time as _time
if not isinstance(d, _date):
    d = _date.fromisoformat(d) if isinstance(d, str) else _date(*d)
assert isinstance(t, _time)
dt = datetime.combine(d, t)

Type guard

from datetime import date as _date

def is_date_instance(v) -> bool:
    return isinstance(v, _date)

Try / catch

try:
    dt = datetime.combine(d, t)
except TypeError:
    dt = datetime.combine(_date.fromisoformat(d), t)  # when d was a str

Prevention

When it happens

Trigger: datetime.combine('2024-06-01', time(9,0)); datetime.combine((2024,6,1), t); datetime.combine(datetime_field.date() is forgotten and the datetime itself... (a datetime IS a date, so that passes) — but date strings, structs, or None fail; passing a pandas period or custom date-like lacking the interface.

Common situations: Form/API handlers receiving dates as strings and combining directly without parsing; using a tuple from strptime/struct_time; dateutil parsed fields; ORM values that arrive as strings.

Related errors


AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14). Data as JSON: /api/errors/2cf80c4b917d4880. Report an issue: GitHub.