django/django · error · GDALException

Unable to retrieve date & time information from the field.

Error message

Unable to retrieve date & time information from the field.

What it means

Raised by Field.as_datetime (django/contrib/gis/gdal/field.py:92) as a GDALException when OGR_F_GetFieldAsDateTimeEx returns a falsy status, meaning GDAL could not interpret the field's contents as a date/time. This surfaces when accessing the `.value` of an OFTDate/OFTTime/OFTDateTime field whose stored bytes are not parseable as a datetime.

Source

Thrown at django/contrib/gis/gdal/field.py:92

        if not self.is_set:
            return None
        yy, mm, dd, hh, mn, tz = [c_int() for _ in range(6)]
        ss = c_float()
        status = capi.get_field_as_datetime_x(
            self._feat.ptr,
            self._index,
            byref(yy),
            byref(mm),
            byref(dd),
            byref(hh),
            byref(mn),
            byref(ss),
            byref(tz),
        )
        if status:
            return (yy, mm, dd, hh, mn, ss, tz)
        else:
            raise GDALException(
                "Unable to retrieve date & time information from the field."
            )

    # #### Field Properties ####
    @property
    def is_set(self):
        "Return True if the value of this field isn't null, False otherwise."
        return capi.is_field_set(self._feat.ptr, self._index)

    @property
    def name(self):
        "Return the name of this Field."
        name = capi.get_field_name(self.ptr)
        return force_str(name, encoding=self._feat.encoding, strings_only=True)

    @property
    def precision(self):
        "Return the precision of this Field."

View on GitHub (pinned to ae25a40be0)

Solutions

  1. Use `.value` (which returns None on failure) instead of `.as_datetime()` directly.
  2. Pre-screen the field's raw string value with `field.as_string()` and your own parser.
  3. Repair the source data: replace zero/null dates with valid values or NULL.
  4. Upgrade GDAL; newer versions parse more datetime formats.

Example fix

// before
yy, mm, dd, hh, mn, ss, tz = field.as_datetime()   # raises on bad date
// after
val = field.value                                    # returns None gracefully
if val is None:
    val = parse_fallback(field.as_string())
Defensive patterns

Strategy: fallback

Validate before calling

raw = field.as_string()
is_plausible_date = bool(raw and raw.strip() not in ('', '0000-00-00', '00000000'))
if not is_plausible_date:
    return None  # skip as_datetime on null/zero dates
return field.as_datetime()

Type guard

def looks_like_date(raw: str) -> bool:
    return bool(raw) and raw.strip() not in ('', '0000-00-00', '00000000')

Try / catch

from django.contrib.gis.gdal.error import GDALException
try:
    components = field.as_datetime()
except GDALException:
    components = None  # fall back to raw string parsing
if components is None:
    parsed = parse_fallback(field.as_string())

Prevention

When it happens

Trigger: A date field containing an empty string, '0000-00-00', a malformed timestamp, or non-date text. A field whose declared type is OFTDateTime but whose actual content was written by a buggy exporter. Accessing `.value` on such a field.

Common situations: Shapefile/DBF date columns with null/zero placeholders ('00000000'). GeoPackage datetime fields with timezone formats the GDAL build doesn't parse. Corrupt date cells from spreadsheet-to-GIS conversions. Note: the OFTDate/OFTTime/OFTDateTime `.value` properties catch GDALException and return None, so this error typically surfaces only when calling as_datetime() directly.

Related errors


AI-assisted analysis of django/django@ae25a40be0 (2026-08-06). Data as JSON: /api/errors/ffd7b01f7cca5101. Report an issue: GitHub.