Skip to content

Commit 99d5e5b

Browse files
DOC: Add doc-string examples for pd.read_sql using custom parse_dates arg values
1 parent e91cbd7 commit 99d5e5b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

pandas/io/sql.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,41 @@ def read_sql(
478478
-------
479479
DataFrame or Iterator[DataFrame]
480480
481+
Examples
482+
--------
483+
Read data from SQL via either a SQL tablename or a SQL query
484+
485+
>>> pd.read_sql('table_name', 'postgres:///db_name') # doctest:+SKIP
486+
487+
>>> pd.read_sql('SELECT * FROM table_name', 'postgres:///db_name') # doctest:+SKIP
488+
489+
Apply dateparsing to columns through the "parse_dates" argument
490+
491+
>>> pd.read_sql('table_name',
492+
... 'postgres:///db_name',
493+
... parse_dates=["date_column"]) # doctest:+SKIP
494+
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:
498+
1. Ignore errors while parsing the values of "date_column"
499+
500+
>>> pd.read_sql('table_name',
501+
... 'postgres:///db_name',
502+
... parse_dates={"date_column": {"errors": "ignore"}) # doctest:+SKIP
503+
504+
2. Apply a dayfirst dateparsing order on the values of "date_column"
505+
506+
>>> pd.read_sql('table_name',
507+
... 'postgres:///db_name',
508+
... parse_dates={"date_column": {"dayfirst": True}) # doctest:+SKIP
509+
510+
3. Apply custom formatting when dateparsing the values of "date_column"
511+
512+
>>> pd.read_sql('table_name',
513+
... 'postgres:///db_name',
514+
... parse_dates={"date_column": {"format": "%d/%m/%Y"}) # doctest:+SKIP
515+
481516
See Also
482517
--------
483518
read_sql_table : Read SQL database table into a DataFrame.

0 commit comments

Comments
 (0)