Skip to content

Commit 0539255

Browse files
Update doctests
1 parent 99d5e5b commit 0539255

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

pandas/io/sql.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -478,45 +478,51 @@ def read_sql(
478478
-------
479479
DataFrame or Iterator[DataFrame]
480480
481+
See Also
482+
--------
483+
read_sql_table : Read SQL database table into a DataFrame.
484+
read_sql_query : Read SQL query into a DataFrame.
485+
481486
Examples
482487
--------
483-
Read data from SQL via either a SQL tablename or a SQL query
488+
Read data from SQL via either a SQL query or a SQL tablename (latter not
489+
possible for SQLite tables)
490+
>>> from sqlite3 import connect
491+
>>> conn = connect('file.db')
492+
>>> df = pd.DataFrame(data=[[0, '10/11/12'], [1, '12/11/10']],
493+
... columns=['int_column', 'date_column'])
494+
>>> df.to_sql('test_data', conn)
484495
485-
>>> pd.read_sql('table_name', 'postgres:///db_name') # doctest:+SKIP
496+
>>> pd.read_sql('SELECT * FROM test_data', con)
486497
487-
>>> pd.read_sql('SELECT * FROM table_name', 'postgres:///db_name') # doctest:+SKIP
498+
>>> pd.read_sql('table_name', 'postgres:///db_name') # doctest:+SKIP
488499
489-
Apply dateparsing to columns through the "parse_dates" argument
500+
Apply dateparsing to columns through the ``parse_dates`` argument
490501
491-
>>> pd.read_sql('table_name',
492-
... 'postgres:///db_name',
493-
... parse_dates=["date_column"]) # doctest:+SKIP
502+
>>> pd.read_sql('SELECT * FROM test_data',
503+
... conn,
504+
... parse_dates=["date_column"])
494505
495-
The "parse_dates" argument calls pd.to_datetime on the provided columns. Custom
496-
argument values for applying pd.to_datetime on a column are specified via a
497-
dictionary format:
506+
The ``parse_dates`` argument calls ``pd.to_datetime`` on the provided columns.
507+
Custom argument values for applying ``pd.to_datetime`` on a column are specified
508+
via a dictionary format:
498509
1. Ignore errors while parsing the values of "date_column"
499510
500-
>>> pd.read_sql('table_name',
501-
... 'postgres:///db_name',
502-
... parse_dates={"date_column": {"errors": "ignore"}) # doctest:+SKIP
511+
>>> pd.read_sql('SELECT * FROM test_data',
512+
... conn,
513+
... parse_dates={"date_column": {"errors": "ignore"})
503514
504515
2. Apply a dayfirst dateparsing order on the values of "date_column"
505516
506-
>>> pd.read_sql('table_name',
507-
... 'postgres:///db_name',
508-
... parse_dates={"date_column": {"dayfirst": True}) # doctest:+SKIP
517+
>>> pd.read_sql('SELECT * FROM test_data',
518+
... conn,
519+
... parse_dates={"date_column": {"dayfirst": True})
509520
510521
3. Apply custom formatting when dateparsing the values of "date_column"
511522
512-
>>> pd.read_sql('table_name',
513-
... 'postgres:///db_name',
514-
... parse_dates={"date_column": {"format": "%d/%m/%Y"}) # doctest:+SKIP
515-
516-
See Also
517-
--------
518-
read_sql_table : Read SQL database table into a DataFrame.
519-
read_sql_query : Read SQL query into a DataFrame.
523+
>>> pd.read_sql('SELECT * FROM test_data',
524+
... conn,
525+
... parse_dates={"date_column": {"format": "%d/%m/%Y"})
520526
"""
521527
pandas_sql = pandasSQL_builder(con)
522528

0 commit comments

Comments
 (0)