@@ -478,45 +478,66 @@ def read_sql(
478
478
-------
479
479
DataFrame or Iterator[DataFrame]
480
480
481
- Examples
481
+ See Also
482
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
483
+ read_sql_table : Read SQL database table into a DataFrame.
484
+ read_sql_query : Read SQL query into a DataFrame.
494
485
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:
486
+ Examples
487
+ --------
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)
495
+
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
500
+
501
+ >>> pd.read_sql('test_data', 'postgres:///db_name') # doctest:+SKIP
502
+
503
+ Apply dateparsing to columns through the ``parse_dates`` argument
504
+
505
+ >>> pd.read_sql('SELECT int_column, date_column FROM test_data',
506
+ ... conn,
507
+ ... parse_dates=["date_column"])
508
+ int_column date_column
509
+ 0 0 2012-10-11
510
+ 1 1 2010-12-11
511
+
512
+ The ``parse_dates`` argument calls ``pd.to_datetime`` on the provided columns.
513
+ Custom argument values for applying ``pd.to_datetime`` on a column are specified
514
+ via a dictionary format:
498
515
1. Ignore errors while parsing the values of "date_column"
499
516
500
- >>> pd.read_sql('table_name',
501
- ... 'postgres:///db_name',
502
- ... parse_dates={"date_column": {"errors": "ignore"}) # doctest:+SKIP
517
+ >>> pd.read_sql('SELECT int_column, date_column FROM test_data',
518
+ ... conn,
519
+ ... parse_dates={"date_column": {"errors": "ignore"}})
520
+ int_column date_column
521
+ 0 0 2012-10-11
522
+ 1 1 2010-12-11
503
523
504
524
2. Apply a dayfirst dateparsing order on the values of "date_column"
505
525
506
- >>> pd.read_sql('table_name',
507
- ... 'postgres:///db_name',
508
- ... parse_dates={"date_column": {"dayfirst": True}) # doctest:+SKIP
526
+ >>> pd.read_sql('SELECT int_column, date_column FROM test_data',
527
+ ... conn,
528
+ ... parse_dates={"date_column": {"dayfirst": True}})
529
+ int_column date_column
530
+ 0 0 2012-11-10
531
+ 1 1 2010-11-12
509
532
510
533
3. Apply custom formatting when dateparsing the values of "date_column"
511
534
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.
535
+ >>> pd.read_sql('SELECT int_column, date_column FROM test_data',
536
+ ... conn,
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
520
541
"""
521
542
pandas_sql = pandasSQL_builder (con )
522
543
0 commit comments