def test_numpy_same_methods(self): v = Variable([], np.float32(0.0)) self.assertEqual(v.item(), 0) self.assertIs(type(v.item()), float) v = Coordinate('x', np.arange(5)) self.assertEqual(2, v.searchsorted(2))
def test_data(self): x = Coordinate('x', np.arange(3.0)) # data should be initially saved as an ndarray self.assertIs(type(x._data), NumpyArrayAdapter) self.assertEqual(float, x.dtype) self.assertArrayEqual(np.arange(3), x) self.assertEqual(float, x.values.dtype) # after inspecting x.values, the Coordinate value will be saved as an Index self.assertIsInstance(x._data, PandasIndexAdapter) with self.assertRaisesRegexp(TypeError, 'cannot be modified'): x[:] = 0
def test_avoid_index_dtype_inference(self): # verify our work-around for (pandas<0.14): # https://github.com/pydata/pandas/issues/6370 data = pd.date_range('2000-01-01', periods=3).to_pydatetime() t = Coordinate('t', data) self.assertArrayEqual(t.values[:2], data[:2]) self.assertArrayEqual(t[:2].values, data[:2]) self.assertArrayEqual(t.values[:2], data[:2]) self.assertArrayEqual(t[:2].values, data[:2]) self.assertEqual(t.dtype, object) self.assertEqual(t[:2].dtype, object)
def test_name(self): coord = Coordinate('x', [10.0]) self.assertEqual(coord.name, 'x') with self.assertRaises(AttributeError): coord.name = 'y'
def test_to_index(self): data = 0.5 * np.arange(10) v = Coordinate(['time'], data, {'foo': 'bar'}) self.assertTrue(pd.Index(data, name='time').identical(v.to_index()))
def test_init(self): with self.assertRaisesRegexp(ValueError, 'must be 1-dimensional'): Coordinate((), 0)
def test_name(self): coord = Coordinate("x", [10.0]) self.assertEqual(coord.name, "x") with self.assertRaises(AttributeError): coord.name = "y"
def test_to_index(self): data = 0.5 * np.arange(10) v = Coordinate(["time"], data, {"foo": "bar"}) self.assertTrue(pd.Index(data, name="time").identical(v.to_index()))