版本 0.16.2 (2015 年 6 月 12 日)#
這是繼 0.16.1 之後的又一個小型錯誤修復版本,包含大量錯誤修復以及一些新功能(pipe() 方法)、增強功能和效能改進。
我們建議所有使用者升級到此版本。
亮點包括
新功能#
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 中
DataFrameHTML 表示的硬編碼大小限制,將其留給 IPython 本身處理(僅適用於 IPython v3.0 及以上版本)。這消除了 notebook 中出現的大資料框的重複捲軸(GH 10231)。請注意,notebook 有一個
toggle output scrolling(切換輸出滾動)功能,用於限制顯示非常大的資料框(透過點選輸出左側)。您還可以使用 pandas 選項配置 DataFrame 的顯示方式,參見 此處。DataFrame.quantile的axis引數現在也接受index和column。(GH 9543)
API 更改#
Holiday現在如果建構函式中同時使用了offset和observance,將引發NotImplementedError,而不是返回不正確的結果(GH 10217)。
效能改進#
Bug 修復#
當給出一個單行
Series時,Series.hist中的錯誤會引發一個錯誤(GH 10214)HDFStore.select修改了傳入的列列表的錯誤(GH 7212)在 Python 3 中,當
display.width設定為None時,Categoricalrepr 中的錯誤(GH 10087)當使用某些
orient和CategoricalIndex時,to_json中的錯誤會導致段錯誤(GH 10317)某些 nan 函式的返回 dtype 不一致的錯誤(GH 10251)
檢查是否傳遞了有效軸時,
DataFrame.quantile中的錯誤(GH 9543)對
Categorical進行groupby.apply聚合時,未保留類別的錯誤(GH 10138)當
datetime具有小數時,to_csv中的date_format被忽略的錯誤(GH 10209)DataFrame.to_json在處理混合資料型別時的錯誤(GH 10289)合併時快取更新的錯誤(GH 10264)
mean()中整數 dtype 可能溢位的錯誤(GH 10172)Panel.from_dict未在指定時設定 dtype 的錯誤(GH 10058)在傳遞 array-likes 時,
Index.union引發AttributeError的錯誤。(GH 10149)Timestamp的microsecond、quarter、dayofyear、week和daysinmonth屬性返回np.int型別,而不是內建的int的錯誤。(GH 10050)訪問
daysinmonth、dayofweek屬性時,NaT引發AttributeError的錯誤。(GH 10096)使用
max_seq_items=None設定時,Index repr 中的錯誤(GH 10182)。在各種平臺上使用
dateutil獲取時區資料時的錯誤( GH 9059, GH 8639, GH 9663, GH 10121)顯示具有混合頻率的日期時間時的錯誤;以正確的精度顯示“ms”日期時間。(GH 10170)
setitem中將型別提升應用於整個塊的錯誤(GH 10280)Series算術方法可能錯誤地保留名稱的錯誤(GH 10068)當按多個鍵分組,其中一個鍵是分類型別時,
GroupBy.get_group中的錯誤。(GH 10132)timedelta 算術運算後
DatetimeIndex和TimedeltaIndex名稱丟失的錯誤( 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)指定
fill_value時,Series.align重置name的錯誤(GH 10067)read_csv在空 DataFrame 上未設定索引名稱的錯誤(GH 10184)SparseSeries.abs重置name的錯誤(GH 10241)TimedeltaIndex切片可能重置 freq 的錯誤(GH 10292)當組鍵包含
NaT時,GroupBy.get_group引發ValueError的錯誤(GH 6992)SparseSeries建構函式忽略輸入資料名稱的錯誤(GH 10258)當移除
NaN類別且底層 dtype 是浮點型別時,Categorical.remove_categories引起ValueError的錯誤(GH 10156)infer_freq 推斷的時間規則(WOM-5XXX)不受 to_offset 支援的錯誤(GH 9425)
DataFrame.to_hdf()中,對於無效(非字串)的列名,table 格式會引發一個看似無關的錯誤。現在顯式禁止這種情況。(GH 9057)處理空
DataFrame掩碼的錯誤(GH 10126)。MySQL 介面無法處理數字表/列名稱的錯誤(GH 10255)
當
date_parser返回一個時間解析度非[ns]的datetime64陣列時,read_csv中的錯誤(GH 10245)當結果的 ndim=0 時,
Panel.apply中的錯誤(GH 10332)read_hdf中無法傳遞auto_close的錯誤(GH 9327)。read_hdf中無法使用已開啟的 store 的錯誤(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