@@ -1424,3 +1424,98 @@ These, by default, index the three axes ``items, major_axis, minor_axis``. On an
1424
1424
store.close()
1425
1425
import os
1426
1426
os.remove(' store.h5' )
1427
+
1428
+
1429
+ .. _io.sql :
1430
+
1431
+ DataBase Queries
1432
+ ----------------
1433
+
1434
+ The :mod: `pandas.io.sql ` module provides a collection of query wrappers to both
1435
+ facilitate data retrieval and to reduce dependency on DB-specific API. There
1436
+ wrappers only support the Python database adapters which respect the `Python
1437
+ DB-API <http://www.python.org/dev/peps/pep-0249/> `_.
1438
+
1439
+ Suppose you want to query some data with different types from a table such as:
1440
+
1441
+ +-----+------------+-------+-------+-------+
1442
+ | id | Date | Col_1 | Col_2 | Col_3 |
1443
+ +=====+============+=======+=======+=======+
1444
+ | 26 | 2012-10-18 | X | 25.7 | True |
1445
+ +-----+------------+-------+-------+-------+
1446
+ | 42 | 2012-10-19 | Y | -12.4 | False |
1447
+ +-----+------------+-------+-------+-------+
1448
+ | 63 | 2012-10-20 | Z | 5.73 | True |
1449
+ +-----+------------+-------+-------+-------+
1450
+
1451
+ Functions from :mod: `pandas.io.sql ` can extract some data into a DataFrame. In
1452
+ the following example, we use `SQlite <http://www.sqlite.org/ >`_ SQL database
1453
+ engine. You can use a temporary SQLite database where data are stored in
1454
+ "memory". Just do:
1455
+
1456
+ .. code-block :: python
1457
+
1458
+ import sqlite3
1459
+ from pandas.io import sql
1460
+ # Create your connection.
1461
+ cnx = sqlite3.connect(' :memory:' )
1462
+
1463
+ .. ipython :: python
1464
+ :suppress:
1465
+
1466
+ import sqlite3
1467
+ from pandas.io import sql
1468
+ cnx = sqlite3.connect(' :memory:' )
1469
+
1470
+ .. ipython :: python
1471
+ :suppress:
1472
+
1473
+ cu = cnx.cursor()
1474
+ # Create a table named 'data'.
1475
+ cu.execute(""" CREATE TABLE data(id integer,
1476
+ date date,
1477
+ Col_1 string,
1478
+ Col_2 float,
1479
+ Col_3 bool);""" )
1480
+ cu.executemany(' INSERT INTO data VALUES (?,?,?,?,?)' ,
1481
+ [(26 , datetime(2010 ,10 ,18 ), ' X' , 27.5 , True ),
1482
+ (42 , datetime(2010 ,10 ,19 ), ' Y' , - 12.5 , False ),
1483
+ (63 , datetime(2010 ,10 ,20 ), ' Z' , 5.73 , True )])
1484
+
1485
+
1486
+ Let ``data `` be the name of your SQL table. With a query and your database
1487
+ connection, just use the :func: `~pandas.io.sql.read_frame ` function to get the
1488
+ query results into a DataFrame:
1489
+
1490
+ .. ipython :: python
1491
+
1492
+ sql.read_frame(" SELECT * FROM data;" , cnx)
1493
+
1494
+ You can also specify the name of the column as the DataFrame index:
1495
+
1496
+ .. ipython :: python
1497
+
1498
+ sql.read_frame(" SELECT * FROM data;" , cnx, index_col = ' id' )
1499
+ sql.read_frame(" SELECT * FROM data;" , cnx, index_col = ' date' )
1500
+
1501
+ Of course, you can specify more "complex" query.
1502
+
1503
+ .. ipython :: python
1504
+
1505
+ sql.read_frame(" SELECT id, Col_1, Col_2 FROM data WHERE id = 42;" , cnx)
1506
+
1507
+ .. ipython :: python
1508
+ :suppress:
1509
+
1510
+ cu.close()
1511
+ cnx.close()
1512
+
1513
+ .. note ::
1514
+
1515
+ For now, writing your DataFrame into a database works only with
1516
+ SQLite. Moreover, the index will currently be dropped.
1517
+
1518
+ .. todo ::
1519
+
1520
+ - methods list
1521
+
0 commit comments