版本 0.7.0 (2012 年 2 月 9 日)#

新特性#

  • 新增统一的 merge 函数,可高效执行数据库/关系代数操作的全范围。重构现有 join 方法以使用新基础架构,从而显着提升性能 (GH 220, GH 249, GH 267)

  • 新增 统一的连接函数,用于沿一个轴连接 Series、DataFrame 或 Panel 对象。可以形成其他轴的并集或交集。提升了 Series.appendDataFrame.append 的性能 (GH 468, GH 479, GH 273)

  • 可以向 DataFrame.append 传递多个 DataFrame 进行连接(堆叠),也可以向 Series.append 传递多个 Series

  • 可以 向 DataFrame 构造函数传递 dict 列表(例如,JSON 对象列表)(GH 526)

  • 现在可以通过 __getitem__ 设置 DataFrame 中的多个列,这对转换很有用 (GH 342)

  • 处理 DataFrame.apply 中不同索引的输出值 (GH 498)

In [1]: df = pd.DataFrame(np.random.randn(10, 4))
In [2]: df.apply(lambda x: x.describe())
Out[2]:
               0          1          2          3
count  10.000000  10.000000  10.000000  10.000000
mean    0.190912  -0.395125  -0.731920  -0.403130
std     0.730951   0.813266   1.112016   0.961912
min    -0.861849  -2.104569  -1.776904  -1.469388
25%    -0.411391  -0.698728  -1.501401  -1.076610
50%     0.380863  -0.228039  -1.191943  -1.004091
75%     0.658444   0.057974  -0.034326   0.461706
max     1.212112   0.577046   1.643563   1.071804

[8 rows x 4 columns]
  • Series 和 DataFrame 添加 reorder_levels 方法 (GH 534)

  • DataFrame 和 Panel 添加 字典式的 get 函数 (GH 521)

  • 添加 DataFrame.iterrows 方法,用于高效地迭代 DataFrame 的行

  • 添加 DataFrame.to_panel 方法,代码改编自 LongPanel.to_long

  • 添加 reindex_axis 方法到 DataFrame

  • DataFrameSeries 上的二进制算术函数添加 level 选项

  • Series 和 DataFrame 的 reindexalign 方法添加 level 选项,用于跨层广播值 (GH 542, GH 552, 其他)

  • Panel 添加基于属性的项目访问,并添加 IPython 补全 (GH 563)

  • Series.plot 添加 logy 选项,用于 Y 轴的对数缩放

  • DataFrame.to_string 添加 indexheader 选项

  • 可以DataFrame.join 传递多个 DataFrame 以基于索引进行连接 (GH 115)

  • 可以Panel.join 传递多个 Panel 进行连接 (GH 115)

  • DataFrame.to_string 添加 justify 参数,以允许不同的列标题对齐方式

  • GroupBy 添加 sort 选项,以允许禁用组键的排序,从而可能提高速度 (GH 595)

  • 可以 向 Series 构造函数传递 MaskedArray (GH 563)

  • 通过属性和 IPython 补全添加 Panel 项目访问 (GH 554)

  • 实现 DataFrame.lookup,一种花式索引的类似方法,用于根据行和列标签序列检索值 (GH 338)

  • 可以向 DataFrame 上的 groupby 传递 函数列表 进行聚合,生成具有层级列的聚合结果 (GH 166)

  • 可以在 Series 和 DataFrame 上调用 cummincummax 分别获取累积最小值和最大值 (GH 647)

  • 添加 value_range 作为获取 DataFrame 最小值和最大值的实用函数 (GH 288)

  • read_csv, read_table, to_csvfrom_csv 添加 encoding 参数,用于非 ASCII 文本 (GH 717)

  • pandas 对象添加 abs 方法

  • 添加 crosstab 函数,用于轻松计算频率表

  • 索引对象添加 isin 方法

  • DataFrame 的 xs 方法添加 level 参数。

整数索引的 API 变更#

0.7.0 版本中一个潜在风险最高但也是最重要的 API 变更,是对**整数索引**在基于标签索引中的处理方式进行了全面审查。以下是一个示例

In [3]: s = pd.Series(np.random.randn(10), index=range(0, 20, 2))
In [4]: s
Out[4]:
0    -1.294524
2     0.413738
4     0.276662
6    -0.472035
8    -0.013960
10   -0.362543
12   -0.006154
14   -0.923061
16    0.895717
18    0.805244
Length: 10, dtype: float64

In [5]: s[0]
Out[5]: -1.2945235902555294

In [6]: s[2]
Out[6]: 0.41373810535784006

In [7]: s[4]
Out[7]: 0.2766617129497566

这与之前的行为完全相同。然而,如果您请求 Series 中**不存在**的键,在 0.6.1 及更早版本中,Series 会 *回退* 到基于位置的查找。现在这会引发 KeyError

In [2]: s[1]
KeyError: 1

此更改对 DataFrame 也有同样的影响

In [3]: df = pd.DataFrame(np.random.randn(8, 4), index=range(0, 16, 2))

In [4]: df
    0        1       2       3
0   0.88427  0.3363 -0.1787  0.03162
2   0.14451 -0.1415  0.2504  0.58374
4  -1.44779 -0.9186 -1.4996  0.27163
6  -0.26598 -2.4184 -0.2658  0.11503
8  -0.58776  0.3144 -0.8566  0.61941
10  0.10940 -0.7175 -1.0108  0.47990
12 -1.16919 -0.3087 -0.6049 -0.43544
14 -0.07337  0.3410  0.0424 -0.16037

In [5]: df.ix[3]
KeyError: 3

为了支持纯粹基于整数的索引,添加了以下方法

方法

描述

Series.iget_value(i)

检索位置 i 处存储的值

Series.iget(i)

iget_value 的别名

DataFrame.irow(i)

检索第 i

DataFrame.icol(j)

检索第 j

DataFrame.iget_value(i, j)

检索第 i 行、第 j 列的值

关于基于标签切片的 API 微调#

使用 ix 进行基于标签的切片现在要求索引是排序的(单调),**除非**起始点和结束点都包含在索引中

In [1]: s = pd.Series(np.random.randn(6), index=list('gmkaec'))

In [2]: s
Out[2]:
g   -1.182230
m   -0.276183
k   -0.243550
a    1.628992
e    0.073308
c   -0.539890
dtype: float64

那么这是可以的

In [3]: s.ix['k':'e']
Out[3]:
k   -0.243550
a    1.628992
e    0.073308
dtype: float64

但这不是

In [12]: s.ix['b':'h']
KeyError 'b'

如果索引已排序,则“范围选择”是可能的

In [4]: s2 = s.sort_index()

In [5]: s2
Out[5]:
a    1.628992
c   -0.539890
e    0.073308
g   -1.182230
k   -0.243550
m   -0.276183
dtype: float64

In [6]: s2.ix['b':'h']
Out[6]:
c   -0.539890
e    0.073308
g   -1.182230
dtype: float64

Series [] 运算符的变更#

为了方便表示,在通过 [] 获取和设置值时(即 __getitem____setitem__ 方法),您可以向 Series 传递标签序列或标签切片。其行为将与向 ix 传递类似输入时的行为相同,**整数索引除外**

In [8]: s = pd.Series(np.random.randn(6), index=list('acegkm'))

In [9]: s
Out[9]:
a   -1.206412
c    2.565646
e    1.431256
g    1.340309
k   -1.170299
m   -0.226169
Length: 6, dtype: float64

In [10]: s[['m', 'a', 'c', 'e']]
Out[10]:
m   -0.226169
a   -1.206412
c    2.565646
e    1.431256
Length: 4, dtype: float64

In [11]: s['b':'l']
Out[11]:
c    2.565646
e    1.431256
g    1.340309
k   -1.170299
Length: 4, dtype: float64

In [12]: s['c':'k']
Out[12]:
c    2.565646
e    1.431256
g    1.340309
k   -1.170299
Length: 4, dtype: float64

对于整数索引,其行为将与之前完全相同(效仿 ndarray

In [13]: s = pd.Series(np.random.randn(6), index=range(0, 12, 2))

In [14]: s[[4, 0, 2]]
Out[14]:
4    0.132003
0    0.410835
2    0.813850
Length: 3, dtype: float64

In [15]: s[1:5]
Out[15]:
2    0.813850
4    0.132003
6   -0.827317
8   -0.076467
Length: 4, dtype: float64

如果您希望对具有标签语义的整数索引使用序列和切片进行索引,请使用 ix

其他 API 变更#

  • 已弃用的 LongPanel 类已被完全移除

  • 如果在 DataFrame 的列上调用 Series.sort,现在会引发异常。之前通过 df[col].sort() 可能会意外地改变 DataFrame 的列,而不是使用无副作用的方法 df[col].order() (GH 316)

  • 其他零散的重命名和弃用,它们将(无害地)引发 FutureWarning

  • DataFrame.reset_index 添加了可选参数 drop (GH 699)

性能改进#

  • Cython 化的 GroupBy 聚合 不再预先排序数据,从而显着提高了速度 (GH 93)。通过巧妙地操作 Cython 中的 ndarray 数据类型,使用 Python 函数的 GroupBy 聚合显着提速 (GH 496)。

  • DataFrame 构造函数在传递的列标签与数据不匹配时提供更好的错误消息 (GH 497)

  • 当传递 Python 函数时,大幅提高多重 GroupBy 聚合的性能,并在 Cython 中重用 ndarray 对象 (GH 496)

  • 可以在 HDFStore 中存储使用元组和浮点数作为索引的对象 (GH 492)

  • Series.to_string 默认不打印长度,添加 length 选项 (GH 489)

  • 改进多重 groupby 的 Cython 代码,无需排序数据即可进行聚合 (GH 93)

  • 通过在 MultiIndex 中存储元组来提高 MultiIndex 重建索引的速度,并测试向后解封兼容性

  • 通过使用专门的 Cython take 函数提高列重建索引性能

  • 进一步优化 Series.__getitem__ 在标准用例中的性能

  • 在某些情况下(例如获取切片等)避免创建 Index dict,这是先前版本的一个回归

  • 如果 NumPy 未安装,setup.py 中的错误消息更友好

  • 在 Panel 类中也使用一组通用的 NA 处理操作(sum, mean 等)(GH 536)

  • 当在具有常规(非层级)索引的 DataFrame 上调用 reset_index 时的默认名称分配 (GH 476)

  • 在 Series/DataFrame 统计操作中传递 level 参数时,尽可能使用 Cython 化的分组器 (GH 545)

  • 将跳表数据结构移植到 C 语言,在大多数典型用例中将 rolling_median 的速度提高了约 5-10 倍 (GH 374)

贡献者#

本次发布共有 18 人贡献了补丁。名字旁带有“+”的人是首次贡献补丁。

  • Adam Klein

  • Bayle Shanks +

  • Chris Billington +

  • Dieter Vandenbussche

  • Fabrizio Pollastri +

  • Graham Taylor +

  • Gregg Lind +

  • Josh Klein +

  • Luca Beltrame

  • Olivier Grisel +

  • Skipper Seabold

  • Thomas Kluyver

  • Thomas Wiecki +

  • Wes McKinney

  • Wouter Overmeire

  • Yaroslav Halchenko

  • fabriziop +

  • theandygross +