-
-
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
Changes from 4 commits
99d5e5b
46ffa21
602040f
98eecb1
82ff533
7409ccb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -482,6 +482,62 @@ def read_sql( | |
-------- | ||
read_sql_table : Read SQL database table into a DataFrame. | ||
read_sql_query : Read SQL query into a DataFrame. | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. I would assume Our docs state
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Made explanation more explicit |
||
>>> from sqlite3 import connect | ||
>>> conn = connect('file.db') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool, didn't know this! Changed it. |
||
>>> df = pd.DataFrame(data=[[0, '10/11/12'], [1, '12/11/10']], | ||
... columns=['int_column', 'date_column']) | ||
>>> df.to_sql('test_data', conn) | ||
|
||
>>> pd.read_sql('SELECT int_column, date_column FROM test_data', conn) | ||
int_column date_column | ||
0 0 10/11/12 | ||
1 1 12/11/10 | ||
|
||
>>> 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 commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
>>> 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
0 0 2012-10-11 | ||
1 1 2010-12-11 | ||
|
||
The ``parse_dates`` argument calls ``pd.to_datetime`` on the provided columns. | ||
Custom argument values for applying ``pd.to_datetime`` on a column are specified | ||
via a dictionary format: | ||
1. Ignore errors while parsing the values of "date_column" | ||
|
||
>>> 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
0 0 2012-10-11 | ||
1 1 2010-12-11 | ||
|
||
2. Apply a dayfirst dateparsing order on the values of "date_column" | ||
|
||
>>> pd.read_sql('SELECT int_column, date_column FROM test_data', | ||
... conn, | ||
... parse_dates={"date_column": {"dayfirst": True}}) | ||
int_column date_column | ||
0 0 2012-11-10 | ||
1 1 2010-11-12 | ||
|
||
3. Apply custom formatting when dateparsing the values of "date_column" | ||
|
||
>>> 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
0 0 2012-11-10 | ||
1 1 2010-11-12 | ||
""" | ||
pandas_sql = pandasSQL_builder(con) | ||
|
||
|
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