版本 0.10.1 (2013 年 1 月 22 日)#
這是相較於 0.10.0 的一個小版本釋出,包含新功能、增強功能和錯誤修復。特別地,Jeff Reback 貢獻了大量新的 HDFStore 功能。
之前因帶有 inplace 選項的函式而出現的非預期 API 破壞已被回滾,並添加了棄用警告。
API 更改#
新功能#
MySQL 資料庫支援(Dan Allan 貢獻)
HDFStore#
您可能需要升級現有的資料檔案。請訪問主文件中的相容性部分。
您可以透過將列表傳遞給 data_columns 來指定(並索引)您希望能夠對其進行查詢的某些列。
In [1]: store = pd.HDFStore("store.h5")
In [2]: df = pd.DataFrame(
...: np.random.randn(8, 3),
...: index=pd.date_range("1/1/2000", periods=8),
...: columns=["A", "B", "C"],
...: )
...:
In [3]: df["string"] = "foo"
In [4]: df.loc[df.index[4:6], "string"] = np.nan
In [5]: df.loc[df.index[7:9], "string"] = "bar"
In [6]: df["string2"] = "cool"
In [7]: df
Out[7]:
A B C string string2
2000-01-01 0.469112 -0.282863 -1.509059 foo cool
2000-01-02 -1.135632 1.212112 -0.173215 foo cool
2000-01-03 0.119209 -1.044236 -0.861849 foo cool
2000-01-04 -2.104569 -0.494929 1.071804 foo cool
2000-01-05 0.721555 -0.706771 -1.039575 NaN cool
2000-01-06 0.271860 -0.424972 0.567020 NaN cool
2000-01-07 0.276232 -1.087401 -0.673690 foo cool
2000-01-08 0.113648 -1.478427 0.524988 bar cool
# on-disk operations
In [8]: store.append("df", df, data_columns=["B", "C", "string", "string2"])
In [9]: store.select("df", "B>0 and string=='foo'")
Out[9]:
A B C string string2
2000-01-02 -1.135632 1.212112 -0.173215 foo cool
# this is in-memory version of this type of selection
In [10]: df[(df.B > 0) & (df.string == "foo")]
Out[10]:
A B C string string2
2000-01-02 -1.135632 1.212112 -0.173215 foo cool
檢索可索引列或資料列中的唯一值。
# note that this is deprecated as of 0.14.0
# can be replicated by: store.select_column('df','index').unique()
store.unique("df", "index")
store.unique("df", "string")
您現在可以在資料列中儲存 datetime64。
In [11]: df_mixed = df.copy()
In [12]: df_mixed["datetime64"] = pd.Timestamp("20010102")
In [13]: df_mixed.loc[df_mixed.index[3:4], ["A", "B"]] = np.nan
In [14]: store.append("df_mixed", df_mixed)
In [15]: df_mixed1 = store.select("df_mixed")
In [16]: df_mixed1
Out[16]:
A B C string string2 datetime64
2000-01-01 0.469112 -0.282863 -1.509059 foo cool 2001-01-02
2000-01-02 -1.135632 1.212112 -0.173215 foo cool 2001-01-02
2000-01-03 0.119209 -1.044236 -0.861849 foo cool 2001-01-02
2000-01-04 NaN NaN 1.071804 foo cool 2001-01-02
2000-01-05 0.721555 -0.706771 -1.039575 NaN cool 2001-01-02
2000-01-06 0.271860 -0.424972 0.567020 NaN cool 2001-01-02
2000-01-07 0.276232 -1.087401 -0.673690 foo cool 2001-01-02
2000-01-08 0.113648 -1.478427 0.524988 bar cool 2001-01-02
In [17]: df_mixed1.dtypes.value_counts()
Out[17]:
float64 3
str 2
datetime64[us] 1
Name: count, dtype: int64
您可以傳遞 columns 關鍵字來選擇過濾要返回的列列表,這等同於傳遞一個 Term('columns',list_of_columns_to_filter)。
In [18]: store.select("df", columns=["A", "B"])
Out[18]:
A B
2000-01-01 0.469112 -0.282863
2000-01-02 -1.135632 1.212112
2000-01-03 0.119209 -1.044236
2000-01-04 -2.104569 -0.494929
2000-01-05 0.721555 -0.706771
2000-01-06 0.271860 -0.424972
2000-01-07 0.276232 -1.087401
2000-01-08 0.113648 -1.478427
HDFStore 現在在追加表時序列化 MultiIndex DataFrame。
In [19]: index = pd.MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'],
....: ['one', 'two', 'three']],
....: labels=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3],
....: [0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
....: names=['foo', 'bar'])
....:
In [20]: df = pd.DataFrame(np.random.randn(10, 3), index=index,
....: columns=['A', 'B', 'C'])
....:
In [21]: df
Out[21]:
A B C
foo bar
foo one -0.116619 0.295575 -1.047704
two 1.640556 1.905836 2.772115
three 0.088787 -1.144197 -0.633372
bar one 0.925372 -0.006438 -0.820408
two -0.600874 -1.039266 0.824758
baz two -0.824095 -0.337730 -0.927764
three -0.840123 0.248505 -0.109250
qux one 0.431977 -0.460710 0.336505
two -3.207595 -1.535854 0.409769
three -0.673145 -0.741113 -0.110891
In [22]: store.append('mi', df)
In [23]: store.select('mi')
Out[23]:
A B C
foo bar
foo one -0.116619 0.295575 -1.047704
two 1.640556 1.905836 2.772115
three 0.088787 -1.144197 -0.633372
bar one 0.925372 -0.006438 -0.820408
two -0.600874 -1.039266 0.824758
baz two -0.824095 -0.337730 -0.927764
three -0.840123 0.248505 -0.109250
qux one 0.431977 -0.460710 0.336505
two -3.207595 -1.535854 0.409769
three -0.673145 -0.741113 -0.110891
# the levels are automatically included as data columns
In [24]: store.select('mi', "foo='bar'")
Out[24]:
A B C
foo bar
bar one 0.925372 -0.006438 -0.820408
two -0.600874 -1.039266 0.824758
透過 append_to_multiple 進行多表建立和透過 select_as_multiple 進行選擇可以建立/從多個表中選擇並返回一個組合結果,透過在選擇器表上使用 where。
In [19]: df_mt = pd.DataFrame(
....: np.random.randn(8, 6),
....: index=pd.date_range("1/1/2000", periods=8),
....: columns=["A", "B", "C", "D", "E", "F"],
....: )
....:
In [20]: df_mt["foo"] = "bar"
# you can also create the tables individually
In [21]: store.append_to_multiple(
....: {"df1_mt": ["A", "B"], "df2_mt": None}, df_mt, selector="df1_mt"
....: )
....:
In [22]: store
Out[22]:
<class 'pandas.HDFStore'>
File path: store.h5
# individual tables were created
In [23]: store.select("df1_mt")
Out[23]:
A B
2000-01-01 0.404705 0.577046
2000-01-02 -1.344312 0.844885
2000-01-03 0.357021 -0.674600
2000-01-04 0.276662 -0.472035
2000-01-05 0.895717 0.805244
2000-01-06 -1.170299 -0.226169
2000-01-07 -0.076467 -1.187678
2000-01-08 1.024180 0.569605
In [24]: store.select("df2_mt")
Out[24]:
C D E F foo
2000-01-01 -1.715002 -1.039268 -0.370647 -1.157892 bar
2000-01-02 1.075770 -0.109050 1.643563 -1.469388 bar
2000-01-03 -1.776904 -0.968914 -1.294524 0.413738 bar
2000-01-04 -0.013960 -0.362543 -0.006154 -0.923061 bar
2000-01-05 -1.206412 2.565646 1.431256 1.340309 bar
2000-01-06 0.410835 0.813850 0.132003 -0.827317 bar
2000-01-07 1.130127 -1.436737 -1.413681 1.607920 bar
2000-01-08 0.875906 -2.211372 0.974466 -2.006747 bar
# as a multiple
In [25]: store.select_as_multiple(
....: ["df1_mt", "df2_mt"], where=["A>0", "B>0"], selector="df1_mt"
....: )
....:
Out[25]:
A B C D E F foo
2000-01-01 0.404705 0.577046 -1.715002 -1.039268 -0.370647 -1.157892 bar
2000-01-05 0.895717 0.805244 -1.206412 2.565646 1.431256 1.340309 bar
2000-01-08 1.024180 0.569605 0.875906 -2.211372 0.974466 -2.006747 bar
增強功能
HDFStore現在可以讀取原生的 PyTables 表格式表。您可以透過傳遞
nan_rep = 'my_nan_rep'來追加,以更改磁碟上的預設 nan 表示(它會與np.nan相互轉換),預設為nan。您可以將
index傳遞給append。預設為True。這將自動在表的可索引列和資料列上建立索引。您可以將
chunksize=an integer傳遞給append,以更改寫入的塊大小(預設為 50000)。這會顯著降低寫入時的記憶體使用量。您可以將
expectedrows=an integer傳遞給第一個append,以設定PyTables預計的總行數。這將最佳化讀寫效能。Select現在支援傳遞start和stop來在選擇中提供選擇空間限制。大大改進了檔案解析器對 ISO8601(例如,yyyy-mm-dd)日期解析的支援(GH 2698)
允許
DataFrame.merge處理對於 64 位整數來說過大的組合大小(GH 2690)Series 現在具有一元否定(-series)和求反(~series)運算子(GH 2686)
DataFrame.plot 現在包含一個
logx引數,用於將 x 軸更改為對數尺度(GH 2327)Series 算術運算子現在可以處理常量和 ndarray 輸入(GH 2574)
ExcelFile 現在接受一個
kind引數來指定檔案型別(GH 2613)Series.str 方法的更快實現(GH 2602)
錯誤修復
HDFStore表現在可以正確儲存float32型別(但不能與float64混合)修復了指定請求片段時 Google Analytics 的字首(GH 2713)。
提供了一個重置 Google Analytics token 儲存的函式,以便使用者可以從設定不當的客戶端金鑰中恢復(GH 2687)。
修復了傳遞 MultiIndex 時導致段錯誤(segfault)的 groupby 錯誤(GH 2706)
修復了將具有 datetime64 值的 Series 傳遞給
to_datetime時產生錯誤輸出值的錯誤(GH 2699)修復了
HDFStore表示式中pattern in的錯誤,當模式不是有效的正則表示式時(GH 2694)修復了在聚合布林資料時出現的效能問題(GH 2692)
當使用布林掩碼鍵和新值 Series 時,Series __setitem__ 現在會將傳入的值與原始 Series 對齊(GH 2686)
修復了由於對具有非常大組合值的 MultiIndex 級別進行計數排序而導致的 MemoryError(GH 2684)
修復了當索引是具有固定偏移時區的 DatetimeIndex 時導致繪圖失敗的錯誤(GH 2683)
當偏移量大於 5 個工作日且開始日期在週末時,修正了工作日減法邏輯(GH 2680)
修復了 C 檔案解析器在檔案列數多於資料列數時的行為(GH 2668)
修復了檔案讀取器在存在隱式列和指定
usecols值時導致列與資料錯位的錯誤。具有數值或日期時間索引的 DataFrame 現在在繪圖前已排序(GH 2609)
修復了 DataFrame.from_records 在傳遞 columns、index 但 records 為空時的錯誤(GH 2633)
修復了 Series 在 dtype 為 datetime64 時的多個操作錯誤(GH 2689, GH 2629, GH 2626)
請參閱 完整的釋出說明 或 GitHub 上的問題跟蹤器以獲取完整列表。
貢獻者#
共有 17 人為本次釋出貢獻了補丁。名字旁帶有“+”的人是首次貢獻補丁。
Andy Hayden +
Anton I. Sipos +
Chang She
Christopher Whelan
Damien Garaud +
Dan Allan +
Dieter Vandenbussche
Garrett Drapala +
Jay Parlar +
Thouis (Ray) Jones +
Vincent Arel-Bundock +
Wes McKinney
elpres
herrfz +
jreback
svaksha +
y-p