版本 0.16.2 (2015年6月12日)#
这是 0.16.1 版的一个小错误修复版本,包含了大量的错误修复以及一些新功能(pipe()
方法)、增强功能和性能改进。
我们建议所有用户升级到此版本。
主要亮点包括
v0.16.2 版新特性
新功能#
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
)
现在代码和逻辑都从上到下流动。关键字参数紧邻其函数。总的来说,代码更具可读性。
在上面的例子中,函数 f
、g
和 h
都期望 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 管道,它通过进程传输文本流。最近,dplyr 和 magrittr 为 R 引入了流行的 (%>%)
管道运算符。
其他增强功能#
为 Index/Series StringMethods 添加了
rsplit
(GH 10303)移除了 IPython notebook 中
DataFrame
HTML 表示的硬编码大小限制,将其交由 IPython 自身处理(仅适用于 IPython v3.0 或更高版本)。这消除了 notebook 中大型框架出现重复滚动条的问题 (GH 10231)。请注意,notebook 有一个
toggle output scrolling
功能,用于限制超大型框架的显示(通过点击输出左侧)。您也可以使用 pandas 选项配置 DataFrame 的显示方式,详见此处。DataFrame.quantile
的axis
参数现在也接受index
和column
。( GH 9543)
API 变更#
如果在构造函数中同时使用了
offset
和observance
,Holiday
现在会引发NotImplementedError
,而不是返回不正确的结果 (GH 10217)。
性能改进#
错误修复#
Series.hist
中的错误,当给定单行Series
时会引发错误 (GH 10214)HDFStore.select
会修改传入的列列表的错误 (GH 7212)Python 3 中,
Categorical
repr 在display.width
为None
时的错误 (GH 10087)to_json
在特定 orient 和CategoricalIndex
结合使用时会导致段错误 (GH 10317)一些 NaN 函数返回的 dtypes 不一致的错误 (GH 10251)
DataFrame.quantile
在检查是否传入了有效轴时的错误 (GH 9543)groupby.apply
对Categorical
进行聚合时未保留类别的错误 (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)Timestamp
的microsecond
、quarter
、dayofyear
、week
和daysinmonth
属性返回np.int
类型而不是内置int
的错误。( GH 10050)NaT
在访问daysinmonth
、dayofweek
属性时会引发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)DatetimeIndex
和TimedeltaIndex
名称在 timedelta 算术后丢失的错误 (GH 9926)从包含
datetime64
的嵌套dict
构造DataFrame
时的错误 (GH 10160)从具有
datetime64
键的dict
构造Series
时的错误 (GH 9456)Series.plot(label="LABEL")
未正确设置标签的错误 (GH 10119)plot
未默认使用 matplotlibaxes.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
.equals
的DataFrame
(GH 10181)。
贡献者#
共有 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