def test_concat_periods(self): periods = pd.period_range('2000-01-01', periods=10) coords = [Coordinate('t', periods[:5]), Coordinate('t', periods[5:])] expected = Coordinate('t', periods) actual = Coordinate.concat(coords, dim='t') assert actual.identical(expected) assert isinstance(actual.to_index(), pd.PeriodIndex) positions = [list(range(5)), list(range(5, 10))] actual = Coordinate.concat(coords, dim='t', positions=positions) assert actual.identical(expected) assert isinstance(actual.to_index(), pd.PeriodIndex)
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_concat_multiindex(self): idx = pd.MultiIndex.from_product([[0, 1, 2], ['a', 'b']]) coords = [Coordinate('x', idx[:2]), Coordinate('x', idx[2:])] expected = Coordinate('x', idx) actual = Coordinate.concat(coords, dim='x') assert actual.identical(expected) assert isinstance(actual.to_index(), pd.MultiIndex)
def test_data(self): x = Coordinate('x', np.arange(3.0)) # data should be initially saved as an ndarray self.assertIs(type(x._data), np.ndarray) 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_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_multiindex_default_level_names(self): midx = pd.MultiIndex.from_product([['a', 'b'], [1, 2]]) v = Coordinate(['x'], midx, {'foo': 'bar'}) self.assertEqual(v.to_index().names, ('x_level_0', 'x_level_1'))
def test_name(self): coord = Coordinate("x", [10.0]) self.assertEqual(coord.name, "x") with self.assertRaises(AttributeError): coord.name = "y"
def test_multiindex_default_level_names(self): midx = pd.MultiIndex.from_product([["a", "b"], [1, 2]]) v = Coordinate(["x"], midx, {"foo": "bar"}) self.assertEqual(v.to_index().names, ("x_level_0", "x_level_1"))
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()))