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

新功能#

  • 新的统一合并函数,用于高效执行全套数据库/关系代数操作。重构了现有连接方法以使用新基础设施,从而显著提升了性能(GH 220, GH 249, GH 267

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

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

  • 可以将字典列表(例如,JSON 对象列表)传递给 DataFrame 构造函数(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

  • 为 DataFrame添加 reindex_axis 方法

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

  • 为 Series 和 DataFrame 上的 reindexalign 方法添加 level 选项,用于在级别间广播值(GH 542, GH 552, 其他)

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

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

  • DataFrame.to_string添加 indexheader 选项

  • 可以将多个 DataFrame 传递给 DataFrame.join 以在索引上进行连接(GH 115

  • 可以将多个 Panel 传递给 Panel.joinGH 115

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

  • 为 GroupBy添加 sort 选项,允许禁用组键的排序以提高潜在的速度(GH 595

  • 可以将 MaskedArray 传递给 Series 构造函数(GH 563

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

  • 实现 DataFrame.lookup,这是用于给定行和列标签序列检索值的花式索引模拟(GH 338

  • 可以将函数列表传递给 DataFrame 上的 groupby 进行聚合,从而得到具有分层列的聚合结果(GH 166

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

  • 添加 value_range 作为获取数据帧最小值和最大值的实用函数(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

  • drop 作为可选参数添加到 DataFrame.reset_indexGH 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 字典,这是之前版本的回归

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

  • 在 Panel 类中也使用通用的 NA 处理操作(求和、平均值等)(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 +