A pandas `datetime64[ns, tz]` column is scanned as microsecond `TIMESTAMPTZ`: nanoseconds are truncated, distinct timestamps collapse, and pre-1970 values move forward to the next microsecond (Arrow and Polars give `TIMESTAMPTZ_NS`)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 55/100
調査の方向性
Start by reading src/numpy/type.cpp around NumpyToLogicalType and src/numpy/numpy_scan.cpp around the timestamp conversion described in the report. Compare pandas scanning with the Arrow and Polars paths, which preserve nanoseconds, and check existing tests for NumPy datetime scans. Done means a timezone-aware datetime64[ns] column retains distinct nanosecond timestamps, including the pre-1970 value, or the conversion handles negative values correctly if compatibility requires microseconds.
索引モデルが issue の本文から書いたものです。
説明
What happens?
Scanning a pandas DataFrame with a time-zone-aware nanosecond column (datetime64[ns, UTC], what
pd.to_datetime(..., utc=True) produces from nanosecond input) gives a TIMESTAMP WITH TIME ZONE (microsecond) column. The sub-microsecond
digits are dropped by truncating toward zero, so CREATE TABLE t AS SELECT * FROM df stores fewer distinct
timestamps than the DataFrame has. A value 1 ns before the epoch becomes 1970-01-01 00:00:00+00, another day and
year. The same DataFrame converted to Arrow or Polars, and the same column without a time zone, are stored exactly
(TIMESTAMPTZ_NS / TIMESTAMP_NS). No error or warning.
To Reproduce
import duckdb, pandas as pd, pyarrow as pa, polars as pl
con = duckdb.connect()
ticks = pd.DataFrame({'ts': pd.to_datetime(['2026-10-17 09:30:00.000000100', '2026-10-17 09:30:00.000000900',
'2026-10-17 09:30:00.000001000', '1969-12-31 23:59:59.999999999']).tz_localize('UTC'),
'px': [100.0, 100.5, 101.0, 1.0]})
con.execute("CREATE TABLE from_pandas AS SELECT * FROM ticks")
arrow_ticks = pa.Table.from_pandas(ticks)
con.execute("CREATE TABLE from_arrow AS SELECT * FROM arrow_ticks")
polars_ticks = pl.from_pandas(ticks)
con.execute("CREATE TABLE from_polars AS SELECT * FROM polars_ticks")
for t in ('from_pandas', 'from_arrow', 'from_polars'):
rows = con.execute(f"SELECT ts::VARCHAR, typeof(ts) FROM {t} ORDER BY px").fetchall()
distinct = con.execute(f"SELECT count(DISTINCT ts) FROM {t}").fetchone()[0]
print(f"{t:12s} distinct={distinct} " + ' '.join(r[0] for r in rows) + f" [{rows[0][1]}]")
naive = ticks.assign(ts=ticks.ts.dt.tz_localize(None))
con.execute("CREATE TABLE naive_pandas AS SELECT * FROM naive")
print('naive pandas', con.execute("SELECT count(DISTINCT ts), any_value(typeof(ts)) FROM naive_pandas").fetchone())
Output (the same with 2.1.0.dev2610310630, 2.0.1.dev2611081931 and 2.0.0):
from_pandas distinct=3 1970-01-01 00:00:00+00 2026-10-17 09:30:00+00 2026-10-17 09:30:00+00 2026-10-17 09:30:00.000001+00 [TIMESTAMP WITH TIME ZONE]
from_arrow distinct=4 1969-12-31 23:59:59.999999999+00 2026-10-17 09:30:00.0000001+00 2026-10-17 09:30:00.0000009+00 2026-10-17 09:30:00.000001+00 [TIMESTAMPTZ_NS]
from_polars distinct=4 1969-12-31 23:59:59.999999999+00 2026-10-17 09:30:00.0000001+00 2026-10-17 09:30:00.0000009+00 2026-10-17 09:30:00.000001+00 [TIMESTAMPTZ_NS]
naive pandas (4, 'TIMESTAMP_NS')
Expected: from_pandas like from_arrow (4 distinct TIMESTAMPTZ_NS values). The two ticks 100 ns and 900 ns after
09:30:00 become the same instant, and the 1969 tick becomes 1970-01-01. A UNIQUE / PRIMARY KEY on the timestamp
would reject the load; without one the duplicates are stored silently, and ordering ties no longer match the source.
Cause. NumpyToLogicalType maps every time-zone-aware datetime64 column to LogicalType::TIMESTAMP_TZ,
including DATETIME_NS, while the naive DATETIME_NS maps to TIMESTAMP_NS
(src/numpy/type.cpp#L166-L171).
The scan then converts with Timestamp::FromEpochNanoSeconds ("Our timezone type is US, so we need to convert from NS
to US",
src/numpy/numpy_scan.cpp#L461-L467;
the same on v2.0-cyanoptera,
#L654-L660).
That function is ns / NANOS_PER_MICRO, an integer division that truncates toward zero, so negative values move
forward. The comment predates the nanosecond time-zone type: since 2.0 DuckDB has TIMESTAMPTZ_NS, and the Arrow and
Polars scans of the same column, as well as the Parquet reader for TIMESTAMP(NANOS, isAdjustedToUTC=true), already
use it. Mapping DATETIME_NS with a time zone to TIMESTAMP_TZ_NS and copying the values unchanged would fix it. If
the microsecond type is kept for compatibility (duckdb/duckdb#28039 lists operators that TIMESTAMPTZ_NS does not
support yet), the conversion should at least round toward negative infinity, so that pre-1970 values do not move to
the next microsecond.
OS:
Ubuntu 22.04, x86_64
DuckDB Package Version:
2.1.0.dev2610310630 (main); also 2.0.1.dev2611081931 (v2.0-cyanoptera) and 2.0.0
Python Version:
3.12.16
Full Name:
Ke Han
Affiliation:
Purdue University
What is the latest build you tested with? If possible, we recommend testing with the latest nightly build.
I have tested with a nightly build
Did you include all relevant data sets for reproducing the issue?
Yes
Did you include all code required to reproduce the issue?
- Yes, I have
Did you include all relevant configuration to reproduce the issue?
- Yes, I have
- 主要言語
- Python
- スター
- 206
- フォーク
- 126
- 平均マージ
- 20時間 18分
- マージ済み PR(30日)
- 36
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
duckdb/duckdb-python のほかの issue
-
needs triage
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
duckdb/duckdb-python#752 ·
メンテナーはふだん 1 日以内に返信
-
`read_json` accepts a list of paths at runtime but the type stub only allows a single path.対応中かも @aleks-drozy が 118 日前に担当しました。 オープンneeds triage
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
duckdb/duckdb-python#576 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Missing ROW_GROUPS_PER_FILE argument in write_parquet function対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープンneeds triage
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
duckdb/duckdb-python#386 ·
メンテナーはふだん 1 日以内に返信
-
import duckdb fails with 'Encountered an error while initializing the extension' when sys.modules has no __main__ (DetectEnvironment imports __main__ unconditionally)対応中かも @chouaibmouidis-py が 2 日前に担当しました。 オープン
難易度 3/5 半日 初心者へのやさしさ 68/100
duckdb/duckdb-python#795 ·
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 42/100
duckdb/duckdb-python#789 ·
メンテナーはふだん 1 日以内に返信
duckdb/duckdb-python の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
GanyuanRan/Aegis#112 ·
-
defect from-review
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
khuisman/mcp-gee-sweet#1669 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 90/100
marimo-team/marimo#12153 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
can4hou6joeng4/boss-agent-cli#3290 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信