本文整理汇总了Python中pandas.util.testing.assert_index_equal方法的典型用法代码示例。如果您正苦于以下问题:Python testing.assert_index_equal方法的具体用法?Python testing.assert_index_equal怎么用?Python testing.assert_index_equal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.util.testing
的用法示例。
在下文中一共展示了testing.assert_index_equal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: assert_frame_equal
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_index_equal [as 别名]
def assert_frame_equal(self, left, right, *args, **kwargs):
tm.assert_index_equal(
left.columns, right.columns,
exact=kwargs.get('check_column_type', 'equiv'),
check_names=kwargs.get('check_names', True),
check_exact=kwargs.get('check_exact', False),
check_categorical=kwargs.get('check_categorical', True),
obj='{obj}.columns'.format(obj=kwargs.get('obj', 'DataFrame')))
jsons = (left.dtypes == 'json').index
for col in jsons:
self.assert_series_equal(left[col], right[col],
*args, **kwargs)
left = left.drop(columns=jsons)
right = right.drop(columns=jsons)
tm.assert_frame_equal(left, right, *args, **kwargs)
示例2: assert_frame_equal
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_index_equal [as 别名]
def assert_frame_equal(self, left, right, *args, **kwargs):
# TODO(EA): select_dtypes
tm.assert_index_equal(
left.columns, right.columns,
exact=kwargs.get('check_column_type', 'equiv'),
check_names=kwargs.get('check_names', True),
check_exact=kwargs.get('check_exact', False),
check_categorical=kwargs.get('check_categorical', True),
obj='{obj}.columns'.format(obj=kwargs.get('obj', 'DataFrame')))
decimals = (left.dtypes == 'decimal').index
for col in decimals:
self.assert_series_equal(left[col], right[col],
*args, **kwargs)
left = left.drop(columns=decimals)
right = right.drop(columns=decimals)
tm.assert_frame_equal(left, right, *args, **kwargs)
示例3: test_getitem
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_index_equal [as 别名]
def test_getitem(self):
r = self.frame.rolling(window=5)
tm.assert_index_equal(r._selected_obj.columns, self.frame.columns)
r = self.frame.rolling(window=5)[1]
assert r._selected_obj.name == self.frame.columns[1]
# technically this is allowed
r = self.frame.rolling(window=5)[1, 3]
tm.assert_index_equal(r._selected_obj.columns,
self.frame.columns[[1, 3]])
r = self.frame.rolling(window=5)[[1, 3]]
tm.assert_index_equal(r._selected_obj.columns,
self.frame.columns[[1, 3]])
示例4: test_agg_consistency
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_index_equal [as 别名]
def test_agg_consistency(self):
df = DataFrame({'A': range(5), 'B': range(0, 10, 2)})
r = df.rolling(window=3)
result = r.agg([np.sum, np.mean]).columns
expected = pd.MultiIndex.from_product([list('AB'), ['sum', 'mean']])
tm.assert_index_equal(result, expected)
result = r['A'].agg([np.sum, np.mean]).columns
expected = Index(['sum', 'mean'])
tm.assert_index_equal(result, expected)
result = r.agg({'A': [np.sum, np.mean]}).columns
expected = pd.MultiIndex.from_tuples([('A', 'sum'), ('A', 'mean')])
tm.assert_index_equal(result, expected)
示例5: test_pairwise_with_self
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_index_equal [as 别名]
def test_pairwise_with_self(self, f):
# DataFrame with itself, pairwise=True
# note that we may construct the 1st level of the MI
# in a non-motononic way, so compare accordingly
results = []
for i, df in enumerate(self.df1s):
result = f(df)
tm.assert_index_equal(result.index.levels[0],
df.index,
check_names=False)
tm.assert_numpy_array_equal(safe_sort(result.index.levels[1]),
safe_sort(df.columns.unique()))
tm.assert_index_equal(result.columns, df.columns)
results.append(df)
for i, result in enumerate(results):
if i > 0:
self.compare(result, results[0])
示例6: test_no_pairwise_with_other
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_index_equal [as 别名]
def test_no_pairwise_with_other(self, f):
# DataFrame with another DataFrame, pairwise=False
results = [f(df, self.df2) if df.columns.is_unique else None
for df in self.df1s]
for (df, result) in zip(self.df1s, results):
if result is not None:
with catch_warnings(record=True):
warnings.simplefilter("ignore", RuntimeWarning)
# we can have int and str columns
expected_index = df.index.union(self.df2.index)
expected_columns = df.columns.union(self.df2.columns)
tm.assert_index_equal(result.index, expected_index)
tm.assert_index_equal(result.columns, expected_columns)
else:
with pytest.raises(ValueError,
match="'arg1' columns are not unique"):
f(df, self.df2)
with pytest.raises(ValueError,
match="'arg2' columns are not unique"):
f(self.df2, df)
示例7: test_constructor_dict_mixed
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_index_equal [as 别名]
def test_constructor_dict_mixed(self):
data = {k: v.values for k, v in self.panel.iteritems()}
result = Panel(data)
exp_major = Index(np.arange(len(self.panel.major_axis)))
tm.assert_index_equal(result.major_axis, exp_major)
result = Panel(data, items=self.panel.items,
major_axis=self.panel.major_axis,
minor_axis=self.panel.minor_axis)
assert_panel_equal(result, self.panel)
data['ItemC'] = self.panel['ItemC']
result = Panel(data)
assert_panel_equal(result, self.panel)
# corner, blow up
data['ItemB'] = data['ItemB'][:-1]
pytest.raises(Exception, Panel, data)
data['ItemB'] = self.panel['ItemB'].values[:, :-1]
pytest.raises(Exception, Panel, data)
示例8: test_axis_dummies
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_index_equal [as 别名]
def test_axis_dummies(self):
from pandas.core.reshape.reshape import make_axis_dummies
minor_dummies = make_axis_dummies(self.panel, 'minor').astype(np.uint8)
assert len(minor_dummies.columns) == len(self.panel.index.levels[1])
major_dummies = make_axis_dummies(self.panel, 'major').astype(np.uint8)
assert len(major_dummies.columns) == len(self.panel.index.levels[0])
mapping = {'A': 'one', 'B': 'one', 'C': 'two', 'D': 'two'}
transformed = make_axis_dummies(self.panel, 'minor',
transform=mapping.get).astype(np.uint8)
assert len(transformed.columns) == 2
tm.assert_index_equal(transformed.columns, Index(['one', 'two']))
# TODO: test correctness
示例9: test_dropna
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_index_equal [as 别名]
def test_dropna(self):
sp = SparseSeries([0, 0, 0, nan, nan, 5, 6], fill_value=0)
sp_valid = sp.dropna()
expected = sp.to_dense().dropna()
expected = expected[expected != 0]
exp_arr = pd.SparseArray(expected.values, fill_value=0, kind='block')
tm.assert_sp_array_equal(sp_valid.values, exp_arr)
tm.assert_index_equal(sp_valid.index, expected.index)
assert len(sp_valid.sp_values) == 2
result = self.bseries.dropna()
expected = self.bseries.to_dense().dropna()
assert not isinstance(result, SparseSeries)
tm.assert_series_equal(result, expected)
示例10: test_take
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_index_equal [as 别名]
def test_take(self):
def assert_take_ok(mgr, axis, indexer):
mat = mgr.as_array()
taken = mgr.take(indexer, axis)
tm.assert_numpy_array_equal(np.take(mat, indexer, axis),
taken.as_array(), check_dtype=False)
tm.assert_index_equal(mgr.axes[axis].take(indexer),
taken.axes[axis])
for mgr in self.MANAGERS:
for ax in range(mgr.ndim):
# take/fancy indexer
assert_take_ok(mgr, ax, [])
assert_take_ok(mgr, ax, [0, 0, 0])
assert_take_ok(mgr, ax, lrange(mgr.shape[ax]))
if mgr.shape[ax] >= 3:
assert_take_ok(mgr, ax, [0, 1, 2])
assert_take_ok(mgr, ax, [-1, -2, -3])
示例11: test_astype_index
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_index_equal [as 别名]
def test_astype_index(self, all_data, dropna):
# as an int/uint index to Index
all_data = all_data[:10]
if dropna:
other = all_data[~all_data.isna()]
else:
other = all_data
dtype = all_data.dtype
idx = pd.Index(np.array(other))
assert isinstance(idx, ABCIndexClass)
result = idx.astype(dtype)
expected = idx.astype(object).astype(dtype)
tm.assert_index_equal(result, expected)
示例12: test_take
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_index_equal [as 别名]
def test_take(self):
data = np.arange(100, dtype='i8') * 24 * 3600 * 10**9
np.random.shuffle(data)
idx = self.index_cls._simple_new(data, freq='D')
arr = self.array_cls(idx)
takers = [1, 4, 94]
result = arr.take(takers)
expected = idx.take(takers)
tm.assert_index_equal(self.index_cls(result), expected)
takers = np.array([1, 4, 94])
result = arr.take(takers)
expected = idx.take(takers)
tm.assert_index_equal(self.index_cls(result), expected)
示例13: test_nan_handling
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_index_equal [as 别名]
def test_nan_handling(self):
# Nans are represented as -1 in codes
c = Categorical(["a", "b", np.nan, "a"])
tm.assert_index_equal(c.categories, Index(["a", "b"]))
tm.assert_numpy_array_equal(c._codes, np.array([0, 1, -1, 0],
dtype=np.int8))
c[1] = np.nan
tm.assert_index_equal(c.categories, Index(["a", "b"]))
tm.assert_numpy_array_equal(c._codes, np.array([0, -1, -1, 0],
dtype=np.int8))
# Adding nan to categories should make assigned nan point to the
# category!
c = Categorical(["a", "b", np.nan, "a"])
tm.assert_index_equal(c.categories, Index(["a", "b"]))
tm.assert_numpy_array_equal(c._codes, np.array([0, 1, -1, 0],
dtype=np.int8))
示例14: test_categories_assigments
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_index_equal [as 别名]
def test_categories_assigments(self):
s = Categorical(["a", "b", "c", "a"])
exp = np.array([1, 2, 3, 1], dtype=np.int64)
s.categories = [1, 2, 3]
tm.assert_numpy_array_equal(s.__array__(), exp)
tm.assert_index_equal(s.categories, Index([1, 2, 3]))
# lengthen
with pytest.raises(ValueError):
s.categories = [1, 2, 3, 4]
# shorten
with pytest.raises(ValueError):
s.categories = [1, 2]
# Combinations of sorted/unique:
示例15: test_ordered_api
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_index_equal [as 别名]
def test_ordered_api(self):
# GH 9347
cat1 = Categorical(list('acb'), ordered=False)
tm.assert_index_equal(cat1.categories, Index(['a', 'b', 'c']))
assert not cat1.ordered
cat2 = Categorical(list('acb'), categories=list('bca'), ordered=False)
tm.assert_index_equal(cat2.categories, Index(['b', 'c', 'a']))
assert not cat2.ordered
cat3 = Categorical(list('acb'), ordered=True)
tm.assert_index_equal(cat3.categories, Index(['a', 'b', 'c']))
assert cat3.ordered
cat4 = Categorical(list('acb'), categories=list('bca'), ordered=True)
tm.assert_index_equal(cat4.categories, Index(['b', 'c', 'a']))
assert cat4.ordered