版本 0.7.3 (2012年4月12日)#

这是继 0.7.2 之后的一个次要版本,修复了许多小错误并添加了一些不错的新功能。还需要注意一些 API 更改;这些更改应该不会影响太多用户,尽管它们构成了行为上的变化,但我们倾向于将其称为“错误修复”。有关完整列表,请参阅完整的发布说明或 GitHub 上的问题跟踪器。

新特性#

from pandas.tools.plotting import scatter_matrix

scatter_matrix(df, alpha=0.2)  # noqa F821
  • 为 Series 和 DataFrame 的 plot 方法添加 stacked 参数,用于绘制堆叠条形图

df.plot(kind="bar", stacked=True)  # noqa F821
df.plot(kind="barh", stacked=True)  # noqa F821
  • DataFrame.plotSeries.plot 添加对数 x 轴和 y 轴缩放选项

  • 为 Series 和 DataFrame 添加 kurt 方法,用于计算峰度

NA 布尔比较 API 更改#

恢复了非数值 Series 中 NA 值(通常表示为 NaNNone)处理方式的一些更改

In [1]: series = pd.Series(["Steve", np.nan, "Joe"])

In [2]: series == "Steve"
Out[2]:
0     True
1    False
2    False
Length: 3, dtype: bool

In [3]: series != "Steve"
Out[3]:
0    False
1     True
2     True
Length: 3, dtype: bool

在比较中,NA / NaN 总是会被当作 False,除了使用 != 时为 True。在存在 NA 数据的情况下,使用布尔运算,特别是求反时,请务必小心。如果您担心这一点,可以考虑在布尔数组操作中添加一个显式的 NA 过滤器。

In [4]: mask = series == "Steve"

In [5]: series[mask & series.notnull()]
Out[5]:
0    Steve
Length: 1, dtype: object

虽然在比较中传播 NA 对某些用户来说似乎是正确的行为(纯粹从技术角度来看也可以争辩说这是正确的做法),但经过评估认为,在任何地方,包括在数值数组中传播 NA 会给用户带来大量问题。因此,采用了“实用性胜于纯粹性”的方法。未来可能会在某个时候重新讨论此问题。

其他 API 更改#

在对分组的 Series 调用 apply 时,返回值也将是一个 Series,以便与 DataFrame 的 groupby 行为更一致

In [6]: df = pd.DataFrame(
   ...:     {
   ...:         "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"],
   ...:         "B": ["one", "one", "two", "three", "two", "two", "one", "three"],
   ...:         "C": np.random.randn(8),
   ...:         "D": np.random.randn(8),
   ...:     }
   ...: )
   ...:

In [7]: df
Out[7]:
   A      B         C         D
0  foo    one  0.469112 -0.861849
1  bar    one -0.282863 -2.104569
2  foo    two -1.509059 -0.494929
3  bar  three -1.135632  1.071804
4  foo    two  1.212112  0.721555
5  bar    two -0.173215 -0.706771
6  foo    one  0.119209 -1.039575
7  foo  three -1.044236  0.271860

[8 rows x 4 columns]

In [8]: grouped = df.groupby("A")["C"]

In [9]: grouped.describe()
Out[9]:
   count      mean       std       min       25%       50%       75%       max
A
bar    3.0 -0.530570  0.526860 -1.135632 -0.709248 -0.282863 -0.228039 -0.173215
foo    5.0 -0.150572  1.113308 -1.509059 -1.044236  0.119209  0.469112  1.212112

[2 rows x 8 columns]

In [10]: grouped.apply(lambda x: x.sort_values()[-2:])  # top 2 values
Out[10]:
A
bar  1   -0.282863
     5   -0.173215
foo  0    0.469112
     4    1.212112
Name: C, Length: 4, dtype: float64

贡献者#

共有 15 人为此版本贡献了补丁。名字旁带有“+”号的人是首次贡献补丁。

  • Abraham Flaxman +

  • Adam Klein

  • Andreas H. +

  • Chang She

  • Dieter Vandenbussche

  • Jacques Kvam +

  • K.-Michael Aye +

  • Kamil Kisiel +

  • Martin Blais +

  • Skipper Seabold

  • Thomas Kluyver

  • Wes McKinney

  • Wouter Overmeire

  • Yaroslav Halchenko

  • lgautier +