def test_comp(setup): df1 = DataFrame(pd.DataFrame(np.random.rand(4, 3))) df2 = DataFrame(pd.DataFrame(np.random.rand(4, 3))) with enter_mode(build=True): assert not df1.data == df2.data assert df1.data == df1.data for op in [operator.eq, operator.ne, operator.lt, operator.gt, operator.le, operator.ge]: eq_df = op(df1, df2) pd.testing.assert_index_equal(eq_df.index_value.to_pandas(), df1.index_value.to_pandas()) # index not identical df3 = DataFrame(pd.DataFrame(np.random.rand(4, 3), index=[1, 2, 3, 4])) with pytest.raises(ValueError): op(df1, df3) # columns not identical df4 = DataFrame(pd.DataFrame(np.random.rand(4, 3), columns=['a', 'b', 'c'])) with pytest.raises(ValueError): op(df1, df4) # test datetime df = DataFrame(pd.DataFrame(pd.date_range('20130101', periods=6))) for op in [operator.eq, operator.ne, operator.lt, operator.gt, operator.le, operator.ge]: r_df = op(df, datetime(2013, 1, 2)) pd.testing.assert_index_equal(r_df.index_value.to_pandas(), df.index_value.to_pandas())
def test_set_tensor_inputs(): t1 = tensor([1, 2], chunk_size=2) t2 = tensor([2, 3], chunk_size=2) t3 = t1 + t2 t1c = copy(t1) t2c = copy(t2) assert t1c is not t1 assert t2c is not t2 assert t3.op.lhs is t1.data assert t3.op.rhs is t2.data assert t3.op.inputs == [t1.data, t2.data] assert t3.inputs == [t1.data, t2.data] with pytest.raises(StopIteration): t3.inputs = [] t1 = tensor([1, 2], chunk_size=2) t2 = tensor([True, False], chunk_size=2) t3 = t1[t2] t1c = copy(t1) t2c = copy(t2) t3c = copy(t3) t3c.inputs = [t1c, t2c] with enter_mode(build=True): assert t3c.op.input is t1c.data assert t3c.op.indexes[0] is t2c.data
def testSetTensorInputs(self): t1 = tensor([1, 2], chunk_size=2) t2 = tensor([2, 3], chunk_size=2) t3 = t1 + t2 t1c = copy(t1) t2c = copy(t2) self.assertIsNot(t1c, t1) self.assertIsNot(t2c, t2) self.assertIs(t3.op.lhs, t1.data) self.assertIs(t3.op.rhs, t2.data) self.assertEqual(t3.op.inputs, [t1.data, t2.data]) self.assertEqual(t3.inputs, [t1.data, t2.data]) with self.assertRaises(StopIteration): t3.inputs = [] t1 = tensor([1, 2], chunk_size=2) t2 = tensor([True, False], chunk_size=2) t3 = t1[t2] t1c = copy(t1) t2c = copy(t2) t3c = copy(t3) t3c.inputs = [t1c, t2c] with enter_mode(build=True): self.assertIs(t3c.op.input, t1c.data) self.assertIs(t3c.op.indexes[0], t2c.data)
def wrapped2(): wrapped() with option_context({'eager_mode': True}): assert options.eager_mode assert not is_eager_mode() with enter_mode(kernel=False): assert not is_kernel_mode() assert is_kernel_mode()
def wrapped3(): wrapped() with option_context({'eager_mode': True}): assert options.eager_mode assert not is_kernel_mode() with enter_mode(kernel=True, build=True): assert is_kernel_mode() assert is_build_mode() assert not is_kernel_mode() assert not is_build_mode() with pytest.raises(ValueError): with enter_mode(kernel=True, build=True): raise ValueError('meant to raise error') assert not is_kernel_mode() assert not is_build_mode() @enter_mode(kernel=True) def wrapped4(): raise ValueError('meant to raise error') with pytest.raises(ValueError): wrapped4() assert not is_kernel_mode() assert not is_build_mode()
def test_arrow_list_array_creation(): # create from pandas Series series = pd.Series([['a', 'b'], ['c'], ['d', 'e']]) array = ArrowListArray(series) assert isinstance(array.dtype, ArrowListDtype) assert isinstance(array.dtype.value_type, ArrowStringDtype) assert isinstance(array._arrow_array, pa.ChunkedArray) # create from list lst = [['a'], ['b', 'c'], ['d', 'e']] array = ArrowListArray(lst) assert isinstance(array.dtype, ArrowListDtype) assert isinstance(array.dtype.value_type, ArrowStringDtype) assert isinstance(array._arrow_array, pa.ChunkedArray) # create from pyarrow Array a = pa.array([[1.], [2., 3.], [4.]]) array = ArrowListArray(a) assert isinstance(array.dtype, ArrowListDtype) assert array.dtype.value_type == np.float64 assert isinstance(array._arrow_array, pa.ChunkedArray) # create from ArrowListArray array2 = ArrowListArray(array) assert isinstance(array2._arrow_array, pa.ChunkedArray) # test _from_sequence array = ArrowListArray._from_sequence([[1, 2], [3, 4], [5]]) assert isinstance(array.dtype, ArrowListDtype) assert array.dtype.value_type == np.int64 assert isinstance(array._arrow_array, pa.ChunkedArray) # test pandas_only with option_context({'dataframe.arrow_array.pandas_only': True}): array = ArrowListArray._from_sequence([[1, 2], [3, 4], [5]]) assert isinstance(array.dtype, ArrowListDtype) assert isinstance(array._ndarray, np.ndarray) # test pandas_only and in kernel mode with enter_mode(kernel=True), \ option_context({'dataframe.arrow_array.pandas_only': True}), \ pytest.raises(ImportError): ArrowListArray._from_sequence([[1, 2], [3, 4], [5]])
def test_comp(setup): df1 = DataFrame(pd.DataFrame(np.random.rand(4, 3))) df2 = DataFrame(pd.DataFrame(np.random.rand(4, 3))) with enter_mode(build=True): assert not df1.data == df2.data assert df1.data == df1.data for op in [ operator.eq, operator.ne, operator.lt, operator.gt, operator.le, operator.ge ]: eq_df = op(df1, df2) pd.testing.assert_index_equal(eq_df.index_value.to_pandas(), df1.index_value.to_pandas()) # index not identical df3 = DataFrame(pd.DataFrame(np.random.rand(4, 3), index=[1, 2, 3, 4])) with pytest.raises(ValueError): op(df1, df3) # columns not identical df4 = DataFrame( pd.DataFrame(np.random.rand(4, 3), columns=['a', 'b', 'c'])) with pytest.raises(ValueError): op(df1, df4) # test datetime df = DataFrame(pd.DataFrame(pd.date_range('20130101', periods=6))) for op in [ operator.eq, operator.ne, operator.lt, operator.gt, operator.le, operator.ge ]: r_df = op(df, datetime(2013, 1, 2)) pd.testing.assert_index_equal(r_df.index_value.to_pandas(), df.index_value.to_pandas()) # test period type raw = pd.period_range("2000-01-01", periods=10, freq="D") raw_series = pd.Series(raw) series = Series(raw, chunk_size=5) r = series >= series[1] pd.testing.assert_series_equal(r.to_pandas(), raw_series >= raw_series[1])
def test_build_mode(): t1 = ones((2, 3), chunk_size=2) assert t1 == 2 with enter_mode(build=True): assert t1 != 2
def testBuildMode(self): t1 = ones((2, 3), chunk_size=2) self.assertTrue(t1 == 2) with enter_mode(build=True): self.assertFalse(t1 == 2)