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

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

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

主要亮点包括

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

  • 关于如何将 numbapandas 结合使用的文档,请参见此处

新功能#

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.
"""

pipe 方法的灵感来源于 Unix 管道,它通过进程传输文本流。最近,dplyrmagrittrR 引入了流行的 (%>%) 管道运算符。

更多信息请参阅文档。(GH 10129)

其他增强功能#

  • 为 Index/Series StringMethods 添加了 rsplit (GH 10303)

  • 移除了 IPython notebook 中 DataFrame HTML 表示的硬编码大小限制,将其交由 IPython 自身处理(仅适用于 IPython v3.0 或更高版本)。这消除了 notebook 中大型框架出现重复滚动条的问题 (GH 10231)。

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

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

API 变更#

  • 如果在构造函数中同时使用了 offsetobservanceHoliday 现在会引发 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 函数返回的 dtypes 不一致的错误 (GH 10251)

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

  • groupby.applyCategorical 进行聚合时未保留类别的错误 (GH 10138)

  • to_csv 中的错误,当 datetime 为小数时,date_format 会被忽略 (GH 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)

  • 当基于多个键进行分组,其中一个键是 categorical 时,GroupBy.get_group 中的错误。( GH 10132)

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

  • 从包含 datetime64 的嵌套 dict 构造 DataFrame 时的错误 (GH 10160)

  • 从具有 datetime64 键的 dict 构造 Series 时的错误 (GH 9456)

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

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

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

  • Series.align 在指定 fill_value 时重置 name 的错误 (GH 10067)

  • read_csv 导致空 DataFrame 未设置索引名称的错误 (GH 10184)

  • SparseSeries.abs 重置 name 的错误 (GH 10241)

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

  • GroupBy.get_group 在分组键包含 NaT 时引发 ValueError 的错误 (GH 6992)

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

  • Categorical.remove_categories 中的错误,当底层 dtype 为浮点类型时,移除 NaN 类别会导致 ValueError (GH 10156)

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

  • DataFrame.to_hdf() 中的错误,其中表格格式会因无效(非字符串)列名而引发看似不相关的错误。现在明确禁止这种情况。( GH 9057)

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

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

  • read_csv 中的错误,当 date_parser 返回的 datetime64 数组的时间分辨率不是 [ns] 时 (GH 10245)

  • Panel.apply 中结果 ndim=0 时的错误 (GH 10332)

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

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

  • 添加空 DataFrames 时的错误,现在结果是一个与空 DataFrame .equalsDataFrame (GH 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