In [266]: for chunk in reader:
.....: print(chunk)
.....:
Empty DataFrame
Columns: []
Index: []
a b
0 1 2
a b
1 3 4
```
### 表模式(Table schema)
*New in version 0.20.0.*
表模式([Table schema](https://specs.frictionlessdata.io/json-table-schema/))是用于将表格数据集描述为JSON对象的一种规范。 JSON包含有关字段名称,类型和其他属性的信息。 你可以使用面向`table`来构建一个JSON字符串包含两个字段,`schema`和`data`。
``` python
In [267]: df = pd.DataFrame({'A': [1, 2, 3],
.....: 'B': ['a', 'b', 'c'],
.....: 'C': pd.date_range('2016-01-01', freq='d', periods=3)},
.....: index=pd.Index(range(3), name='idx'))
.....:
In [268]: df
Out[268]:
A B C
idx
0 1 a 2016-01-01
1 2 b 2016-01-02
2 3 c 2016-01-03
In [269]: df.to_json(orient='table', date_format="iso")
Out[269]: '{"schema": {"fields":[{"name":"idx","type":"integer"},{"name":"A","type":"integer"},{"name":"B","type":"string"},{"name":"C","type":"datetime"}],"primaryKey":["idx"],"pandas_version":"0.20.0"}, "data": [{"idx":0,"A":1,"B":"a","C":"2016-01-01T00:00:00.000Z"},{"idx":1,"A":2,"B":"b","C":"2016-01-02T00:00:00.000Z"},{"idx":2,"A":3,"B":"c","C":"2016-01-03T00:00:00.000Z"}]}'
```
`schema`字段包含`fields`主键,它本身包含一个列名称到列对的列表,包括`Index`或`MultiIndex`(请参阅下面的类型列表)。 如果(多)索引是唯一的,则`schema`字段也包含一个`primaryKey`字段。
第二个字段`data`包含用面向`records`来序列化数据。 索引是包括的,并且任何日期时间都是ISO 8601格式,正如表模式规范所要求的那样。
表模式规范中描述了所有支持的全部类型列表。 此表显示了pandas类型的映射:
Pandas type | Table Schema type
---|---
int64 | integer
float64 | number
bool | boolean
datetime64[ns] | datetime
timedelta64[ns] | duration
categorical | any
object | str
关于生成的表模式的一些注意事项:
- `schema`对象包含`pandas_version`的字段。 它包含模式的pandas方言版本,并将随每个修订增加。
- 序列化时,所有日期都转换为UTC。 甚至是时区的初始值,也被视为UTC,偏移量为0。
``` python
In [270]: from pandas.io.json import build_table_schema
In [271]: s = pd.Series(pd.date_range('2016', periods=4))
In [272]: build_table_schema(s)
Out[272]:
{'fields': [{'name': 'index', 'type': 'integer'},
{'name': 'values', 'type': 'datetime'}],
'primaryKey': ['index'],
'pandas_version': '0.20.0'}
```
- 具有时区的日期时间(在序列化之前),包括具有时区名称的附加字段`tz`(例如:`'US / Central'`)。
``` python
In [273]: s_tz = pd.Series(pd.date_range('2016', periods=12,
.....: tz='US/Central'))
.....:
In [274]: build_table_schema(s_tz)
Out[274]:
{'fields': [{'name': 'index', 'type': 'integer'},
{'name': 'values', 'type': 'datetime', 'tz': 'US/Central'}],
'primaryKey': ['index'],
'pandas_version': '0.20.0'}
```
- 时间段在序列化之前是转换为时间戳的,因此具有转换为UTC的相同方式。 此外,时间段将包含具有时间段频率的附加字段`freq`,例如:`'A-DEC'`。
``` python
In [275]: s_per = pd.Series(1, index=pd.period_range('2016', freq='A-DEC',
.....: periods=4))
.....:
In [276]: build_table_schema(s_per)
Out[276]:
{'fields': [{'name': 'index', 'type': 'datetime', 'freq': 'A-DEC'},
{'name': 'values', 'type': 'integer'}],
'primaryKey': ['index'],
'pandas_version': '0.20.0'}
```
- 分类使用`any`类型和`enum`约束来列出可能值的集合。 此外,还包括一个`ordered`字段:
``` python
In [277]: s_cat = pd.Series(pd.Categorical(['a', 'b', 'a']))
In [278]: build_table_schema(s_cat)
Out[278]:
{'fields': [{'name': 'index', 'type': 'integer'},
{'name': 'values',
'type': 'any',
'constraints': {'enum': ['a', 'b']},
'ordered': False}],
'primaryKey': ['index'],
'pandas_version': '0.20.0'}
```
- 如果索引是唯一的,则包含`primaryKey`字段,它包含了标签数组:
``` python
In [279]: s_dupe = pd.Series([1, 2], index=[1, 1])
In [280]: build_table_schema(s_dupe)
Out[280]:
{'fields': [{'name': 'index', 'type': 'integer'},
{'name': 'values', 'type': 'integer'}],
'pandas_version': '0.20.0'}
```
- `primaryKey `的形式与多索引相同,但在这种情况下,`primaryKey`是一个数组:
``` python
In [281]: s_multi = pd.Series(1, index=pd.MultiIndex.from_product([('a', 'b'),
.....: (0, 1)]))
.....:
In [282]: build_table_schema(s_multi)
Out[282]:
{'fields': [{'name': 'level_0', 'type': 'string'},
{'name': 'level_1', 'type': 'integer'},
{'name': 'values', 'type': 'integer'}],
'primaryKey': FrozenList(['level_0', 'level_1']),
'pandas_version': '0.20.0'}
```
- 默认命名大致遵循以下规则:
- 对于series,使用`object.name`。 如果没有,那么名称就是`values`
- 对于`DataFrames`,使用列名称的字符串化版本
- 对于`Index`(不是`MultiIndex`),使用`index.name`,如果为None,则使用回退`index`。
- 对于`MultiIndex`,使用`mi.names`。 如果任何级别没有名称,则使用`level_`。
*New in version 0.23.0.*
`read_json`也接受`orient ='table'`作为参数。 这允许以可循环移动的方式保存诸如dtypes和索引名称之类的元数据。
``` python
In [283]: df = pd.DataFrame({'foo': [1, 2, 3, 4],
.....: 'bar': ['a', 'b', 'c', 'd'],
.....: 'baz': pd.date_range('2018-01-01', freq='d', periods=4),
.....: 'qux': pd.Categorical(['a', 'b', 'c', 'c'])
.....: }, index=pd.Index(range(4), name='idx'))
.....:
In [284]: df
Out[284]:
foo bar baz qux
idx
0 1 a 2018-01-01 a
1 2 b 2018-01-02 b
2 3 c 2018-01-03 c
3 4 d 2018-01-04 c
In [285]: df.dtypes
Out[285]:
foo int64
bar object
baz datetime64[ns]
qux category
dtype: object
In [286]: df.to_json('test.json', orient='table')
In [287]: new_df = pd.read_json('test.json', orient='table')
In [288]: new_df
Out[288]:
foo bar baz qux
idx
0 1 a 2018-01-01 a
1 2 b 2018-01-02 b
2 3 c 2018-01-03 c
3 4 d 2018-01-04 c
In [289]: new_df.dtypes
Out[289]:
foo int64
bar object
baz datetime64[ns]
qux category
dtype: object
```
请注意,作为 [Index](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Index.html#pandas.Index) 名称的文字字符串'index'是不能循环移动的,也不能在 [MultiIndex](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.MultiIndex.html#pandas.MultiIndex) 中用以`'level_'`开头的任何名称。 这些默认情况下在 [DataFrame.to_json()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_json.html#pandas.DataFrame.to_json) 中用于指示缺失值和后续读取无法区分的目的。
``` python
In [290]: df.index.name = 'index'
In [291]: df.to_json('test.json', orient='table')
In [292]: new_df = pd.read_json('test.json', orient='table')
In [293]: print(new_df.index.name)
None
```
## HTML
### 读取HTML的内容
::: danger 警告:
我们**强烈建议**你阅读 [HTML Table Parsing gotchas](https://www.pypandas.cn/docs/user_guide/io.html#io-html-gotchas)里面相关的围绕BeautifulSoup4/html5lib/lxml解析器部分的问题。
:::
顶级的`read_html()`函数能接受HTML字符串/文件/URL格式,并且能解析HTML 表格为pandas`DataFrames`的列表,让我们看看下面的几个例子。
::: tip 注意:
`read_html`返回的是一个`DataFrame`对象的`list`,即便在HTML页面里只包含单个表格。
:::
读取一个没有选项的URL:
```python
In [294]: url = 'https://www.fdic.gov/bank/individual/failed/banklist.html'
In [295]: dfs = pd.read_html(url)
In [296]: dfs
Out[296]:
[ Bank Name City ST CERT Acquiring Institution Closing Date Updated Date
0 The Enloe State Bank Cooper TX 10716 Legend Bank, N. A. May 31, 2019 June 18, 2019
1 Washington Federal Bank for Savings Chicago IL 30570 Royal Savings Bank December 15, 2017 February 1, 2019
2 The Farmers and Merchants State Bank of Argonia Argonia KS 17719 Conway Bank October 13, 2017 February 21, 2018
3 Fayette County Bank Saint Elmo IL 1802 United Fidelity Bank, fsb May 26, 2017 January 29, 2019
4 Guaranty Bank, (d/b/a BestBank in Georgia & Mi... Milwaukee WI 30003 First-Citizens Bank & Trust Company May 5, 2017 March 22, 2018
.. ... ... .. ... ... ... ...
551 Superior Bank, FSB Hinsdale IL 32646 Superior Federal, FSB July 27, 2001 August 19, 2014
552 Malta National Bank Malta OH 6629 North Valley Bank May 3, 2001 November 18, 2002
553 First Alliance Bank & Trust Co. Manchester NH 34264 Southern New Hampshire Bank & Trust February 2, 2001 February 18, 2003
554 National State Bank of Metropolis Metropolis IL 3815 Banterra Bank of Marion December 14, 2000 March 17, 2005
555 Bank of Honolulu Honolulu HI 21029 Bank of the Orient October 13, 2000 March 17, 2005
[556 rows x 7 columns]]
```
::: tip 注意:
上面的URL数据修改了每个周一以至于上面的数据结果跟下面的数据结果可能有轻微的不同。
:::
从上面的URL读取文件内容并且传递它给`read_html`作为一个字符串:
```python
In [297]: with open(file_path, 'r') as f:
.....: dfs = pd.read_html(f.read())
.....:
In [298]: dfs
Out[298]:
[ Bank Name City ST CERT Acquiring Institution Closing Date Updated Date
0 Banks of Wisconsin d/b/a Bank of Kenosha Kenosha WI 35386 North Shore Bank, FSB May 31, 2013 May 31, 2013
1 Central Arizona Bank Scottsdale AZ 34527 Western State Bank May 14, 2013 May 20, 2013
2 Sunrise Bank Valdosta GA 58185 Synovus Bank May 10, 2013 May 21, 2013
3 Pisgah Community Bank Asheville NC 58701 Capital Bank, N.A. May 10, 2013 May 14, 2013
4 Douglas County Bank Douglasville GA 21649 Hamilton State Bank April 26, 2013 May 16, 2013
.. ... ... .. ... ... ... ...
500 Superior Bank, FSB Hinsdale IL 32646 Superior Federal, FSB July 27, 2001 June 5, 2012
501 Malta National Bank Malta OH 6629 North Valley Bank May 3, 2001 November 18, 2002
502 First Alliance Bank & Trust Co. Manchester NH 34264 Southern New Hampshire Bank & Trust February 2, 2001 February 18, 2003
503 National State Bank of Metropolis Metropolis IL 3815 Banterra Bank of Marion December 14, 2000 March 17, 2005
504 Bank of Honolulu Honolulu HI 21029 Bank of the Orient October 13, 2000 March 17, 2005
[505 rows x 7 columns]]
```
甚至如果你想,你还可以传递一个`StringIO`的实例:
```python
In [299]: with open(file_path, 'r') as f:
.....: sio = StringIO(f.read())
.....:
In [300]: dfs = pd.read_html(sio)
In [301]: dfs
Out[301]:
[ Bank Name City ST CERT Acquiring Institution Closing Date Updated Date
0 Banks of Wisconsin d/b/a Bank of Kenosha Kenosha WI 35386 North Shore Bank, FSB May 31, 2013 May 31, 2013
1 Central Arizona Bank Scottsdale AZ 34527 Western State Bank May 14, 2013 May 20, 2013
2 Sunrise Bank Valdosta GA 58185 Synovus Bank May 10, 2013 May 21, 2013
3 Pisgah Community Bank Asheville NC 58701 Capital Bank, N.A. May 10, 2013 May 14, 2013
4 Douglas County Bank Douglasville GA 21649 Hamilton State Bank April 26, 2013 May 16, 2013
.. ... ... .. ... ... ... ...
500 Superior Bank, FSB Hinsdale IL 32646 Superior Federal, FSB July 27, 2001 June 5, 2012
501 Malta National Bank Malta OH 6629 North Valley Bank May 3, 2001 November 18, 2002
502 First Alliance Bank & Trust Co. Manchester NH 34264 Southern New Hampshire Bank & Trust February 2, 2001 February 18, 2003
503 National State Bank of Metropolis Metropolis IL 3815 Banterra Bank of Marion December 14, 2000 March 17, 2005
504 Bank of Honolulu Honolulu HI 21029 Bank of the Orient October 13, 2000 March 17, 2005
[505 rows x 7 columns]]
```
::: tip 注意:
以下的例子在IPython的程序中不会运行,因为有太多的网络接入函数减缓了文档的创建。如果你的程序报错或者例子不运行,请立即向[ pandas GitHub issues page](https://www.github.com/pandas-dev/pandas/issues) 上报。
:::
读取一个URL并匹配表格里面所包含的具体文本内容:
```python
match = 'Metcalf Bank'
df_list = pd.read_html(url, match=match)
```
指定一个标题行(通过默认的\| 或者\ | 定位并伴随一个\被用来作为列的索引,如果是多行含有\,则多索引就会被创建);如果已经指定,标题行则从数据减去已解析的标题元素中获取(\元素)。
```python
dfs = pd.read_html(url, header=0)
```
指定一个索引列:
```python
dfs = pd.read_html(url, index_col=0)
```
指定跳过行的数量:
```python
dfs = pd.read_html(url, skiprows=0)
```
指定使用列表来跳过行的数量(`xrange`(只在Python 2 中)也有效):
```python
dfs = pd.read_html(url, skiprows=range(2))
```
指定一个HTML属性:
```python
dfs1 = pd.read_html(url, attrs={'id': 'table'})
dfs2 = pd.read_html(url, attrs={'class': 'sortable'})
print(np.array_equal(dfs1[0], dfs2[0])) # Should be True
```
指定值将会被转换为NaN(非数值):
```python
dfs = pd.read_html(url, na_values=['No Acquirer'])
```
*New in version 0.19.*
指定是否保持默认的NaN值的设置:
```python
dfs = pd.read_html(url, keep_default_na=False)
```
*New in version 0.19.*
指定列的转换器。这对于有前置零的数字文本数据很有用。默认情况下,数值列会转换成数值类型且前置零会丢失。为了避免这种情况,我们能转换这些列为字符串。
```python
url_mcc = 'https://en.wikipedia.org/wiki/Mobile_country_code'
dfs = pd.read_html(url_mcc, match='Telekom Albania', header=0,
converters={'MNC': str})
```
*New in version 0.19.*
把上面的一些结合使用:
```python
dfs = pd.read_html(url, match='Metcalf Bank', index_col=0)
```
读取pandas`to_html`输出(同时一些精确的浮点会失去):
```python
df = pd.DataFrame(np.random.randn(2, 2))
s = df.to_html(float_format='{0:.40g}'.format)
dfin = pd.read_html(s, index_col=0)
```
如果`lxml`后端是你提供的唯一解析器,那么它将在解析失败时报错。如果你能提供的解析器只有一个就选字符串,但是传递一个字符串列表会是很好的训练,例如,这个函数期望是一个字符串序列。你可以这样使用:
```python
dfs = pd.read_html(url, 'Metcalf Bank', index_col=0, flavor=['lxml'])
```
或者你可以传递`flavor='lxml'`而不要列表:
```python
dfs = pd.read_html(url, 'Metcalf Bank', index_col=0, flavor='lxml')
```
然而,如果你已经安装了bs4 和 html5lib并且传递`None`或`['lxml', 'bs4']`,那么极大可能会解析成功。注意*一旦解析成功了,函数将会返回。*
```python
dfs = pd.read_html(url, 'Metcalf Bank', index_col=0, flavor=['lxml', 'bs4'])
```
### 写入HTML文件
`DataFrame`对象具有实例的方法`to_html`,它能渲染`DataFrame`的内容为HTML表格。这个函数的参数同上面的`to_string`方法的一样。
::: tip 注意:
为了简洁起见,这儿显示的不是所有的`DataFrame.to_html`可选项。所有的选项设置见`to_html()`。
:::
```python
In [302]: df = pd.DataFrame(np.random.randn(2, 2))
In [303]: df
Out[303]:
0 1
0 -0.184744 0.496971
1 -0.856240 1.857977
In [304]: print(df.to_html()) # raw html
|
0 |
1 |
| 0 |
-0.184744 |
0.496971 |
| 1 |
-0.856240 |
1.857977 |
```
HTML:
| **-** | **0** | **1** |
| --- | --- | --- |
| 0 | -0.184744 | 0.496971 |
| 1 | -0.856240 | 1.857977 |
```python
In [302]: df = pd.DataFrame(np.random.randn(2, 2))
In [303]: df
Out[303]:
0 1
0 -0.184744 0.496971
1 -0.856240 1.857977
In [304]: print(df.to_html()) # raw html
|
0 |
1 |
| 0 |
-0.184744 |
0.496971 |
| 1 |
-0.856240 |
1.857977 |
```
`columns `参数将限制列的显示:
```python
In [305]: print(df.to_html(columns=[0]))
|
0 |
| 0 |
-0.184744 |
| 1 |
-0.856240 |
```
HTML:
| **-** | **0** |
| --- | --- |
| 0 | -0.184744 |
| 1 | -0.856240 |
`float_format`采用可调用的 Python来控制浮点值的精确度:
```python
In [306]: print(df.to_html(float_format='{0:.10f}'.format))
|
0 |
1 |
| 0 |
-0.1847438576 |
0.4969711327 |
| 1 |
-0.8562396763 |
1.8579766508 |
```
HTML:
| **-** | **0** | **1** |
| --- | --- | --- |
| 0 | -0.1847438576 | 0.4969711327 |
| 1 | -0.8562396763 | 1.8579766508 |
默认情况下,`bold_rows`可以加粗行标签,但是你可以关掉它:
```python
In [307]: print(df.to_html(bold_rows=False))
|
0 |
1 |
| 0 |
-0.184744 |
0.496971 |
| 1 |
-0.856240 |
1.857977 |
```
| **-** | **0** | **1** |
| --- | --- | --- |
| 0 | -0.184744 | 0.496971 |
| 1 | -0.856240 | 1.857977 |
`classes `参数提供了能生成HTML表的CSS类的功能。注意这些类是已添加到现有的`'dataframe' `类中的。
```python
In [308]: print(df.to_html(classes=['awesome_table_class', 'even_more_awesome_class']))
|
0 |
1 |
| 0 |
-0.184744 |
0.496971 |
| 1 |
-0.856240 |
1.857977 |
```
`render_links`参数提供了向包含URL的单元格添加超链接的功能。
*New in version 0.24.*
```python
In [309]: url_df = pd.DataFrame({
.....: 'name': ['Python', 'Pandas'],
.....: 'url': ['https://www.python.org/', 'http://pandas.pydata.org']})
.....:
In [310]: print(url_df.to_html(render_links=True))
```
HTML:
| **-** | **name** | **url** |
| --- | --- | --- |
| 0 | Python | [https://www.python.org/](https://www.python.org/) |
| 1 | Pandas | [http://pandas.pydata.org](http://pandas.pydata.org) |
最后,`escape`参数允许你控制是否对生成的 HTML字符“<”, “>”和 “&”进行转义(默认是`True`)。因此,获取不转义的HTML字符就设置为`escape=False`。
```python
In [311]: df = pd.DataFrame({'a': list('&<>'), 'b': np.random.randn(3)})
```
转义的:
```python
In [312]: print(df.to_html())
|
a |
b |
| 0 |
& |
-0.474063 |
| 1 |
< |
-0.230305 |
| 2 |
> |
-0.400654 |
```
| **-** | **a** | **b** |
| --- | --- | --- |
| 0 | & | -0.474063 |
| 1 | < | -0.230305 |
| 2 | > | -0.400654 |
不转义的:
```python
In [313]: print(df.to_html(escape=False))
|
a |
b |
| 0 |
& |
-0.474063 |
| 1 |
< |
-0.230305 |
| 2 |
> |
-0.400654 |
```
| **-** | **a** | **b** |
| --- | --- | --- |
| 0 | & | -0.474063 |
| 1 | < | -0.230305 |
| 2 | > | -0.400654 |
::: tip 注意:
一些浏览器在渲染上面的两个HTML表格的时候可能看不出区别。
:::
### HTML表格解析陷阱
在使用顶级的pandas io函数`read_html`来解析HTML表格的时候,围绕这些库,存在一些版本的问题。
**[lxml](https://lxml.de/)问题**:
- 优点:
- [lxml](https://lxml.de/) 是非常快的。
- [lxml](https://lxml.de/)要求Cython正确安装。
- 缺点:
- [lxml](https://lxml.de/) 不能保证它的解析结果除非使用[严格有效地标记](https://validator.w3.org/docs/help.html#validation_basics)。
- 鉴于上述情况,我们选择允许用户使用 [lxml](https://lxml.de/) 作为后端,但是如果 [lxml](https://lxml.de/) 解析失败,**这个后端将使用[html5lib](https://github.com/html5lib/html5lib-python)**。
- 因此,强烈推荐你安装**[BeautifulSoup4](https://www.crummy.com/software/BeautifulSoup)**和**[html5lib](https://github.com/html5lib/html5lib-python)**这两个库。这样即使[lxml](https://lxml.de/)解析失败,你仍然能够得到一个有效的结果(前提是其他所有内容都有效)。
**[BeautifulSoup4](https://www.crummy.com/software/BeautifulSoup)使用[lxml](https://lxml.de/)作为后端的问题**:
- 以上问题仍然会存在因为**[BeautifulSoup4](https://www.crummy.com/software/BeautifulSoup)**本质上是一个围绕后端解析的包装器。
**[BeautifulSoup4](https://www.crummy.com/software/BeautifulSoup)使用[html5lib](https://github.com/html5lib/html5lib-python)作为后端的问题**:
- 优点:
- [html5lib](https://github.com/html5lib/html5lib-python)比[lxml](https://lxml.de/)宽容得多,所以会以一种更理智的方式处理*现实生活中的标记*,而不是仅仅,比如在未通知你的情况下删除元素。
- [html5lib](https://github.com/html5lib/html5lib-python)*能自动从无效标记中生成有效的 HTML5 标记*。这在解析HTML表格的时候是相当重要的,因为它保证了它是有效的文件。然而这不意味着它是“正确的“,因为修复标记的过程没有一个定义。
- [html5lib](https://github.com/html5lib/html5lib-python)是纯净的Python,除了它自己的安装步骤没有其他的步骤。
- 缺点:
- 使用[html5lib](https://github.com/html5lib/html5lib-python)最大的缺点就是太慢了。但是考虑到网络上许多表格并不足以如解析算法运行时的那么重要,它更可能像是正在通过网络上的URL读取原始文本过程中的瓶颈。例如当IO(输入-输出) 时,对于非常大的表,事实可能并非如此。
## Excel 文件
[read_excel()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html#pandas.read_excel)方法使用Python的`xlrd`模块来读取Excel 2003(`.xls`)版的文件,而Excel 2007+ (`.xlsx`)版本的是用`xlrd`或者`openpyxl`模块来读取的。[to_excel()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_excel.html#pandas.DataFrame.to_excel)方法则是用来把`DataFrame`数据存储为Excel格式。一般来说,它的语法同使用[csv](https://www.pypandas.cn/docs/user_guide/io.html#io-read-csv-table)数据是类似的,更多高级的用法可以参考[cookbook](https://www.pypandas.cn/docs/user_guide/cookbook.html#cookbook-excel)。
### 读取 Excel 文件
在大多数基本的使用案例中,`read_excel`会读取Excel文件通过一个路径,并且`sheet_name `会表明需要解析哪一张表格。
```python
# Returns a DataFrame
pd.read_excel('path_to_file.xls', sheet_name='Sheet1')
```
#### `ExcelFile` 类
为了更方便地读取同一个文件的多张表格,`ExcelFile`类可用来打包文件并传递给`read_excel`。因为仅需读取一次内存,所以这种方式读取一个文件的多张表格会有性能上的优势。
```python
xlsx = pd.ExcelFile('path_to_file.xls')
df = pd.read_excel(xlsx, 'Sheet1')
```
`ExcelFile`类也能用来作为上下文管理器。
```python
with pd.ExcelFile('path_to_file.xls') as xls:
df1 = pd.read_excel(xls, 'Sheet1')
df2 = pd.read_excel(xls, 'Sheet2')
```
`sheet_names`属性能将文件中的所有表格名字生成一组列表。
`ExcelFile`一个主要的用法就是用来解析多张表格的不同参数:
```python
data = {}
# For when Sheet1's format differs from Sheet2
with pd.ExcelFile('path_to_file.xls') as xls:
data['Sheet1'] = pd.read_excel(xls, 'Sheet1', index_col=None,
na_values=['NA'])
data['Sheet2'] = pd.read_excel(xls, 'Sheet2', index_col=1)
```
注意如果所有的表格解析同一个参数,那么这组表格名的列表能轻易地传递给`read_excel`且不会有性能上地损失。
```python
# using the ExcelFile class
data = {}
with pd.ExcelFile('path_to_file.xls') as xls:
data['Sheet1'] = pd.read_excel(xls, 'Sheet1', index_col=None,
na_values=['NA'])
data['Sheet2'] = pd.read_excel(xls, 'Sheet2', index_col=None,
na_values=['NA'])
# equivalent using the read_excel function
data = pd.read_excel('path_to_file.xls', ['Sheet1', 'Sheet2'],
index_col=None, na_values=['NA'])
```
`ExcelFile`也能同`xlrd.book.Book`对象作为一个参数被调用。这种方法让用户可以控制Excel文件被如何读取。例如,表格可以根据需求加载通过调用`xlrd.open_workbook()`伴随`on_demand=True`。
```python
import xlrd
xlrd_book = xlrd.open_workbook('path_to_file.xls', on_demand=True)
with pd.ExcelFile(xlrd_book) as xls:
df1 = pd.read_excel(xls, 'Sheet1')
df2 = pd.read_excel(xls, 'Sheet2')
```
#### 指定表格
::: tip 注意
第二个参数是`sheet_name`,不要同`ExcelFile.sheet_names`搞混淆。
:::
::: tip 注意
ExcelFile's的属性`sheet_names`提供的是多张表格所生成的列表。
:::
- `sheet_name`参数允许指定单张表格或多张表格被读取。
- `sheet_name`的默认值是0,这表明读取的是第一张表格。
- 在工作簿里面,使用字符串指向特定的表格名称。
- 使用整数指向表格的索引,索引遵守Python的约定是从0开始的。
- 无论是使用一组字符串还是整数的列表,返回的都是指定表格的字典。
- 使用`None`值则会返回所有可用表格的一组字典。
```python
# Returns a DataFrame
pd.read_excel('path_to_file.xls', 'Sheet1', index_col=None, na_values=['NA'])
```
使用表格索引:
```python
# Returns a DataFrame
pd.read_excel('path_to_file.xls', 0, index_col=None, na_values=['NA'])
```
使用所有默认值:
```python
# Returns a DataFrame
pd.read_excel('path_to_file.xls')
```
使用None获取所有表格:
```python
# Returns a dictionary of DataFrames
pd.read_excel('path_to_file.xls', sheet_name=None)
```
使用列表获取多张表格:
```python
# Returns the 1st and 4th sheet, as a dictionary of DataFrames.
pd.read_excel('path_to_file.xls', sheet_name=['Sheet1', 3])
```
`read_excel`能读取不止一张表格,通过`sheet_name`能设置为读取表格名称的列表,表格位置的列表,还能设置为`None`来读取所有表格。多张表格能通过表格索引或表格名称分别使用整数或字符串来指定读取。
#### `MultiIndex`读取
`read_excel`能用`MultiIndex`读取多个索引,通过`index_col`方法来传递列的列表和`header`将行的列表传递给`MultiIndex`的列。无论是`index`还是`columns`,如果已经具有序列化的层级名称,则可以通过指定组成层级的行/列来读取它们。
例如,用`MultiIndex`读取没有名称的索引:
```python
In [314]: df = pd.DataFrame({'a': [1, 2, 3, 4], 'b': [5, 6, 7, 8]},
.....: index=pd.MultiIndex.from_product([['a', 'b'], ['c', 'd']]))
.....:
In [315]: df.to_excel('path_to_file.xlsx')
In [316]: df = pd.read_excel('path_to_file.xlsx', index_col=[0, 1])
In [317]: df
Out[317]:
a b
a c 1 5
d 2 6
b c 3 7
d 4 8
```
如果索引具有层级名称,它们将使用相同的参数进行解析:
```python
In [318]: df.index = df.index.set_names(['lvl1', 'lvl2'])
In [319]: df.to_excel('path_to_file.xlsx')
In [320]: df = pd.read_excel('path_to_file.xlsx', index_col=[0, 1])
In [321]: df
Out[321]:
a b
lvl1 lvl2
a c 1 5
d 2 6
b c 3 7
d 4 8
```
如果源文件具有`MultiIndex`索引和多列,那么可以使用`index_col`和`header`指定列表的每个值。
```python
In [322]: df.columns = pd.MultiIndex.from_product([['a'], ['b', 'd']],
.....: names=['c1', 'c2'])
.....:
In [323]: df.to_excel('path_to_file.xlsx')
In [324]: df = pd.read_excel('path_to_file.xlsx', index_col=[0, 1], header=[0, 1])
In [325]: df
Out[325]:
c1 a
c2 b d
lvl1 lvl2
a c 1 5
d 2 6
b c 3 7
d 4 8
```
#### 解析特定的列
常常会有这样的情况,当用户想要插入几列数据到Excel表格里面作为临时计算,但是你又不想要读取这些列的时候,`read_excel`提供的`usecols`方法就派上用场了,它让你可以解析指定的列。
*Deprecated since version 0.24.0.*
不推荐`usecols`方法使用单个整数值,请在`usecols`中使用包括从0开始的整数列表。
如果`usecols`是一个整数,那么它将被认为是暗示解析最后一列。
```python
pd.read_excel('path_to_file.xls', 'Sheet1', usecols=2)
```
你也可以将逗号分隔的一组Excel列和范围指定为字符串:
```python
pd.read_excel('path_to_file.xls', 'Sheet1', usecols='A,C:E')
```
如果`usecols`是一组整数列,那么将认为是解析的文件列索引。
```python
pd.read_excel('path_to_file.xls', 'Sheet1', usecols=[0, 2, 3])
```
元素的顺序是可以忽略的,因此`usecols=[0, 1]`是等价于`[1, 0]`的。
*New in version 0.24.*
如果`usecols`是字符串列表,那么可以认为每个字符串对应的就是表格的每一个列名,列名是由`name`中的用户提供或从文档标题行推断出来。这些字符串定义了那些列将要被解析:
```python
pd.read_excel('path_to_file.xls', 'Sheet1', usecols=['foo', 'bar'])
```
元素的顺序同样被忽略,因此`usecols=['baz', 'joe']`等同于`['joe', 'baz']`。
*New in version 0.24.*
如果`usecols`是可调用的,那么该调用函数将会根据列名来调用,也会返回根据可调用函数为`True`的列名。
```python
pd.read_excel('path_to_file.xls', 'Sheet1', usecols=lambda x: x.isalpha())
```
#### 解析日期
当读取excel文件的时候,像日期时间的值通常会自动转换为恰当的dtype(数据类型)。但是如果你有一列字符串看起来很像日期(实际上并不是excel里面的日期格式),那么你就能使用`parse_dates`方法来解析这些字符串为日期:
```python
pd.read_excel('path_to_file.xls', 'Sheet1', parse_dates=['date_strings'])
```
#### 单元格转换
Excel里面的单元格内容是可以通过`converters`方法来进行转换的。例如,把一列转换为布尔值:
```python
pd.read_excel('path_to_file.xls', 'Sheet1', converters={'MyBools': bool})
```
这个方法可以处理缺失值并且能对缺失的数据进行如期的转换。由于转换是在单元格之间发生而不是整列,因此不能保证dtype为数组。例如一列含有缺失值的整数是不能转换为具有整数dtype的数组,因为NaN严格的被认为是浮点数。你能够手动地标记缺失数据为恢复整数dtype:
```python
def cfun(x):
return int(x) if x else -1
pd.read_excel('path_to_file.xls', 'Sheet1', converters={'MyInts': cfun})
```
#### 数据类型规范
*New in version 0.20.*
作为另一个种转换器,使用*dtype*能指定整列地类型,它能让字典映射列名为数据类型。使用`str`或`object`来转译不能判断类型的数据:
```python
pd.read_excel('path_to_file.xls', dtype={'MyInts': 'int64', 'MyText': str})
```
### 写入Excel文件
#### 写入Excel文件到磁盘
你可以使用`to_excel`方法把`DataFrame`对象写入到Excel文件的一张表格中。它的参数大部分同前面`to_csv `提到的相同,第一个参数是excel文件的名字,而可选的第二个参数是`DataFrame`应该写入的表格名称,例如:
```python
df.to_excel('path_to_file.xlsx', sheet_name='Sheet1')
```
文件以`.xls` 结尾的将用`xlwt`写入,而那些以`.xlsx`结尾的则使用`xlsxwriter`(如果可用的话)或`openpyxl`来写入。
`DataFrame `将尝试以模拟REPL(“读取-求值-输出" 循环的简写)输出的方式写入。`index_label`将代替第一行放置到第二行,你也能放置它到第一行通过在`to_excel()`里设置`merge_cells`选项为`False`:
```python
df.to_excel('path_to_file.xlsx', index_label='label', merge_cells=False)
```
为了把`DataFrames`数据分开写入Excel文件的不同表格中,可以使用`ExcelWriter`方法。
```python
with pd.ExcelWriter('path_to_file.xlsx') as writer:
df1.to_excel(writer, sheet_name='Sheet1')
df2.to_excel(writer, sheet_name='Sheet2')
```
::: tip 注意
为了从`read_excel`内部获取更多点的性能,Excel存储所有数值型数据为浮点数。但这会产生意外的情况当读取数据的时候,如果没有损失信息的话(`1.0 --> 1`),pandas默认的转换整数为浮点数。你可以通过`convert_float=False`禁止这种行为,这可能会在性能上有轻微的优化。
:::
#### 写入Excel文件到内存
Pandas支持写入Excel文件到类缓存区对象如`StringIO`或`BytesIO`,使用`ExcelWriter`方法。
```python
# Safe import for either Python 2.x or 3.x
try:
from io import BytesIO
except ImportError:
from cStringIO import StringIO as BytesIO
bio = BytesIO()
# By setting the 'engine' in the ExcelWriter constructor.
writer = pd.ExcelWriter(bio, engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
# Save the workbook
writer.save()
# Seek to the beginning and read to copy the workbook to a variable in memory
bio.seek(0)
workbook = bio.read()
```
::: tip 注意
虽然`engine`是可选方法,但是推荐使用。设置engine决定了工作簿生成的版本。设置`engine='xlrd'`将生成 Excel 2003版的工作簿(xls)。而使用`'openpyxl'`或`'xlsxwriter'`将生成Excel 2007版的工作簿(xlsx)。如果省略,将直接生成Excel 2007版的。
:::
### Excel写入引擎
Pandas选择Excel写入有两种方式:
1. 使用`engine`参数
2. 文件名的扩展(通过默认的配置方式指定)
默认的,pandas使用[ XlsxWriter](https://xlsxwriter.readthedocs.io/)为`.xlsx`,使用[openpyxl](https://openpyxl.readthedocs.io/)为`.xlsm`,并且使用[xlwt](http://www.python-excel.org/)为`.xls`文件。如果你安装了多个引擎,你可以通过[setting the config options](https://www.pypandas.cn/docs/user_guide/options.html#options)`io.excel.xlsx.writer`和`io.excel.xls.writer`方法设置默认引擎。如果[ XlsxWriter](https://xlsxwriter.readthedocs.io/)不可用,pandas将回退使用[openpyxl](https://openpyxl.readthedocs.io/)为`xlsx`文件。
为了指定你想要使用的写入方式,你可以设置引擎的主要参数为`to_excel`和`ExcelWriter`。内置引擎是:
- `openpyxl`: 要求2.4或者更高的版本。
- `xlsxwriter`
- `xlwt`
```python
# By setting the 'engine' in the DataFrame 'to_excel()' methods.
df.to_excel('path_to_file.xlsx', sheet_name='Sheet1', engine='xlsxwriter')
# By setting the 'engine' in the ExcelWriter constructor.
writer = pd.ExcelWriter('path_to_file.xlsx', engine='xlsxwriter')
# Or via pandas configuration.
from pandas import options # noqa: E402
options.io.excel.xlsx.writer = 'xlsxwriter'
df.to_excel('path_to_file.xlsx', sheet_name='Sheet1')
```
### 样式
通过pandas产生的Excel工作表的样式可以使用`DataFrame`的`to_excel`方法的以下参数进行修改。
- `float_format`:格式化字符串用于浮点数(默认是`None`)。
- `freeze_panes`:两个整数的元组,表示要固化的最底行和最右列。这些参数中的每个都是以1为底,因此(1, 1)将固化第一行和第一列(默认是`None`)。
使用[ XlsxWriter](https://xlsxwriter.readthedocs.io/)引擎提供的多种方法来修改用`to_excel`方法创建的Excel工作表的样式。你能在[ XlsxWriter](https://xlsxwriter.readthedocs.io/)文档里面找到绝佳的例子:[https://xlsxwriter.readthedocs.io/working_with_pandas.html](https://xlsxwriter.readthedocs.io/working_with_pandas.html)
## OpenDocument 电子表格
*New in version 0.25.*
[`read_excel`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html#pandas.read_excel "`read_excel`")方法也能使用`odfpy`模块来读取OpenDocument电子表格。读取OpenDocument电子表格的语法和方法同使用`engine='odf'`来操作[Excel files](https://www.pypandas.cn/docs/user_guide/io.html#excel-files "Excel files")的方法类似。
```python
# Returns a DataFrame
pd.read_excel('path_to_file.ods', engine='odf')
```
::: tip 注意
目前pandas仅支持读取OpenDocument电子表格,写入是不行的。
:::
## 剪贴板
使用`read_clipboard()`方法是一种便捷的获取数据的方式,通过把剪贴的内容暂存,然后传递给`read_csv`方法。例如,你可以复制以下文本来剪贴(在许多操作系统上是CTRL-C):
```python
A B C
x 1 4 p
y 2 5 q
z 3 6 r
```
接着直接使用`DataFrame`来导入数据:
```python
>>> clipdf = pd.read_clipboard()
>>> clipdf
A B C
x 1 4 p
y 2 5 q
z 3 6 r
```
`to_clipboard`方法可以把`DataFrame`内容写入到剪贴板。使用下面的方法你可以粘贴剪贴板的内容到其他应用(在许多系统中用的是CTRL-V)。这里我们解释一下如何使用`DataFrame`把内容写入到剪贴板并读回。
```python
>>> df = pd.DataFrame({'A': [1, 2, 3],
... 'B': [4, 5, 6],
... 'C': ['p', 'q', 'r']},
... index=['x', 'y', 'z'])
>>> df
A B C
x 1 4 p
y 2 5 q
z 3 6 r
>>> df.to_clipboard()
>>> pd.read_clipboard()
A B C
x 1 4 p
y 2 5 q
z 3 6 r
```
我们可以看到返回了同样的内容,那就是我们早先写入剪贴板的内容。
::: tip 注意
要使用上面的这些方法,你可能需要在Linux上面安装(带有PyQt5, PyQt4 or qtpy)的xclip或者xsel 。
:::
## 序列化(Pickling)
所有的pandas对象都具有`to_pickle`方法,该方法使用Python的` cPickle`模块以序列化格式存储数据结构到磁盘上。
```python
In [326]: df
Out[326]:
c1 a
c2 b d
lvl1 lvl2
a c 1 5
d 2 6
b c 3 7
d 4 8
In [327]: df.to_pickle('foo.pkl')
```
在`pandas`中命名的`read_pickle`函数能够从文件中加载任意序列化的pandas对象(或者任何其他的序列化对象):
```python
In [328]: pd.read_pickle('foo.pkl')
Out[328]:
c1 a
c2 b d
lvl1 lvl2
a c 1 5
d 2 6
b c 3 7
d 4 8
```
::: danger 警告
加载来自不信任来源的序列化数据是不安全的。
参见:https://docs.python.org/3/library/pickle.html
:::
::: danger 警告
[`read_pickle()`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_pickle.html#pandas.read_pickle "`read_pickle()`")仅在pandas的0.20.3版本及以下版本兼容。
:::
### 压缩序列化文件
*New in version 0.20.0.*
[`read_pickle()`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_pickle.html#pandas.read_pickle "`read_pickle()`"),[`DataFrame.to_pickle()`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_pickle.html#pandas.DataFrame.to_pickle)和[`Series.to_pickle()`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.to_pickle.html#pandas.Series.to_pickle)能够读取和写入压缩的序列化文件。读取和写入所支持的压缩文件类型有`gzip`, `bz2`, `xz`。`zip`文件格式仅支持读取,并且必须仅包含一个要读取的数据文件。
压缩类型可以是显式参数,也可以从文件扩展名中推断出来。如果文件名是以`'.gz'`,` '.bz2'`,` '.zip'`, 或者` '.xz'`结尾的,那么可以推断出应分别使用`gzip`, `bz2`,`zip`,或 `xz`压缩类型。
```python
In [329]: df = pd.DataFrame({
.....: 'A': np.random.randn(1000),
.....: 'B': 'foo',
.....: 'C': pd.date_range('20130101', periods=1000, freq='s')})
.....:
In [330]: df
Out[330]:
A B C
0 -0.288267 foo 2013-01-01 00:00:00
1 -0.084905 foo 2013-01-01 00:00:01
2 0.004772 foo 2013-01-01 00:00:02
3 1.382989 foo 2013-01-01 00:00:03
4 0.343635 foo 2013-01-01 00:00:04
.. ... ... ...
995 -0.220893 foo 2013-01-01 00:16:35
996 0.492996 foo 2013-01-01 00:16:36
997 -0.461625 foo 2013-01-01 00:16:37
998 1.361779 foo 2013-01-01 00:16:38
999 -1.197988 foo 2013-01-01 00:16:39
[1000 rows x 3 columns]
```
使用显式压缩类型:
```python
In [331]: df.to_pickle("data.pkl.compress", compression="gzip")
In [332]: rt = pd.read_pickle("data.pkl.compress", compression="gzip")
In [333]: rt
Out[333]:
A B C
0 -0.288267 foo 2013-01-01 00:00:00
1 -0.084905 foo 2013-01-01 00:00:01
2 0.004772 foo 2013-01-01 00:00:02
3 1.382989 foo 2013-01-01 00:00:03
4 0.343635 foo 2013-01-01 00:00:04
.. ... ... ...
995 -0.220893 foo 2013-01-01 00:16:35
996 0.492996 foo 2013-01-01 00:16:36
997 -0.461625 foo 2013-01-01 00:16:37
998 1.361779 foo 2013-01-01 00:16:38
999 -1.197988 foo 2013-01-01 00:16:39
[1000 rows x 3 columns]
```
从扩展名推断压缩类型:
```python
In [334]: df.to_pickle("data.pkl.xz", compression="infer")
In [335]: rt = pd.read_pickle("data.pkl.xz", compression="infer")
In [336]: rt
Out[336]:
A B C
0 -0.288267 foo 2013-01-01 00:00:00
1 -0.084905 foo 2013-01-01 00:00:01
2 0.004772 foo 2013-01-01 00:00:02
3 1.382989 foo 2013-01-01 00:00:03
4 0.343635 foo 2013-01-01 00:00:04
.. ... ... ...
995 -0.220893 foo 2013-01-01 00:16:35
996 0.492996 foo 2013-01-01 00:16:36
997 -0.461625 foo 2013-01-01 00:16:37
998 1.361779 foo 2013-01-01 00:16:38
999 -1.197988 foo 2013-01-01 00:16:39
[1000 rows x 3 columns]
```
默认是使用“推断”:
```python
In [337]: df.to_pickle("data.pkl.gz")
In [338]: rt = pd.read_pickle("data.pkl.gz")
In [339]: rt
Out[339]:
A B C
0 -0.288267 foo 2013-01-01 00:00:00
1 -0.084905 foo 2013-01-01 00:00:01
2 0.004772 foo 2013-01-01 00:00:02
3 1.382989 foo 2013-01-01 00:00:03
4 0.343635 foo 2013-01-01 00:00:04
.. ... ... ...
995 -0.220893 foo 2013-01-01 00:16:35
996 0.492996 foo 2013-01-01 00:16:36
997 -0.461625 foo 2013-01-01 00:16:37
998 1.361779 foo 2013-01-01 00:16:38
999 -1.197988 foo 2013-01-01 00:16:39
[1000 rows x 3 columns]
In [340]: df["A"].to_pickle("s1.pkl.bz2")
In [341]: rt = pd.read_pickle("s1.pkl.bz2")
In [342]: rt
Out[342]:
0 -0.288267
1 -0.084905
2 0.004772
3 1.382989
4 0.343635
...
995 -0.220893
996 0.492996
997 -0.461625
998 1.361779
999 -1.197988
Name: A, Length: 1000, dtype: float64
```
## msgpack(一种二进制格式)
pandas支持`msgpack`格式的对象序列化。他是一种轻量级可移植的二进制格式,同二进制的JSON类似,具有高效的空间利用率以及不错的写入(序列化)和读取(反序列化)性能。
::: danger 警告
从0.25版本开始,不推荐使用msgpack格式,并且之后的版本也将删除它。推荐使用pyarrow对pandas对象进行在线的转换。
:::
::: danger 警告
[`read_msgpack()`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_msgpack.html#pandas.read_msgpack "`read_msgpack()`")仅在pandas的0.20.3版本及以下版本兼容。
:::
```python
In [343]: df = pd.DataFrame(np.random.rand(5, 2), columns=list('AB'))
In [344]: df.to_msgpack('foo.msg')
In [345]: pd.read_msgpack('foo.msg')
Out[345]:
A B
0 0.275432 0.293583
1 0.842639 0.165381
2 0.608925 0.778891
3 0.136543 0.029703
4 0.318083 0.604870
In [346]: s = pd.Series(np.random.rand(5), index=pd.date_range('20130101', periods=5))
```
你可以传递一组对象列表并得到反序列化的结果。
```python
In [347]: pd.to_msgpack('foo.msg', df, 'foo', np.array([1, 2, 3]), s)
In [348]: pd.read_msgpack('foo.msg')
Out[348]:
[ A B
0 0.275432 0.293583
1 0.842639 0.165381
2 0.608925 0.778891
3 0.136543 0.029703
4 0.318083 0.604870, 'foo', array([1, 2, 3]), 2013-01-01 0.330824
2013-01-02 0.790825
2013-01-03 0.308468
2013-01-04 0.092397
2013-01-05 0.703091
Freq: D, dtype: float64]
```
你能传递`iterator=True`参数来迭代解压后的结果:
```python
In [349]: for o in pd.read_msgpack('foo.msg', iterator=True):
.....: print(o)
.....:
A B
0 0.275432 0.293583
1 0.842639 0.165381
2 0.608925 0.778891
3 0.136543 0.029703
4 0.318083 0.604870
foo
[1 2 3]
2013-01-01 0.330824
2013-01-02 0.790825
2013-01-03 0.308468
2013-01-04 0.092397
2013-01-05 0.703091
Freq: D, dtype: float64
```
你也能传递`append=True`参数,给现有的包添加写入:
```python
In [350]: df.to_msgpack('foo.msg', append=True)
In [351]: pd.read_msgpack('foo.msg')
Out[351]:
[ A B
0 0.275432 0.293583
1 0.842639 0.165381
2 0.608925 0.778891
3 0.136543 0.029703
4 0.318083 0.604870, 'foo', array([1, 2, 3]), 2013-01-01 0.330824
2013-01-02 0.790825
2013-01-03 0.308468
2013-01-04 0.092397
2013-01-05 0.703091
Freq: D, dtype: float64, A B
0 0.275432 0.293583
1 0.842639 0.165381
2 0.608925 0.778891
3 0.136543 0.029703
4 0.318083 0.604870]
```
不像其他io方法,`to_msgpack`既可以基于每个对象使用`df.to_msgpack()`方法,也可以在混合pandas对象的时候使用顶层`pd.to_msgpack(...)`方法,该方法可以让你打包任意的Python列表、字典、标量的集合。
```python
In [352]: pd.to_msgpack('foo2.msg', {'dict': [{'df': df}, {'string': 'foo'},
.....: {'scalar': 1.}, {'s': s}]})
.....:
In [353]: pd.read_msgpack('foo2.msg')
Out[353]:
{'dict': ({'df': A B
0 0.275432 0.293583
1 0.842639 0.165381
2 0.608925 0.778891
3 0.136543 0.029703
4 0.318083 0.604870},
{'string': 'foo'},
{'scalar': 1.0},
{'s': 2013-01-01 0.330824
2013-01-02 0.790825
2013-01-03 0.308468
2013-01-04 0.092397
2013-01-05 0.703091
Freq: D, dtype: float64})}
```
### 读/写API
Msgpacks也能读写字符串。
```python
In [354]: df.to_msgpack()
Out[354]: b'\x84\xa3typ\xadblock_manager\xa5klass\xa9DataFrame\xa4axes\x92\x86\xa3typ\xa5index\xa5klass\xa5Index\xa4name\xc0\xa5dtype\xa6object\xa4data\x92\xa1A\xa1B\xa8compress\xc0\x86\xa3typ\xabrange_index\xa5klass\xaaRangeIndex\xa4name\xc0\xa5start\x00\xa4stop\x05\xa4step\x01\xa6blocks\x91\x86\xa4locs\x86\xa3typ\xa7ndarray\xa5shape\x91\x02\xa4ndim\x01\xa5dtype\xa5int64\xa4data\xd8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\xa8compress\xc0\xa6values\xc7P\x00\xc84 \x84\xac\xa0\xd1?\x0f\xa4.\xb5\xe6\xf6\xea?\xb9\x85\x9aLO|\xe3?\xac\xf0\xd7\x81>z\xc1?\\\xca\x97\ty[\xd4?\x9c\x9b\x8a:\x11\xca\xd2?\x14zX\xd01+\xc5?4=\x19b\xad\xec\xe8?\xc0!\xe9\xf4\x8ej\x9e?\xa7>_\xac\x17[\xe3?\xa5shape\x92\x02\x05\xa5dtype\xa7float64\xa5klass\xaaFloatBlock\xa8compress\xc0'
```
此外你可以连接字符串生成一个原始的对象列表。
```python
In [355]: pd.read_msgpack(df.to_msgpack() + s.to_msgpack())
Out[355]:
[ A B
0 0.275432 0.293583
1 0.842639 0.165381
2 0.608925 0.778891
3 0.136543 0.029703
4 0.318083 0.604870, 2013-01-01 0.330824
2013-01-02 0.790825
2013-01-03 0.308468
2013-01-04 0.092397
2013-01-05 0.703091
Freq: D, dtype: float64]
```
## HDF5(PyTables) (一种以.h5结尾的分层数据格式)
`HDFStore`是一个能读写pandas的类似字典的对象,它能使用高性能的HDF5格式,该格式是用优秀的[PyTables](https://www.pytables.org/ "PyTables")库写的。一些更高级的用法参考[cookbook](https://www.pypandas.cn/docs/user_guide/cookbook.html#cookbook-hdf "cookbook")。
::: danger 警告
pandas要求使用的`PyTables`版本要 > = 3.0.0。当使用索引来检索存储的时候,`PyTables`< 3.2的版本会出现索引bug。如果返回一个结果的子集,那么你就需要升级`PyTables` 的版本 >= 3.2才行。先前创建的存储数据将会使用更新后的版本再次写入。
:::
```python
In [356]: store = pd.HDFStore('store.h5')
In [357]: print(store)
File path: store.h5
```
对象能够被写入文件就像成对的键-值添加到字典里面一样:
```python
In [358]: index = pd.date_range('1/1/2000', periods=8)
In [359]: s = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])
In [360]: df = pd.DataFrame(np.random.randn(8, 3), index=index,
.....: columns=['A', 'B', 'C'])
.....:
# store.put('s', s) is an equivalent method
In [361]: store['s'] = s
In [362]: store['df'] = df
In [363]: store
Out[363]:
File path: store.h5
```
在当前或者之后的Python会话中,你都能检索存储的对象:
```python
# store.get('df') is an equivalent method
In [364]: store['df']
Out[364]:
A B C
2000-01-01 -0.426936 -1.780784 0.322691
2000-01-02 1.638174 -2.184251 0.049673
2000-01-03 -1.022803 0.889445 2.827717
2000-01-04 1.767446 -1.305266 -0.378355
2000-01-05 0.486743 0.954551 0.859671
2000-01-06 -1.170458 -1.211386 -0.852728
2000-01-07 -0.450781 1.064650 1.014927
2000-01-08 -0.810399 0.254343 -0.875526
# dotted (attribute) access provides get as well
In [365]: store.df
Out[365]:
A B C
2000-01-01 -0.426936 -1.780784 0.322691
2000-01-02 1.638174 -2.184251 0.049673
2000-01-03 -1.022803 0.889445 2.827717
2000-01-04 1.767446 -1.305266 -0.378355
2000-01-05 0.486743 0.954551 0.859671
2000-01-06 -1.170458 -1.211386 -0.852728
2000-01-07 -0.450781 1.064650 1.014927
2000-01-08 -0.810399 0.254343 -0.875526
```
使用键删除指定的对象:
```python
# store.remove('df') is an equivalent method
In [366]: del store['df']
In [367]: store
Out[367]:
File path: store.h5
```
关闭存储对象并使用环境管理器:
```python
In [368]: store.close()
In [369]: store
Out[369]:
File path: store.h5
In [370]: store.is_open
Out[370]: False
# Working with, and automatically closing the store using a context manager
In [371]: with pd.HDFStore('store.h5') as store:
.....: store.keys()
.....:
```
### 读/写 API
`HDFStore `支持顶层的API,用`read_hdf`来读取,和使用`to_hdf`来写入,类似于`read_csv` 和`to_csv`的用法。
```python
In [372]: df_tl = pd.DataFrame({'A': list(range(5)), 'B': list(range(5))})
In [373]: df_tl.to_hdf('store_tl.h5', 'table', append=True)
In [374]: pd.read_hdf('store_tl.h5', 'table', where=['index>2'])
Out[374]:
A B
3 3 3
4 4 4
```
HDFStore默认不会删除全是缺失值的行,但是通过设置`dropna=True`参数就能改变。
```python
In [375]: df_with_missing = pd.DataFrame({'col1': [0, np.nan, 2],
.....: 'col2': [1, np.nan, np.nan]})
.....:
In [376]: df_with_missing
Out[376]:
col1 col2
0 0.0 1.0
1 NaN NaN
2 2.0 NaN
In [377]: df_with_missing.to_hdf('file.h5', 'df_with_missing',
.....: format='table', mode='w')
.....:
In [378]: pd.read_hdf('file.h5', 'df_with_missing')
Out[378]:
col1 col2
0 0.0 1.0
1 NaN NaN
2 2.0 NaN
In [379]: df_with_missing.to_hdf('file.h5', 'df_with_missing',
.....: format='table', mode='w', dropna=True)
.....:
In [380]: pd.read_hdf('file.h5', 'df_with_missing')
Out[380]:
col1 col2
0 0.0 1.0
2 2.0 NaN
```
### 固定格式
上面的例子表明了使用`put`进行存储的情况,该存储将`HDF5`以固定数组格式写入`PyTables`,这就是所谓的`fixed`格式。这些类型的存储一旦被写入后将**不能**再添加数据(虽然你能轻易地删除它们并再次写入),**也不能**查询;必须全部检索它们。它们也不支持没有唯一列名的数据表。`fixed`格式提供了非常快速的写入功能,并且比`table`存储在读取方面更快捷。默认的指定格式是使用`put` 或者`to_hdf` 亦或通过` format='fixed'`或` format='f'`格式。
::: danger 警告
如果你尝试使用`where`来检索,`fixed`格式将会报错` TypeError`:
```python
>>> pd.DataFrame(np.random.randn(10, 2)).to_hdf('test_fixed.h5', 'df')
>>> pd.read_hdf('test_fixed.h5', 'df', where='index>5')
TypeError: cannot pass a where specification when reading a fixed format.
this store must be selected in its entirety
```
:::
### 表格格式
`HDFStore `支持在磁盘上使用另一种`PyTables`格式,即`table`格式。从概念上来讲,`table`在外形上同具有行和列的DataFrame极度相似。`table`也能被添加到同样的或其他的会话中。此外,删除和查询操作也是支持的。通过指定格式为`format='table'`或`format='t'`到`append`方法或`put`或者`to_hdf`。
`put/append/to_hdf`方法中使用的格式也可以设置为可选`pd.set_option('io.hdf.default_format','table')`,以默认的`table`格式存储。
```python
In [381]: store = pd.HDFStore('store.h5')
In [382]: df1 = df[0:4]
In [383]: df2 = df[4:]
# append data (creates a table automatically)
In [384]: store.append('df', df1)
In [385]: store.append('df', df2)
In [386]: store
Out[386]:
File path: store.h5
# select the entire object
In [387]: store.select('df')
Out[387]:
A B C
2000-01-01 -0.426936 -1.780784 0.322691
2000-01-02 1.638174 -2.184251 0.049673
2000-01-03 -1.022803 0.889445 2.827717
2000-01-04 1.767446 -1.305266 -0.378355
2000-01-05 0.486743 0.954551 0.859671
2000-01-06 -1.170458 -1.211386 -0.852728
2000-01-07 -0.450781 1.064650 1.014927
2000-01-08 -0.810399 0.254343 -0.875526
# the type of stored data
In [388]: store.root.df._v_attrs.pandas_type
Out[388]: 'frame_table'
```
::: tip 注意
你也可以通过创建`table`来传递`format='table'`或者` format='t`到`put`操作。
:::
### 分层键
存储的键能够指定为字符串,这些分层的路径名就像这样的格式(例如:`foo/bar/bah`)。它将生成子存储的层次结构(或者在PyTables中叫做`Groups` )。键可以不带前面的'/'指定而且**总是**单独的(例如:'foo' 指的就是'/foo')。删除操作能够删除所有子存储及**之后**的数据,所以要小心该操作。
```python
In [389]: store.put('foo/bar/bah', df)
In [390]: store.append('food/orange', df)
In [391]: store.append('food/apple', df)
In [392]: store
Out[392]:
File path: store.h5
# a list of keys are returned
In [393]: store.keys()
Out[393]: ['/df', '/food/apple', '/food/orange', '/foo/bar/bah']
# remove all nodes under this level
In [394]: store.remove('food')
In [395]: store
Out[395]:
File path: store.h5
```
你能遍历组层次结构使用`walk`方法,该方法将为每个组键及其内容的相对键生成一个元组。
*New in version 0.24.0.*
```python
In [396]: for (path, subgroups, subkeys) in store.walk():
.....: for subgroup in subgroups:
.....: print('GROUP: {}/{}'.format(path, subgroup))
.....: for subkey in subkeys:
.....: key = '/'.join([path, subkey])
.....: print('KEY: {}'.format(key))
.....: print(store.get(key))
.....:
GROUP: /foo
KEY: /df
A B C
2000-01-01 -0.426936 -1.780784 0.322691
2000-01-02 1.638174 -2.184251 0.049673
2000-01-03 -1.022803 0.889445 2.827717
2000-01-04 1.767446 -1.305266 -0.378355
2000-01-05 0.486743 0.954551 0.859671
2000-01-06 -1.170458 -1.211386 -0.852728
2000-01-07 -0.450781 1.064650 1.014927
2000-01-08 -0.810399 0.254343 -0.875526
GROUP: /foo/bar
KEY: /foo/bar/bah
A B C
2000-01-01 -0.426936 -1.780784 0.322691
2000-01-02 1.638174 -2.184251 0.049673
2000-01-03 -1.022803 0.889445 2.827717
2000-01-04 1.767446 -1.305266 -0.378355
2000-01-05 0.486743 0.954551 0.859671
2000-01-06 -1.170458 -1.211386 -0.852728
2000-01-07 -0.450781 1.064650 1.014927
2000-01-08 -0.810399 0.254343 -0.875526
```
::: danger 警告
分层键对于存储在根节点下的项目,无法使用如上的方法将其作为点(属性)进行检索。
```python
In [8]: store.foo.bar.bah
AttributeError: 'HDFStore' object has no attribute 'foo'
# you can directly access the actual PyTables node but using the root node
In [9]: store.root.foo.bar.bah
Out[9]:
/foo/bar/bah (Group) ''
children := ['block0_items' (Array), 'block0_values' (Array), 'axis0' (Array), 'axis1' (Array)]
```
相反,使用基于显式字符串的键:
```python
In [397]: store['foo/bar/bah']
Out[397]:
A B C
2000-01-01 -0.426936 -1.780784 0.322691
2000-01-02 1.638174 -2.184251 0.049673
2000-01-03 -1.022803 0.889445 2.827717
2000-01-04 1.767446 -1.305266 -0.378355
2000-01-05 0.486743 0.954551 0.859671
2000-01-06 -1.170458 -1.211386 -0.852728
2000-01-07 -0.450781 1.064650 1.014927
2000-01-08 -0.810399 0.254343 -0.875526
```
:::
### 存储类型
#### 在表格中存储混合类型
支持混合数据类型存储。字符串使用添加列的最大尺寸以固定宽度进行存储。后面尝试添加更长的字符串将会报错``ValueError``。
添加参数``min_itemsize={`values`: size}``将给字符串列设置一个更大的最小值。目前支持的存储类型有 ``floats,strings, ints, bools, datetime64`` 。对于字符串列,添加参数 ``nan_rep = 'nan'``将改变磁盘上默认的nan值(转变为*np.nan*),原本默认是*nan*。
``` python
In [398]: df_mixed = pd.DataFrame({'A': np.random.randn(8),
.....: 'B': np.random.randn(8),
.....: 'C': np.array(np.random.randn(8), dtype='float32'),
.....: 'string': 'string',
.....: 'int': 1,
.....: 'bool': True,
.....: 'datetime64': pd.Timestamp('20010102')},
.....: index=list(range(8)))
.....:
In [399]: df_mixed.loc[df_mixed.index[3:5],
.....: ['A', 'B', 'string', 'datetime64']] = np.nan
.....:
In [400]: store.append('df_mixed', df_mixed, min_itemsize={'values': 50})
In [401]: df_mixed1 = store.select('df_mixed')
In [402]: df_mixed1
Out[402]:
A B C string int bool datetime64
0 -0.980856 0.298656 0.151508 string 1 True 2001-01-02
1 -0.906920 -1.294022 0.587939 string 1 True 2001-01-02
2 0.988185 -0.618845 0.043096 string 1 True 2001-01-02
3 NaN NaN 0.362451 NaN 1 True NaT
4 NaN NaN 1.356269 NaN 1 True NaT
5 -0.772889 -0.340872 1.798994 string 1 True 2001-01-02
6 -0.043509 -0.303900 0.567265 string 1 True 2001-01-02
7 0.768606 -0.871948 -0.044348 string 1 True 2001-01-02
In [403]: df_mixed1.dtypes.value_counts()
Out[403]:
float64 2
float32 1
datetime64[ns] 1
int64 1
bool 1
object 1
dtype: int64
# we have provided a minimum string column size
In [404]: store.root.df_mixed.table
Out[404]:
/df_mixed/table (Table(8,)) ''
description := {
"index": Int64Col(shape=(), dflt=0, pos=0),
"values_block_0": Float64Col(shape=(2,), dflt=0.0, pos=1),
"values_block_1": Float32Col(shape=(1,), dflt=0.0, pos=2),
"values_block_2": Int64Col(shape=(1,), dflt=0, pos=3),
"values_block_3": Int64Col(shape=(1,), dflt=0, pos=4),
"values_block_4": BoolCol(shape=(1,), dflt=False, pos=5),
"values_block_5": StringCol(itemsize=50, shape=(1,), dflt=b'', pos=6)}
byteorder := 'little'
chunkshape := (689,)
autoindex := True
colindexes := {
"index": Index(6, medium, shuffle, zlib(1)).is_csi=False}
```
#### 存储多层索引数据表
存储多层索引``DataFrames``为表格与从同类索引 ``DataFrames``中存储/选取是非常类似的。
``` python
In [405]: index = pd.MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'],
.....: ['one', 'two', 'three']],
.....: codes=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3],
.....: [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
.....: names=['foo', 'bar'])
.....:
In [406]: df_mi = pd.DataFrame(np.random.randn(10, 3), index=index,
.....: columns=['A', 'B', 'C'])
.....:
In [407]: df_mi
Out[407]:
A B C
foo bar
foo one 0.031885 0.641045 0.479460
two -0.630652 -0.182400 -0.789979
three -0.282700 -0.813404 1.252998
bar one 0.758552 0.384775 -1.133177
two -1.002973 -1.644393 -0.311536
baz two -0.615506 -0.084551 -1.318575
three 0.923929 -0.105981 0.429424
qux one -1.034590 0.542245 -0.384429
two 0.170697 -0.200289 1.220322
three -1.001273 0.162172 0.376816
In [408]: store.append('df_mi', df_mi)
In [409]: store.select('df_mi')
Out[409]:
A B C
foo bar
foo one 0.031885 0.641045 0.479460
two -0.630652 -0.182400 -0.789979
three -0.282700 -0.813404 1.252998
bar one 0.758552 0.384775 -1.133177
two -1.002973 -1.644393 -0.311536
baz two -0.615506 -0.084551 -1.318575
three 0.923929 -0.105981 0.429424
qux one -1.034590 0.542245 -0.384429
two 0.170697 -0.200289 1.220322
three -1.001273 0.162172 0.376816
# the levels are automatically included as data columns
In [410]: store.select('df_mi', 'foo=bar')
Out[410]:
A B C
foo bar
bar one 0.758552 0.384775 -1.133177
two -1.002973 -1.644393 -0.311536
```
### 查询
#### 查询表格
``select`` 和 ``delete`` 操作有一个可选项即能指定选择/删除仅有数据的子集。 这允许用户拥有一个很大的磁盘表并仅检索一部分数据。
在底层里使用``Term`` 类指定查询为布尔表达式。
- 支持的 ``DataFrames``索引器有 ``index`` 和 ``columns`` .
- 如果指定为``data_columns``,这些将作为额外的索引器。
有效的比较运算符有:
``=, ==, !=, >, >=, <, <=``
有效的布尔表达式包含如下几种:
- ``|`` : 选择
- ``&`` : 并列
- ``(`` 和 ``)`` : 用来分组
这些规则同在pandas的索引中使用布尔表达式是类似的。
::: tip 注意
- ``=`` 将自动扩展为比较运算符 ``==``
- ``~`` 不是运算符,且只在有限的条件下使用
- 如果传递的表达式时列表/元组,他们将通过 ``&``符号合并
:::
以下都是有效的表达式:
- ``'index >= date'``
- ``"columns = ['A', 'D']"``
- ``"columns in ['A', 'D']"``
- ``'columns = A'``
- ``'columns == A'``
- ``"~(columns = ['A', 'B'])"``
- ``'index > df.index[3] & string = "bar"'``
- ``'(index > df.index[3] & index <= df.index[6]) | string = "bar"'``
- ``"ts >= Timestamp('2012-02-01')"``
- ``"major_axis>=20130101"``
`indexers`在子表达式的左边的有:
`columns`, `major_axis`, `ts`
(在比较运算符后面)子表达式可以是:
- 能被求值的函数,比如:``Timestamp('2012-02-01')``
- 字符串,比如: ``"bar"``
- 类似日期,比如: ``20130101``或者 ``"20130101"``
- 列表,比如: ``"['A', 'B']"``
- 以本地命名空间定义的变量,比如:``date``
::: tip 注意
在查询表达式中插入字符串进行查询是不推荐的。如果将带有%的字符串分配给变量,然后在表达式中使用该变量。那么,这样做
``` python
string = "HolyMoly'"
store.select('df', 'index == string')
```
来代替下面这样
``` python
string = "HolyMoly'"
store.select('df', 'index == %s' % string)
```
因为后者将 **不会** 起作用并引起 ``SyntaxError``。注意 ``string``变量的双引号里面有一个单引号。
如果你一定要插入,使用说明符格式 ``'%r'``
``` python
store.select('df', 'index == %r' % string)
```
它将会引用变量 ``string``.
:::
这儿有一些例子:
``` python
In [411]: dfq = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD'),
.....: index=pd.date_range('20130101', periods=10))
.....:
In [412]: store.append('dfq', dfq, format='table', data_columns=True)
```
使用布尔表达式同内联求值函数。
``` python
In [413]: store.select('dfq', "index>pd.Timestamp('20130104') & columns=['A', 'B']")
Out[413]:
A B
2013-01-05 0.450263 0.755221
2013-01-06 0.019915 0.300003
2013-01-07 1.878479 -0.026513
2013-01-08 3.272320 0.077044
2013-01-09 -0.398346 0.507286
2013-01-10 0.516017 -0.501550
```
内联列引用
``` python
In [414]: store.select('dfq', where="A>0 or C>0")
Out[414]:
A B C D
2013-01-01 -0.161614 -1.636805 0.835417 0.864817
2013-01-02 0.843452 -0.122918 -0.026122 -1.507533
2013-01-03 0.335303 -1.340566 -1.024989 1.125351
2013-01-05 0.450263 0.755221 -1.506656 0.808794
2013-01-06 0.019915 0.300003 -0.727093 -1.119363
2013-01-07 1.878479 -0.026513 0.573793 0.154237
2013-01-08 3.272320 0.077044 0.397034 -0.613983
2013-01-10 0.516017 -0.501550 0.138212 0.218366
```
关键词``columns`` 能用来筛选列字段并返回为列表,这等价于传递``'columns=list_of_columns_to_filter'``:
``` python
In [415]: store.select('df', "columns=['A', 'B']")
Out[415]:
A B
2000-01-01 -0.426936 -1.780784
2000-01-02 1.638174 -2.184251
2000-01-03 -1.022803 0.889445
2000-01-04 1.767446 -1.305266
2000-01-05 0.486743 0.954551
2000-01-06 -1.170458 -1.211386
2000-01-07 -0.450781 1.064650
2000-01-08 -0.810399 0.254343
```
``start`` and ``stop`` 参数能指定总的搜索范围。这些是根据表中的总行数得出来的。
::: tip 注意
如果查询表达式有未知的引用变量,那么``select`` 将会报错 ``ValueError`` 。通常这就意味着你正在尝试选取的一列并**不在**当前数据列中。
如果查询表达式无效,那么``select``将会报错``SyntaxError`` 。
:::
#### timedelta64[ns]的用法
你能使用``timedelta64[ns]``进行存储和查询。使用``()``来指定查询的条目,浮点数可以带符号(和小数),timedelta的单位可以是``D,s,ms,us,ns``。看示例:
```python
In [416]: from datetime import timedelta
In [417]: dftd = pd.DataFrame({'A': pd.Timestamp('20130101'),
.....: 'B': [pd.Timestamp('20130101') + timedelta(days=i,
.....: seconds=10)
.....: for i in range(10)]})
.....:
In [418]: dftd['C'] = dftd['A'] - dftd['B']
In [419]: dftd
Out[419]:
A B C
0 2013-01-01 2013-01-01 00:00:10 -1 days +23:59:50
1 2013-01-01 2013-01-02 00:00:10 -2 days +23:59:50
2 2013-01-01 2013-01-03 00:00:10 -3 days +23:59:50
3 2013-01-01 2013-01-04 00:00:10 -4 days +23:59:50
4 2013-01-01 2013-01-05 00:00:10 -5 days +23:59:50
5 2013-01-01 2013-01-06 00:00:10 -6 days +23:59:50
6 2013-01-01 2013-01-07 00:00:10 -7 days +23:59:50
7 2013-01-01 2013-01-08 00:00:10 -8 days +23:59:50
8 2013-01-01 2013-01-09 00:00:10 -9 days +23:59:50
9 2013-01-01 2013-01-10 00:00:10 -10 days +23:59:50
In [420]: store.append('dftd', dftd, data_columns=True)
In [421]: store.select('dftd', "C<'-3.5D'")
Out[421]:
A B C
4 2013-01-01 2013-01-05 00:00:10 -5 days +23:59:50
5 2013-01-01 2013-01-06 00:00:10 -6 days +23:59:50
6 2013-01-01 2013-01-07 00:00:10 -7 days +23:59:50
7 2013-01-01 2013-01-08 00:00:10 -8 days +23:59:50
8 2013-01-01 2013-01-09 00:00:10 -9 days +23:59:50
9 2013-01-01 2013-01-10 00:00:10 -10 days +23:59:50
```
#### 索引
你能在表格中已经有数据的情况下(在``append/put``操作之后)使用``create_table_index``创建/修改表格的索引。给表格创建索引是**强**推荐的操作。当你使用带有索引的``select``当作``where``查询条件的时候,这将极大的加快你的查询速度。
::: tip 注意
索引会自动创建在可索引对象和任意你指定的数据列。你可以传递``index=False`` 到``append``来关闭这个操作。
:::
```python
# we have automagically already created an index (in the first section)
In [422]: i = store.root.df.table.cols.index.index
In [423]: i.optlevel, i.kind
Out[423]: (6, 'medium')
# change an index by passing new parameters
In [424]: store.create_table_index('df', optlevel=9, kind='full')
In [425]: i = store.root.df.table.cols.index.index
In [426]: i.optlevel, i.kind
Out[426]: (9, 'full')
```
通常当有大量数据添加保存的时候,关闭添加列的索引创建,等结束后再创建是非常有效的。
```python
In [427]: df_1 = pd.DataFrame(np.random.randn(10, 2), columns=list('AB'))
In [428]: df_2 = pd.DataFrame(np.random.randn(10, 2), columns=list('AB'))
In [429]: st = pd.HDFStore('appends.h5', mode='w')
In [430]: st.append('df', df_1, data_columns=['B'], index=False)
In [431]: st.append('df', df_2, data_columns=['B'], index=False)
In [432]: st.get_storer('df').table
Out[432]:
/df/table (Table(20,)) ''
description := {
"index": Int64Col(shape=(), dflt=0, pos=0),
"values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1),
"B": Float64Col(shape=(), dflt=0.0, pos=2)}
byteorder := 'little'
chunkshape := (2730,)
```
当完成添加后再创建索引。
```python
In [433]: st.create_table_index('df', columns=['B'], optlevel=9, kind='full')
In [434]: st.get_storer('df').table
Out[434]:
/df/table (Table(20,)) ''
description := {
"index": Int64Col(shape=(), dflt=0, pos=0),
"values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1),
"B": Float64Col(shape=(), dflt=0.0, pos=2)}
byteorder := 'little'
chunkshape := (2730,)
autoindex := True
colindexes := {
"B": Index(9, full, shuffle, zlib(1)).is_csi=True}
In [435]: st.close()
```
看[这里](https://stackoverflow.com/questions/17893370/ptrepack-sortby-needs-full-index "这里")关于如何在现存的表格中创建完全分类索引(CSI)。
#### 通过数据列查询
你可以指定(并建立索引)某些你希望能够执行查询的列(除了可索引的列,你始终可以查询这些列)。例如,假设你要在磁盘上执行此常规操作,仅返回与该查询匹配的帧。你可以指定``data_columns = True``来强制所有列为``data_columns``。
```python
In [436]: df_dc = df.copy()
In [437]: df_dc['string'] = 'foo'
In [438]: df_dc.loc[df_dc.index[4:6], 'string'] = np.nan
In [439]: df_dc.loc[df_dc.index[7:9], 'string'] = 'bar'
In [440]: df_dc['string2'] = 'cool'
In [441]: df_dc.loc[df_dc.index[1:3], ['B', 'C']] = 1.0
In [442]: df_dc
Out[442]:
A B C string string2
2000-01-01 -0.426936 -1.780784 0.322691 foo cool
2000-01-02 1.638174 1.000000 1.000000 foo cool
2000-01-03 -1.022803 1.000000 1.000000 foo cool
2000-01-04 1.767446 -1.305266 -0.378355 foo cool
2000-01-05 0.486743 0.954551 0.859671 NaN cool
2000-01-06 -1.170458 -1.211386 -0.852728 NaN cool
2000-01-07 -0.450781 1.064650 1.014927 foo cool
2000-01-08 -0.810399 0.254343 -0.875526 bar cool
# on-disk operations
In [443]: store.append('df_dc', df_dc, data_columns=['B', 'C', 'string', 'string2'])
In [444]: store.select('df_dc', where='B > 0')
Out[444]:
A B C string string2
2000-01-02 1.638174 1.000000 1.000000 foo cool
2000-01-03 -1.022803 1.000000 1.000000 foo cool
2000-01-05 0.486743 0.954551 0.859671 NaN cool
2000-01-07 -0.450781 1.064650 1.014927 foo cool
2000-01-08 -0.810399 0.254343 -0.875526 bar cool
# getting creative
In [445]: store.select('df_dc', 'B > 0 & C > 0 & string == foo')
Out[445]:
A B C string string2
2000-01-02 1.638174 1.00000 1.000000 foo cool
2000-01-03 -1.022803 1.00000 1.000000 foo cool
2000-01-07 -0.450781 1.06465 1.014927 foo cool
# this is in-memory version of this type of selection
In [446]: df_dc[(df_dc.B > 0) & (df_dc.C > 0) & (df_dc.string == 'foo')]
Out[446]:
A B C string string2
2000-01-02 1.638174 1.00000 1.000000 foo cool
2000-01-03 -1.022803 1.00000 1.000000 foo cool
2000-01-07 -0.450781 1.06465 1.014927 foo cool
# we have automagically created this index and the B/C/string/string2
# columns are stored separately as ``PyTables`` columns
In [447]: store.root.df_dc.table
Out[447]:
/df_dc/table (Table(8,)) ''
description := {
"index": Int64Col(shape=(), dflt=0, pos=0),
"values_block_0": Float64Col(shape=(1,), dflt=0.0, pos=1),
"B": Float64Col(shape=(), dflt=0.0, pos=2),
"C": Float64Col(shape=(), dflt=0.0, pos=3),
"string": StringCol(itemsize=3, shape=(), dflt=b'', pos=4),
"string2": StringCol(itemsize=4, shape=(), dflt=b'', pos=5)}
byteorder := 'little'
chunkshape := (1680,)
autoindex := True
colindexes := {
"index": Index(6, medium, shuffle, zlib(1)).is_csi=False,
"B": Index(6, medium, shuffle, zlib(1)).is_csi=False,
"C": Index(6, medium, shuffle, zlib(1)).is_csi=False,
"string": Index(6, medium, shuffle, zlib(1)).is_csi=False,
"string2": Index(6, medium, shuffle, zlib(1)).is_csi=False}
```
把很多列变成*数据列*会存在性能下降的情况,因此它取决于用户。此外,在第一次添加/插入操作后你不能改变数据列(也不能索引)(当然,你能读取数据和创建一个新表!)。
#### 迭代器
你能传递``iterator=True``或者``chunksize=number_in_a_chunk``给``select`` 和``select_as_multiple``,然后在结果中返回一个迭代器。默认一个块返回50,000行。
```python
In [448]: for df in store.select('df', chunksize=3):
.....: print(df)
.....:
A B C
2000-01-01 -0.426936 -1.780784 0.322691
2000-01-02 1.638174 -2.184251 0.049673
2000-01-03 -1.022803 0.889445 2.827717
A B C
2000-01-04 1.767446 -1.305266 -0.378355
2000-01-05 0.486743 0.954551 0.859671
2000-01-06 -1.170458 -1.211386 -0.852728
A B C
2000-01-07 -0.450781 1.064650 1.014927
2000-01-08 -0.810399 0.254343 -0.875526
```
::: tip 注意
你也能使用``read_hdf`` 打开迭代器,然后迭代结束会自动关闭保存。
```python
for df in pd.read_hdf('store.h5', 'df', chunksize=3):
print(df)
```
:::
注意,chunksize主要适用于**源**行。因此,如果你正在进行查询,chunksize将细分表中的总行和应用的查询,并在大小可能不相等的块上返回一个迭代器。
这是生成查询并使用它创建大小相等的返回块的方法。
```python
In [449]: dfeq = pd.DataFrame({'number': np.arange(1, 11)})
In [450]: dfeq
Out[450]:
number
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
In [451]: store.append('dfeq', dfeq, data_columns=['number'])
In [452]: def chunks(l, n):
.....: return [l[i:i + n] for i in range(0, len(l), n)]
.....:
In [453]: evens = [2, 4, 6, 8, 10]
In [454]: coordinates = store.select_as_coordinates('dfeq', 'number=evens')
In [455]: for c in chunks(coordinates, 2):
.....: print(store.select('dfeq', where=c))
.....:
number
1 2
3 4
number
5 6
7 8
number
9 10
```
#### 高级查询
##### 选取单列
使用``select_column``方法可以找到单个可索引列或数据列。例如,这将让你非常快速地得到索引。它会返回一个由行号索引的``Series``结果。目前不接受``where``选择器。
```python
In [456]: store.select_column('df_dc', 'index')
Out[456]:
0 2000-01-01
1 2000-01-02
2 2000-01-03
3 2000-01-04
4 2000-01-05
5 2000-01-06
6 2000-01-07
7 2000-01-08
Name: index, dtype: datetime64[ns]
In [457]: store.select_column('df_dc', 'string')
Out[457]:
0 foo
1 foo
2 foo
3 foo
4 NaN
5 NaN
6 foo
7 bar
Name: string, dtype: object
```
##### 选取坐标
有时候你想要得到查询的坐标(又叫做 索引的定位),使用``Int64Index``将返回结果的定位。这些坐标也可以传递给之后的``where``操作。
```python
In [458]: df_coord = pd.DataFrame(np.random.randn(1000, 2),
.....: index=pd.date_range('20000101', periods=1000))
.....:
In [459]: store.append('df_coord', df_coord)
In [460]: c = store.select_as_coordinates('df_coord', 'index > 20020101')
In [461]: c
Out[461]:
Int64Index([732, 733, 734, 735, 736, 737, 738, 739, 740, 741,
...
990, 991, 992, 993, 994, 995, 996, 997, 998, 999],
dtype='int64', length=268)
In [462]: store.select('df_coord', where=c)
Out[462]:
0 1
2002-01-02 0.440865 -0.151651
2002-01-03 -1.195089 0.285093
2002-01-04 -0.925046 0.386081
2002-01-05 -1.942756 0.277699
2002-01-06 0.811776 0.528965
... ... ...
2002-09-22 1.061729 0.618085
2002-09-23 -0.209744 0.677197
2002-09-24 -1.808184 0.185667
2002-09-25 -0.208629 0.928603
2002-09-26 1.579717 -1.259530
[268 rows x 2 columns]
```
##### 使用位置遮罩选取
有时你的查询可能涉及到创建要选择的行列表。通常这个``mask``将得到索引操作的``index``结果。下面这个例子显示了选取日期索引的月份等于5的操作。
```python
In [463]: df_mask = pd.DataFrame(np.random.randn(1000, 2),
.....: index=pd.date_range('20000101', periods=1000))
.....:
In [464]: store.append('df_mask', df_mask)
In [465]: c = store.select_column('df_mask', 'index')
In [466]: where = c[pd.DatetimeIndex(c).month == 5].index
In [467]: store.select('df_mask', where=where)
Out[467]:
0 1
2000-05-01 -1.199892 1.073701
2000-05-02 -1.058552 0.658487
2000-05-03 -0.015418 0.452879
2000-05-04 1.737818 0.426356
2000-05-05 -0.711668 -0.021266
... ... ...
2002-05-27 0.656196 0.993383
2002-05-28 -0.035399 -0.269286
2002-05-29 0.704503 2.574402
2002-05-30 -1.301443 2.770770
2002-05-31 -0.807599 0.420431
[93 rows x 2 columns]
```
##### 存储对象
如果你想检查存储对象,可以通过``get_storer``找到。你能使用这种编程方法获得一个对象的行数。
```python
In [468]: store.get_storer('df_dc').nrows
Out[468]: 8
```
#### 多表查询
``append_to_multiple``和``select_as_multiple``方法能一次性执行多表的添加/选取操作。这个方法是让一个表(称为选择器表)索引大多数/所有列,并执行查询。其他表是带索引的数据表,它会匹配选择器表的索引。然后,你能在选择器表执行非常快速的查询并返回大量数据。这个方法类似于有个非常宽的表,但能高效的查询。
``append_to_multiple``方法根据``d``把单个DataFrame划分为多个表,这里的d指的是字典,即将表名映射到该表中所需的“列”列表。如果使用*None*代替列表,则该表将具有给定DataFrame的其余未指定列。``selector``参数定义了哪张表是选择器表(即你可以从中执行查询的)。``dropna``参数将删除输入``DataFrame ``的行来确保表格是同步的。这意味着如果其中一张表写入的一行全是``np.NaN``,那么将从所有表中删除这行。
如果``dropna``是False,则**用户负责同步表**。记住全是``np.Nan``的行是不会写入HDFStore,因此如果你选择``dropna=False``,一些表会比其他表具有更多行,而且``select_as_multiple ``将不会有作用或返回意外的结果。
```python
In [469]: df_mt = pd.DataFrame(np.random.randn(8, 6),
.....: index=pd.date_range('1/1/2000', periods=8),
.....: columns=['A', 'B', 'C', 'D', 'E', 'F'])
.....:
In [470]: df_mt['foo'] = 'bar'
In [471]: df_mt.loc[df_mt.index[1], ('A', 'B')] = np.nan
# you can also create the tables individually
In [472]: store.append_to_multiple({'df1_mt': ['A', 'B'], 'df2_mt': None},
.....: df_mt, selector='df1_mt')
.....:
In [473]: store
Out[473]:
File path: store.h5
# individual tables were created
In [474]: store.select('df1_mt')
Out[474]:
A B
2000-01-01 0.475158 0.427905
2000-01-02 NaN NaN
2000-01-03 -0.201829 0.651656
2000-01-04 -0.766427 -1.852010
2000-01-05 1.642910 -0.055583
2000-01-06 0.187880 1.536245
2000-01-07 -1.801014 0.244721
2000-01-08 3.055033 -0.683085
In [475]: store.select('df2_mt')
Out[475]:
C D E F foo
2000-01-01 1.846285 -0.044826 0.074867 0.156213 bar
2000-01-02 0.446978 -0.323516 0.311549 -0.661368 bar
2000-01-03 -2.657254 0.649636 1.520717 1.604905 bar
2000-01-04 -0.201100 -2.107934 -0.450691 -0.748581 bar
2000-01-05 0.543779 0.111444 0.616259 -0.679614 bar
2000-01-06 0.831475 -0.566063 1.130163 -1.004539 bar
2000-01-07 0.745984 1.532560 0.229376 0.526671 bar
2000-01-08 -0.922301 2.760888 0.515474 -0.129319 bar
# as a multiple
In [476]: store.select_as_multiple(['df1_mt', 'df2_mt'], where=['A>0', 'B>0'],
.....: selector='df1_mt')
.....:
Out[476]:
A B C D E F foo
2000-01-01 0.475158 0.427905 1.846285 -0.044826 0.074867 0.156213 bar
2000-01-06 0.187880 1.536245 0.831475 -0.566063 1.130163 -1.004539 bar
```
### 从表中删除
你能够通过``where``指定有选择地从表中删除数据。删除行,重点理解``PyTables``删除行是通过先抹去行,接着**删除**后面的数据。因此,根据数据的方向来删除会是非常耗时的操作。所以为了获得最佳性能,首先让要删除的数据维度可索引是很有必要的。
数据根据(在磁盘上)可索引项来排序,这里有个简单的用例。你能存储面板数据(也叫时间序列-截面数据),在``major_axis``中存储日期,而``minor_axis``中存储ids。数据像下面这样交错:
- date_1
- id_1
- id_2
- .
- id_n
- date_2
- id_1
- .
- id_n
应该清楚的是在 ``major_axis`` 上的删除操作将非常快,正如数据块被删除,接着后面的数据也会移动。另一方面,在``minor_axis`` 的操作将非常耗时。在这种情况下,几乎可以肯定使用``where``操作来选取所有除开缺失数据的列重写表格会更快。
::: danger 警告
请注意HDF5**不会自动回收空间**在h5文件中。于是重复地删除(或者移除节点)再添加操作**将会导致文件体积增大**。
要重新打包和清理文件,请使用[ptrepack](https://www.pypandas.cn/docs/user_guide/io.html#io-hdf5-ptrepack "ptrepack")。
:::
### 注意事项
#### 压缩
``PyTables`` 允许存储地数据被压缩。这适用于所有类型地存储,不仅仅是表格。这两个参数
``complevel``和 ``complib``可用来控制压缩。
``complevel``指定数据会以何种方式压缩。
``complib`` 指定要使用的压缩库。如果没有指定,那将使用默认的 ``zlib``库。压缩库通常会从压缩率或速度两方面来优化,而结果取决于数据类型。选择哪种压缩类型取决于你的具体需求和数据。下面是支持的压缩库列表:
- [zlib](https://zlib.net/): 默认的压缩库。经典的压缩方式,能获得好的压缩率但是速度有点慢。
- [lzo](https://www.oberhumer.com/opensource/lzo/): 快速地压缩和解压。
- [bzip2](http://bzip.org/): 不错的压缩率。
- [blosc](http://www.blosc.org/): 快速地压缩和解压。
*New in version 0.20.2:* 支持另一种blosc压缩机:
- [blosc:blosclz](http://www.blosc.org/) 这是默认地``blosc``压缩机
- [blosc:lz4](https://fastcompression.blogspot.dk/p/lz4.html):
一款紧凑、快速且流行的压缩机。
- [blosc:lz4hc](https://fastcompression.blogspot.dk/p/lz4.html):
调整后的LZ4版本可产生更好的压缩比,但会牺牲速度。
- [blosc:snappy](https://google.github.io/snappy/):
一款在很多地方使用的流行压缩机。
- [blosc:zlib](https://zlib.net/): 经典款;虽然比前一款速度慢,但是可实现更好的压缩比。
- [blosc:zstd](https://facebook.github.io/zstd/): 极其平衡的编解码器;它是以上所有压缩机中提供最佳压缩比的,且速度相当快。
如果 ``complib``定义为其他的,不在上表中的库 ,那么就会出现 ``ValueError``。
::: tip 注意
如果你的平台上缺失指定的 ``complib`` 库,压缩机会使用默认 ``zlib``库。
:::
文件中所有的对象都可以启用压缩:
``` python
store_compressed = pd.HDFStore('store_compressed.h5', complevel=9,
complib='blosc:blosclz')
```
或者在未启用压缩的存储中进行即时压缩(这仅适用于表格):
``` python
store.append('df', df, complib='zlib', complevel=5)
```
#### ptrepack
``PyTables``不是在一开始的时候开启压缩,而是在表被写入后再压缩,这提供了更好的写入性能。你能使用 ``PyTables`` 提供的实用程序``ptrepack``实现。此外,事实上在``ptrepack`` 之后会改变压缩等级。
```
ptrepack --chunkshape=auto --propindexes --complevel=9 --complib=blosc in.h5 out.h5
```
另外, ``ptrepack in.h5 out.h5`` 将重新打包文件让你可以重用之前删除的空间。或者,它能简单的删除文件并再次写入亦或使用 ``copy`` 方法。
#### 注意事项
::: danger 警告
``HDFStore`` **不是一个安全的写入线程**. ``PyTables`` 的底层仅支持(通过线程或进程的)并发读取。如果你要同时读取和写入,那么你需要单个进程的单个线程里序列化这些操作,否则会破坏你的数据。更多信息参见([GH2397](https://github.com/pandas-dev/pandas/issues/2397))。
:::
- 如果你用锁来管理多个进程间的写入, 那么你可能要在释放写锁之前使用[``fsync()``](https://docs.python.org/3/library/os.html#os.fsync) 。方便起见,你能用 ``store.flush(fsync=True)`` 操作。
- 一旦 ``table``创建的列(DataFrame)固定了; 那只有相同的列才可以添加数据。
- 注意时区 (例如, ``pytz.timezone('US/Eastern')``)在不同的时区版本间不相等。
因此,如果使用时区库的一个版本将数据本地化到HDFStore中的特定时区,并且使用另一个版本更新该数据,则由于这些时区不相等,因此数据将转换为UTC。使用相同版本的时区库或在更新的时区定义中使用 ``tz_convert``。
::: danger 警告
如果列名没能用作属性选择器,那么``PyTables`` 将显示``NaturalNameWarning`` 。
自然标识符仅包括字母、数字和下划线,且不能以数字开头。其他标识符不能用``where`` 从句,这通常不是个好主意。
:::
### 数据类型
``HDFStore`` 将对象数据类型映射到 ``PyTables`` 的底层数据类型。这意味着以下的已知类型都有效:
Type | Represents missing values
---|---
floating : float64, float32, float16 | np.nan
integer : int64, int32, int8, uint64,uint32, uint8 |
boolean |
datetime64[ns] | NaT
timedelta64[ns] | NaT
categorical : see the section below |
object : strings | np.nan
不支持``unicode`` 列,这会出现 **映射失败**.
#### 数据类别
你可以写入含``category`` 类型的数据到 ``HDFStore``。如果它是对象数组,那查询方式是一样的。然而, 含``category``的数据会以更高效的方式存储。
``` python
In [477]: dfcat = pd.DataFrame({'A': pd.Series(list('aabbcdba')).astype('category'),
.....: 'B': np.random.randn(8)})
.....:
In [478]: dfcat
Out[478]:
A B
0 a 1.706605
1 a 1.373485
2 b -0.758424
3 b -0.116984
4 c -0.959461
5 d -1.517439
6 b -0.453150
7 a -0.827739
In [479]: dfcat.dtypes
Out[479]:
A category
B float64
dtype: object
In [480]: cstore = pd.HDFStore('cats.h5', mode='w')
In [481]: cstore.append('dfcat', dfcat, format='table', data_columns=['A'])
In [482]: result = cstore.select('dfcat', where="A in ['b', 'c']")
In [483]: result
Out[483]:
A B
2 b -0.758424
3 b -0.116984
4 c -0.959461
6 b -0.453150
In [484]: result.dtypes
Out[484]:
A category
B float64
dtype: object
```
#### 字符串列
**min_itemsize**
对于字符串列, ``HDFStore``的底层使用的固定列宽(列的大小)。字符串列大小的计算方式是: **在第一个添加的时候**,传递给 ``HDFStore``(该列)数据长度的最大值。 随后的添加可能会引入**更大**一列字符串,这超过了该列所能容纳的内容,这将引发异常(不然,你可以悄悄地截断这些列,让信息丢失)。之后将放松这一点,允许用户指定截断。
在第一个表创建的时候,传递 ``min_itemsize`` 将优先指定特定字符串列的最小长度。``min_itemsize``可以是整数或将列名映射为整数的字典。 你可以将``values``作为键传递,以允许所有可索引对象或data_columns具有此min_itemsize。
传递 ``min_itemsize``字典将导致所有可传递列自动创建 data_columns。
::: tip 注意
如果你没有传递任意 ``data_columns``,那么``min_itemsize``将会传递任意字符串的最大长度。
:::
``` python
In [485]: dfs = pd.DataFrame({'A': 'foo', 'B': 'bar'}, index=list(range(5)))
In [486]: dfs
Out[486]:
A B
0 foo bar
1 foo bar
2 foo bar
3 foo bar
4 foo bar
# A and B have a size of 30
In [487]: store.append('dfs', dfs, min_itemsize=30)
In [488]: store.get_storer('dfs').table
Out[488]:
/dfs/table (Table(5,)) ''
description := {
"index": Int64Col(shape=(), dflt=0, pos=0),
"values_block_0": StringCol(itemsize=30, shape=(2,), dflt=b'', pos=1)}
byteorder := 'little'
chunkshape := (963,)
autoindex := True
colindexes := {
"index": Index(6, medium, shuffle, zlib(1)).is_csi=False}
# A is created as a data_column with a size of 30
# B is size is calculated
In [489]: store.append('dfs2', dfs, min_itemsize={'A': 30})
In [490]: store.get_storer('dfs2').table
Out[490]:
/dfs2/table (Table(5,)) ''
description := {
"index": Int64Col(shape=(), dflt=0, pos=0),
"values_block_0": StringCol(itemsize=3, shape=(1,), dflt=b'', pos=1),
"A": StringCol(itemsize=30, shape=(), dflt=b'', pos=2)}
byteorder := 'little'
chunkshape := (1598,)
autoindex := True
colindexes := {
"index": Index(6, medium, shuffle, zlib(1)).is_csi=False,
"A": Index(6, medium, shuffle, zlib(1)).is_csi=False}
```
**nan_rep**
字符串列将序列化 ``np.nan`` (缺失值)以 ``nan_rep`` 的字符串形式。默认的字符串值为``nan``。你可能会无意中将实际的``nan``值转换为缺失值。
``` python
In [491]: dfss = pd.DataFrame({'A': ['foo', 'bar', 'nan']})
In [492]: dfss
Out[492]:
A
0 foo
1 bar
2 nan
In [493]: store.append('dfss', dfss)
In [494]: store.select('dfss')
Out[494]:
A
0 foo
1 bar
2 NaN
# here you need to specify a different nan rep
In [495]: store.append('dfss2', dfss, nan_rep='_nan_')
In [496]: store.select('dfss2')
Out[496]:
A
0 foo
1 bar
2 nan
```
### 外部兼容性
``HDFStore``以特定格式写入``table``对象,这些格式适用于产生无损往返的pandas对象。 对于外部兼容性, ``HDFStore`` 能读取本地的 ``PyTables`` 格式表格。
可以编写一个``HDFStore`` 对象,该对象可以使用``rhdf5`` 库 ([Package website](https://www.bioconductor.org/packages/release/bioc/html/rhdf5.html))轻松导入到``R`` 中。创建表格存储可以像这样:
``` python
In [497]: df_for_r = pd.DataFrame({"first": np.random.rand(100),
.....: "second": np.random.rand(100),
.....: "class": np.random.randint(0, 2, (100, ))},
.....: index=range(100))
.....:
In [498]: df_for_r.head()
Out[498]:
first second class
0 0.366979 0.794525 0
1 0.296639 0.635178 1
2 0.395751 0.359693 0
3 0.484648 0.970016 1
4 0.810047 0.332303 0
In [499]: store_export = pd.HDFStore('export.h5')
In [500]: store_export.append('df_for_r', df_for_r, data_columns=df_dc.columns)
In [501]: store_export
Out[501]:
File path: export.h5
```
在这个R文件中使用``rhdf5``库能读入数据到``data.frame``对象中。下面这个示例函数从值中读取相应的列名和数据值,再组合它们到``data.frame``中:
``` R
# Load values and column names for all datasets from corresponding nodes and
# insert them into one data.frame object.
library(rhdf5)
loadhdf5data <- function(h5File) {
listing <- h5ls(h5File)
# Find all data nodes, values are stored in *_values and corresponding column
# titles in *_items
data_nodes <- grep("_values", listing$name)
name_nodes <- grep("_items", listing$name)
data_paths = paste(listing$group[data_nodes], listing$name[data_nodes], sep = "/")
name_paths = paste(listing$group[name_nodes], listing$name[name_nodes], sep = "/")
columns = list()
for (idx in seq(data_paths)) {
# NOTE: matrices returned by h5read have to be transposed to obtain
# required Fortran order!
data <- data.frame(t(h5read(h5File, data_paths[idx])))
names <- t(h5read(h5File, name_paths[idx]))
entry <- data.frame(data)
colnames(entry) <- names
columns <- append(columns, entry)
}
data <- data.frame(columns)
return(data)
}
```
现在你能导入 ``DataFrame`` 到R中:
``` R
> data = loadhdf5data("transfer.hdf5")
> head(data)
first second class
1 0.4170220047 0.3266449 0
2 0.7203244934 0.5270581 0
3 0.0001143748 0.8859421 1
4 0.3023325726 0.3572698 1
5 0.1467558908 0.9085352 1
6 0.0923385948 0.6233601 1
```
::: tip 注意
R函数列出了整个HDF5文件的内容,并从所有匹配的节点组合了``data.frame`` 对象,因此,如果你已将多个``DataFrame``对象存储到单个HDF5文件中,那么只能用它作为起点。
:::
### 性能
- 同``fixed``存储相比较,``tables`` 格式会有写入的性能损失。这样的好处就是便于(大量的数据)能添加/删除和查询。与常规存储相比,写入时间通常更长。但是查询的时间就相当快,特别是在有索引的轴上。
- 你可以传递 ``chunksize=`` 给``append``, 指定写入块的大小(默认是50000)。这将极大地降低写入时地内存使用情况。
- 你可以将`` expectedrows =``传递给第一个``append``来设置``PyTables``将预期的总行数。 这将优化读取/写入性能。
- 重复的行可以写入表格,但是在选取的时候会进行筛选(会选最后一项;然后表在主要、次要对上是唯一的)。
- 如果你企图存储已经序列化的PyTables类型数据(而不是存储为本地数据),那将引发A ``PerformanceWarning`` 。更多信息和解决办法参见[Here](https://stackoverflow.com/questions/14355151/how-to-make-pandas-hdfstore-put-operation-faster/14370190#14370190)。
## Feather
*New in version 0.20.0.*
Feather provides binary columnar serialization for data frames. It is designed to make reading and writing data
frames efficient, and to make sharing data across data analysis languages easy.
Feather is designed to faithfully serialize and de-serialize DataFrames, supporting all of the pandas
dtypes, including extension dtypes such as categorical and datetime with tz.
Several caveats.
- This is a newer library, and the format, though stable, is not guaranteed to be backward compatible
to the earlier versions.
- The format will NOT write an ``Index``, or ``MultiIndex`` for the
``DataFrame`` and will raise an error if a non-default one is provided. You
can ``.reset_index()`` to store the index or ``.reset_index(drop=True)`` to
ignore it.
- Duplicate column names and non-string columns names are not supported
- Non supported types include ``Period`` and actual Python object types. These will raise a helpful error message
on an attempt at serialization.
See the [Full Documentation](https://github.com/wesm/feather).
``` python
In [502]: df = pd.DataFrame({'a': list('abc'),
.....: 'b': list(range(1, 4)),
.....: 'c': np.arange(3, 6).astype('u1'),
.....: 'd': np.arange(4.0, 7.0, dtype='float64'),
.....: 'e': [True, False, True],
.....: 'f': pd.Categorical(list('abc')),
.....: 'g': pd.date_range('20130101', periods=3),
.....: 'h': pd.date_range('20130101', periods=3, tz='US/Eastern'),
.....: 'i': pd.date_range('20130101', periods=3, freq='ns')})
.....:
In [503]: df
Out[503]:
a b c d e f g h i
0 a 1 3 4.0 True a 2013-01-01 2013-01-01 00:00:00-05:00 2013-01-01 00:00:00.000000000
1 b 2 4 5.0 False b 2013-01-02 2013-01-02 00:00:00-05:00 2013-01-01 00:00:00.000000001
2 c 3 5 6.0 True c 2013-01-03 2013-01-03 00:00:00-05:00 2013-01-01 00:00:00.000000002
In [504]: df.dtypes
Out[504]:
a object
b int64
c uint8
d float64
e bool
f category
g datetime64[ns]
h datetime64[ns, US/Eastern]
i datetime64[ns]
dtype: object
```
Write to a feather file.
``` python
In [505]: df.to_feather('example.feather')
```
Read from a feather file.
``` python
In [506]: result = pd.read_feather('example.feather')
In [507]: result
Out[507]:
a b c d e f g h i
0 a 1 3 4.0 True a 2013-01-01 2013-01-01 00:00:00-05:00 2013-01-01 00:00:00.000000000
1 b 2 4 5.0 False b 2013-01-02 2013-01-02 00:00:00-05:00 2013-01-01 00:00:00.000000001
2 c 3 5 6.0 True c 2013-01-03 2013-01-03 00:00:00-05:00 2013-01-01 00:00:00.000000002
# we preserve dtypes
In [508]: result.dtypes
Out[508]:
a object
b int64
c uint8
d float64
e bool
f category
g datetime64[ns]
h datetime64[ns, US/Eastern]
i datetime64[ns]
dtype: object
```
## Parquet
*New in version 0.21.0.*
[Apache Parquet](https://parquet.apache.org/) provides a partitioned binary columnar serialization for data frames. It is designed to
make reading and writing data frames efficient, and to make sharing data across data analysis
languages easy. Parquet can use a variety of compression techniques to shrink the file size as much as possible
while still maintaining good read performance.
Parquet is designed to faithfully serialize and de-serialize ``DataFrame`` s, supporting all of the pandas
dtypes, including extension dtypes such as datetime with tz.
Several caveats.
- Duplicate column names and non-string columns names are not supported.
- The ``pyarrow`` engine always writes the index to the output, but ``fastparquet`` only writes non-default
indexes. This extra column can cause problems for non-Pandas consumers that are not expecting it. You can
force including or omitting indexes with the ``index`` argument, regardless of the underlying engine.
- Index level names, if specified, must be strings.
- Categorical dtypes can be serialized to parquet, but will de-serialize as ``object`` dtype.
- Non supported types include ``Period`` and actual Python object types. These will raise a helpful error message
on an attempt at serialization.
You can specify an ``engine`` to direct the serialization. This can be one of ``pyarrow``, or ``fastparquet``, or ``auto``.
If the engine is NOT specified, then the ``pd.options.io.parquet.engine`` option is checked; if this is also ``auto``,
then ``pyarrow`` is tried, and falling back to ``fastparquet``.
See the documentation for [pyarrow](https://arrow.apache.org/docs/python/) and [fastparquet](https://fastparquet.readthedocs.io/en/latest/).
::: tip Note
These engines are very similar and should read/write nearly identical parquet format files.
Currently ``pyarrow`` does not support timedelta data, ``fastparquet>=0.1.4`` supports timezone aware datetimes.
These libraries differ by having different underlying dependencies (``fastparquet`` by using ``numba``, while ``pyarrow`` uses a c-library).
:::
``` python
In [509]: df = pd.DataFrame({'a': list('abc'),
.....: 'b': list(range(1, 4)),
.....: 'c': np.arange(3, 6).astype('u1'),
.....: 'd': np.arange(4.0, 7.0, dtype='float64'),
.....: 'e': [True, False, True],
.....: 'f': pd.date_range('20130101', periods=3),
.....: 'g': pd.date_range('20130101', periods=3, tz='US/Eastern')})
.....:
In [510]: df
Out[510]:
a b c d e f g
0 a 1 3 4.0 True 2013-01-01 2013-01-01 00:00:00-05:00
1 b 2 4 5.0 False 2013-01-02 2013-01-02 00:00:00-05:00
2 c 3 5 6.0 True 2013-01-03 2013-01-03 00:00:00-05:00
In [511]: df.dtypes
Out[511]:
a object
b int64
c uint8
d float64
e bool
f datetime64[ns]
g datetime64[ns, US/Eastern]
dtype: object
```
Write to a parquet file.
``` python
In [512]: df.to_parquet('example_pa.parquet', engine='pyarrow')
In [513]: df.to_parquet('example_fp.parquet', engine='fastparquet')
```
Read from a parquet file.
``` python
In [514]: result = pd.read_parquet('example_fp.parquet', engine='fastparquet')
In [515]: result = pd.read_parquet('example_pa.parquet', engine='pyarrow')
In [516]: result.dtypes
Out[516]:
a object
b int64
c uint8
d float64
e bool
f datetime64[ns]
g datetime64[ns, US/Eastern]
dtype: object
```
Read only certain columns of a parquet file.
``` python
In [517]: result = pd.read_parquet('example_fp.parquet',
.....: engine='fastparquet', columns=['a', 'b'])
.....:
In [518]: result = pd.read_parquet('example_pa.parquet',
.....: engine='pyarrow', columns=['a', 'b'])
.....:
In [519]: result.dtypes
Out[519]:
a object
b int64
dtype: object
```
### Handling indexes
Serializing a ``DataFrame`` to parquet may include the implicit index as one or
more columns in the output file. Thus, this code:
``` python
In [520]: df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
In [521]: df.to_parquet('test.parquet', engine='pyarrow')
```
creates a parquet file with three columns if you use ``pyarrow`` for serialization:
``a``, ``b``, and ``__index_level_0__``. If you’re using ``fastparquet``, the
index [may or may not](https://fastparquet.readthedocs.io/en/latest/api.html#fastparquet.write)
be written to the file.
This unexpected extra column causes some databases like Amazon Redshift to reject
the file, because that column doesn’t exist in the target table.
If you want to omit a dataframe’s indexes when writing, pass ``index=False`` to
[``to_parquet()``](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_parquet.html#pandas.DataFrame.to_parquet):
``` python
In [522]: df.to_parquet('test.parquet', index=False)
```
This creates a parquet file with just the two expected columns, ``a`` and ``b``.
If your ``DataFrame`` has a custom index, you won’t get it back when you load
this file into a ``DataFrame``.
Passing ``index=True`` will always write the index, even if that’s not the
underlying engine’s default behavior.
### Partitioning Parquet files
*New in version 0.24.0.*
Parquet supports partitioning of data based on the values of one or more columns.
``` python
In [523]: df = pd.DataFrame({'a': [0, 0, 1, 1], 'b': [0, 1, 0, 1]})
In [524]: df.to_parquet(fname='test', engine='pyarrow',
.....: partition_cols=['a'], compression=None)
.....:
```
The *fname* specifies the parent directory to which data will be saved.
The *partition_cols* are the column names by which the dataset will be partitioned.
Columns are partitioned in the order they are given. The partition splits are
determined by the unique values in the partition columns.
The above example creates a partitioned dataset that may look like:
```
test
├── a=0
│ ├── 0bac803e32dc42ae83fddfd029cbdebc.parquet
│ └── ...
└── a=1
├── e6ab24a4f45147b49b54a662f0c412a3.parquet
└── ...
```
## SQL queries
The ``pandas.io.sql`` module provides a collection of query wrappers to both
facilitate data retrieval and to reduce dependency on DB-specific API. Database abstraction
is provided by SQLAlchemy if installed. In addition you will need a driver library for
your database. Examples of such drivers are [psycopg2](http://initd.org/psycopg/)
for PostgreSQL or [pymysql](https://github.com/PyMySQL/PyMySQL) for MySQL.
For [SQLite](https://docs.python.org/3/library/sqlite3.html) this is
included in Python’s standard library by default.
You can find an overview of supported drivers for each SQL dialect in the
[SQLAlchemy docs](https://docs.sqlalchemy.org/en/latest/dialects/index.html).
If SQLAlchemy is not installed, a fallback is only provided for sqlite (and
for mysql for backwards compatibility, but this is deprecated and will be
removed in a future version).
This mode requires a Python database adapter which respect the [Python
DB-API](https://www.python.org/dev/peps/pep-0249/).
See also some [cookbook examples](cookbook.html#cookbook-sql) for some advanced strategies.
The key functions are:
Method | Description
---|---
[read_sql_table](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sql.html#pandas.read_sql)(table_name, con[, schema, …]) | Read SQL database table into a DataFrame.
[read_sql_query](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sql_query.html#pandas.read_sql_query)(sql, con[, index_col, …]) | Read SQL query into a DataFrame.
[read_sql](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sql.html#pandas.read_sql)(sql, con[, index_col, …]) | Read SQL query or database table into a DataFrame.
[DataFrame.to_sql](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html#pandas.DataFrame.to_sql)(self, name, con[, schema, …]) | Write records stored in a DataFrame to a SQL database.
::: tip Note
The function [``read_sql()``](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sql.html#pandas.read_sql) is a convenience wrapper around
[``read_sql_table()``](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sql_table.html#pandas.read_sql_table) and [``read_sql_query()``](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sql_query.html#pandas.read_sql_query) (and for
backward compatibility) and will delegate to specific function depending on
the provided input (database table name or sql query).
Table names do not need to be quoted if they have special characters.
:::
In the following example, we use the [SQlite](https://www.sqlite.org/) SQL database
engine. You can use a temporary SQLite database where data are stored in
“memory”.
To connect with SQLAlchemy you use the ``create_engine()`` function to create an engine
object from database URI. You only need to create the engine once per database you are
connecting to.
For more information on ``create_engine()`` and the URI formatting, see the examples
below and the SQLAlchemy [documentation](https://docs.sqlalchemy.org/en/latest/core/engines.html)
``` python
In [525]: from sqlalchemy import create_engine
# Create your engine.
In [526]: engine = create_engine('sqlite:///:memory:')
```
If you want to manage your own connections you can pass one of those instead:
``` python
with engine.connect() as conn, conn.begin():
data = pd.read_sql_table('data', conn)
```
### Writing DataFrames
Assuming the following data is in a ``DataFrame`` ``data``, we can insert it into
the database using [``to_sql()``](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html#pandas.DataFrame.to_sql).
id | Date | Col_1 | Col_2 | Col_3
---|---|---|---|---
26 | 2012-10-18 | X | 25.7 | True
42 | 2012-10-19 | Y | -12.4 | False
63 | 2012-10-20 | Z | 5.73 | True
``` python
In [527]: data
Out[527]:
id Date Col_1 Col_2 Col_3
0 26 2010-10-18 X 27.50 True
1 42 2010-10-19 Y -12.50 False
2 63 2010-10-20 Z 5.73 True
In [528]: data.to_sql('data', engine)
```
With some databases, writing large DataFrames can result in errors due to
packet size limitations being exceeded. This can be avoided by setting the
``chunksize`` parameter when calling ``to_sql``. For example, the following
writes ``data`` to the database in batches of 1000 rows at a time:
``` python
In [529]: data.to_sql('data_chunked', engine, chunksize=1000)
```
#### SQL data types
[``to_sql()``](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html#pandas.DataFrame.to_sql) will try to map your data to an appropriate
SQL data type based on the dtype of the data. When you have columns of dtype
``object``, pandas will try to infer the data type.
You can always override the default type by specifying the desired SQL type of
any of the columns by using the ``dtype`` argument. This argument needs a
dictionary mapping column names to SQLAlchemy types (or strings for the sqlite3
fallback mode).
For example, specifying to use the sqlalchemy ``String`` type instead of the
default ``Text`` type for string columns:
``` python
In [530]: from sqlalchemy.types import String
In [531]: data.to_sql('data_dtype', engine, dtype={'Col_1': String})
```
::: tip Note
Due to the limited support for timedelta’s in the different database
flavors, columns with type ``timedelta64`` will be written as integer
values as nanoseconds to the database and a warning will be raised.
:::
::: tip Note
Columns of ``category`` dtype will be converted to the dense representation
as you would get with ``np.asarray(categorical)`` (e.g. for string categories
this gives an array of strings).
Because of this, reading the database table back in does **not** generate
a categorical.
:::
### Datetime data types
Using SQLAlchemy, [``to_sql()``](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html#pandas.DataFrame.to_sql) is capable of writing
datetime data that is timezone naive or timezone aware. However, the resulting
data stored in the database ultimately depends on the supported data type
for datetime data of the database system being used.
The following table lists supported data types for datetime data for some
common databases. Other database dialects may have different data types for
datetime data.
Database | SQL Datetime Types | Timezone Support
---|---|---
SQLite | TEXT | No
MySQL | TIMESTAMP or DATETIME | No
PostgreSQL | TIMESTAMP or TIMESTAMP WITH TIME ZONE | Yes
When writing timezone aware data to databases that do not support timezones,
the data will be written as timezone naive timestamps that are in local time
with respect to the timezone.
[``read_sql_table()``](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sql_table.html#pandas.read_sql_table) is also capable of reading datetime data that is
timezone aware or naive. When reading ``TIMESTAMP WITH TIME ZONE`` types, pandas
will convert the data to UTC.
#### Insertion method
*New in version 0.24.0.*
The parameter ``method`` controls the SQL insertion clause used.
Possible values are:
- ``None``: Uses standard SQL ``INSERT`` clause (one per row).
- ``'multi'``: Pass multiple values in a single ``INSERT`` clause.
It uses a special SQL syntax not supported by all backends.
This usually provides better performance for analytic databases
like Presto and Redshift, but has worse performance for
traditional SQL backend if the table contains many columns.
For more information check the SQLAlchemy [documention](http://docs.sqlalchemy.org/en/latest/core/dml.html#sqlalchemy.sql.expression.Insert.values.params.*args).
- callable with signature ``(pd_table, conn, keys, data_iter)``:
This can be used to implement a more performant insertion method based on
specific backend dialect features.
Example of a callable using PostgreSQL [COPY clause](https://www.postgresql.org/docs/current/static/sql-copy.html):
``` python
# Alternative to_sql() *method* for DBs that support COPY FROM
import csv
from io import StringIO
def psql_insert_copy(table, conn, keys, data_iter):
# gets a DBAPI connection that can provide a cursor
dbapi_conn = conn.connection
with dbapi_conn.cursor() as cur:
s_buf = StringIO()
writer = csv.writer(s_buf)
writer.writerows(data_iter)
s_buf.seek(0)
columns = ', '.join('"{}"'.format(k) for k in keys)
if table.schema:
table_name = '{}.{}'.format(table.schema, table.name)
else:
table_name = table.name
sql = 'COPY {} ({}) FROM STDIN WITH CSV'.format(
table_name, columns)
cur.copy_expert(sql=sql, file=s_buf)
```
### Reading tables
[``read_sql_table()``](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sql_table.html#pandas.read_sql_table) will read a database table given the
table name and optionally a subset of columns to read.
::: tip Note
In order to use [``read_sql_table()``](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sql_table.html#pandas.read_sql_table), you **must** have the
SQLAlchemy optional dependency installed.
:::
``` python
In [532]: pd.read_sql_table('data', engine)
Out[532]:
index id Date Col_1 Col_2 Col_3
0 0 26 2010-10-18 X 27.50 True
1 1 42 2010-10-19 Y -12.50 False
2 2 63 2010-10-20 Z 5.73 True
```
You can also specify the name of the column as the ``DataFrame`` index,
and specify a subset of columns to be read.
``` python
In [533]: pd.read_sql_table('data', engine, index_col='id')
Out[533]:
index Date Col_1 Col_2 Col_3
id
26 0 2010-10-18 X 27.50 True
42 1 2010-10-19 Y -12.50 False
63 2 2010-10-20 Z 5.73 True
In [534]: pd.read_sql_table('data', engine, columns=['Col_1', 'Col_2'])
Out[534]:
Col_1 Col_2
0 X 27.50
1 Y -12.50
2 Z 5.73
```
And you can explicitly force columns to be parsed as dates:
``` python
In [535]: pd.read_sql_table('data', engine, parse_dates=['Date'])
Out[535]:
index id Date Col_1 Col_2 Col_3
0 0 26 2010-10-18 X 27.50 True
1 1 42 2010-10-19 Y -12.50 False
2 2 63 2010-10-20 Z 5.73 True
```
If needed you can explicitly specify a format string, or a dict of arguments
to pass to [``pandas.to_datetime()``](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html#pandas.to_datetime):
``` python
pd.read_sql_table('data', engine, parse_dates={'Date': '%Y-%m-%d'})
pd.read_sql_table('data', engine,
parse_dates={'Date': {'format': '%Y-%m-%d %H:%M:%S'}})
```
You can check if a table exists using ``has_table()``
### Schema support
Reading from and writing to different schema’s is supported through the ``schema``
keyword in the [``read_sql_table()``](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sql_table.html#pandas.read_sql_table) and [``to_sql()``](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html#pandas.DataFrame.to_sql)
functions. Note however that this depends on the database flavor (sqlite does not
have schema’s). For example:
``` python
df.to_sql('table', engine, schema='other_schema')
pd.read_sql_table('table', engine, schema='other_schema')
```
### Querying
You can query using raw SQL in the [``read_sql_query()``](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sql_query.html#pandas.read_sql_query) function.
In this case you must use the SQL variant appropriate for your database.
When using SQLAlchemy, you can also pass SQLAlchemy Expression language constructs,
which are database-agnostic.
``` python
In [536]: pd.read_sql_query('SELECT * FROM data', engine)
Out[536]:
index id Date Col_1 Col_2 Col_3
0 0 26 2010-10-18 00:00:00.000000 X 27.50 1
1 1 42 2010-10-19 00:00:00.000000 Y -12.50 0
2 2 63 2010-10-20 00:00:00.000000 Z 5.73 1
```
Of course, you can specify a more “complex” query.
``` python
In [537]: pd.read_sql_query("SELECT id, Col_1, Col_2 FROM data WHERE id = 42;", engine)
Out[537]:
id Col_1 Col_2
0 42 Y -12.5
```
The [``read_sql_query()``](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sql_query.html#pandas.read_sql_query) function supports a ``chunksize`` argument.
Specifying this will return an iterator through chunks of the query result:
``` python
In [538]: df = pd.DataFrame(np.random.randn(20, 3), columns=list('abc'))
In [539]: df.to_sql('data_chunks', engine, index=False)
```
``` python
In [540]: for chunk in pd.read_sql_query("SELECT * FROM data_chunks",
.....: engine, chunksize=5):
.....: print(chunk)
.....:
a b c
0 -0.900850 -0.323746 0.037100
1 0.057533 -0.032842 0.550902
2 1.026623 1.035455 -0.965140
3 -0.252405 -1.255987 0.639156
4 1.076701 -0.309155 -0.800182
a b c
0 -0.206623 0.496077 -0.219935
1 0.631362 -1.166743 1.808368
2 0.023531 0.987573 0.471400
3 -0.982250 -0.192482 1.195452
4 -1.758855 0.477551 1.412567
a b c
0 -1.120570 1.232764 0.417814
1 1.688089 -0.037645 -0.269582
2 0.646823 -0.603366 1.592966
3 0.724019 -0.515606 -0.180920
4 0.038244 -2.292866 -0.114634
a b c
0 -0.970230 -0.963257 -0.128304
1 0.498621 -1.496506 0.701471
2 -0.272608 -0.119424 -0.882023
3 -0.253477 0.714395 0.664179
4 0.897140 0.455791 1.549590
```
You can also run a plain query without creating a ``DataFrame`` with
``execute()``. This is useful for queries that don’t return values,
such as INSERT. This is functionally equivalent to calling ``execute`` on the
SQLAlchemy engine or db connection object. Again, you must use the SQL syntax
variant appropriate for your database.
``` python
from pandas.io import sql
sql.execute('SELECT * FROM table_name', engine)
sql.execute('INSERT INTO table_name VALUES(?, ?, ?)', engine,
params=[('id', 1, 12.2, True)])
```
### Engine connection examples
To connect with SQLAlchemy you use the ``create_engine()`` function to create an engine
object from database URI. You only need to create the engine once per database you are
connecting to.
``` python
from sqlalchemy import create_engine
engine = create_engine('postgresql://scott:tiger@localhost:5432/mydatabase')
engine = create_engine('mysql+mysqldb://scott:tiger@localhost/foo')
engine = create_engine('oracle://scott:tiger@127.0.0.1:1521/sidname')
engine = create_engine('mssql+pyodbc://mydsn')
# sqlite:///
# where is relative:
engine = create_engine('sqlite:///foo.db')
# or absolute, starting with a slash:
engine = create_engine('sqlite:////absolute/path/to/foo.db')
```
For more information see the examples the SQLAlchemy [documentation](https://docs.sqlalchemy.org/en/latest/core/engines.html)
### Advanced SQLAlchemy queries
You can use SQLAlchemy constructs to describe your query.
Use ``sqlalchemy.text()`` to specify query parameters in a backend-neutral way
``` python
In [541]: import sqlalchemy as sa
In [542]: pd.read_sql(sa.text('SELECT * FROM data where Col_1=:col1'),
.....: engine, params={'col1': 'X'})
.....:
Out[542]:
index id Date Col_1 Col_2 Col_3
0 0 26 2010-10-18 00:00:00.000000 X 27.5 1
```
If you have an SQLAlchemy description of your database you can express where conditions using SQLAlchemy expressions
``` python
In [543]: metadata = sa.MetaData()
In [544]: data_table = sa.Table('data', metadata,
.....: sa.Column('index', sa.Integer),
.....: sa.Column('Date', sa.DateTime),
.....: sa.Column('Col_1', sa.String),
.....: sa.Column('Col_2', sa.Float),
.....: sa.Column('Col_3', sa.Boolean),
.....: )
.....:
In [545]: pd.read_sql(sa.select([data_table]).where(data_table.c.Col_3 is True), engine)
Out[545]:
Empty DataFrame
Columns: [index, Date, Col_1, Col_2, Col_3]
Index: []
```
You can combine SQLAlchemy expressions with parameters passed to [``read_sql()``](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sql.html#pandas.read_sql) using ``sqlalchemy.bindparam()``
``` python
In [546]: import datetime as dt
In [547]: expr = sa.select([data_table]).where(data_table.c.Date > sa.bindparam('date'))
In [548]: pd.read_sql(expr, engine, params={'date': dt.datetime(2010, 10, 18)})
Out[548]:
index Date Col_1 Col_2 Col_3
0 1 2010-10-19 Y -12.50 False
1 2 2010-10-20 Z 5.73 True
```
### Sqlite fallback
The use of sqlite is supported without using SQLAlchemy.
This mode requires a Python database adapter which respect the [Python
DB-API](https://www.python.org/dev/peps/pep-0249/).
You can create connections like so:
``` python
import sqlite3
con = sqlite3.connect(':memory:')
```
And then issue the following queries:
``` python
data.to_sql('data', con)
pd.read_sql_query("SELECT * FROM data", con)
```
## Google BigQuery
::: danger Warning
Starting in 0.20.0, pandas has split off Google BigQuery support into the
separate package ``pandas-gbq``. You can ``pip install pandas-gbq`` to get it.
:::
The ``pandas-gbq`` package provides functionality to read/write from Google BigQuery.
pandas integrates with this external package. if ``pandas-gbq`` is installed, you can
use the pandas methods ``pd.read_gbq`` and ``DataFrame.to_gbq``, which will call the
respective functions from ``pandas-gbq``.
Full documentation can be found [here](https://pandas-gbq.readthedocs.io/).
## Stata format
### Writing to stata format
The method ``to_stata()`` will write a DataFrame
into a .dta file. The format version of this file is always 115 (Stata 12).
``` python
In [549]: df = pd.DataFrame(np.random.randn(10, 2), columns=list('AB'))
In [550]: df.to_stata('stata.dta')
```
Stata data files have limited data type support; only strings with
244 or fewer characters, ``int8``, ``int16``, ``int32``, ``float32``
and ``float64`` can be stored in ``.dta`` files. Additionally,
Stata reserves certain values to represent missing data. Exporting a
non-missing value that is outside of the permitted range in Stata for
a particular data type will retype the variable to the next larger
size. For example, ``int8`` values are restricted to lie between -127
and 100 in Stata, and so variables with values above 100 will trigger
a conversion to ``int16``. ``nan`` values in floating points data
types are stored as the basic missing data type (``.`` in Stata).
::: tip Note
It is not possible to export missing data values for integer data types.
:::
The Stata writer gracefully handles other data types including ``int64``,
``bool``, ``uint8``, ``uint16``, ``uint32`` by casting to
the smallest supported type that can represent the data. For example, data
with a type of ``uint8`` will be cast to ``int8`` if all values are less than
100 (the upper bound for non-missing ``int8`` data in Stata), or, if values are
outside of this range, the variable is cast to ``int16``.
::: danger Warning
Conversion from ``int64`` to ``float64`` may result in a loss of precision
if ``int64`` values are larger than 2**53.
:::
::: danger Warning
``StataWriter`` and
``to_stata()`` only support fixed width
strings containing up to 244 characters, a limitation imposed by the version
115 dta file format. Attempting to write Stata dta files with strings
longer than 244 characters raises a ``ValueError``.
:::
### Reading from Stata format
The top-level function ``read_stata`` will read a dta file and return
either a ``DataFrame`` or a ``StataReader`` that can
be used to read the file incrementally.
``` python
In [551]: pd.read_stata('stata.dta')
Out[551]:
index A B
0 0 1.031231 0.196447
1 1 0.190188 0.619078
2 2 0.036658 -0.100501
3 3 0.201772 1.763002
4 4 0.454977 -1.958922
5 5 -0.628529 0.133171
6 6 -1.274374 2.518925
7 7 -0.517547 -0.360773
8 8 0.877961 -1.881598
9 9 -0.699067 -1.566913
```
Specifying a ``chunksize`` yields a
``StataReader`` instance that can be used to
read ``chunksize`` lines from the file at a time. The ``StataReader``
object can be used as an iterator.
``` python
In [552]: reader = pd.read_stata('stata.dta', chunksize=3)
In [553]: for df in reader:
.....: print(df.shape)
.....:
(3, 3)
(3, 3)
(3, 3)
(1, 3)
```
For more fine-grained control, use ``iterator=True`` and specify
``chunksize`` with each call to
``read()``.
``` python
In [554]: reader = pd.read_stata('stata.dta', iterator=True)
In [555]: chunk1 = reader.read(5)
In [556]: chunk2 = reader.read(5)
```
Currently the ``index`` is retrieved as a column.
The parameter ``convert_categoricals`` indicates whether value labels should be
read and used to create a ``Categorical`` variable from them. Value labels can
also be retrieved by the function ``value_labels``, which requires ``read()``
to be called before use.
The parameter ``convert_missing`` indicates whether missing value
representations in Stata should be preserved. If ``False`` (the default),
missing values are represented as ``np.nan``. If ``True``, missing values are
represented using ``StataMissingValue`` objects, and columns containing missing
values will have ``object`` data type.
::: tip Note
[``read_stata()``](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_stata.html#pandas.read_stata) and
``StataReader`` support .dta formats 113-115
(Stata 10-12), 117 (Stata 13), and 118 (Stata 14).
:::
::: tip Note
Setting ``preserve_dtypes=False`` will upcast to the standard pandas data types:
``int64`` for all integer types and ``float64`` for floating point data. By default,
the Stata data types are preserved when importing.
:::
#### Categorical data
``Categorical`` data can be exported to Stata data files as value labeled data.
The exported data consists of the underlying category codes as integer data values
and the categories as value labels. Stata does not have an explicit equivalent
to a ``Categorical`` and information about whether the variable is ordered
is lost when exporting.
::: danger Warning
Stata only supports string value labels, and so ``str`` is called on the
categories when exporting data. Exporting ``Categorical`` variables with
non-string categories produces a warning, and can result a loss of
information if the ``str`` representations of the categories are not unique.
:::
Labeled data can similarly be imported from Stata data files as ``Categorical``
variables using the keyword argument ``convert_categoricals`` (``True`` by default).
The keyword argument ``order_categoricals`` (``True`` by default) determines
whether imported ``Categorical`` variables are ordered.
::: tip Note
When importing categorical data, the values of the variables in the Stata
data file are not preserved since ``Categorical`` variables always
use integer data types between ``-1`` and ``n-1`` where ``n`` is the number
of categories. If the original values in the Stata data file are required,
these can be imported by setting ``convert_categoricals=False``, which will
import original data (but not the variable labels). The original values can
be matched to the imported categorical data since there is a simple mapping
between the original Stata data values and the category codes of imported
Categorical variables: missing values are assigned code ``-1``, and the
smallest original value is assigned ``0``, the second smallest is assigned
``1`` and so on until the largest original value is assigned the code ``n-1``.
:::
::: tip Note
Stata supports partially labeled series. These series have value labels for
some but not all data values. Importing a partially labeled series will produce
a ``Categorical`` with string categories for the values that are labeled and
numeric categories for values with no label.
:::
## SAS formats
The top-level function [``read_sas()``](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_sas.html#pandas.read_sas) can read (but not write) SAS
*xport* (.XPT) and (since v0.18.0) *SAS7BDAT* (.sas7bdat) format files.
SAS files only contain two value types: ASCII text and floating point
values (usually 8 bytes but sometimes truncated). For xport files,
there is no automatic type conversion to integers, dates, or
categoricals. For SAS7BDAT files, the format codes may allow date
variables to be automatically converted to dates. By default the
whole file is read and returned as a ``DataFrame``.
Specify a ``chunksize`` or use ``iterator=True`` to obtain reader
objects (``XportReader`` or ``SAS7BDATReader``) for incrementally
reading the file. The reader objects also have attributes that
contain additional information about the file and its variables.
Read a SAS7BDAT file:
``` python
df = pd.read_sas('sas_data.sas7bdat')
```
Obtain an iterator and read an XPORT file 100,000 lines at a time:
``` python
def do_something(chunk):
pass
rdr = pd.read_sas('sas_xport.xpt', chunk=100000)
for chunk in rdr:
do_something(chunk)
```
The [specification](https://support.sas.com/techsup/technote/ts140.pdf) for the xport file format is available from the SAS
web site.
No official documentation is available for the SAS7BDAT format.
## Other file formats
pandas itself only supports IO with a limited set of file formats that map
cleanly to its tabular data model. For reading and writing other file formats
into and from pandas, we recommend these packages from the broader community.
### netCDF
[xarray](https://xarray.pydata.org/) provides data structures inspired by the pandas ``DataFrame`` for working
with multi-dimensional datasets, with a focus on the netCDF file format and
easy conversion to and from pandas.
## Performance considerations
This is an informal comparison of various IO methods, using pandas
0.20.3. Timings are machine dependent and small differences should be
ignored.
``` python
In [1]: sz = 1000000
In [2]: df = pd.DataFrame({'A': np.random.randn(sz), 'B': [1] * sz})
In [3]: df.info()
RangeIndex: 1000000 entries, 0 to 999999
Data columns (total 2 columns):
A 1000000 non-null float64
B 1000000 non-null int64
dtypes: float64(1), int64(1)
memory usage: 15.3 MB
```
Given the next test set:
``` python
from numpy.random import randn
sz = 1000000
df = pd.DataFrame({'A': randn(sz), 'B': [1] * sz})
def test_sql_write(df):
if os.path.exists('test.sql'):
os.remove('test.sql')
sql_db = sqlite3.connect('test.sql')
df.to_sql(name='test_table', con=sql_db)
sql_db.close()
def test_sql_read():
sql_db = sqlite3.connect('test.sql')
pd.read_sql_query("select * from test_table", sql_db)
sql_db.close()
def test_hdf_fixed_write(df):
df.to_hdf('test_fixed.hdf', 'test', mode='w')
def test_hdf_fixed_read():
pd.read_hdf('test_fixed.hdf', 'test')
def test_hdf_fixed_write_compress(df):
df.to_hdf('test_fixed_compress.hdf', 'test', mode='w', complib='blosc')
def test_hdf_fixed_read_compress():
pd.read_hdf('test_fixed_compress.hdf', 'test')
def test_hdf_table_write(df):
df.to_hdf('test_table.hdf', 'test', mode='w', format='table')
def test_hdf_table_read():
pd.read_hdf('test_table.hdf', 'test')
def test_hdf_table_write_compress(df):
df.to_hdf('test_table_compress.hdf', 'test', mode='w',
complib='blosc', format='table')
def test_hdf_table_read_compress():
pd.read_hdf('test_table_compress.hdf', 'test')
def test_csv_write(df):
df.to_csv('test.csv', mode='w')
def test_csv_read():
pd.read_csv('test.csv', index_col=0)
def test_feather_write(df):
df.to_feather('test.feather')
def test_feather_read():
pd.read_feather('test.feather')
def test_pickle_write(df):
df.to_pickle('test.pkl')
def test_pickle_read():
pd.read_pickle('test.pkl')
def test_pickle_write_compress(df):
df.to_pickle('test.pkl.compress', compression='xz')
def test_pickle_read_compress():
pd.read_pickle('test.pkl.compress', compression='xz')
```
When writing, the top-three functions in terms of speed are are
``test_pickle_write``, ``test_feather_write`` and ``test_hdf_fixed_write_compress``.
``` python
In [14]: %timeit test_sql_write(df)
2.37 s ± 36.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [15]: %timeit test_hdf_fixed_write(df)
194 ms ± 65.9 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [26]: %timeit test_hdf_fixed_write_compress(df)
119 ms ± 2.15 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [16]: %timeit test_hdf_table_write(df)
623 ms ± 125 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [27]: %timeit test_hdf_table_write_compress(df)
563 ms ± 23.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [17]: %timeit test_csv_write(df)
3.13 s ± 49.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [30]: %timeit test_feather_write(df)
103 ms ± 5.88 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [31]: %timeit test_pickle_write(df)
109 ms ± 3.72 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [32]: %timeit test_pickle_write_compress(df)
3.33 s ± 55.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
```
When reading, the top three are ``test_feather_read``, ``test_pickle_read`` and
``test_hdf_fixed_read``.
``` python
In [18]: %timeit test_sql_read()
1.35 s ± 14.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [19]: %timeit test_hdf_fixed_read()
14.3 ms ± 438 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [28]: %timeit test_hdf_fixed_read_compress()
23.5 ms ± 672 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [20]: %timeit test_hdf_table_read()
35.4 ms ± 314 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [29]: %timeit test_hdf_table_read_compress()
42.6 ms ± 2.1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [22]: %timeit test_csv_read()
516 ms ± 27.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [33]: %timeit test_feather_read()
4.06 ms ± 115 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [34]: %timeit test_pickle_read()
6.5 ms ± 172 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [35]: %timeit test_pickle_read_compress()
588 ms ± 3.57 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
```
Space on disk (in bytes)
```
34816000 Aug 21 18:00 test.sql
24009240 Aug 21 18:00 test_fixed.hdf
7919610 Aug 21 18:00 test_fixed_compress.hdf
24458892 Aug 21 18:00 test_table.hdf
8657116 Aug 21 18:00 test_table_compress.hdf
28520770 Aug 21 18:00 test.csv
16000248 Aug 21 18:00 test.feather
16000848 Aug 21 18:00 test.pkl
7554108 Aug 21 18:00 test.pkl.compress
```
| |