Skip to content

Commit 0dde33c

Browse files
avinashpanchamluckyvs1
authored andcommitted
DOC: Add doc-string examples for pd.read_sql using custom parse_dates arg values (pandas-dev#38475)
1 parent 65b4489 commit 0dde33c

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

ci/code_checks.sh

+4
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ if [[ -z "$CHECK" || "$CHECK" == "doctests" ]]; then
178178
pytest -q --doctest-modules pandas/core/strings/
179179
RET=$(($RET + $?)) ; echo $MSG "DONE"
180180

181+
MSG='Doctests sql.py' ; echo $MSG
182+
pytest -q --doctest-modules pandas/io/sql.py
183+
RET=$(($RET + $?)) ; echo $MSG "DONE"
184+
181185
# Directories
182186

183187
MSG='Doctests arrays'; echo $MSG

pandas/io/sql.py

+58
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,64 @@ def read_sql(
482482
--------
483483
read_sql_table : Read SQL database table into a DataFrame.
484484
read_sql_query : Read SQL query into a DataFrame.
485+
486+
Examples
487+
--------
488+
Read data from SQL via either a SQL query or a SQL tablename.
489+
When using a SQLite database only SQL queries are accepted,
490+
providing only the SQL tablename will result in an error.
491+
492+
>>> from sqlite3 import connect
493+
>>> conn = connect(':memory:')
494+
>>> df = pd.DataFrame(data=[[0, '10/11/12'], [1, '12/11/10']],
495+
... columns=['int_column', 'date_column'])
496+
>>> df.to_sql('test_data', conn)
497+
498+
>>> pd.read_sql('SELECT int_column, date_column FROM test_data', conn)
499+
int_column date_column
500+
0 0 10/11/12
501+
1 1 12/11/10
502+
503+
>>> pd.read_sql('test_data', 'postgres:///db_name') # doctest:+SKIP
504+
505+
Apply date parsing to columns through the ``parse_dates`` argument
506+
507+
>>> pd.read_sql('SELECT int_column, date_column FROM test_data',
508+
... conn,
509+
... parse_dates=["date_column"])
510+
int_column date_column
511+
0 0 2012-10-11
512+
1 1 2010-12-11
513+
514+
The ``parse_dates`` argument calls ``pd.to_datetime`` on the provided columns.
515+
Custom argument values for applying ``pd.to_datetime`` on a column are specified
516+
via a dictionary format:
517+
1. Ignore errors while parsing the values of "date_column"
518+
519+
>>> pd.read_sql('SELECT int_column, date_column FROM test_data',
520+
... conn,
521+
... parse_dates={"date_column": {"errors": "ignore"}})
522+
int_column date_column
523+
0 0 2012-10-11
524+
1 1 2010-12-11
525+
526+
2. Apply a dayfirst date parsing order on the values of "date_column"
527+
528+
>>> pd.read_sql('SELECT int_column, date_column FROM test_data',
529+
... conn,
530+
... parse_dates={"date_column": {"dayfirst": True}})
531+
int_column date_column
532+
0 0 2012-11-10
533+
1 1 2010-11-12
534+
535+
3. Apply custom formatting when date parsing the values of "date_column"
536+
537+
>>> pd.read_sql('SELECT int_column, date_column FROM test_data',
538+
... conn,
539+
... parse_dates={"date_column": {"format": "%d/%m/%y"}})
540+
int_column date_column
541+
0 0 2012-11-10
542+
1 1 2010-11-12
485543
"""
486544
pandas_sql = pandasSQL_builder(con)
487545

0 commit comments

Comments
 (0)