用法:
pandas.testing.assert_frame_equal(left, right, check_dtype=True, check_index_type='equiv', check_column_type='equiv', check_frame_type=True, check_less_precise=NoDefault.no_default, check_names=True, by_blocks=False, check_exact=False, check_datetimelike_compat=False, check_categorical=True, check_like=False, check_freq=True, check_flags=True, rtol=1e-05, atol=1e-08, obj='DataFrame')
檢查左右 DataFrame 是否相等。
此函數旨在比較兩個 DataFrame 並輸出任何差異。 Is 主要用於單元測試。附加參數允許改變執行的相等檢查的嚴格性。
- left: DataFrame
要比較的第一個 DataFrame。
- right: DataFrame
要比較的第二個 DataFrame。
- check_dtype:布爾值,默認為真
是否檢查 DataFrame dtype 是否相同。
- check_index_type:bool 或 {‘equiv’},默認 ‘equiv’
是否檢查Index類,dtype和inferred_type是否相同。
- check_column_type:bool 或 {‘equiv’},默認 ‘equiv’
是否檢查列類、dtype 和inferred_type 是否相同。作為
assert_index_equal()
的exact
參數傳遞。- check_frame_type:布爾值,默認為真
是否檢查 DataFrame 類是否相同。
- check_less_precise:bool 或 int,默認為 False
指定比較精度。僅在 check_exact 為 False 時使用。比較小數點後的 5 位 (False) 或 3 位 (True)。如果是 int,則指定要比較的數字。
比較兩個數時,如果第一個數的幅度小於 1e-5,我們直接比較這兩個數,並在指定的精度內檢查它們是否相等。否則,我們比較第二個數與第一個數的比值,並在指定的精度內檢查它是否等於 1。
- check_names:布爾值,默認為真
是否檢查 DataFrame 的
index
和column
屬性的names
屬性是否相同。- by_blocks:布爾值,默認為 False
指定如何比較內部數據。如果為 False,則按列進行比較。如果為 True,則按塊進行比較。
- check_exact:布爾值,默認為 False
是否準確比較數字。
- check_datetimelike_compat:布爾值,默認為 False
比較 datetime-like 這是可比較的忽略 dtype。
- check_categorical:布爾值,默認為真
是否準確比較內部分類。
- check_like:布爾值,默認為 False
如果為 True,則忽略索引和列的順序。注意:索引標簽必須匹配它們各自的行(與列相同) - 相同的標簽必須具有相同的數據。
- check_freq:布爾值,默認為真
是否檢查 DatetimeIndex 或 TimedeltaIndex 上的
freq
屬性。- check_flags:布爾值,默認為真
是否檢查
flags
屬性。- rtol:浮點數,默認 1e-5
相對容差。僅在 check_exact 為 False 時使用。
- atol:浮點數,默認 1e-8
絕對的寬容。僅在 check_exact 為 False 時使用。
- obj:str,默認“數據幀”
指定要比較的對象名稱,內部用於顯示適當的斷言消息。
參數:
例子:
此示例顯示了比較兩個相同但具有不同 dtype 列的 DataFrame。
>>> from pandas.testing import assert_frame_equal >>> df1 = pd.DataFrame({'a':[1, 2], 'b':[3, 4]}) >>> df2 = pd.DataFrame({'a':[1, 2], 'b':[3.0, 4.0]})
df1 等於自身。
>>> assert_frame_equal(df1, df1)
df1 與 df2 不同,因為列 ‘b’ 的類型不同。
>>> assert_frame_equal(df1, df2) Traceback (most recent call last): ... AssertionError:Attributes of DataFrame.iloc[:, 1] (column name="b") are different
屬性 “dtype” 不同 [left]:int64 [right]:float64
使用 check_dtype 忽略列中的不同 dtype。
>>> assert_frame_equal(df1, df2, check_dtype=False)
相關用法
- Python pandas.testing.assert_index_equal用法及代碼示例
- Python pandas.testing.assert_series_equal用法及代碼示例
- Python pandas.testing.assert_extension_array_equal用法及代碼示例
- Python pandas.tseries.offsets.BusinessMonthEnd用法及代碼示例
- Python pandas.tseries.offsets.BQuarterBegin用法及代碼示例
- Python pandas.tseries.offsets.DateOffset用法及代碼示例
- Python pandas.to_numeric用法及代碼示例
- Python pandas.to_datetime用法及代碼示例
- Python pandas.timedelta_range用法及代碼示例
- Python pandas.to_markdown()用法及代碼示例
- Python pandas.to_timedelta用法及代碼示例
- Python pandas.tseries.offsets.BusinessMonthBegin用法及代碼示例
- Python pandas.tseries.offsets.BQuarterEnd用法及代碼示例
- Python pandas.tseries.offsets.BYearBegin用法及代碼示例
- Python pandas.tseries.offsets.BYearEnd用法及代碼示例
- Python pandas.arrays.IntervalArray.is_empty用法及代碼示例
- Python pandas.DataFrame.ewm用法及代碼示例
- Python pandas.api.types.is_timedelta64_ns_dtype用法及代碼示例
- Python pandas.DataFrame.dot用法及代碼示例
- Python pandas.DataFrame.apply用法及代碼示例
注:本文由純淨天空篩選整理自pandas.pydata.org大神的英文原創作品 pandas.testing.assert_frame_equal。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。