Skip to content

Commit 90d8511

Browse files
committed
Probe utcoffset() in convert_to_timestamp to catch deferred toordinal
The previous fix only handled NotImplementedError raised during pd.Timestamp construction, but pandas 3.0 also defers the toordinal crash to utcoffset() — which pd.DataFrame() calls when aligning a mixed-tz column. test_query_data exercises this path via result_set_to_pandas() → pd.DataFrame(...) and still failed. Probe utcoffset() right after construction; if it raises NotImplementedError, fall back to a naive Timestamp so the column becomes datetime64[ms] (no tz) instead of datetime64[ms, <tz>]. NaT short-circuits the probe (it doesn't support utcoffset()). Verified: 0 failures across {ms,us,ns} × {UTC,Etc/UTC,Asia/Shanghai, America/Los_Angeles} × Long.MIN/MAX/0/±1/Int32 boundaries on Python 3.13 and 3.14 with pandas 3.0.3, for both isoformat() and pd.DataFrame() construction.
1 parent f51011b commit 90d8511

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

iotdb-client/client-py/iotdb/utils/rpc_utils.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,34 @@ def verify_success_with_redirection_for_multi_devices(status: TSStatus, devices:
7979

8080
def convert_to_timestamp(time: int, precision: str, timezone: str):
8181
try:
82-
return pd.Timestamp(time, unit=precision, tz=timezone)
82+
ts = pd.Timestamp(time, unit=precision, tz=timezone)
8383
except OutOfBoundsDatetime:
8484
try:
85-
return pd.Timestamp(time, unit=precision).tz_localize(timezone)
85+
ts = pd.Timestamp(time, unit=precision).tz_localize(timezone)
8686
except NotImplementedError:
8787
return pd.Timestamp(time, unit=precision)
8888
except ValueError:
8989
logger.warning(
9090
f"Timezone string '{timezone}' cannot be recognized by pandas. "
9191
f"Falling back to local timezone: '{get_localzone_name()}'."
9292
)
93-
return pd.Timestamp(time, unit=precision, tz=get_localzone_name())
93+
ts = pd.Timestamp(time, unit=precision, tz=get_localzone_name())
9494
except NotImplementedError:
9595
# Timestamp falls outside Python's stdlib datetime range (year < 1 or
9696
# > 9999). pandas >= 3.0 cannot attach a timezone to such Timestamps
9797
# because tz conversion relies on toordinal(). Return naive instead.
9898
return pd.Timestamp(time, unit=precision)
99+
# Construction succeeded but utcoffset() may still fail downstream (e.g.
100+
# in pd.DataFrame's tz alignment) when the Timestamp's year is outside
101+
# 1..9999 and tz is not the canonical UTC. Probe early and fall back to
102+
# naive so callers like result_set_to_pandas() don't blow up. NaT does
103+
# not need this check (and does not support utcoffset()).
104+
if ts is not pd.NaT:
105+
try:
106+
ts.utcoffset()
107+
except NotImplementedError:
108+
return pd.Timestamp(time, unit=precision)
109+
return ts
99110

100111

101112
unit_map = {

0 commit comments

Comments
 (0)