Skip to content

Commit 4f305bf

Browse files
Update doctests
1 parent 0539255 commit 4f305bf

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

pandas/io/sql.py

+24-9
Original file line numberDiff line numberDiff line change
@@ -493,36 +493,51 @@ def read_sql(
493493
... columns=['int_column', 'date_column'])
494494
>>> df.to_sql('test_data', conn)
495495
496-
>>> pd.read_sql('SELECT * FROM test_data', con)
496+
>>> pd.read_sql('SELECT int_column, date_column FROM test_data', conn)
497+
int_column date_column
498+
0 0 10/11/12
499+
1 1 12/11/10
497500
498-
>>> pd.read_sql('table_name', 'postgres:///db_name') # doctest:+SKIP
501+
>>> pd.read_sql('test_data', 'postgres:///db_name') # doctest:+SKIP
499502
500503
Apply dateparsing to columns through the ``parse_dates`` argument
501504
502-
>>> pd.read_sql('SELECT * FROM test_data',
505+
>>> pd.read_sql('SELECT int_column, date_column FROM test_data',
503506
... conn,
504507
... parse_dates=["date_column"])
508+
int_column date_column
509+
0 0 2012-10-11
510+
1 1 2010-12-11
505511
506512
The ``parse_dates`` argument calls ``pd.to_datetime`` on the provided columns.
507513
Custom argument values for applying ``pd.to_datetime`` on a column are specified
508514
via a dictionary format:
509515
1. Ignore errors while parsing the values of "date_column"
510516
511-
>>> pd.read_sql('SELECT * FROM test_data',
517+
>>> pd.read_sql('SELECT int_column, date_column FROM test_data',
512518
... conn,
513-
... parse_dates={"date_column": {"errors": "ignore"})
519+
... parse_dates={"date_column": {"errors": "ignore"}})
520+
int_column date_column
521+
0 0 2012-10-11
522+
1 1 2010-12-11
514523
515524
2. Apply a dayfirst dateparsing order on the values of "date_column"
516525
517-
>>> pd.read_sql('SELECT * FROM test_data',
526+
>>> pd.read_sql('SELECT int_column, date_column FROM test_data',
518527
... conn,
519-
... parse_dates={"date_column": {"dayfirst": True})
528+
... parse_dates={"date_column": {"dayfirst": True}})
529+
int_column date_column
530+
0 0 2012-11-10
531+
1 1 2010-11-12
520532
521533
3. Apply custom formatting when dateparsing the values of "date_column"
522534
523-
>>> pd.read_sql('SELECT * FROM test_data',
535+
>>> pd.read_sql('SELECT int_column, date_column FROM test_data',
524536
... conn,
525-
... parse_dates={"date_column": {"format": "%d/%m/%Y"})
537+
... parse_dates={"date_column": {"format": "%d/%m/%y"}})
538+
int_column date_column
539+
0 0 2012-11-10
540+
1 1 2010-11-12
526541
"""
527542
pandas_sql = pandasSQL_builder(con)
528543

0 commit comments

Comments
 (0)