本文整理汇总了Python中pandas.util.testing.assert_numpy_array_equal方法的典型用法代码示例。如果您正苦于以下问题:Python testing.assert_numpy_array_equal方法的具体用法?Python testing.assert_numpy_array_equal怎么用?Python testing.assert_numpy_array_equal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.util.testing
的用法示例。
在下文中一共展示了testing.assert_numpy_array_equal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_searchsorted
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_numpy_array_equal [as 别名]
def test_searchsorted(self, data_for_sorting, as_series):
b, c, a = data_for_sorting
arr = type(data_for_sorting)._from_sequence([a, b, c])
if as_series:
arr = pd.Series(arr)
assert arr.searchsorted(a) == 0
assert arr.searchsorted(a, side="right") == 1
assert arr.searchsorted(b) == 1
assert arr.searchsorted(b, side="right") == 2
assert arr.searchsorted(c) == 2
assert arr.searchsorted(c, side="right") == 3
result = arr.searchsorted(arr.take([0, 2]))
expected = np.array([0, 2], dtype=np.intp)
tm.assert_numpy_array_equal(result, expected)
# sorter
sorter = np.array([1, 2, 0])
assert data_for_sorting.searchsorted(a, sorter=sorter) == 0
示例2: test_pairwise_with_self
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_numpy_array_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])
示例3: test_stack_sparse_frame
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_numpy_array_equal [as 别名]
def test_stack_sparse_frame(self, float_frame, float_frame_int_kind,
float_frame_fill0, float_frame_fill2):
def _check(frame):
dense_frame = frame.to_dense() # noqa
wp = Panel.from_dict({'foo': frame})
from_dense_lp = wp.to_frame()
from_sparse_lp = spf.stack_sparse_frame(frame)
tm.assert_numpy_array_equal(from_dense_lp.values,
from_sparse_lp.values)
_check(float_frame)
_check(float_frame_int_kind)
# for now
pytest.raises(Exception, _check, float_frame_fill0)
pytest.raises(Exception, _check, float_frame_fill2)
示例4: test_delete
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_numpy_array_equal [as 别名]
def test_delete(self):
newb = self.fblock.copy()
newb.delete(0)
assert isinstance(newb.mgr_locs, BlockPlacement)
tm.assert_numpy_array_equal(newb.mgr_locs.as_array,
np.array([2, 4], dtype=np.int64))
assert (newb.values[0] == 1).all()
newb = self.fblock.copy()
newb.delete(1)
assert isinstance(newb.mgr_locs, BlockPlacement)
tm.assert_numpy_array_equal(newb.mgr_locs.as_array,
np.array([0, 4], dtype=np.int64))
assert (newb.values[1] == 2).all()
newb = self.fblock.copy()
newb.delete(2)
tm.assert_numpy_array_equal(newb.mgr_locs.as_array,
np.array([0, 2], dtype=np.int64))
assert (newb.values[1] == 1).all()
newb = self.fblock.copy()
with pytest.raises(Exception):
newb.delete(3)
示例5: test_take
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_numpy_array_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])
示例6: test_nonzero
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_numpy_array_equal [as 别名]
def test_nonzero(self):
# Tests regression #21172.
sa = pd.SparseArray([
float('nan'),
float('nan'),
1, 0, 0,
2, 0, 0, 0,
3, 0, 0
])
expected = np.array([2, 5, 9], dtype=np.int32)
result, = sa.nonzero()
tm.assert_numpy_array_equal(expected, result)
sa = pd.SparseArray([0, 0, 1, 0, 0, 2, 0, 0, 0, 3, 0, 0])
result, = sa.nonzero()
tm.assert_numpy_array_equal(expected, result)
示例7: test_int_internal
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_numpy_array_equal [as 别名]
def test_int_internal(self):
idx = _make_index(4, np.array([2, 3], dtype=np.int32), kind='integer')
assert isinstance(idx, IntIndex)
assert idx.npoints == 2
tm.assert_numpy_array_equal(idx.indices,
np.array([2, 3], dtype=np.int32))
idx = _make_index(4, np.array([], dtype=np.int32), kind='integer')
assert isinstance(idx, IntIndex)
assert idx.npoints == 0
tm.assert_numpy_array_equal(idx.indices,
np.array([], dtype=np.int32))
idx = _make_index(4, np.array([0, 1, 2, 3], dtype=np.int32),
kind='integer')
assert isinstance(idx, IntIndex)
assert idx.npoints == 4
tm.assert_numpy_array_equal(idx.indices,
np.array([0, 1, 2, 3], dtype=np.int32))
示例8: test_conversions
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_numpy_array_equal [as 别名]
def test_conversions(data_missing):
# astype to object series
df = pd.DataFrame({'A': data_missing})
result = df['A'].astype('object')
expected = pd.Series(np.array([np.nan, 1], dtype=object), name='A')
tm.assert_series_equal(result, expected)
# convert to object ndarray
# we assert that we are exactly equal
# including type conversions of scalars
result = df['A'].astype('object').values
expected = np.array([np.nan, 1], dtype=object)
tm.assert_numpy_array_equal(result, expected)
for r, e in zip(result, expected):
if pd.isnull(r):
assert pd.isnull(e)
elif is_integer(r):
# PY2 can be int or long
assert r == e
assert is_integer(e)
else:
assert r == e
assert type(r) == type(e)
示例9: test_array_interface_tz
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_numpy_array_equal [as 别名]
def test_array_interface_tz(self):
tz = "US/Central"
data = DatetimeArray(pd.date_range('2017', periods=2, tz=tz))
result = np.asarray(data)
expected = np.array([pd.Timestamp('2017-01-01T00:00:00', tz=tz),
pd.Timestamp('2017-01-02T00:00:00', tz=tz)],
dtype=object)
tm.assert_numpy_array_equal(result, expected)
result = np.asarray(data, dtype=object)
tm.assert_numpy_array_equal(result, expected)
result = np.asarray(data, dtype='M8[ns]')
expected = np.array(['2017-01-01T06:00:00',
'2017-01-02T06:00:00'], dtype="M8[ns]")
tm.assert_numpy_array_equal(result, expected)
示例10: test_searchsorted
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_numpy_array_equal [as 别名]
def test_searchsorted(self):
data = np.arange(10, dtype='i8') * 24 * 3600 * 10**9
arr = self.array_cls(data, freq='D')
# scalar
result = arr.searchsorted(arr[1])
assert result == 1
result = arr.searchsorted(arr[2], side="right")
assert result == 3
# own-type
result = arr.searchsorted(arr[1:3])
expected = np.array([1, 2], dtype=np.intp)
tm.assert_numpy_array_equal(result, expected)
result = arr.searchsorted(arr[1:3], side="right")
expected = np.array([2, 3], dtype=np.intp)
tm.assert_numpy_array_equal(result, expected)
# Following numpy convention, NaT goes at the beginning
# (unlike NaN which goes at the end)
result = arr.searchsorted(pd.NaT)
assert result == 0
示例11: test_array_tz
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_numpy_array_equal [as 别名]
def test_array_tz(self, tz_naive_fixture):
# GH#23524
tz = tz_naive_fixture
dti = pd.date_range('2016-01-01', periods=3, tz=tz)
arr = DatetimeArray(dti)
expected = dti.asi8.view('M8[ns]')
result = np.array(arr, dtype='M8[ns]')
tm.assert_numpy_array_equal(result, expected)
result = np.array(arr, dtype='datetime64[ns]')
tm.assert_numpy_array_equal(result, expected)
# check that we are not making copies when setting copy=False
result = np.array(arr, dtype='M8[ns]', copy=False)
assert result.base is expected.base
assert result.base is not None
result = np.array(arr, dtype='datetime64[ns]', copy=False)
assert result.base is expected.base
assert result.base is not None
示例12: test_array_i8_dtype
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_numpy_array_equal [as 别名]
def test_array_i8_dtype(self, tz_naive_fixture):
tz = tz_naive_fixture
dti = pd.date_range('2016-01-01', periods=3, tz=tz)
arr = DatetimeArray(dti)
expected = dti.asi8
result = np.array(arr, dtype='i8')
tm.assert_numpy_array_equal(result, expected)
result = np.array(arr, dtype=np.int64)
tm.assert_numpy_array_equal(result, expected)
# check that we are still making copies when setting copy=False
result = np.array(arr, dtype='i8', copy=False)
assert result.base is not expected.base
assert result.base is None
示例13: test_array_interface
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_numpy_array_equal [as 别名]
def test_array_interface(self, period_index):
arr = PeriodArray(period_index)
# default asarray gives objects
result = np.asarray(arr)
expected = np.array(list(arr), dtype=object)
tm.assert_numpy_array_equal(result, expected)
# to object dtype (same as default)
result = np.asarray(arr, dtype=object)
tm.assert_numpy_array_equal(result, expected)
# to other dtypes
with pytest.raises(TypeError):
np.asarray(arr, dtype='int64')
with pytest.raises(TypeError):
np.asarray(arr, dtype='float64')
result = np.asarray(arr, dtype='S20')
expected = np.asarray(arr).astype('S20')
tm.assert_numpy_array_equal(result, expected)
示例14: test_nan_handling
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_numpy_array_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))
示例15: test_categories_assigments
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_numpy_array_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: