呆鳥說:根據(jù) Pandas 開發(fā)團隊發(fā)布的消息,3月以后,Pandas 就要進入 2.x 時代了,Python 數(shù)據(jù)分析師快來入坑吧!
具體鏈接如下:https://pandas.pydata.org/docs/dev/whatsnew/index.html
主要改進
- 可配置選項,
mode.dtype_backend
返回 pyarrow 數(shù)據(jù)類型 - 使用 pip 安裝可選的支持庫
-
Index
支持 Numpy 的 numeric 數(shù)據(jù)類型 - 使用
Copy_on_write
(寫入時復制)機制,提高寫入性能
具體說明如下:
一、加入 pyarrow 數(shù)據(jù)類型
加入對 Apache Arrow 的支持,是 Pandas 2.x 最大的變化。首先介紹一下什么是 Arrow。
Arrow 是 Apache 軟件基金會支持的內(nèi)存分析開發(fā)平臺,它可以快速處理和移動大規(guī)模數(shù)據(jù),為數(shù)據(jù)的扁平化和分層制定了標準化的,與語言無關(guān)的列式內(nèi)存格式,以便在硬件層面上進行更高效的數(shù)據(jù)分析操作。
pyarrow 是為 Python 社區(qū)提供的 Arrow 支持庫,與 NumPy 和 Pandas 的集成度非常高,從 2.0 版開始,Pandas 專門加入了對 pyarrow 數(shù)據(jù)類型的支持。
使用 pyarrow,可以讓 pandas 處理數(shù)據(jù)的數(shù)據(jù)操作更快,內(nèi)存使用效率更高,尤其是在處理超大數(shù)據(jù)集時,其優(yōu)勢更明顯。
以下內(nèi)容是 Pandas 2.0 開發(fā)公告介紹的對 arrow 的支持說明。
Pandas 之前在 read_csv()
、read_excel()
、read_json()
、read_sql()
、to_numeric()
等函數(shù)中使用 use_nullable_dtypes
關(guān)鍵字參數(shù),讓這些函數(shù)可以自動轉(zhuǎn)換 nullable
數(shù)據(jù)類型,為了簡化操作,Pandas 新增了一個 nullable_dtypes
選項,允許在沒有明確指定時,把關(guān)鍵字參數(shù)在全局范圍內(nèi)設為 True
。啟用該選項的方式如下:
pd.options.mode.nullable_dtypes = True
這個選項僅用于函數(shù)的 use_nullable_dtypes
關(guān)鍵字。
Pandas 又新增了一個全局配置項: mode.dtype_backend
,用于連接上述 read_csv()
等函數(shù)中的 use_nullable_dtypes=True
參數(shù),以選擇 nullable 數(shù)據(jù)類型。
DataFrame.convert_dtypes()
和 Series.convert_dtypes()
兩種方法也可以使用
mode.dtype_backend
選項。
mode.dtype_backend
的默認值為 pandas
,返回的是 Numpy 支持的 nullable 數(shù)據(jù)類型。但現(xiàn)在也可以設置為 pyarrow
,返回 pyarrow 支持的 nullable 數(shù)據(jù)類型,即 ArrowDtype
。
示例代碼如下:
In [13]: import io
In [14]: data = io.StringIO("""a,b,c,d,e,f,g,h,i
....: 1,2.5,True,a,,,,,
....: 3,4.5,False,b,6,7.5,True,a,
....: """)
....:
In [15]: with pd.option_context("mode.dtype_backend", "pandas"):
....: df = pd.read_csv(data, use_nullable_dtypes=True)
....:
In [16]: df.dtypes
Out[16]:
a Int64
b Float64
c boolean
d string[python]
e Int64
f Float64
g boolean
h string[python]
i Int64
dtype: object
In [17]: data.seek(0)
Out[17]: 0
# 主要看下面這行代碼
In [18]: with pd.option_context("mode.dtype_backend", "pyarrow"):
....: df_pyarrow = pd.read_csv(data, use_nullable_dtypes=True, engine="pyarrow")
....:
In [19]: df_pyarrow.dtypes
Out[19]:
a int64[pyarrow]
b double[pyarrow]
c bool[pyarrow]
d string[pyarrow]
e int64[pyarrow]
f double[pyarrow]
g bool[pyarrow]
h string[pyarrow]
i null[pyarrow]
dtype: object
二、使用 pip 安裝可選的支持庫
使用 pip 安裝 pandas 時,可以指定要安裝的可選支持庫。
pip install "pandas[performance, aws]>=2.0.0"
三、Index
支持 Numpy 的 numeric 數(shù)據(jù)類型
Pandas 2.0 開始,可以在 Index
中使用 numpy 的數(shù)字型數(shù)據(jù)類型。Pandas 之前只能用 int64
、 uint64
和 float64
等數(shù)據(jù)類型,從 2.0 開始,Pandas 支持所有 numpy 的 numeric 數(shù)據(jù),如 int8
、int16
、int32
、int64
、uint8
、uint16
、uint32
、uint64
、float32
、float64
等。
示例代碼如下:
In [1]: pd.Index([1, 2, 3], dtype=np.int8)
Out[1]: Index([1, 2, 3], dtype='int8')
In [2]: pd.Index([1, 2, 3], dtype=np.uint16)
Out[2]: Index([1, 2, 3], dtype='uint16')
In [3]: pd.Index([1, 2, 3], dtype=np.float32)
Out[3]: Index([1.0, 2.0, 3.0], dtype='float32')
四、提高寫入性能
- 為以下方法新增了惰性復制機制,推遲復制,直到修改相關(guān)對象時才真正復制。啟用 Copy-on-Write 機制之后,以下方法僅返回視圖,這比常規(guī)的性能有了顯著提升。
(以下僅為部分支持該機制的方法,詳見文檔)
*DataFrame.reset_index() / Series.reset_index()
*DataFrame.set_index()
*DataFrame.reindex() / Series.reindex()
*DataFrame.reindex_like() / Series.reindex_like()
*DataFrame.drop()
*DataFrame.dropna() / Series.dropna()
*DataFrame.select_dtypes()
*DataFrame.align() / Series.align()
*Series.to_frame()
*DataFrame.rename() / Series.rename()
*DataFrame.add_prefix() / Series.add_prefix()
*DataFrame.add_suffix() / Series.add_suffix()
*DataFrame.drop_duplicates() / Series.drop_duplicates()
*DataFrame.filter() / Series.filter()
*DataFrame.head() / Series.head()
*DataFrame.tail() / Series.tail()
*DataFrame.pop() / Series.pop()
*DataFrame.replace() / Series.replace()
*DataFrame.shift() / Series.shift()
*DataFrame.sort_index() / Series.sort_index()
*DataFrame.sort_values() / Series.sort_values()
*DataFrame.truncate()
*DataFrame.iterrows()
*DataFrame.fillna() / Series.fillna()
*DataFrame.where() / Series.where()
*DataFrame.astype() / Series.astype()
*concat()
- 以 Series 的形式處理 DataFrame 的單個列(例如,
df["col"]
)時,每次構(gòu)建都返回一個新對象,啟用Copy-on-Write
時,不再多次返回相同的 Series 對象。 - 使用已有的 Series 構(gòu)建 Series,且默認選項為
copy=False
時,Series 構(gòu)造函數(shù)將使用惰性復制機制,即推遲復制,直到發(fā)生數(shù)據(jù)修改時才真正復制。 - 使用已有的 DataFrame 構(gòu)建 DataFrame,且默認選項為
copy=False
時,DataFrame 構(gòu)造函數(shù)也使用惰性復制機制。 - 使用 Series 字典構(gòu)建 DataFrame,且默認選項為
copy=False
時,也使用惰性復制機制。 - 啟用
Copy-on-Write
時,使用鏈式賦值設置值(例如,df["a"][1:3] = 0
)將引發(fā)異常。在此模式下,鏈式賦值不能正常運行。 -
DataFrame.replace()
在inplace=True
時,使用Copy-on-Write
。 -
DataFrame.transpose()
使用Copy-on-Write
機制。 - 算術(shù)運算,如,
ser *= 2
也支持Copy-on-Write
。
啟用本選項的方式如下:
# 方式一
pd.set_option("mode.copy_on_write", True)
# 方式二
pd.options.mode.copy_on_write = True
# 局部啟用的方式
with pd.option_context("mode.copy_on_write", True):
...
推薦書單
《Pandas1.x實例精解》詳細闡述了與Pandas相關(guān)的基本解決方案,主要包括Pandas基礎(chǔ),DataFrame基本操作,創(chuàng)建和保留DataFrame,開始數(shù)據(jù)分析,探索性數(shù)據(jù)分析,選擇數(shù)據(jù)子集,過濾行,對齊索引,分組以進行聚合、過濾和轉(zhuǎn)換,將數(shù)據(jù)重組為規(guī)整形式,組合Pandas對象,時間序列分析,使用Matplotlib、Pandas和Seaborn進行可視化,調(diào)試和測試等內(nèi)容。此外,該書還提供了相應的示例、代碼,以幫助讀者進一步理解相關(guān)方案的實現(xiàn)過程。