Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REGR: fix roundtripping datetimes with sqlite type detection #55690

Merged

Conversation

jorisvandenbossche
Copy link
Member

#54985 was already a related regression fix in 2.1.1, but that only fixed the adapter (conversion to sqlite), not the converter (conversion back from sqlite to python). This converter only gets used when explicitly asking sqlite to detect dtypes.

@jorisvandenbossche jorisvandenbossche added Regression Functionality that used to work in a prior pandas version IO SQL to_sql, read_sql, read_sql_query labels Oct 25, 2023
@jorisvandenbossche jorisvandenbossche added this to the 2.1.2 milestone Oct 25, 2023
@jorisvandenbossche jorisvandenbossche marked this pull request as ready for review October 25, 2023 22:14

sqlite3.register_adapter(time, _adapt_time)

sqlite3.register_adapter(date, adapt_date_iso)
sqlite3.register_adapter(datetime, adapt_datetime_iso)

convert_date = lambda val: date.fromisoformat(val.decode())
convert_datetime = lambda val: datetime.fromisoformat(val.decode())
convert_timestamp = lambda val: datetime.fromtimestamp(int(val))
convert_timestamp = lambda val: datetime.fromisoformat(val.decode())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lithomas1 I think you added those from the documentation, but the actual implementation in cpython that we inherited is different: https://github.com/erlend-aasland/cpython/blob/07988dc731bc1e0f6ce2aac201d52e4b78618877/Lib/sqlite3/dbapi2.py#L71-L87

I didn't add that implementation exactly, as I think it should essentially be the same as parsing with fromisoformat (I suppose that didn't exist yet when this was originally implemented in cpython)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super sorry about this.

I just copied from https://docs.python.org/3/library/sqlite3.html#sqlite3-adapter-converter-recipes blindly when I was doing the changes for Python 3.12, and didn't pay super close attention since tests didn't fail.

Unfortunately, looks like my oversight bit us twice :( .

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, not having test coverage doesn't help, and I have to say that the python docs are also quite misleading because it doesn't seem to work by copying verbatim

@lithomas1
Copy link
Member

The changes LGTM assuming this is green.

Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
@jorisvandenbossche jorisvandenbossche merged commit aeb3644 into pandas-dev:main Oct 26, 2023
36 of 39 checks passed
@lumberbot-app
Copy link

lumberbot-app bot commented Oct 26, 2023

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
git checkout 2.1.x
git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
git cherry-pick -x -m1 aeb3644567a10296cc09aed03bf7e0a3780e67ee
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #55690: REGR: fix roundtripping datetimes with sqlite type detection'
  1. Push to a named branch:
git push YOURFORK 2.1.x:auto-backport-of-pr-55690-on-2.1.x
  1. Create a PR against branch 2.1.x, I would have named this PR:

"Backport PR #55690 on branch 2.1.x (REGR: fix roundtripping datetimes with sqlite type detection)"

And apply the correct labels and milestones.

Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove the Still Needs Manual Backport label once the PR gets merged.

If these instructions are inaccurate, feel free to suggest an improvement.

@jorisvandenbossche jorisvandenbossche deleted the fix-sqlite-regr branch October 26, 2023 09:58
jorisvandenbossche added a commit to jorisvandenbossche/pandas that referenced this pull request Oct 26, 2023
…dev#55690)

Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
(cherry picked from commit aeb3644)
@jorisvandenbossche
Copy link
Member Author

Manual backport -> #55702

jorisvandenbossche added a commit that referenced this pull request Oct 26, 2023
… with sqlite type detection) (#55702)

REGR: fix roundtripping datetimes with sqlite type detection (#55690)

(cherry picked from commit aeb3644)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO SQL to_sql, read_sql, read_sql_query Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: after pandas 2.1.0 sqlite isnt able to auto parse dates based on schema
3 participants