Skip to content

Commit 8059d86

Browse files
Backport PR #55690 on branch 2.1.x (REGR: fix roundtripping datetimes with sqlite type detection) (#55702)
REGR: fix roundtripping datetimes with sqlite type detection (#55690) (cherry picked from commit aeb3644)
1 parent 47a7cbf commit 8059d86

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
@@ -2090,19 +2090,17 @@ def _adapt_time(t) -> str:
20902090
# Python 3.12+ doesn't auto-register adapters for us anymore
20912091

20922092
adapt_date_iso = lambda val: val.isoformat()
2093-
adapt_datetime_iso = lambda val: val.isoformat()
2093+
adapt_datetime_iso = lambda val: val.isoformat(" ")
20942094

20952095
sqlite3.register_adapter(time, _adapt_time)
20962096

20972097
sqlite3.register_adapter(date, adapt_date_iso)
20982098
sqlite3.register_adapter(datetime, adapt_datetime_iso)
20992099

21002100
convert_date = lambda val: date.fromisoformat(val.decode())
2101-
convert_datetime = lambda val: datetime.fromisoformat(val.decode())
2102-
convert_timestamp = lambda val: datetime.fromtimestamp(int(val))
2101+
convert_timestamp = lambda val: datetime.fromisoformat(val.decode())
21032102

21042103
sqlite3.register_converter("date", convert_date)
2105-
sqlite3.register_converter("datetime", convert_datetime)
21062104
sqlite3.register_converter("timestamp", convert_timestamp)
21072105

21082106
def sql_schema(self) -> str:

pandas/tests/io/test_sql.py

+18
Original file line numberDiff line numberDiff line change
@@ -2970,6 +2970,24 @@ def test_roundtripping_datetimes(self):
29702970
assert result == "2020-12-31 12:00:00.000000"
29712971

29722972

2973+
@pytest.fixture
2974+
def sqlite_builtin_detect_types():
2975+
with contextlib.closing(
2976+
sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
2977+
) as closing_conn:
2978+
with closing_conn as conn:
2979+
yield conn
2980+
2981+
2982+
def test_roundtripping_datetimes_detect_types(sqlite_builtin_detect_types):
2983+
# https://github.com/pandas-dev/pandas/issues/55554
2984+
conn = sqlite_builtin_detect_types
2985+
df = DataFrame({"t": [datetime(2020, 12, 31, 12)]}, dtype="datetime64[ns]")
2986+
df.to_sql("test", conn, if_exists="replace", index=False)
2987+
result = pd.read_sql("select * from test", conn).iloc[0, 0]
2988+
assert result == Timestamp("2020-12-31 12:00:00.000000")
2989+
2990+
29732991
@pytest.mark.db
29742992
class TestMySQLAlchemy(_TestSQLAlchemy):
29752993
"""

0 commit comments

Comments
 (0)