Skip to content

Commit aeb3644

Browse files
REGR: fix roundtripping datetimes with sqlite type detection (#55690)
Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 54814c3 commit aeb3644

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

doc/source/whatsnew/v2.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Fixed regressions
2828
- Fixed regression in :meth:`DataFrameGroupBy.agg` and :meth:`SeriesGroupBy.agg` where if the option ``compute.use_numba`` was set to True, groupby methods not supported by the numba engine would raise a ``TypeError`` (:issue:`55520`)
2929
- Fixed performance regression with wide DataFrames, typically involving methods where all columns were accessed individually (:issue:`55256`, :issue:`55245`)
3030
- Fixed regression in :func:`merge_asof` raising ``TypeError`` for ``by`` with datetime and timedelta dtypes (:issue:`55453`)
31+
- Fixed regression in :meth:`DataFrame.to_sql` not roundtripping datetime columns correctly for sqlite when using ``detect_types`` (:issue:`55554`)
3132

3233
.. ---------------------------------------------------------------------------
3334
.. _whatsnew_212.bug_fixes:

pandas/io/sql.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -2094,19 +2094,17 @@ def _adapt_time(t) -> str:
20942094
# Python 3.12+ doesn't auto-register adapters for us anymore
20952095

20962096
adapt_date_iso = lambda val: val.isoformat()
2097-
adapt_datetime_iso = lambda val: val.isoformat()
2097+
adapt_datetime_iso = lambda val: val.isoformat(" ")
20982098

20992099
sqlite3.register_adapter(time, _adapt_time)
21002100

21012101
sqlite3.register_adapter(date, adapt_date_iso)
21022102
sqlite3.register_adapter(datetime, adapt_datetime_iso)
21032103

21042104
convert_date = lambda val: date.fromisoformat(val.decode())
2105-
convert_datetime = lambda val: datetime.fromisoformat(val.decode())
2106-
convert_timestamp = lambda val: datetime.fromtimestamp(int(val))
2105+
convert_timestamp = lambda val: datetime.fromisoformat(val.decode())
21072106

21082107
sqlite3.register_converter("date", convert_date)
2109-
sqlite3.register_converter("datetime", convert_datetime)
21102108
sqlite3.register_converter("timestamp", convert_timestamp)
21112109

21122110
def sql_schema(self) -> str:

pandas/tests/io/test_sql.py

+18
Original file line numberDiff line numberDiff line change
@@ -3368,6 +3368,24 @@ def test_roundtripping_datetimes(sqlite_engine):
33683368
assert result == "2020-12-31 12:00:00.000000"
33693369

33703370

3371+
@pytest.fixture
3372+
def sqlite_builtin_detect_types():
3373+
with contextlib.closing(
3374+
sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
3375+
) as closing_conn:
3376+
with closing_conn as conn:
3377+
yield conn
3378+
3379+
3380+
def test_roundtripping_datetimes_detect_types(sqlite_builtin_detect_types):
3381+
# https://github.com/pandas-dev/pandas/issues/55554
3382+
conn = sqlite_builtin_detect_types
3383+
df = DataFrame({"t": [datetime(2020, 12, 31, 12)]}, dtype="datetime64[ns]")
3384+
df.to_sql("test", conn, if_exists="replace", index=False)
3385+
result = pd.read_sql("select * from test", conn).iloc[0, 0]
3386+
assert result == Timestamp("2020-12-31 12:00:00.000000")
3387+
3388+
33713389
@pytest.mark.db
33723390
def test_psycopg2_schema_support(postgresql_psycopg2_engine):
33733391
conn = postgresql_psycopg2_engine

0 commit comments

Comments
 (0)