版本 0.16.2 (2015 年 6 月 12 日)#

这是从 0.16.1 版本发布的一个小的错误修复版本,包含了大量错误修复,以及一些新特性(如 pipe() 方法)、增强功能和性能改进。

我们建议所有用户升级到此版本。

亮点包括

  • 一个新的 pipe 方法,请参见 此处

  • 关于如何在 pandas 中使用 numba 的文档,请参见 此处

新特性#

管道 (Pipe)#

我们引入了一个新方法 DataFrame.pipe()。顾名思义,pipe 应该用于通过一系列函数调用来处理数据。其目标是避免混淆的嵌套函数调用,例如

# df is a DataFrame
# f, g, and h are functions that take and return DataFrames
f(g(h(df), arg1=1), arg2=2, arg3=3)  # noqa F821

逻辑从内向外流动,函数名与其关键字参数分开。这可以重写为

(
    df.pipe(h)  # noqa F821
    .pipe(g, arg1=1)  # noqa F821
    .pipe(f, arg2=2, arg3=3)  # noqa F821
)

现在代码和逻辑都从上到下流动。关键字参数与其函数相邻。总的来说,代码更具可读性。

在上面的示例中,函数 fgh 都期望 DataFrame 作为第一个位置参数。当您希望应用的函数将其数据作为第一个参数以外的任何位置接受时,传递一个 (function, keyword) 元组,指示 DataFrame 应该流向何处。例如

In [1]: import statsmodels.formula.api as sm

In [2]: bb = pd.read_csv("data/baseball.csv", index_col="id")

# sm.ols takes (formula, data)
In [3]: (
...:     bb.query("h > 0")
...:     .assign(ln_h=lambda df: np.log(df.h))
...:     .pipe((sm.ols, "data"), "hr ~ ln_h + year + g + C(lg)")
...:     .fit()
...:     .summary()
...: )
...:
Out[3]:
<class 'statsmodels.iolib.summary.Summary'>
"""
                            OLS Regression Results
==============================================================================
Dep. Variable:                     hr   R-squared:                       0.685
Model:                            OLS   Adj. R-squared:                  0.665
Method:                 Least Squares   F-statistic:                     34.28
Date:                Tue, 22 Nov 2022   Prob (F-statistic):           3.48e-15
Time:                        05:35:23   Log-Likelihood:                -205.92
No. Observations:                  68   AIC:                             421.8
Df Residuals:                      63   BIC:                             432.9
Df Model:                           4
Covariance Type:            nonrobust
===============================================================================
                coef    std err          t      P>|t|      [0.025      0.975]
-------------------------------------------------------------------------------
Intercept   -8484.7720   4664.146     -1.819      0.074   -1.78e+04     835.780
C(lg)[T.NL]    -2.2736      1.325     -1.716      0.091      -4.922       0.375
ln_h           -1.3542      0.875     -1.547      0.127      -3.103       0.395
year            4.2277      2.324      1.819      0.074      -0.417       8.872
g               0.1841      0.029      6.258      0.000       0.125       0.243
==============================================================================
Omnibus:                       10.875   Durbin-Watson:                   1.999
Prob(Omnibus):                  0.004   Jarque-Bera (JB):               17.298
Skew:                           0.537   Prob(JB):                     0.000175
Kurtosis:                       5.225   Cond. No.                     1.49e+07
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 1.49e+07. This might indicate that there are
strong multicollinearity or other numerical problems.
"""

管道方法受 Unix 管道启发,它通过进程传输文本。最近,dplyrmagrittrR 引入了流行的 (%>%) 管道操作符。

有关更多信息,请参见文档。(GH 10129

其他增强功能#

  • rsplit 添加到 Index/Series 的字符串方法中(GH 10303

  • 删除了 IPython notebook 中 DataFrame HTML 表示上的硬编码大小限制,将其留给 IPython 本身处理(仅适用于 IPython v3.0 或更高版本)。这消除了处理大型帧时 notebook 中出现的重复滚动条(GH 10231)。

    请注意,notebook 有一个 toggle output scrolling 功能,可以限制超大帧的显示(通过点击输出左侧)。您还可以使用 pandas 选项配置 DataFrames 的显示方式,请参见此处

  • DataFrame.quantileaxis 参数现在也接受 indexcolumn。(GH 9543

API 变更#

  • Holiday 现在会在构造函数中同时使用 offsetobservance 时引发 NotImplementedError,而不是返回不正确的结果(GH 10217)。

性能改进#

  • 提高了 Series.resampledtype=datetime64[ns] 下的性能(GH 7754

  • 提高了 str.splitexpand=True 时的性能(GH 10081

错误修复#

  • Series.hist 中的一个错误导致在给定一行 Series 时引发错误(GH 10214

  • HDFStore.select 中的一个错误导致修改传入的列列表(GH 7212

  • 在 Python 3 中,Categorical 的 repr 在 display.widthNone 时的错误(GH 10087

  • to_json 在某些 orient 和 CategoricalIndex 的情况下会导致段错误(GH 10317

  • 一些 nan 函数没有一致的返回 dtype 的错误(GH 10251

  • DataFrame.quantile 在检查是否传入了有效轴时的错误(GH 9543

  • groupby.applyCategorical 的聚合不保留类别的错误(GH 10138

  • to_csv 中的一个错误导致如果 datetime 是小数,则忽略 date_formatGH 10209

  • DataFrame.to_json 中混合数据类型的错误(GH 10289

  • 整合时缓存更新的错误(GH 10264

  • mean() 中的一个错误导致整数 dtype 可能溢出(GH 10172

  • Panel.from_dict 中的一个错误导致在指定时未设置 dtype(GH 10058

  • Index.union 中的一个错误导致在传递类数组对象时引发 AttributeError。(GH 10149

  • Timestampmicrosecondquarterdayofyearweekdaysinmonth 属性返回 np.int 类型而不是内置 int 类型的错误。(GH 10050

  • NaT 中的一个错误导致在访问 daysinmonthdayofweek 属性时引发 AttributeError。(GH 10096

  • 在使用 max_seq_items=None 设置时 Index repr 的错误(GH 10182)。

  • 在不同平台上使用 dateutil 获取时区数据的错误( GH 9059, GH 8639, GH 9663, GH 10121

  • 显示混合频率 datetime 的错误;以适当的精度显示 'ms' datetime。(GH 10170

  • setitem 中的一个错误导致类型提升应用于整个块(GH 10280

  • Series 的算术方法可能错误地保留名称的错误(GH 10068

  • GroupBy.get_group 在使用多个键进行分组(其中一个为类别型)时的错误。(GH 10132

  • DatetimeIndexTimedeltaIndex 在 timedelta 算术后丢失名称的错误( GH 9926

  • 使用带有 datetime64 的嵌套 dict 构造 DataFrame 的错误(GH 10160

  • 使用带有 datetime64 键的 dict 构造 Series 的错误(GH 9456

  • Series.plot(label="LABEL") 未正确设置 label 的错误(GH 10119

  • plot 未默认使用 matplotlib axes.grid 设置的错误(GH 9792

  • 导致包含指数但不含小数的字符串在 engine='python' 中使用 read_csv 解析器时被解析为 int 而非 float 的错误(GH 9565

  • Series.align 中的一个错误导致在指定 fill_value 时重置 nameGH 10067

  • read_csv 中的一个错误导致在空 DataFrame 上未设置索引名称(GH 10184

  • SparseSeries.abs 中的一个错误导致重置 nameGH 10241

  • TimedeltaIndex 切片可能重置 freq 的错误(GH 10292

  • GroupBy.get_group 中的一个错误导致当分组键包含 NaT 时引发 ValueErrorGH 6992

  • SparseSeries 构造函数忽略输入数据名称的错误(GH 10258

  • Categorical.remove_categories 中的一个错误导致在底层 dtype 是浮点型时移除 NaN 类别会引发 ValueErrorGH 10156

  • infer_freq 推断出 to_offset 不支持的时间规则 (WOM-5XXX) 的错误(GH 9425

  • DataFrame.to_hdf() 中的一个错误导致对于无效(非字符串)列名,table 格式会引发一个看似不相关的错误。现在已明确禁止这种情况。(GH 9057

  • 处理空 DataFrame 屏蔽的错误(GH 10126)。

  • MySQL 接口无法处理数字表/列名称的错误(GH 10255

  • read_csv 中的一个错误导致使用返回时间分辨率不是 [ns]datetime64 数组的 date_parser 时出错(GH 10245

  • Panel.apply 中的一个错误导致当结果 ndim=0 时出错(GH 10332

  • read_hdf 中的一个错误导致 auto_close 无法传入(GH 9327)。

  • read_hdf 中的一个错误导致无法使用已打开的存储(GH 10330)。

  • 添加空 DataFrames 的错误,现在导致的结果是 DataFrame 等于一个空 DataFrameGH 10181)。

  • to_hdfHDFStore 中的一个错误导致未检查 complib 选择是否有效(GH 4582, GH 8874)。

贡献者#

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

  • Andrew Rosenfeld

  • Artemy Kolchinsky

  • Bernard Willers +

  • Christer van der Meeren

  • Christian Hudon +

  • Constantine Glen Evans +

  • Daniel Julius Lasiman +

  • Evan Wright

  • Francesco Brundu +

  • Gaëtan de Menten +

  • Jake VanderPlas

  • James Hiebert +

  • Jeff Reback

  • Joris Van den Bossche

  • Justin Lecher +

  • Ka Wo Chen +

  • Kevin Sheppard

  • Mortada Mehyar

  • Morton Fox +

  • Robin Wilson +

  • Sinhrks

  • Stephan Hoyer

  • Thomas Grainger

  • Tom Ajamian

  • Tom Augspurger

  • Yoshiki Vázquez Baeza

  • Younggun Kim

  • austinc +

  • behzad nouri

  • jreback

  • lexual

  • rekcahpassyla +

  • scls19fr

  • sinhrks