Skip to content

DOC: Add doc-string examples for pd.read_sql using custom parse_dates arg values #38475

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

Merged
merged 6 commits into from
Dec 17, 2020

Conversation

avinashpancham
Copy link
Contributor

follow on PR for #37823

  • closes #xxxx
  • tests added / passed
  • passes black pandas
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

pandas/io/sql.py Outdated
--------
Read data from SQL via either a SQL tablename or a SQL query

>>> pd.read_sql('table_name', 'postgres:///db_name') # doctest:+SKIP
Copy link
Member

Choose a reason for hiding this comment

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

Maybe the examples can demonstrate connecting to a sqlite db so we don't have to skip all these doctests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

pandas/io/sql.py Outdated
... 'postgres:///db_name',
... parse_dates=["date_column"]) # doctest:+SKIP

The "parse_dates" argument calls pd.to_datetime on the provided columns. Custom
Copy link
Member

Choose a reason for hiding this comment

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

Double backticks around pd.to_datetime

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

pandas/io/sql.py Outdated

>>> pd.read_sql('SELECT * FROM table_name', 'postgres:///db_name') # doctest:+SKIP

Apply dateparsing to columns through the "parse_dates" argument
Copy link
Member

Choose a reason for hiding this comment

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

Double backticks around parse_dates instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@avinashpancham
Copy link
Contributor Author

Added doctests pass.

CI fails due to other reasons, since I only added documentation. Will merge master later to run CI agian.

pandas/io/sql.py Outdated
Read data from SQL via either a SQL query or a SQL tablename (latter not
possible for SQLite tables)
>>> from sqlite3 import connect
>>> conn = connect('file.db')
Copy link
Member

Choose a reason for hiding this comment

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

You can use ":memory:" instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool, didn't know this! Changed it.

pandas/io/sql.py Outdated
Examples
--------
Read data from SQL via either a SQL query or a SQL tablename (latter not
possible for SQLite tables)
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a newline here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

pandas/io/sql.py Outdated

>>> pd.read_sql('test_data', 'postgres:///db_name') # doctest:+SKIP

Apply dateparsing to columns through the ``parse_dates`` argument
Copy link
Member

@mroeschke mroeschke Dec 16, 2020

Choose a reason for hiding this comment

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

date parsing (and elsewhere)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

pandas/io/sql.py Outdated
>>> pd.read_sql('SELECT int_column, date_column FROM test_data',
... conn,
... parse_dates=["date_column"])
int_column date_column
Copy link
Member

Choose a reason for hiding this comment

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

These column names looks offset

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

pandas/io/sql.py Outdated
>>> pd.read_sql('SELECT int_column, date_column FROM test_data',
... conn,
... parse_dates={"date_column": {"errors": "ignore"}})
int_column date_column
Copy link
Member

Choose a reason for hiding this comment

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

Same

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

pandas/io/sql.py Outdated
>>> pd.read_sql('SELECT int_column, date_column FROM test_data',
... conn,
... parse_dates={"date_column": {"format": "%d/%m/%y"}})
int_column date_column
Copy link
Member

Choose a reason for hiding this comment

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

Same

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

pandas/io/sql.py Outdated
Examples
--------
Read data from SQL via either a SQL query or a SQL tablename (latter not
possible for SQLite tables)
Copy link
Member

Choose a reason for hiding this comment

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

I would assume parse_dates should still work for sqlite.

Our docs state

Dict of {column_name: arg dict}, where the arg dict corresponds to the keyword arguments of pandas.to_datetime() Especially useful with databases without native Datetime support, such as SQLite.

Copy link
Contributor Author

@avinashpancham avinashpancham Dec 16, 2020

Choose a reason for hiding this comment

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

Sorry, I will clarify this. What I mean is that SQLite only accepts queries not just table names:

pd.read_sql('SELECT * FROM test_data', conn) # works for SQLite
pd.read_sql('test_data', conn) # does not work for SQLite
# pandas.io.sql.DatabaseError: Execution failed on sql 'test_data': near "test_data": syntax error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made explanation more explicit

@mroeschke mroeschke added Docs IO SQL to_sql, read_sql, read_sql_query labels Dec 16, 2020
@mroeschke mroeschke added this to the 1.3 milestone Dec 16, 2020
@jreback jreback merged commit d4b6233 into pandas-dev:master Dec 17, 2020
@jreback
Copy link
Contributor

jreback commented Dec 17, 2020

very nice @avinashpancham

luckyvs1 pushed a commit to luckyvs1/pandas that referenced this pull request Jan 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs IO SQL to_sql, read_sql, read_sql_query
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants