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

這是繼 0.16.1 之後的又一個小型錯誤修復版本,包含大量錯誤修復以及一些新功能(pipe() 方法)、增強功能和效能改進。

我們建議所有使用者升級到此版本。

亮點包括

  • 新增 pipe 方法,參見 此處

  • 關於如何使用 numba 配合 pandas 的文件,參見 此處

新功能#

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 添加了 rsplitGH 10303

  • 移除了 IPython notebook 中 DataFrame HTML 表示的硬編碼大小限制,將其留給 IPython 本身處理(僅適用於 IPython v3.0 及以上版本)。這消除了 notebook 中出現的大資料框的重複捲軸(GH 10231)。

    請注意,notebook 有一個 toggle output scrolling(切換輸出滾動)功能,用於限制顯示非常大的資料框(透過點選輸出左側)。您還可以使用 pandas 選項配置 DataFrame 的顯示方式,參見 此處

  • DataFrame.quantileaxis 引數現在也接受 indexcolumn。(GH 9543

API 更改#

  • Holiday 現在如果建構函式中同時使用了 offsetobservance,將引發 NotImplementedError,而不是返回不正確的結果(GH 10217)。

效能改進#

  • 使用 dtype=datetime64[ns] 時,改進了 Series.resample 的效能(GH 7754

  • expand=True 時,提高了 str.split 的效能(GH 10081

Bug 修復#

  • 當給出一個單行 Series 時,Series.hist 中的錯誤會引發一個錯誤(GH 10214

  • HDFStore.select 修改了傳入的列列表的錯誤(GH 7212

  • 在 Python 3 中,當 display.width 設定為 None 時,Categorical repr 中的錯誤(GH 10087

  • 當使用某些 orientCategoricalIndex 時,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

  • Timestampmicrosecondquarterdayofyearweekdaysinmonth 屬性返回 np.int 型別,而不是內建的 int 的錯誤。(GH 10050

  • 訪問 daysinmonthdayofweek 屬性時,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 算術運算後 DatetimeIndexTimedeltaIndex 名稱丟失的錯誤( 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

  • 指定 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 .equalsDataFrameGH 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