Skip to content

BUG write timedelta64 values to SQL in nanoseconds - #71974

Open
2Zest wants to merge 1 commit into
pandas-dev:mainfrom
2Zest:fix/tosql-timedelta-nanoseconds-71941
Open

2Zest wants to merge 1 commit into
pandas-dev:mainfrom
2Zest:fix/tosql-timedelta-nanoseconds-71941

Conversation

@2Zest

@2Zest 2Zest commented Nov 9, 2026

Copy link
Copy Markdown

Closes #71941

Summary

DataFrame.to_sql documents timedelta values as nanoseconds, but the SQL insertion path views the native timedelta buffer directly. Since pandas 3 uses timedelta64[us] by default, values were written 1,000 times too small.

The insertion path now converts NumPy-backed timedeltas to timedelta64[ns] before extracting integer values. Arrow-backed timedeltas already used the nanosecond conversion and remain unchanged.

Tests

  • python -m compileall -q pandas/io/sql.py pandas/tests/io/test_sql.py
  • python -m pytest pandas/tests/io/test_sql.py -k test_to_sql_timedelta_writes_nanoseconds -q (blocked locally because the in-place pandas extension module pandas._libs.pandas_parser is not built)
  • git diff --check

I used OpenAI Codex to develop this pull request, reviewed and understood every change, and included the regression test.

@github-actions github-actions Bot added the Needs Issue Assignment PR whose author is not assigned to a linked issue label Nov 9, 2026
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Thanks for the pull request, @2Zest! It's linked to #71941, but that issue isn't assigned to you yet. To make sure two people don't unknowingly work on the same thing, we ask contributors to claim an issue first. You can comment /take on #71941 to claim it, but be aware that certain issues cannot be taken if they are labeled e.g. Needs Triage — see the contributing guide for more details.

This pull request is unlikely to be reviewed until #71941 is assigned to you.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Issue Assignment PR whose author is not assigned to a linked issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: to_sql writes timedelta64 columns in their own unit instead of nanoseconds (1000x too small with the default "us")

1 participant