def test_encoding_kwarg(self): ds = Dataset({'x': ('y', np.arange(10.0))}) kwargs = dict(encoding={'x': {'dtype': 'f4'}}) with self.roundtrip(ds, save_kwargs=kwargs) as actual: self.assertEqual(actual.x.encoding['dtype'], 'f4') self.assertEqual(ds.x.encoding, {}) kwargs = dict(encoding={'x': {'foo': 'bar'}}) with self.assertRaisesRegexp(ValueError, 'unexpected encoding'): with self.roundtrip(ds, save_kwargs=kwargs) as actual: pass kwargs = dict(encoding={'x': 'foo'}) with self.assertRaisesRegexp(ValueError, 'must be castable'): with self.roundtrip(ds, save_kwargs=kwargs) as actual: pass kwargs = dict(encoding={'invalid': {}}) with self.assertRaises(KeyError): with self.roundtrip(ds, save_kwargs=kwargs) as actual: pass ds = Dataset({'t': pd.date_range('2000-01-01', periods=3)}) units = 'days since 1900-01-01' kwargs = dict(encoding={'t': {'units': units}}) with self.roundtrip(ds, save_kwargs=kwargs) as actual: self.assertEqual(actual.t.encoding['units'], units) self.assertDatasetIdentical(actual, ds)
def test_roundtrip_strings_with_fill_value(self): values = np.array(['ab', 'cdef', np.nan], dtype=object) encoding = {'_FillValue': np.string_('X'), 'dtype': np.dtype('S1')} original = Dataset({'x': ('t', values, {}, encoding)}) expected = original.copy(deep=True) expected['x'][:2] = values[:2].astype('S') with self.roundtrip(original) as actual: self.assertDatasetIdentical(expected, actual) original = Dataset({'x': ('t', values, {}, {'_FillValue': '\x00'})}) if not isinstance(self, Only32BitTypes): # these stores can save unicode strings expected = original.copy(deep=True) if isinstance(self, BaseNetCDF4Test): # netCDF4 can't keep track of an empty _FillValue for VLEN # variables expected['x'][-1] = '' elif (isinstance( self, (NetCDF3ViaNetCDF4DataTest, NetCDF4ClassicViaNetCDF4DataTest)) or (has_netCDF4 and type(self) is GenericNetCDFDataTest)): # netCDF4 can't keep track of an empty _FillValue for nc3, either: # https://github.com/Unidata/netcdf4-python/issues/273 expected['x'][-1] = np.string_('') with self.roundtrip(original) as actual: self.assertDatasetIdentical(expected, actual)
def test_to_and_from_dataframe(self): x = np.random.randn(10) y = np.random.randn(10) t = list('abcdefghij') ds = Dataset( OrderedDict([ ('a', ('t', x)), ('b', ('t', y)), ('t', ('t', t)), ])) expected = pd.DataFrame(np.array([x, y]).T, columns=['a', 'b'], index=pd.Index(t, name='t')) actual = ds.to_dataframe() # use the .equals method to check all DataFrame metadata assert expected.equals(actual), (expected, actual) # check roundtrip self.assertDatasetIdentical(ds, Dataset.from_dataframe(actual)) # test a case with a MultiIndex w = np.random.randn(2, 3) ds = Dataset({'w': (('x', 'y'), w)}) ds['y'] = ('y', list('abc')) exp_index = pd.MultiIndex.from_arrays( [[0, 0, 0, 1, 1, 1], ['a', 'b', 'c', 'a', 'b', 'c']], names=['x', 'y']) expected = pd.DataFrame(w.reshape(-1), columns=['w'], index=exp_index) actual = ds.to_dataframe() self.assertTrue(expected.equals(actual)) # check roundtrip self.assertDatasetIdentical(ds, Dataset.from_dataframe(actual))
def test_coordinate(self): a = Dataset() vec = np.random.random((10, )) attributes = {'foo': 'bar'} a['x'] = ('x', vec, attributes) self.assertTrue('x' in a.coordinates) self.assertIsInstance(a.coordinates['x'].as_index, pd.Index) self.assertVariableEqual(a.coordinates['x'], a.variables['x']) b = Dataset() b['x'] = ('x', vec, attributes) self.assertVariableEqual(a['x'], b['x']) self.assertEqual(a.dimensions, b.dimensions) # this should work a['x'] = ('x', vec[:5]) a['z'] = ('x', np.arange(5)) with self.assertRaises(ValueError): # now it shouldn't, since there is a conflicting length a['x'] = ('x', vec[:4]) arr = np.random.random(( 10, 1, )) scal = np.array(0) with self.assertRaises(ValueError): a['y'] = ('y', arr) with self.assertRaises(ValueError): a['y'] = ('y', scal) self.assertTrue('y' not in a.dimensions)
def test_groupby_sum(self): array = self.make_groupby_example_array() grouped = array.groupby('abc') expected_sum_all = Dataset( {'foo': Variable(['abc'], np.array([self.x[:, :9].sum(), self.x[:, 10:].sum(), self.x[:, 9:10].sum()]).T), 'abc': Variable(['abc'], np.array(['a', 'b', 'c']))})['foo'] self.assertDataArrayAllClose(expected_sum_all, grouped.reduce(np.sum)) self.assertDataArrayAllClose(expected_sum_all, grouped.sum()) expected = DataArray([array['y'].values[idx].sum() for idx in [slice(9), slice(10, None), slice(9, 10)]], [['a', 'b', 'c']], ['abc']) actual = array['y'].groupby('abc').apply(np.sum) self.assertDataArrayAllClose(expected, actual) actual = array['y'].groupby('abc').sum() self.assertDataArrayAllClose(expected, actual) expected_sum_axis1 = Dataset( {'foo': (['x', 'abc'], np.array([self.x[:, :9].sum(1), self.x[:, 10:].sum(1), self.x[:, 9:10].sum(1)]).T), 'x': self.ds['x'], 'abc': Variable(['abc'], np.array(['a', 'b', 'c']))})['foo'] self.assertDataArrayAllClose(expected_sum_axis1, grouped.reduce(np.sum, 'y')) self.assertDataArrayAllClose(expected_sum_axis1, grouped.sum('y'))
def test_decode_cf_with_drop_variables(self): original = Dataset({ 't': ('t', [0, 1, 2], { 'units': 'days since 2000-01-01' }), 'x': ("x", [9, 8, 7], { 'units': 'km' }), 'foo': (('t', 'x'), [[0, 0, 0], [1, 1, 1], [2, 2, 2]], { 'units': 'bar' }), 'y': ('t', [5, 10, -999], { '_FillValue': -999 }) }) expected = Dataset({ 't': pd.date_range('2000-01-01', periods=3), 'x': ("x", [0, 1, 2]), 'foo': (('t', 'x'), [[0, 0, 0], [1, 1, 1], [2, 2, 2]], { 'units': 'bar' }), 'y': ('t', [5, 10, np.nan]) }) actual = conventions.decode_cf(original, drop_variables=("x", )) actual2 = conventions.decode_cf(original, drop_variables="x") self.assertDatasetIdentical(expected, actual) self.assertDatasetIdentical(expected, actual2)
def test_pipe(self): df = Dataset({'A': ('x', [1, 2, 3])}) f = lambda x, y: x**y result = df.pipe(f, 2) expected = Dataset({'A': ('x', [1, 4, 9])}) self.assertDatasetIdentical(result, expected) result = df.A.pipe(f, 2) self.assertDataArrayIdentical(result, expected.A)
def test_concat_constant_index(self): # GH425 ds1 = Dataset({'foo': 1.5}, {'y': 1}) ds2 = Dataset({'foo': 2.5}, {'y': 1}) expected = Dataset({'foo': ('y', [1.5, 2.5]), 'y': [1, 1]}) for mode in ['different', 'all', ['foo']]: actual = concat([ds1, ds2], 'y', data_vars=mode) self.assertDatasetIdentical(expected, actual) with self.assertRaisesRegexp(ValueError, 'not equal across datasets'): concat([ds1, ds2], 'y', data_vars='minimal')
def test_dataset_repr_with_netcdf4_datetimes(self): # regression test for #347 attrs = {'units': 'days since 0001-01-01', 'calendar': 'noleap'} with warnings.catch_warnings(): warnings.filterwarnings('ignore', 'unable to decode time') ds = decode_cf(Dataset({'time': ('time', [0, 1], attrs)})) self.assertIn('(time) object', repr(ds)) attrs = {'units': 'days since 1900-01-01'} ds = decode_cf(Dataset({'time': ('time', [0, 1], attrs)})) self.assertIn('(time) datetime64[ns]', repr(ds))
def test_dataset(self): original = Dataset({ 't': ('t', [0, 1, 2], {'units': 'days since 2000-01-01'}), 'foo': ('t', [0, 0, 0], {'coordinates': 'y', 'units': 'bar'}), 'y': ('t', [5, 10, -999], {'_FillValue': -999}) }) expected = Dataset({'foo': ('t', [0, 0, 0], {'units': 'bar'})}, {'t': pd.date_range('2000-01-01', periods=3), 'y': ('t', [5.0, 10.0, np.nan])}) actual = conventions.decode_cf(original) self.assertDatasetIdentical(expected, actual)
def test_init(self): var1 = Variable('x', 2 * np.arange(100)) var2 = Variable('x', np.arange(1000)) var3 = Variable(['x', 'y'], np.arange(1000).reshape(100, 10)) with self.assertRaisesRegexp(ValueError, 'but already exists'): Dataset({'a': var1, 'b': var2}) with self.assertRaisesRegexp(ValueError, 'must be defined with 1-d'): Dataset({'a': var1, 'x': var3}) # verify handling of DataArrays expected = Dataset({'x': var1, 'z': var3}) actual = Dataset({'z': expected['z']}) self.assertDatasetIdentical(expected, actual)
def test_reset_coords(self): data = DataArray(np.zeros((3, 4)), { 'bar': ('x', ['a', 'b', 'c']), 'baz': ('y', range(4)) }, dims=['x', 'y'], name='foo') actual = data.reset_coords() expected = Dataset({ 'foo': (['x', 'y'], np.zeros((3, 4))), 'bar': ('x', ['a', 'b', 'c']), 'baz': ('y', range(4)) }) self.assertDatasetIdentical(actual, expected) actual = data.reset_coords(['bar', 'baz']) self.assertDatasetIdentical(actual, expected) actual = data.reset_coords('bar') expected = Dataset( { 'foo': (['x', 'y'], np.zeros((3, 4))), 'bar': ('x', ['a', 'b', 'c']) }, {'baz': ('y', range(4))}) self.assertDatasetIdentical(actual, expected) actual = data.reset_coords(['bar']) self.assertDatasetIdentical(actual, expected) actual = data.reset_coords(drop=True) expected = DataArray(np.zeros((3, 4)), dims=['x', 'y'], name='foo') self.assertDataArrayIdentical(actual, expected) actual = data.copy() actual.reset_coords(drop=True, inplace=True) self.assertDataArrayIdentical(actual, expected) actual = data.reset_coords('bar', drop=True) expected = DataArray(np.zeros((3, 4)), {'baz': ('y', range(4))}, dims=['x', 'y'], name='foo') self.assertDataArrayIdentical(actual, expected) with self.assertRaisesRegexp(ValueError, 'cannot reset coord'): data.reset_coords(inplace=True) with self.assertRaises(KeyError): data.reset_coords('foo', drop=True) with self.assertRaisesRegexp(ValueError, 'cannot be found'): data.reset_coords('not_found') with self.assertRaisesRegexp(ValueError, 'cannot remove index'): data.reset_coords('y')
def test_dataset_math(self): # more comprehensive tests with multiple dataset variables obs = Dataset( { 'tmin': ('x', np.arange(5)), 'tmax': ('x', 10 + np.arange(5)) }, { 'x': ('x', 0.5 * np.arange(5)), 'loc': ('x', range(-2, 3)) }) actual = 2 * obs['tmax'] expected = DataArray(2 * (10 + np.arange(5)), obs.coords, name='tmax') self.assertDataArrayIdentical(actual, expected) actual = obs['tmax'] - obs['tmin'] expected = DataArray(10 * np.ones(5), obs.coords) self.assertDataArrayIdentical(actual, expected) sim = Dataset({ 'tmin': ('x', 1 + np.arange(5)), 'tmax': ('x', 11 + np.arange(5)), # does *not* include 'loc' as a coordinate 'x': ('x', 0.5 * np.arange(5)) }) actual = sim['tmin'] - obs['tmin'] expected = DataArray(np.ones(5), obs.coords, name='tmin') self.assertDataArrayIdentical(actual, expected) actual = -obs['tmin'] + sim['tmin'] self.assertDataArrayIdentical(actual, expected) actual = sim['tmin'].copy() actual -= obs['tmin'] self.assertDataArrayIdentical(actual, expected) actual = sim.copy() actual['tmin'] = sim['tmin'] - obs['tmin'] expected = Dataset( { 'tmin': ('x', np.ones(5)), 'tmax': ('x', sim['tmax'].values) }, obs.coords) self.assertDatasetIdentical(actual, expected) actual = sim.copy() actual['tmin'] -= obs['tmin'] self.assertDatasetIdentical(actual, expected)
def test_squeeze(self): data = Dataset({'foo': (['x', 'y', 'z'], [[[1], [2]]])}) for args in [[], [['x']], [['x', 'z']]]: def get_args(v): return [set(args[0]) & set(v.dimensions)] if args else [] expected = Dataset({ k: v.squeeze(*get_args(v)) for k, v in iteritems(data.variables) }) self.assertDatasetIdentical(expected, data.squeeze(*args)) # invalid squeeze with self.assertRaisesRegexp(ValueError, 'cannot select a dimension'): data.squeeze('y')
def test_unselect(self): data = create_test_data() self.assertEqual(data, data.unselect()) expected = Dataset({k: data[k] for k in data if k != 'time'}) actual = data.unselect('time') self.assertEqual(expected, actual) expected = Dataset({k: data[k] for k in ['dim2', 'dim3', 'time']}) actual = data.unselect('dim1') self.assertEqual(expected, actual) with self.assertRaisesRegexp(ValueError, 'does not exist in this'): data.unselect('not_found_here')
def test_concat_errors(self): data = create_test_data() split_data = [data.isel(dim1=slice(3)), data.isel(dim1=slice(3, None))] with self.assertRaisesRegexp(ValueError, 'must supply at least one'): concat([], 'dim1') with self.assertRaisesRegexp(ValueError, 'are not coordinates'): concat([data, data], 'new_dim', coords=['not_found']) with self.assertRaisesRegexp(ValueError, 'global attributes not'): data0, data1 = deepcopy(split_data) data1.attrs['foo'] = 'bar' concat([data0, data1], 'dim1', compat='identical') self.assertDatasetIdentical( data, concat([data0, data1], 'dim1', compat='equals')) with self.assertRaisesRegexp(ValueError, 'encountered unexpected'): data0, data1 = deepcopy(split_data) data1['foo'] = ('bar', np.random.randn(10)) concat([data0, data1], 'dim1') with self.assertRaisesRegexp(ValueError, 'not equal across datasets'): data0, data1 = deepcopy(split_data) data1['dim2'] = 2 * data1['dim2'] concat([data0, data1], 'dim1', coords='minimal') with self.assertRaisesRegexp(ValueError, 'it is not 1-dimensional'): concat([data0, data1], 'dim1') with self.assertRaisesRegexp(ValueError, 'compat.* invalid'): concat(split_data, 'dim1', compat='foobar') with self.assertRaisesRegexp(ValueError, 'unexpected value for'): concat([data, data], 'new_dim', coords='foobar') with self.assertRaisesRegexp( ValueError, 'coordinate in some datasets but not others'): concat([Dataset({'x': 0}), Dataset({'x': [1]})], dim='z') with self.assertRaisesRegexp( ValueError, 'coordinate in some datasets but not others'): concat([Dataset({'x': 0}), Dataset({}, {'x': 1})], dim='z') with self.assertRaisesRegexp(ValueError, 'no longer a valid'): concat([data, data], 'new_dim', mode='different') with self.assertRaisesRegexp(ValueError, 'no longer a valid'): concat([data, data], 'new_dim', concat_over='different')
def test_repr(self): data = create_test_data() expected = dedent(""" <xray.Dataset> Dimensions: (dim1: 100, dim2: 50, dim3: 10, time: 20) Coordinates: dim1 X dim2 X dim3 X time X Noncoordinates: var1 0 1 var2 0 1 var3 1 0 Attributes: Empty """).strip() actual = '\n'.join(x.rstrip() for x in repr(data).split('\n')) self.assertEqual(expected, actual) expected = dedent(""" <xray.Dataset> Dimensions: () Coordinates: None Noncoordinates: None Attributes: Empty """).strip() actual = '\n'.join(x.rstrip() for x in repr(Dataset()).split('\n')) self.assertEqual(expected, actual)
def rectify_dim_order(dataset): # return a new dataset with all variable dimensions tranposed into # the order in which they are found in `data` return Dataset(dict((k, v.transpose(*data[k].dims)) for k, v in iteritems(dataset.data_vars)), dataset.coords, attrs=dataset.attrs)
def test_groupby(self): data = Dataset({ 'x': ('x', list('abc')), 'c': ('x', [0, 1, 0]), 'z': (['x', 'y'], np.random.randn(3, 5)) }) groupby = data.groupby('x') self.assertEqual(len(groupby), 3) expected_groups = {'a': 0, 'b': 1, 'c': 2} self.assertEqual(groupby.groups, expected_groups) expected_items = [('a', data.indexed(x=0)), ('b', data.indexed(x=1)), ('c', data.indexed(x=2))] self.assertEqual(list(groupby), expected_items) identity = lambda x: x for k in ['x', 'c', 'y']: actual = data.groupby(k, squeeze=False).apply(identity) self.assertEqual(data, actual) data = create_test_data() for n, (t, sub) in enumerate(list(data.groupby('dim1'))[:3]): self.assertEqual(data['dim1'][n], t) self.assertVariableEqual(data['var1'][n], sub['var1']) self.assertVariableEqual(data['var2'][n], sub['var2']) self.assertVariableEqual(data['var3'][:, n], sub['var3']) # TODO: test the other edge cases with self.assertRaisesRegexp(ValueError, 'must be 1 dimensional'): data.groupby('var1') with self.assertRaisesRegexp(ValueError, 'length does not match'): data.groupby(data['dim1'][:3])
def test_concat(self): ds = Dataset({ 'foo': (['x', 'y'], np.random.random((10, 20))), 'bar': (['x', 'y'], np.random.random((10, 20))) }) foo = ds['foo'] bar = ds['bar'] # from dataset array: expected = DataArray(np.array([foo.values, bar.values]), dims=['w', 'x', 'y']) actual = concat([foo, bar], 'w') self.assertDataArrayEqual(expected, actual) # from iteration: grouped = [g for _, g in foo.groupby('x')] stacked = concat(grouped, ds['x']) self.assertDataArrayIdentical(foo, stacked) # with an index as the 'dim' argument stacked = concat(grouped, ds.indexes['x']) self.assertDataArrayIdentical(foo, stacked) actual = concat([foo[0], foo[1]], pd.Index([0, 1])).reset_coords(drop=True) expected = foo[:2].rename({'x': 'concat_dim'}) self.assertDataArrayIdentical(expected, actual) actual = concat([foo[0], foo[1]], [0, 1]).reset_coords(drop=True) expected = foo[:2].rename({'x': 'concat_dim'}) self.assertDataArrayIdentical(expected, actual) with self.assertRaisesRegexp(ValueError, 'not identical'): concat([foo, bar], dim='w', compat='identical') with self.assertRaisesRegexp(ValueError, 'not a valid argument'): concat([foo, bar], dim='w', data_vars='minimal')
def test_roundtrip_coordinates(self): original = Dataset({'foo': ('x', [0, 1])}, { 'x': [2, 3], 'y': ('a', [42]), 'z': ('x', [4, 5]) }) with self.roundtrip(original) as actual: self.assertDatasetIdentical(original, actual) expected = original.drop('foo') with self.roundtrip(expected) as actual: self.assertDatasetIdentical(expected, actual) expected = original.copy() expected.attrs['coordinates'] = 'something random' with self.assertRaisesRegexp(ValueError, 'cannot serialize'): with self.roundtrip(expected): pass expected = original.copy(deep=True) expected['foo'].attrs['coordinates'] = 'something random' with self.assertRaisesRegexp(ValueError, 'cannot serialize'): with self.roundtrip(expected): pass
def test_open_and_do_math(self): original = Dataset({'foo': ('x', np.random.randn(10))}) with create_tmp_file() as tmp: original.to_netcdf(tmp) with open_mfdataset(tmp) as ds: actual = 1.0 * ds self.assertDatasetAllClose(original, actual)
def test_read_byte_attrs_as_unicode(self): with create_tmp_file() as tmp_file: with nc4.Dataset(tmp_file, 'w') as nc: nc.foo = b'bar' actual = open_dataset(tmp_file) expected = Dataset(attrs={'foo': 'bar'}) self.assertDatasetIdentical(expected, actual)
def create_encoded_masked_and_scaled_data(): attributes = { '_FillValue': -1, 'add_offset': 10, 'scale_factor': np.float32(0.1) } return Dataset({'x': ('t', [-1, -1, 0, 1, 2], attributes)})
def test_open_encodings(self): # Create a netCDF file with explicit time units # and make sure it makes it into the encodings # and survives a round trip with create_tmp_file() as tmp_file: with nc4.Dataset(tmp_file, 'w') as ds: ds.createDimension('time', size=10) ds.createVariable('time', np.int32, dimensions=('time', )) units = 'days since 1999-01-01' ds.variables['time'].setncattr('units', units) ds.variables['time'][:] = np.arange(10) + 4 expected = Dataset() time = pd.date_range('1999-01-05', periods=10) encoding = {'units': units, 'dtype': np.dtype('int32')} expected['time'] = ('time', time, {}, encoding) with open_dataset(tmp_file) as actual: self.assertVariableEqual(actual['time'], expected['time']) actual_encoding = dict( (k, v) for k, v in iteritems(actual['time'].encoding) if k in expected['time'].encoding) self.assertDictEqual(actual_encoding, expected['time'].encoding)
def test_as_variable(self): data = np.arange(10) expected = Variable('x', data) self.assertVariableIdentical(expected, as_variable(expected)) ds = Dataset({'x': expected}) self.assertVariableIdentical(expected, as_variable(ds['x'])) self.assertNotIsInstance(ds['x'], Variable) self.assertIsInstance(as_variable(ds['x']), Variable) self.assertIsInstance(as_variable(ds['x'], strict=False), DataArray) FakeVariable = namedtuple('FakeVariable', 'values dims') fake_xarray = FakeVariable(expected.values, expected.dims) self.assertVariableIdentical(expected, as_variable(fake_xarray)) xarray_tuple = (expected.dims, expected.values) self.assertVariableIdentical(expected, as_variable(xarray_tuple)) with self.assertRaisesRegexp(TypeError, 'cannot convert arg'): as_variable(tuple(data)) with self.assertRaisesRegexp(TypeError, 'cannot infer .+ dimensions'): as_variable(data) actual = as_variable(data, key='x') self.assertVariableIdentical(expected, actual) actual = as_variable(0) expected = Variable([], 0) self.assertVariableIdentical(expected, actual)
def setUp(self): self.attrs = {'attr1': 'value1', 'attr2': 2929} self.x = np.random.random((10, 20)) self.v = Variable(['x', 'y'], self.x) self.va = Variable(['x', 'y'], self.x, self.attrs) self.ds = Dataset({'foo': self.v}) self.dv = self.ds['foo']
def test_remap_label_indexers(self): # TODO: fill in more tests! data = Dataset({'x': ('x', [1, 2, 3])}) test_indexer = lambda x: indexing.remap_label_indexers(data, {'x': x}) self.assertEqual({'x': 0}, test_indexer(1)) self.assertEqual({'x': 0}, test_indexer(np.int32(1))) self.assertEqual({'x': 0}, test_indexer(Variable([], 1)))
def test_coordinates_encoding(self): def equals_latlon(obj): return obj == 'lat lon' or obj == 'lon lat' original = Dataset({ 'temp': ('x', [0, 1]), 'precip': ('x', [0, -1]) }, { 'lat': ('x', [2, 3]), 'lon': ('x', [4, 5]) }) with self.roundtrip(original) as actual: self.assertDatasetIdentical(actual, original) with create_tmp_file() as tmp_file: original.to_netcdf(tmp_file) with open_dataset(tmp_file, decode_coords=False) as ds: self.assertTrue(equals_latlon(ds['temp'].attrs['coordinates'])) self.assertTrue( equals_latlon(ds['precip'].attrs['coordinates'])) self.assertNotIn('coordinates', ds.attrs) self.assertNotIn('coordinates', ds['lat'].attrs) self.assertNotIn('coordinates', ds['lon'].attrs) modified = original.drop(['temp', 'precip']) with self.roundtrip(modified) as actual: self.assertDatasetIdentical(actual, modified) with create_tmp_file() as tmp_file: modified.to_netcdf(tmp_file) with open_dataset(tmp_file, decode_coords=False) as ds: self.assertTrue(equals_latlon(ds.attrs['coordinates'])) self.assertNotIn('coordinates', ds['lat'].attrs) self.assertNotIn('coordinates', ds['lon'].attrs)
def test_roundtrip_object_dtype(self): floats = np.array([0.0, 0.0, 1.0, 2.0, 3.0], dtype=object) floats_nans = np.array([np.nan, np.nan, 1.0, 2.0, 3.0], dtype=object) letters = np.array(['ab', 'cdef', 'g'], dtype=object) letters_nans = np.array(['ab', 'cdef', np.nan], dtype=object) all_nans = np.array([np.nan, np.nan], dtype=object) original = Dataset({ 'floats': ('a', floats), 'floats_nans': ('a', floats_nans), 'letters': ('b', letters), 'letters_nans': ('b', letters_nans), 'all_nans': ('c', all_nans), 'nan': ([], np.nan) }) expected = original.copy(deep=True) if isinstance(self, Only32BitTypes): # for netCDF3 tests, expect the results to come back as characters expected['letters_nans'] = expected['letters_nans'].astype('S') expected['letters'] = expected['letters'].astype('S') with self.roundtrip(original) as actual: try: self.assertDatasetIdentical(expected, actual) except AssertionError: # Most stores use '' for nans in strings, but some don't # first try the ideal case (where the store returns exactly) # the original Dataset), then try a more realistic case. # ScipyDataTest, NetCDF3ViaNetCDF4DataTest and NetCDF4DataTest # all end up using this case. expected['letters_nans'][-1] = '' self.assertDatasetIdentical(expected, actual)