-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
DOC: Add doc-string examples for pd.read_sql using custom parse_dates arg values #38475
Conversation
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
4f305bf
to
46ffa21
Compare
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') |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
date parsing
(and elsewhere)
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made explanation more explicit
c058dcc
to
82ff533
Compare
very nice @avinashpancham |
follow on PR for #37823
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff