@@ -482,6 +482,64 @@ def read_sql(
482
482
--------
483
483
read_sql_table : Read SQL database table into a DataFrame.
484
484
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
485
543
"""
486
544
pandas_sql = pandasSQL_builder (con )
487
545
0 commit comments