pola-rs/polars · error

Timezone is invalid or not supported

Error message

Timezone {tz} is invalid or not supported

What it means

When serializing timestamps with a timezone, timestamp_tz_serializer looks up the fixed offset for the timezone string; with the 'timezones' feature enabled, an unrecognized/invalid tz (not a fixed offset like +02:00 and not a valid IANA zone) reaches a panic arm reading 'Timezone {tz} is invalid or not supported'.

Solutions

  1. Use a valid fixed offset format '[-]HH:MM' (e.g. '+02:00') or a proper IANA name (e.g. 'Europe/Amsterdam').
  2. Validate the tz with chrono-tz / polars timezone utilities before serializing.
  3. Cast the column to a naive timestamp or UTC before JSON output if the original tz is not needed.
  4. Fix the producer that wrote the malformed timezone into the metadata.

Example fix

// before
"timezone": "UTC+2"
// after
"timezone": "+02:00"  // or "Etc/GMT-2"
Defensive patterns

Strategy: validation

Validate before calling

fn tz_is_supported(tz: &str) -> bool {
    // fixed offset [-]HH:MM
    let bytes = tz.as_bytes();
    let fixed = tz.len() == 6 && (bytes[0] == b'+' || bytes[0] == b'-')
        && tz[1..3].parse::<u8>().map(|h| h < 24).unwrap_or(false)
        && &tz[3..4] == ":"
        && tz[4..6].parse::<u8>().map(|m| m < 60).unwrap_or(false);
    fixed || chrono_tz::Tz::from_str(tz).is_ok()
}
assert!(tz_is_supported(tz), "unsupported timezone: {tz}");

Type guard

fn parse_tz(tz: &str) -> Option<String> {
    chrono_tz::Tz::from_str(tz).ok().map(|_| tz.to_string())
        .or_else(|| validate_fixed_offset(tz))
}

Try / catch

// panic arm; validate the timestamp metadata before serializing
let tz = timestamp_meta.timezone.as_deref().unwrap_or("UTC");
assert!(tz_is_supported(tz), "bad tz {tz}; cast column to UTC first");

Prevention

When it happens

Trigger: Calling new_serializer on a Timestamp array whose TimeUnit has a timezone string that is neither a valid fixed offset nor a known IANA timezone, on builds with feature = "timezones" — e.g. via JSON writing of a timestamp column with a misspelled tz like 'UTC+2' or 'America'.

Common situations: Timezone strings produced by another system in non-IANA format ('UTC+02:00', 'GMT-5'); truncated tz names; locale-specific zone aliases; writing JSON from data read with a bad tz configured.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/261d6ab4933f7c40. Report an issue: GitHub.

Appendix: source

Thrown at crates/polars-json/src/json/write/serialize.rs:476

            materialize_serializer(f, array.iter(), offset, take)
        },
        #[cfg(feature = "timezones")]
        _ => match parse_offset_tz(tz) {
            Ok(parsed_tz) => {
                let f = move |x: Option<&i64>, buf: &mut Vec<u8>| {
                    if let Some(x) = x {
                        let dt_str = timestamp_to_datetime(*x, time_unit, &parsed_tz).to_rfc3339();
                        write!(buf, "\"{dt_str}\"").unwrap();
                    } else {
                        buf.extend_from_slice(b"null")
                    }
                };

                materialize_serializer(f, array.iter(), offset, take)
            },
            _ => {
                panic!("Timezone {tz} is invalid or not supported");
            },
        },
        #[cfg(not(feature = "timezones"))]
        _ => {
            panic!("Invalid Offset format (must be [-]00:00) or timezones feature not active");
        },
    }
}

pub fn new_serializer<'a>(
    array: &'a dyn Array,
    offset: usize,
    take: usize,
) -> Box<dyn JsonSerializer<Item = [u8]> + 'a + Send + Sync> {
    match array.dtype().to_storage() {
        ArrowDataType::Boolean => {
            boolean_serializer(array.as_any().downcast_ref().unwrap(), offset, take)
        },

View on GitHub (pinned to fe841f959e)