From d4a4283ffff0732ab7916c5f421abf82dc692e44 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sat, 13 Jan 2018 16:11:01 -0500 Subject: [PATCH 01/41] deprecation notes in testing functions --- xarray/tests/__init__.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/xarray/tests/__init__.py b/xarray/tests/__init__.py index 7fdfca4ee26..7a87dbc859f 100644 --- a/xarray/tests/__init__.py +++ b/xarray/tests/__init__.py @@ -45,6 +45,12 @@ def _importorskip(modname, minversion=None): + """ + This is deprecated - instead use pytest.importorskip. For example: + + matplotlib = pytest.importorskip('matplotlib', minversion='1.9.0') + + """ try: mod = importlib.import_module(modname) has = True @@ -109,6 +115,9 @@ def _importorskip(modname, minversion=None): class TestCase(unittest.TestCase): + """ + These functions are all deprecated. Instead, use functions in xr.testing + """ if PY3: # Python 3 assertCountEqual is roughly equivalent to Python 2 # assertItemsEqual From 0695dab3939b3186588dfa6dd53f509d0222d366 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sat, 13 Jan 2018 16:11:22 -0500 Subject: [PATCH 02/41] gitignore additions --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 68f548bd011..f53aca4faed 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,7 @@ pip-log.txt .tox nosetests.xml .cache +.ropeproject/ # Translations *.mo @@ -51,4 +52,7 @@ doc/_build doc/generated xarray/version.py +# Sync tools +Icon* + .ipynb_checkpoints From 1ef5f17f184d16386bc3a0cae14a915e8b8f29ee Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sat, 13 Jan 2018 23:30:09 -0500 Subject: [PATCH 03/41] self_assert from unittest2pytest --- xarray/tests/test_accessors.py | 8 +- xarray/tests/test_backends.py | 179 +++++----- xarray/tests/test_coding_times.py | 37 +- xarray/tests/test_combine.py | 4 +- xarray/tests/test_conventions.py | 30 +- xarray/tests/test_dask.py | 102 +++--- xarray/tests/test_dataarray.py | 284 ++++++++------- xarray/tests/test_dataset.py | 333 +++++++++--------- xarray/tests/test_duck_array_ops.py | 2 +- xarray/tests/test_extensions.py | 3 +- xarray/tests/test_formatting.py | 40 +-- xarray/tests/test_indexing.py | 16 +- xarray/tests/test_plot.py | 513 ++++++++++++++-------------- xarray/tests/test_utils.py | 91 +++-- xarray/tests/test_variable.py | 342 ++++++++++--------- 15 files changed, 997 insertions(+), 987 deletions(-) diff --git a/xarray/tests/test_accessors.py b/xarray/tests/test_accessors.py index 5233dea81a7..4206b91db8c 100644 --- a/xarray/tests/test_accessors.py +++ b/xarray/tests/test_accessors.py @@ -75,10 +75,10 @@ def test_dask_field_access(self): # Double check that outcome chunksize is unchanged dask_chunks = dask_times_2d.chunks - self.assertEqual(dask_year.data.chunks, dask_chunks) - self.assertEqual(dask_month.data.chunks, dask_chunks) - self.assertEqual(dask_day.data.chunks, dask_chunks) - self.assertEqual(dask_hour.data.chunks, dask_chunks) + assert dask_year.data.chunks == dask_chunks + assert dask_month.data.chunks == dask_chunks + assert dask_day.data.chunks == dask_chunks + assert dask_hour.data.chunks == dask_chunks # Check the actual output from the accessors self.assertDataArrayEqual(years, dask_year.compute()) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 375107a1943..d50197d346f 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -117,11 +117,11 @@ def __getitem__(self, key): array = UnreliableArray([0]) with pytest.raises(UnreliableArrayFailure): array[0] - self.assertEqual(array[0], 0) + assert array[0] == 0 actual = robust_getitem(array, 0, catch=UnreliableArrayFailure, initial_delay=0) - self.assertEqual(actual, 0) + assert actual == 0 class NetCDF3Only(object): @@ -213,11 +213,11 @@ def assert_loads(vars=None): with self.roundtrip(expected) as actual: for k, v in actual.variables.items(): # IndexVariables are eagerly loaded into memory - self.assertEqual(v._in_memory, k in actual.dims) + assert v._in_memory == (k in actual.dims) yield actual for k, v in actual.variables.items(): if k in vars: - self.assertTrue(v._in_memory) + assert v._in_memory self.assertDatasetIdentical(expected, actual) with pytest.raises(AssertionError): @@ -243,14 +243,14 @@ def test_dataset_compute(self): # Test Dataset.compute() for k, v in actual.variables.items(): # IndexVariables are eagerly cached - self.assertEqual(v._in_memory, k in actual.dims) + assert v._in_memory == (k in actual.dims) computed = actual.compute() for k, v in actual.variables.items(): - self.assertEqual(v._in_memory, k in actual.dims) + assert v._in_memory == (k in actual.dims) for v in computed.variables.values(): - self.assertTrue(v._in_memory) + assert v._in_memory self.assertDatasetIdentical(expected, actual) self.assertDatasetIdentical(expected, computed) @@ -334,12 +334,12 @@ def test_roundtrip_string_encoded_characters(self): expected['x'].encoding['dtype'] = 'S1' with self.roundtrip(expected) as actual: self.assertDatasetIdentical(expected, actual) - self.assertEqual(actual['x'].encoding['_Encoding'], 'utf-8') + assert actual['x'].encoding['_Encoding'] == 'utf-8' expected['x'].encoding['_Encoding'] = 'ascii' with self.roundtrip(expected) as actual: self.assertDatasetIdentical(expected, actual) - self.assertEqual(actual['x'].encoding['_Encoding'], 'ascii') + assert actual['x'].encoding['_Encoding'] == 'ascii' def test_roundtrip_datetime_data(self): times = pd.to_datetime(['2000-01-01', '2000-01-02', 'NaT']) @@ -391,10 +391,10 @@ def test_roundtrip_coordinates_with_space(self): def test_roundtrip_boolean_dtype(self): original = create_boolean_data() - self.assertEqual(original['x'].dtype, 'bool') + assert original['x'].dtype == 'bool' with self.roundtrip(original) as actual: self.assertDatasetIdentical(original, actual) - self.assertEqual(actual['x'].dtype, 'bool') + assert actual['x'].dtype == 'bool' def test_orthogonal_indexing(self): in_memory = create_test_data() @@ -532,20 +532,20 @@ def test_unsigned_roundtrip_mask_and_scale(self): encoded = create_encoded_unsigned_masked_scaled_data() with self.roundtrip(decoded) as actual: for k in decoded.variables: - self.assertEqual(decoded.variables[k].dtype, - actual.variables[k].dtype) + assert decoded.variables[k].dtype == \ + actual.variables[k].dtype self.assertDatasetAllClose(decoded, actual, decode_bytes=False) with self.roundtrip(decoded, open_kwargs=dict(decode_cf=False)) as actual: for k in encoded.variables: - self.assertEqual(encoded.variables[k].dtype, - actual.variables[k].dtype) + assert encoded.variables[k].dtype == \ + actual.variables[k].dtype self.assertDatasetAllClose(encoded, actual, decode_bytes=False) with self.roundtrip(encoded, open_kwargs=dict(decode_cf=False)) as actual: for k in encoded.variables: - self.assertEqual(encoded.variables[k].dtype, - actual.variables[k].dtype) + assert encoded.variables[k].dtype == \ + actual.variables[k].dtype self.assertDatasetAllClose(encoded, actual, decode_bytes=False) # make sure roundtrip encoding didn't change the # original dataset. @@ -553,14 +553,14 @@ def test_unsigned_roundtrip_mask_and_scale(self): encoded, create_encoded_unsigned_masked_scaled_data()) with self.roundtrip(encoded) as actual: for k in decoded.variables: - self.assertEqual(decoded.variables[k].dtype, - actual.variables[k].dtype) + assert decoded.variables[k].dtype == \ + actual.variables[k].dtype self.assertDatasetAllClose(decoded, actual, decode_bytes=False) with self.roundtrip(encoded, open_kwargs=dict(decode_cf=False)) as actual: for k in encoded.variables: - self.assertEqual(encoded.variables[k].dtype, - actual.variables[k].dtype) + assert encoded.variables[k].dtype == \ + actual.variables[k].dtype self.assertDatasetAllClose(encoded, actual, decode_bytes=False) def test_roundtrip_mask_and_scale(self): @@ -598,12 +598,11 @@ def equals_latlon(obj): 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) + assert equals_latlon(ds['temp'].attrs['coordinates']) + assert equals_latlon(ds['precip'].attrs['coordinates']) + assert 'coordinates' not in ds.attrs + assert 'coordinates' not in ds['lat'].attrs + assert 'coordinates' not in ds['lon'].attrs modified = original.drop(['temp', 'precip']) with self.roundtrip(modified) as actual: @@ -611,9 +610,9 @@ def equals_latlon(obj): 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) + assert equals_latlon(ds.attrs['coordinates']) + assert 'coordinates' not in ds['lat'].attrs + assert 'coordinates' not in ds['lon'].attrs def test_roundtrip_endian(self): ds = Dataset({'x': np.arange(3, 10, dtype='>i2'), @@ -649,8 +648,8 @@ 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, {}) + assert actual.x.encoding['dtype'] == 'f4' + assert ds.x.encoding == {} kwargs = dict(encoding={'x': {'foo': 'bar'}}) with raises_regex(ValueError, 'unexpected encoding'): @@ -671,7 +670,7 @@ def test_encoding_kwarg(self): 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) + assert actual.t.encoding['units'] == units self.assertDatasetIdentical(actual, ds) def test_default_fill_value(self): @@ -679,22 +678,22 @@ def test_default_fill_value(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['_FillValue'], - np.nan) - self.assertEqual(ds.x.encoding, {}) + assert actual.x.encoding['_FillValue'] == \ + np.nan + assert ds.x.encoding == {} # Test default encoding for int: ds = Dataset({'x': ('y', np.arange(10.0))}) kwargs = dict(encoding={'x': {'dtype': 'int16'}}) with self.roundtrip(ds, save_kwargs=kwargs) as actual: - self.assertTrue('_FillValue' not in actual.x.encoding) - self.assertEqual(ds.x.encoding, {}) + assert '_FillValue' not in actual.x.encoding + assert ds.x.encoding == {} # Test default encoding for implicit int: ds = Dataset({'x': ('y', np.arange(10, dtype='int16'))}) with self.roundtrip(ds) as actual: - self.assertTrue('_FillValue' not in actual.x.encoding) - self.assertEqual(ds.x.encoding, {}) + assert '_FillValue' not in actual.x.encoding + assert ds.x.encoding == {} def test_explicitly_omit_fill_value(self): ds = Dataset({'x': ('y', [np.pi, -np.pi])}) @@ -706,8 +705,8 @@ def test_encoding_same_dtype(self): ds = Dataset({'x': ('y', np.arange(10.0, dtype='f4'))}) 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, {}) + assert actual.x.encoding['dtype'] == 'f4' + assert ds.x.encoding == {} def test_append_write(self): # regression for GH1215 @@ -865,7 +864,7 @@ def test_default_to_char_arrays(self): data = Dataset({'x': np.array(['foo', 'zzzz'], dtype='S')}) with self.roundtrip(data) as actual: self.assertDatasetIdentical(data, actual) - self.assertEqual(actual['x'].dtype, np.dtype('S4')) + assert actual['x'].dtype == np.dtype('S4') def test_open_encodings(self): # Create a netCDF file with explicit time units @@ -890,15 +889,14 @@ def test_open_encodings(self): 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) + assert actual_encoding == expected['time'].encoding def test_dump_encodings(self): # regression test for #709 ds = Dataset({'x': ('y', np.arange(10.0))}) kwargs = dict(encoding={'x': {'zlib': True}}) with self.roundtrip(ds, save_kwargs=kwargs) as actual: - self.assertTrue(actual.x.encoding['zlib']) + assert actual.x.encoding['zlib'] def test_dump_and_open_encodings(self): # Create a netCDF file with explicit time units @@ -916,10 +914,9 @@ def test_dump_and_open_encodings(self): with create_tmp_file() as tmp_file2: xarray_dataset.to_netcdf(tmp_file2) with nc4.Dataset(tmp_file2, 'r') as ds: - self.assertEqual( - ds.variables['time'].getncattr('units'), units) - self.assertArrayEqual( - ds.variables['time'], np.arange(10) + 4) + assert ds.variables['time'].getncattr('units') == units + self.assertArrayEqual(ds.variables['time'], + np.arange(10) + 4) def test_compression_encoding(self): data = create_test_data() @@ -930,7 +927,7 @@ def test_compression_encoding(self): 'original_shape': data.var2.shape}) with self.roundtrip(data) as actual: for k, v in iteritems(data['var2'].encoding): - self.assertEqual(v, actual['var2'].encoding[k]) + assert v == actual['var2'].encoding[k] # regression test for #156 expected = data.isel(dim1=0) @@ -1034,7 +1031,7 @@ def test_variable_order(self): ds.coords['c'] = 4 with self.roundtrip(ds) as actual: - self.assertEqual(list(ds.variables), list(actual.variables)) + assert list(ds.variables) == list(actual.variables) def test_unsorted_index_raises(self): # should be fixed in netcdf4 v1.2.1 @@ -1053,7 +1050,7 @@ def test_unsorted_index_raises(self): try: ds2.randovar.values except IndexError as err: - self.assertIn('first by calling .load', str(err)) + assert 'first by calling .load' in str(err) class NetCDF4DataStoreAutocloseTrue(NetCDF4DataTest): @@ -1112,17 +1109,17 @@ def test_auto_chunk(self): original, open_kwargs={'auto_chunk': False}) as actual: for k, v in actual.variables.items(): # only index variables should be in memory - self.assertEqual(v._in_memory, k in actual.dims) + assert v._in_memory == (k in actual.dims) # there should be no chunks - self.assertEqual(v.chunks, None) + assert v.chunks == None with self.roundtrip( original, open_kwargs={'auto_chunk': True}) as actual: for k, v in actual.variables.items(): # only index variables should be in memory - self.assertEqual(v._in_memory, k in actual.dims) + assert v._in_memory == (k in actual.dims) # chunk size should be the same as original - self.assertEqual(v.chunks, original[k].chunks) + assert v.chunks == original[k].chunks def test_chunk_encoding(self): # These datasets have no dask chunks. All chunking specified in @@ -1132,7 +1129,7 @@ def test_chunk_encoding(self): data['var2'].encoding.update({'chunks': chunks}) with self.roundtrip(data) as actual: - self.assertEqual(chunks, actual['var2'].encoding['chunks']) + assert chunks == actual['var2'].encoding['chunks'] # expect an error with non-integer chunks data['var2'].encoding.update({'chunks': (5, 4.5)}) @@ -1149,7 +1146,7 @@ def test_chunk_encoding_with_dask(self): # zarr automatically gets chunk information from dask chunks ds_chunk4 = ds.chunk({'x': 4}) with self.roundtrip(ds_chunk4) as actual: - self.assertEqual((4,), actual['var1'].encoding['chunks']) + assert (4,) == actual['var1'].encoding['chunks'] # should fail if dask_chunks are irregular... ds_chunk_irreg = ds.chunk({'x': (5, 4, 3)}) @@ -1162,14 +1159,14 @@ def test_chunk_encoding_with_dask(self): # ... except if the last chunk is smaller than the first ds_chunk_irreg = ds.chunk({'x': (5, 5, 2)}) with self.roundtrip(ds_chunk_irreg) as actual: - self.assertEqual((5,), actual['var1'].encoding['chunks']) + assert (5,) == actual['var1'].encoding['chunks'] # - encoding specified - # specify compatible encodings for chunk_enc in 4, (4, ): ds_chunk4['var1'].encoding.update({'chunks': chunk_enc}) with self.roundtrip(ds_chunk4) as actual: - self.assertEqual((4,), actual['var1'].encoding['chunks']) + assert (4,) == actual['var1'].encoding['chunks'] # specify incompatible encoding ds_chunk4['var1'].encoding.update({'chunks': (5, 5)}) @@ -1410,7 +1407,7 @@ def test_netcdf3_endianness(self): # regression test for GH416 expected = open_example_dataset('bears.nc', engine='scipy') for var in expected.variables.values(): - self.assertTrue(var.dtype.isnative) + assert var.dtype.isnative @requires_netCDF4 def test_nc4_scipy(self): @@ -1516,12 +1513,12 @@ def test_encoding_unlimited_dims(self): ds = Dataset({'x': ('y', np.arange(10.0))}) with self.roundtrip(ds, save_kwargs=dict(unlimited_dims=['y'])) as actual: - self.assertEqual(actual.encoding['unlimited_dims'], set('y')) + assert actual.encoding['unlimited_dims'] == set('y') self.assertDatasetEqual(ds, actual) ds.encoding = {'unlimited_dims': ['y']} with self.roundtrip(ds) as actual: - self.assertEqual(actual.encoding['unlimited_dims'], set('y')) + assert actual.encoding['unlimited_dims'] == set('y') self.assertDatasetEqual(ds, actual) @@ -1579,11 +1576,11 @@ def test_encoding_unlimited_dims(self): ds = Dataset({'x': ('y', np.arange(10.0))}) with self.roundtrip(ds, save_kwargs=dict(unlimited_dims=['y'])) as actual: - self.assertEqual(actual.encoding['unlimited_dims'], set('y')) + assert actual.encoding['unlimited_dims'] == set('y') self.assertDatasetEqual(ds, actual) ds.encoding = {'unlimited_dims': ['y']} with self.roundtrip(ds) as actual: - self.assertEqual(actual.encoding['unlimited_dims'], set('y')) + assert actual.encoding['unlimited_dims'] == set('y') self.assertDatasetEqual(ds, actual) @@ -1749,9 +1746,9 @@ def test_common_coord_when_datavars_all(self): var_shape = ds[self.var_name].shape - self.assertEqual(var_shape, coord_shape) - self.assertNotEqual(coord_shape1, coord_shape) - self.assertNotEqual(coord_shape2, coord_shape) + assert var_shape == coord_shape + assert coord_shape1 != coord_shape + assert coord_shape2 != coord_shape def test_common_coord_when_datavars_minimal(self): opt = 'minimal' @@ -1766,9 +1763,9 @@ def test_common_coord_when_datavars_minimal(self): var_shape = ds[self.var_name].shape - self.assertNotEqual(var_shape, coord_shape) - self.assertEqual(coord_shape1, coord_shape) - self.assertEqual(coord_shape2, coord_shape) + assert var_shape != coord_shape + assert coord_shape1 == coord_shape + assert coord_shape2 == coord_shape def test_invalid_data_vars_value_should_fail(self): @@ -1830,14 +1827,14 @@ def test_open_mfdataset(self): original.isel(x=slice(5, 10)).to_netcdf(tmp2) with open_mfdataset([tmp1, tmp2], autoclose=self.autoclose) as actual: - self.assertIsInstance(actual.foo.variable.data, da.Array) - self.assertEqual(actual.foo.variable.data.chunks, - ((5, 5),)) + assert isinstance(actual.foo.variable.data, da.Array) + assert actual.foo.variable.data.chunks == \ + ((5, 5),) self.assertDatasetIdentical(original, actual) with open_mfdataset([tmp1, tmp2], chunks={'x': 3}, autoclose=self.autoclose) as actual: - self.assertEqual(actual.foo.variable.data.chunks, - ((3, 2, 3, 2),)) + assert actual.foo.variable.data.chunks == \ + ((3, 2, 3, 2),) with raises_regex(IOError, 'no files to open'): open_mfdataset('foo-bar-baz-*.nc', autoclose=self.autoclose) @@ -1868,7 +1865,7 @@ def test_attrs_mfdataset(self): with open_mfdataset([tmp1, tmp2]) as actual: # presumes that attributes inherited from # first dataset loaded - self.assertEqual(actual.test1, ds1.test1) + assert actual.test1 == ds1.test1 # attributes from ds2 are not retained, e.g., with raises_regex(AttributeError, 'no attribute'): @@ -1949,13 +1946,13 @@ def test_open_dataset(self): with create_tmp_file() as tmp: original.to_netcdf(tmp) with open_dataset(tmp, chunks={'x': 5}) as actual: - self.assertIsInstance(actual.foo.variable.data, da.Array) - self.assertEqual(actual.foo.variable.data.chunks, ((5, 5),)) + assert isinstance(actual.foo.variable.data, da.Array) + assert actual.foo.variable.data.chunks == ((5, 5),) self.assertDatasetIdentical(original, actual) with open_dataset(tmp, chunks=5) as actual: self.assertDatasetIdentical(original, actual) with open_dataset(tmp) as actual: - self.assertIsInstance(actual.foo.variable.data, np.ndarray) + assert isinstance(actual.foo.variable.data, np.ndarray) self.assertDatasetIdentical(original, actual) def test_dask_roundtrip(self): @@ -1981,9 +1978,9 @@ def test_deterministic_names(self): repeat_names = dict((k, v.data.name) for k, v in ds.data_vars.items()) for var_name, dask_name in original_names.items(): - self.assertIn(var_name, dask_name) - self.assertEqual(dask_name[:13], 'open_dataset-') - self.assertEqual(original_names, repeat_names) + assert var_name in dask_name + assert dask_name[:13] == 'open_dataset-' + assert original_names == repeat_names def test_dataarray_compute(self): # Test DataArray.compute() on dask backend. @@ -1991,8 +1988,8 @@ def test_dataarray_compute(self): # however dask is the only tested backend which supports DataArrays actual = DataArray([1, 2]).chunk() computed = actual.compute() - self.assertFalse(actual._in_memory) - self.assertTrue(computed._in_memory) + assert not actual._in_memory + assert computed._in_memory self.assertDataArrayAllClose(actual, computed, decode_bytes=False) def test_vectorized_indexing(self): @@ -2037,8 +2034,8 @@ def test_cmp_local_file(self): self.assertDatasetEqual(actual, expected) # global attributes should be global attributes on the dataset - self.assertNotIn('NC_GLOBAL', actual.attrs) - self.assertIn('history', actual.attrs) + assert 'NC_GLOBAL' not in actual.attrs + assert 'history' in actual.attrs # we don't check attributes exactly with assertDatasetIdentical() # because the test DAP server seems to insert some extra @@ -2408,7 +2405,7 @@ def test_chunks(self): with xr.open_rasterio(tmp_file, chunks=(1, 2, 2)) as actual: import dask.array as da - self.assertIsInstance(actual.data, da.Array) + assert isinstance(actual.data, da.Array) assert 'open_rasterio' in actual.data.name # ref @@ -2483,12 +2480,12 @@ def test_extract_nc4_variable_encoding(self): var = xr.Variable(('x',), [1, 2, 3], {}, {'chunking': (2, 1)}) encoding = _extract_nc4_variable_encoding(var) - self.assertEqual({}, encoding) + assert {} == encoding # regression test var = xr.Variable(('x',), [1, 2, 3], {}, {'shuffle': True}) encoding = _extract_nc4_variable_encoding(var, raise_on_invalid=True) - self.assertEqual({'shuffle': True}, encoding) + assert {'shuffle': True} == encoding def test_extract_h5nc_encoding(self): # not supported with h5netcdf (yet) diff --git a/xarray/tests/test_coding_times.py b/xarray/tests/test_coding_times.py index 6046e280136..5a7dda60922 100644 --- a/xarray/tests/test_coding_times.py +++ b/xarray/tests/test_coding_times.py @@ -9,6 +9,7 @@ from xarray import Variable, coding from . import TestCase, requires_netCDF4 +import pytest @np.vectorize @@ -99,7 +100,7 @@ def test_decode_cf_datetime_overflow(self): for i, day in enumerate(days): result = coding.times.decode_cf_datetime(day, units) - self.assertEqual(result, expected[i]) + assert result == expected[i] def test_decode_cf_datetime_non_standard_units(self): expected = pd.date_range(periods=100, start='1970-01-01', freq='h') @@ -137,12 +138,12 @@ def test_decode_non_standard_calendar(self): warnings.filterwarnings('ignore', 'Unable to decode time axis') actual = coding.times.decode_cf_datetime(noleap_time, units, calendar=calendar) - self.assertEqual(actual.dtype, np.dtype('M8[ns]')) + assert actual.dtype == np.dtype('M8[ns]') abs_diff = abs(actual - expected) # once we no longer support versions of netCDF4 older than 1.1.5, # we could do this check with near microsecond accuracy: # https://github.com/Unidata/netcdf4-python/issues/355 - self.assertTrue((abs_diff <= np.timedelta64(1, 's')).all()) + assert (abs_diff <= np.timedelta64(1, 's')).all() @requires_netCDF4 def test_decode_non_standard_calendar_single_element(self): @@ -155,7 +156,7 @@ def test_decode_non_standard_calendar_single_element(self): 'Unable to decode time axis') actual = coding.times.decode_cf_datetime(num_time, units, calendar=calendar) - self.assertEqual(actual.dtype, np.dtype('M8[ns]')) + assert actual.dtype == np.dtype('M8[ns]') @requires_netCDF4 def test_decode_non_standard_calendar_single_element_fallback(self): @@ -165,13 +166,13 @@ def test_decode_non_standard_calendar_single_element_fallback(self): dt = nc4.netcdftime.datetime(2001, 2, 29) for calendar in ['360_day', 'all_leap', '366_day']: num_time = nc4.date2num(dt, units, calendar) - with self.assertWarns('Unable to decode time axis'): + with pytest.warns('Unable to decode time axis'): actual = coding.times.decode_cf_datetime(num_time, units, calendar=calendar) expected = np.asarray(nc4.num2date(num_time, units, calendar)) print(num_time, calendar, actual, expected) - self.assertEqual(actual.dtype, np.dtype('O')) - self.assertEqual(expected, actual) + assert actual.dtype == np.dtype('O') + assert expected == actual @requires_netCDF4 def test_decode_non_standard_calendar_multidim_time(self): @@ -195,7 +196,7 @@ def test_decode_non_standard_calendar_multidim_time(self): warnings.filterwarnings('ignore', 'Unable to decode time axis') actual = coding.times.decode_cf_datetime(mdim_time, units, calendar=calendar) - self.assertEqual(actual.dtype, np.dtype('M8[ns]')) + assert actual.dtype == np.dtype('M8[ns]') self.assertArrayEqual(actual[:, 0], expected1) self.assertArrayEqual(actual[:, 1], expected2) @@ -214,11 +215,11 @@ def test_decode_non_standard_calendar_fallback(self): warnings.simplefilter('always') actual = coding.times.decode_cf_datetime(num_times, units, calendar=calendar) - self.assertEqual(len(w), 1) - self.assertIn('Unable to decode time axis', - str(w[0].message)) + assert len(w) == 1 + assert 'Unable to decode time axis' in \ + str(w[0].message) - self.assertEqual(actual.dtype, np.dtype('O')) + assert actual.dtype == np.dtype('O') self.assertArrayEqual(actual, expected) @requires_netCDF4 @@ -266,8 +267,7 @@ def test_infer_datetime_units(self): (pd.to_datetime(['NaT']), 'days since 1970-01-01 00:00:00'), ]: - self.assertEqual( - expected, coding.times.infer_datetime_units(dates)) + assert expected == coding.times.infer_datetime_units(dates) def test_cf_timedelta(self): examples = [ @@ -289,13 +289,13 @@ def test_cf_timedelta(self): expected = numbers actual, _ = coding.times.encode_cf_timedelta(timedeltas, units) self.assertArrayEqual(expected, actual) - self.assertEqual(expected.dtype, actual.dtype) + assert expected.dtype == actual.dtype if units is not None: expected = timedeltas actual = coding.times.decode_cf_timedelta(numbers, units) self.assertArrayEqual(expected, actual) - self.assertEqual(expected.dtype, actual.dtype) + assert expected.dtype == actual.dtype expected = np.timedelta64('NaT', 'ns') actual = coding.times.decode_cf_timedelta(np.array(np.nan), 'days') @@ -311,7 +311,7 @@ def test_cf_timedelta_2d(self): actual = coding.times.decode_cf_timedelta(numbers, units) self.assertArrayEqual(expected, actual) - self.assertEqual(expected.dtype, actual.dtype) + assert expected.dtype == actual.dtype def test_infer_timedelta_units(self): for deltas, expected in [ @@ -319,5 +319,4 @@ def test_infer_timedelta_units(self): (pd.to_timedelta(['1h', '1 day 1 hour']), 'hours'), (pd.to_timedelta(['1m', '2m', np.nan]), 'minutes'), (pd.to_timedelta(['1m3s', '1m4s']), 'seconds')]: - self.assertEqual( - expected, coding.times.infer_timedelta_units(deltas)) + assert expected == coding.times.infer_timedelta_units(deltas) diff --git a/xarray/tests/test_combine.py b/xarray/tests/test_combine.py index a72532086da..c6c3854936e 100644 --- a/xarray/tests/test_combine.py +++ b/xarray/tests/test_combine.py @@ -291,8 +291,8 @@ def test_concat_lazy(self): dims=['x', 'y']) for _ in range(2)] # should not raise combined = concat(arrays, dim='z') - self.assertEqual(combined.shape, (2, 3, 3)) - self.assertEqual(combined.dims, ('z', 'x', 'y')) + assert combined.shape == (2, 3, 3) + assert combined.dims == ('z', 'x', 'y') class TestAutoCombine(TestCase): diff --git a/xarray/tests/test_conventions.py b/xarray/tests/test_conventions.py index 1d0beb33028..4ee1e8fb00f 100644 --- a/xarray/tests/test_conventions.py +++ b/xarray/tests/test_conventions.py @@ -29,11 +29,11 @@ def test_wrapper_class(self): array = np.array([[b'a', b'b', b'c'], [b'd', b'e', b'f']], dtype='S') actual = conventions.StackedBytesArray(array) expected = np.array([b'abc', b'def'], dtype='S') - self.assertEqual(actual.dtype, expected.dtype) - self.assertEqual(actual.shape, expected.shape) - self.assertEqual(actual.size, expected.size) - self.assertEqual(actual.ndim, expected.ndim) - self.assertEqual(len(actual), len(expected)) + assert actual.dtype == expected.dtype + assert actual.shape == expected.shape + assert actual.size == expected.size + assert actual.ndim == expected.ndim + assert len(actual) == len(expected) self.assertArrayEqual(expected, actual) self.assertArrayEqual(expected[:1], actual[B[:1]]) with pytest.raises(IndexError): @@ -105,10 +105,10 @@ def test_encoding(self): actual = conventions.BytesToStringArray(raw_array, encoding=encoding) expected = np.array([u'abc', u'ß∂µ∆'], dtype=object) - self.assertEqual(actual.dtype, expected.dtype) - self.assertEqual(actual.shape, expected.shape) - self.assertEqual(actual.size, expected.size) - self.assertEqual(actual.ndim, expected.ndim) + assert actual.dtype == expected.dtype + assert actual.shape == expected.shape + assert actual.size == expected.size + assert actual.ndim == expected.ndim self.assertArrayEqual(expected, actual) self.assertArrayEqual(expected[0], actual[B[0]]) @@ -139,7 +139,7 @@ class TestBoolTypeArray(TestCase): def test_booltype_array(self): x = np.array([1, 0, 1, 1, 0], dtype='i1') bx = conventions.BoolTypeArray(x) - self.assertEqual(bx.dtype, np.bool) + assert bx.dtype == np.bool self.assertArrayEqual(bx, np.array([True, False, True, True, False], dtype=np.bool)) @@ -196,7 +196,7 @@ def test_incompatible_attributes(self): def test_missing_fillvalue(self): v = Variable(['x'], np.array([np.nan, 1, 2, 3])) v.encoding = {'dtype': 'int16'} - with self.assertWarns('floating point data as an integer'): + with pytest.warns('floating point data as an integer'): conventions.encode_cf_variable(v) def test_multidimensional_coordinates(self): @@ -260,7 +260,7 @@ def test_decode_coordinates(self): original = Dataset({'foo': ('t', [1, 2], {'coordinates': 'x'}), 'x': ('t', [4, 5])}) actual = conventions.decode_cf(original) - self.assertEqual(actual.foo.encoding['coordinates'], 'x') + assert actual.foo.encoding['coordinates'] == 'x' def test_0d_int32_encoding(self): original = Variable((), np.int32(0), encoding={'dtype': 'int64'}) @@ -275,7 +275,7 @@ def test_decode_cf_with_multiple_missing_values(self): with warnings.catch_warnings(record=True) as w: actual = conventions.decode_cf_variable('t', original) self.assertDatasetIdentical(expected, actual) - self.assertIn('has multiple fill', str(w[0].message)) + assert 'has multiple fill' in str(w[0].message) def test_decode_cf_with_drop_variables(self): original = Dataset({ @@ -308,11 +308,11 @@ def test_dataset_repr_with_netcdf4_datetimes(self): 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)) + assert '(time) object' in 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)) + assert '(time) datetime64[ns]' in repr(ds) @requires_netCDF4 def test_decode_cf_datetime_transition_to_invalid(self): diff --git a/xarray/tests/test_dask.py b/xarray/tests/test_dask.py index 2e9179d429a..b64a37152f3 100644 --- a/xarray/tests/test_dask.py +++ b/xarray/tests/test_dask.py @@ -30,18 +30,18 @@ def assertLazyAnd(self, expected, actual, test): if isinstance(actual, Dataset): for k, v in actual.variables.items(): if k in actual.dims: - self.assertIsInstance(v.data, np.ndarray) + assert isinstance(v.data, np.ndarray) else: - self.assertIsInstance(v.data, da.Array) + assert isinstance(v.data, da.Array) elif isinstance(actual, DataArray): - self.assertIsInstance(actual.data, da.Array) + assert isinstance(actual.data, da.Array) for k, v in actual.coords.items(): if k in actual.dims: - self.assertIsInstance(v.data, np.ndarray) + assert isinstance(v.data, np.ndarray) else: - self.assertIsInstance(v.data, da.Array) + assert isinstance(v.data, da.Array) elif isinstance(actual, Variable): - self.assertIsInstance(actual.data, da.Array) + assert isinstance(actual.data, da.Array) else: assert False @@ -62,8 +62,8 @@ def setUp(self): def test_basics(self): v = self.lazy_var - self.assertIs(self.data, v.data) - self.assertEqual(self.data.chunks, v.chunks) + assert self.data is v.data + assert self.data.chunks == v.chunks self.assertArrayEqual(self.values, v) def test_copy(self): @@ -78,7 +78,7 @@ def test_chunk(self): ({'x': 3}, ((3, 1), (2, 2, 2))), ({'x': (3, 1)}, ((3, 1), (2, 2, 2)))]: rechunked = self.lazy_var.chunk(chunks) - self.assertEqual(rechunked.chunks, expected) + assert rechunked.chunks == expected self.assertLazyAndIdentical(self.eager_var, rechunked) def test_indexing(self): @@ -97,10 +97,10 @@ def test_squeeze(self): def test_equals(self): v = self.lazy_var - self.assertTrue(v.equals(v)) - self.assertIsInstance(v.data, da.Array) - self.assertTrue(v.identical(v)) - self.assertIsInstance(v.data, da.Array) + assert v.equals(v) + assert isinstance(v.data, da.Array) + assert v.identical(v) + assert isinstance(v.data, da.Array) def test_transpose(self): u = self.eager_var @@ -112,13 +112,13 @@ def test_shift(self): v = self.lazy_var self.assertLazyAndIdentical(u.shift(x=2), v.shift(x=2)) self.assertLazyAndIdentical(u.shift(x=-2), v.shift(x=-2)) - self.assertEqual(v.data.chunks, v.shift(x=1).data.chunks) + assert v.data.chunks == v.shift(x=1).data.chunks def test_roll(self): u = self.eager_var v = self.lazy_var self.assertLazyAndIdentical(u.roll(x=2), v.roll(x=2)) - self.assertEqual(v.data.chunks, v.roll(x=1).data.chunks) + assert v.data.chunks == v.roll(x=1).data.chunks def test_unary_op(self): u = self.eager_var @@ -138,20 +138,20 @@ def test_repr(self): expected = dedent("""\ dask.array""") - self.assertEqual(expected, repr(self.lazy_var)) + assert expected == repr(self.lazy_var) def test_pickle(self): # Test that pickling/unpickling does not convert the dask # backend to numpy a1 = Variable(['x'], build_dask_array('x')) a1.compute() - self.assertFalse(a1._in_memory) + assert not a1._in_memory assert kernel_call_count == 1 a2 = pickle.loads(pickle.dumps(a1)) assert kernel_call_count == 1 self.assertVariableIdentical(a1, a2) - self.assertFalse(a1._in_memory) - self.assertFalse(a2._in_memory) + assert not a1._in_memory + assert not a2._in_memory def test_reduce(self): u = self.eager_var @@ -191,11 +191,11 @@ def test_missing_methods(self): try: v.argsort() except NotImplementedError as err: - self.assertIn('dask', str(err)) + assert 'dask' in str(err) try: v[0].item() except NotImplementedError as err: - self.assertIn('dask', str(err)) + assert 'dask' in str(err) def test_univariate_ufunc(self): u = self.eager_var @@ -257,16 +257,16 @@ def setUp(self): def test_rechunk(self): chunked = self.eager_array.chunk({'x': 2}).chunk({'y': 2}) - self.assertEqual(chunked.chunks, ((2,) * 2, (2,) * 3)) + assert chunked.chunks == ((2,) * 2, (2,) * 3) self.assertLazyAndIdentical(self.lazy_array, chunked) def test_new_chunk(self): chunked = self.eager_array.chunk() - self.assertTrue(chunked.data.name.startswith('xarray-')) + assert chunked.data.name.startswith('xarray-') def test_lazy_dataset(self): lazy_ds = Dataset({'foo': (('x', 'y'), self.data)}) - self.assertIsInstance(lazy_ds.foo.variable.data, da.Array) + assert isinstance(lazy_ds.foo.variable.data, da.Array) def test_lazy_array(self): u = self.eager_array @@ -461,7 +461,7 @@ def counting_get(*args, **kwargs): with dask.set_options(get=counting_get): ds.load() - self.assertEqual(count[0], 1) + assert count[0] == 1 def test_stack(self): data = da.random.normal(size=(2, 3, 4), chunks=(1, 3, 4)) @@ -490,7 +490,7 @@ def test_dataarray_repr(self): Coordinates: y (x) int64 dask.array Dimensions without coordinates: x""") - self.assertEqual(expected, repr(a)) + assert expected == repr(a) assert kernel_call_count == 0 def test_dataset_repr(self): @@ -508,7 +508,7 @@ def test_dataset_repr(self): Dimensions without coordinates: x Data variables: a (x) int64 dask.array""") - self.assertEqual(expected, repr(ds)) + assert expected == repr(ds) assert kernel_call_count == 0 def test_dataarray_pickle(self): @@ -518,16 +518,16 @@ def test_dataarray_pickle(self): nonindex_coord = build_dask_array('coord') a1 = DataArray(data, dims=['x'], coords={'y': ('x', nonindex_coord)}) a1.compute() - self.assertFalse(a1._in_memory) - self.assertFalse(a1.coords['y']._in_memory) + assert not a1._in_memory + assert not a1.coords['y']._in_memory assert kernel_call_count == 2 a2 = pickle.loads(pickle.dumps(a1)) assert kernel_call_count == 2 self.assertDataArrayIdentical(a1, a2) - self.assertFalse(a1._in_memory) - self.assertFalse(a2._in_memory) - self.assertFalse(a1.coords['y']._in_memory) - self.assertFalse(a2.coords['y']._in_memory) + assert not a1._in_memory + assert not a2._in_memory + assert not a1.coords['y']._in_memory + assert not a2.coords['y']._in_memory def test_dataset_pickle(self): # Test that pickling/unpickling converts the dask backend @@ -537,16 +537,16 @@ def test_dataset_pickle(self): ds1 = Dataset(data_vars={'a': ('x', data)}, coords={'y': ('x', nonindex_coord)}) ds1.compute() - self.assertFalse(ds1['a']._in_memory) - self.assertFalse(ds1['y']._in_memory) + assert not ds1['a']._in_memory + assert not ds1['y']._in_memory assert kernel_call_count == 2 ds2 = pickle.loads(pickle.dumps(ds1)) assert kernel_call_count == 2 self.assertDatasetIdentical(ds1, ds2) - self.assertFalse(ds1['a']._in_memory) - self.assertFalse(ds2['a']._in_memory) - self.assertFalse(ds1['y']._in_memory) - self.assertFalse(ds2['y']._in_memory) + assert not ds1['a']._in_memory + assert not ds2['a']._in_memory + assert not ds1['y']._in_memory + assert not ds2['y']._in_memory def test_dataarray_getattr(self): # ipython/jupyter does a long list of getattr() calls to when trying to @@ -575,9 +575,9 @@ def test_values(self): # Test that invoking the values property does not convert the dask # backend to numpy a = DataArray([1, 2]).chunk() - self.assertFalse(a._in_memory) + assert not a._in_memory assert a.values.tolist() == [1, 2] - self.assertFalse(a._in_memory) + assert not a._in_memory def test_from_dask_variable(self): # Test array creation from Variable with dask backend. @@ -607,7 +607,7 @@ def test_to_dask_dataframe(self): expected = dd.from_pandas(expected_pd, chunksize=4) actual = ds.to_dask_dataframe(set_index=True) # test if we have dask dataframes - self.assertIsInstance(actual, dd.DataFrame) + assert isinstance(actual, dd.DataFrame) # use the .equals from pandas to check dataframes are equivalent assert_frame_equal(expected.compute(), actual.compute()) @@ -618,7 +618,7 @@ def test_to_dask_dataframe(self): actual = ds.to_dask_dataframe(set_index=False) - self.assertIsInstance(actual, dd.DataFrame) + assert isinstance(actual, dd.DataFrame) assert_frame_equal(expected.compute(), actual.compute()) def test_to_dask_dataframe_2D(self): @@ -639,7 +639,7 @@ def test_to_dask_dataframe_2D(self): expected = expected.reset_index(drop=False) actual = ds.to_dask_dataframe(set_index=False) - self.assertIsInstance(actual, dd.DataFrame) + assert isinstance(actual, dd.DataFrame) assert_frame_equal(expected, actual.compute()) @pytest.mark.xfail(raises=NotImplementedError) @@ -652,7 +652,7 @@ def test_to_dask_dataframe_2D_set_index(self): expected = ds.compute().to_dataframe() actual = ds.to_dask_dataframe(set_index=True) - self.assertIsInstance(actual, dd.DataFrame) + assert isinstance(actual, dd.DataFrame) assert_frame_equal(expected, actual.compute()) def test_to_dask_dataframe_coordinates(self): @@ -667,7 +667,7 @@ def test_to_dask_dataframe_coordinates(self): index=pd.Index(t, name='t')) expected = dd.from_pandas(expected_pd, chunksize=4) actual = ds.to_dask_dataframe(set_index=True) - self.assertIsInstance(actual, dd.DataFrame) + assert isinstance(actual, dd.DataFrame) assert_frame_equal(expected.compute(), actual.compute()) def test_to_dask_dataframe_not_daskarray(self): @@ -684,7 +684,7 @@ def test_to_dask_dataframe_not_daskarray(self): index=pd.Index(t, name='t')) actual = ds.to_dask_dataframe(set_index=True) - self.assertIsInstance(actual, dd.DataFrame) + assert isinstance(actual, dd.DataFrame) assert_frame_equal(expected, actual.compute()) def test_to_dask_dataframe_no_coordinate(self): @@ -693,12 +693,12 @@ def test_to_dask_dataframe_no_coordinate(self): expected = ds.compute().to_dataframe().reset_index() actual = ds.to_dask_dataframe() - self.assertIsInstance(actual, dd.DataFrame) + assert isinstance(actual, dd.DataFrame) assert_frame_equal(expected, actual.compute()) expected = ds.compute().to_dataframe() actual = ds.to_dask_dataframe(set_index=True) - self.assertIsInstance(actual, dd.DataFrame) + assert isinstance(actual, dd.DataFrame) assert_frame_equal(expected, actual.compute()) def test_to_dask_dataframe_dim_order(self): @@ -707,12 +707,12 @@ def test_to_dask_dataframe_dim_order(self): expected = ds['w'].to_series().reset_index() actual = ds.to_dask_dataframe(dim_order=['x', 'y']) - self.assertIsInstance(actual, dd.DataFrame) + assert isinstance(actual, dd.DataFrame) assert_frame_equal(expected, actual.compute()) expected = ds['w'].T.to_series().reset_index() actual = ds.to_dask_dataframe(dim_order=['y', 'x']) - self.assertIsInstance(actual, dd.DataFrame) + assert isinstance(actual, dd.DataFrame) assert_frame_equal(expected, actual.compute()) with raises_regex(ValueError, 'does not match the set of dimensions'): diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 296873dec45..8428792c8dc 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -50,7 +50,7 @@ def test_repr(self): Dimensions without coordinates: time Attributes: foo: bar""") - self.assertEqual(expected, repr(data_array)) + assert expected == repr(data_array) def test_repr_multiindex(self): expected = dedent("""\ @@ -60,22 +60,22 @@ def test_repr_multiindex(self): * x (x) MultiIndex - level_1 (x) object 'a' 'a' 'b' 'b' - level_2 (x) int64 1 2 1 2""") - self.assertEqual(expected, repr(self.mda)) + assert expected == repr(self.mda) def test_properties(self): self.assertVariableEqual(self.dv.variable, self.v) self.assertArrayEqual(self.dv.values, self.v.values) for attr in ['dims', 'dtype', 'shape', 'size', 'nbytes', 'ndim', 'attrs']: - self.assertEqual(getattr(self.dv, attr), getattr(self.v, attr)) - self.assertEqual(len(self.dv), len(self.v)) + assert getattr(self.dv, attr) == getattr(self.v, attr) + assert len(self.dv) == len(self.v) self.assertVariableEqual(self.dv.variable, self.v) self.assertItemsEqual(list(self.dv.coords), list(self.ds.coords)) for k, v in iteritems(self.dv.coords): self.assertArrayEqual(v, self.ds.coords[k]) with pytest.raises(AttributeError): self.dv.dataset - self.assertIsInstance(self.ds['x'].to_index(), pd.Index) + assert isinstance(self.ds['x'].to_index(), pd.Index) with raises_regex(ValueError, 'must be 1-dimensional'): self.ds['foo'].to_index() with pytest.raises(AttributeError): @@ -174,11 +174,11 @@ def test_struct_array_dims(self): def test_name(self): arr = self.dv - self.assertEqual(arr.name, 'foo') + assert arr.name == 'foo' copied = arr.copy() arr.name = 'bar' - self.assertEqual(arr.name, 'bar') + assert arr.name == 'bar' self.assertDataArrayEqual(copied, arr) actual = DataArray(IndexVariable('x', [3])) @@ -188,15 +188,15 @@ def test_name(self): def test_dims(self): arr = self.dv - self.assertEqual(arr.dims, ('x', 'y')) + assert arr.dims == ('x', 'y') with raises_regex(AttributeError, 'you cannot assign'): arr.dims = ('w', 'z') def test_sizes(self): array = DataArray(np.zeros((3, 4)), dims=['x', 'y']) - self.assertEqual(array.sizes, {'x': 3, 'y': 4}) - self.assertEqual(tuple(array.sizes), array.dims) + assert array.sizes == {'x': 3, 'y': 4} + assert tuple(array.sizes) == array.dims with pytest.raises(TypeError): array.sizes['foo'] = 5 @@ -207,8 +207,8 @@ def test_encoding(self): expected = {'baz': 0} self.dv.encoding = expected - assert expected == self.dv.encoding - self.assertIsNot(expected, self.dv.encoding) + + assert expected is not self.dv.encoding def test_constructor(self): data = np.random.random((2, 3)) @@ -371,63 +371,63 @@ def test_equals_and_identical(self): expected = orig actual = orig.copy() - self.assertTrue(expected.equals(actual)) - self.assertTrue(expected.identical(actual)) + assert expected.equals(actual) + assert expected.identical(actual) actual = expected.rename('baz') - self.assertTrue(expected.equals(actual)) - self.assertFalse(expected.identical(actual)) + assert expected.equals(actual) + assert not expected.identical(actual) actual = expected.rename({'x': 'xxx'}) - self.assertFalse(expected.equals(actual)) - self.assertFalse(expected.identical(actual)) + assert not expected.equals(actual) + assert not expected.identical(actual) actual = expected.copy() actual.attrs['foo'] = 'bar' - self.assertTrue(expected.equals(actual)) - self.assertFalse(expected.identical(actual)) + assert expected.equals(actual) + assert not expected.identical(actual) actual = expected.copy() actual['x'] = ('x', -np.arange(5)) - self.assertFalse(expected.equals(actual)) - self.assertFalse(expected.identical(actual)) + assert not expected.equals(actual) + assert not expected.identical(actual) actual = expected.reset_coords(drop=True) - self.assertFalse(expected.equals(actual)) - self.assertFalse(expected.identical(actual)) + assert not expected.equals(actual) + assert not expected.identical(actual) actual = orig.copy() actual[0] = np.nan expected = actual.copy() - self.assertTrue(expected.equals(actual)) - self.assertTrue(expected.identical(actual)) + assert expected.equals(actual) + assert expected.identical(actual) actual[:] = np.nan - self.assertFalse(expected.equals(actual)) - self.assertFalse(expected.identical(actual)) + assert not expected.equals(actual) + assert not expected.identical(actual) actual = expected.copy() actual['a'] = 100000 - self.assertFalse(expected.equals(actual)) - self.assertFalse(expected.identical(actual)) + assert not expected.equals(actual) + assert not expected.identical(actual) def test_equals_failures(self): orig = DataArray(np.arange(5.0), {'a': 42}, dims='x') - self.assertFalse(orig.equals(np.arange(5))) - self.assertFalse(orig.identical(123)) - self.assertFalse(orig.broadcast_equals({1: 2})) + assert not orig.equals(np.arange(5)) + assert not orig.identical(123) + assert not orig.broadcast_equals({1: 2}) def test_broadcast_equals(self): a = DataArray([0, 0], {'y': 0}, dims='x') b = DataArray([0, 0], {'y': ('x', [0, 0])}, dims='x') - self.assertTrue(a.broadcast_equals(b)) - self.assertTrue(b.broadcast_equals(a)) - self.assertFalse(a.equals(b)) - self.assertFalse(a.identical(b)) + assert a.broadcast_equals(b) + assert b.broadcast_equals(a) + assert not a.equals(b) + assert not a.identical(b) c = DataArray([0], coords={'x': 0}, dims='y') - self.assertFalse(a.broadcast_equals(c)) - self.assertFalse(c.broadcast_equals(a)) + assert not a.broadcast_equals(c) + assert not c.broadcast_equals(a) def test_getitem(self): # strings pull out dataarrays @@ -621,7 +621,7 @@ def test_contains(self): def test_attr_sources_multiindex(self): # make sure attr-style access for multi-index levels # returns DataArray objects - self.assertIsInstance(self.mda.level_1, DataArray) + assert isinstance(self.mda.level_1, DataArray) def test_pickle(self): data = DataArray(np.random.random((3, 3)), dims=('id', 'time')) @@ -631,23 +631,23 @@ def test_pickle(self): @requires_dask def test_chunk(self): unblocked = DataArray(np.ones((3, 4))) - self.assertIsNone(unblocked.chunks) + assert unblocked.chunks is None blocked = unblocked.chunk() - self.assertEqual(blocked.chunks, ((3,), (4,))) + assert blocked.chunks == ((3,), (4,)) blocked = unblocked.chunk(chunks=((2, 1), (2, 2))) - self.assertEqual(blocked.chunks, ((2, 1), (2, 2))) + assert blocked.chunks == ((2, 1), (2, 2)) blocked = unblocked.chunk(chunks=(3, 3)) - self.assertEqual(blocked.chunks, ((3,), (3, 1))) + assert blocked.chunks == ((3,), (3, 1)) - self.assertIsNone(blocked.load().chunks) + assert blocked.load().chunks is None # Check that kwargs are passed import dask.array as da blocked = unblocked.chunk(name_prefix='testname_') - self.assertIsInstance(blocked.data, da.Array) + assert isinstance(blocked.data, da.Array) assert 'testname_' in blocked.data.name def test_isel(self): @@ -906,12 +906,12 @@ def test_loc_assign(self): da = self.ds['foo'] # assignment da.loc['a':'j'] = 0 - self.assertTrue(np.all(da.values == 0)) + assert np.all(da.values == 0) da.loc[{'x': slice('a', 'j')}] = 2 - self.assertTrue(np.all(da.values == 2)) + assert np.all(da.values == 2) da.loc[{'x': slice('a', 'j')}] = 2 - self.assertTrue(np.all(da.values == 2)) + assert np.all(da.values == 2) # Multi dimensional case da = DataArray(np.arange(12).reshape(3, 4), dims=['x', 'y']) @@ -921,7 +921,7 @@ def test_loc_assign(self): da = DataArray(np.arange(12).reshape(3, 4), dims=['x', 'y']) da.loc[0] = 0 - self.assertTrue(np.all(da.values[0] == np.zeros(4))) + assert np.all(da.values[0] == np.zeros(4)) assert da.values[1, 0] != 0 def test_loc_assign_dataarray(self): @@ -964,8 +964,8 @@ def get_data(): def test_loc_single_boolean(self): data = DataArray([0, 1], coords=[[True, False]]) - self.assertEqual(data.loc[True], 0) - self.assertEqual(data.loc[False], 1) + assert data.loc[True] == 0 + assert data.loc[False] == 1 def test_selection_multiindex(self): mindex = pd.MultiIndex.from_product([['a', 'b'], [1, 2], [-1, -2]], @@ -980,7 +980,7 @@ def test_sel(lab_indexer, pos_indexer, replaced_idx=False, self.assertDataArrayIdentical(da, expected_da) else: if renamed_dim: - self.assertEqual(da.dims[0], renamed_dim) + assert da.dims[0] == renamed_dim da = da.rename({renamed_dim: 'x'}) self.assertVariableIdentical(da.variable, expected_da.variable) self.assertVariableNotEqual(da['x'], expected_da['x']) @@ -1030,14 +1030,14 @@ def test_coords(self): assert 2 == len(da.coords) - self.assertEqual(['x', 'y'], list(da.coords)) + assert ['x', 'y'] == list(da.coords) - self.assertTrue(coords[0].identical(da.coords['x'])) - self.assertTrue(coords[1].identical(da.coords['y'])) + assert coords[0].identical(da.coords['x']) + assert coords[1].identical(da.coords['y']) - self.assertIn('x', da.coords) - self.assertNotIn(0, da.coords) - self.assertNotIn('foo', da.coords) + assert 'x' in da.coords + assert 0 not in da.coords + assert 'foo' not in da.coords with pytest.raises(KeyError): da.coords[0] @@ -1227,7 +1227,7 @@ def test_reindex_regressions(self): x = DataArray(x, coords=[[0.1, 0.2, 0.3]]) y = DataArray([2, 5, 6, 7, 8], coords=[[-1.1, 0.21, 0.31, 0.41, 0.51]]) re_dtype = x.reindex_like(y, method='pad').dtype - self.assertEqual(x.dtype, re_dtype) + assert x.dtype == re_dtype def test_reindex_method(self): x = DataArray([10, 20], dims='y', coords={'y': [0, 1]}) @@ -1245,13 +1245,13 @@ def test_rename(self): renamed = self.dv.rename('bar') self.assertDatasetIdentical( renamed.to_dataset(), self.ds.rename({'foo': 'bar'})) - self.assertEqual(renamed.name, 'bar') + assert renamed.name == 'bar' renamed = self.dv.x.rename({'x': 'z'}).rename('z') self.assertDatasetIdentical( renamed, self.ds.rename({'x': 'z'}).z) - self.assertEqual(renamed.name, 'z') - self.assertEqual(renamed.dims, ('z',)) + assert renamed.name == 'z' + assert renamed.dims == ('z',) def test_swap_dims(self): array = DataArray(np.random.randn(3), {'y': ('x', list('abc'))}, 'x') @@ -1323,7 +1323,7 @@ def test_expand_dims(self): attrs={'key': 'entry'}) self.assertDataArrayIdentical(expected, actual) # make sure the attrs are tracked - self.assertTrue(actual.attrs['key'] == 'entry') + assert actual.attrs['key'] == 'entry' roundtripped = actual.squeeze(['z', 'y'], drop=True) self.assertDatasetIdentical(array, roundtripped) @@ -1335,7 +1335,7 @@ def test_expand_dims(self): coords={'x': np.linspace(0.0, 1.0, 3)}, attrs={'key': 'entry'}) self.assertDataArrayIdentical(expected, actual) - self.assertTrue(actual.attrs['key'] == 'entry') + assert actual.attrs['key'] == 'entry' roundtripped = actual.squeeze(['y', 'z'], drop=True) self.assertDatasetIdentical(array, roundtripped) @@ -1483,13 +1483,13 @@ def test_non_overlapping_dataarrays_return_empty_result(self): a = DataArray(range(5), [('x', range(5))]) result = a.isel(x=slice(2)) + a.isel(x=slice(2, None)) - self.assertEqual(len(result['x']), 0) + assert len(result['x']) == 0 def test_empty_dataarrays_return_empty_result(self): a = DataArray(data=[]) result = a * a - self.assertEqual(len(result['dim_0']), 0) + assert len(result['dim_0']) == 0 def test_inplace_math_basics(self): x = self.x @@ -1497,10 +1497,10 @@ def test_inplace_math_basics(self): v = a.variable b = a b += 1 - self.assertIs(b, a) - self.assertIs(b.variable, v) + assert b is a + assert b.variable is v self.assertArrayEqual(b.values, x) - self.assertIs(source_ndarray(b.values), x) + assert source_ndarray(b.values) is x def test_inplace_math_automatic_alignment(self): a = DataArray(range(5), [('x', range(5))]) @@ -1516,14 +1516,14 @@ def test_math_name(self): # the other object has the same name or no name attribute and this # object isn't a coordinate; otherwise reset to None. a = self.dv - self.assertEqual((+a).name, 'foo') - self.assertEqual((a + 0).name, 'foo') - self.assertIs((a + a.rename(None)).name, None) - self.assertIs((a + a.rename('bar')).name, None) - self.assertEqual((a + a).name, 'foo') - self.assertIs((+a['x']).name, 'x') - self.assertIs((a['x'] + 0).name, 'x') - self.assertIs((a + a['x']).name, None) + assert (+a).name == 'foo' + assert (a + 0).name == 'foo' + assert (a + a.rename(None)).name is None + assert (a + a.rename('bar')).name is None + assert (a + a).name == 'foo' + assert (+a['x']).name is 'x' + assert (a['x'] + 0).name is 'x' + assert (a + a['x']).name is None def test_math_with_coords(self): coords = {'x': [-1, -2], 'y': ['ab', 'cd', 'ef'], @@ -1802,13 +1802,13 @@ def test_quantile(self): def test_reduce_keep_attrs(self): # Test dropped attrs vm = self.va.mean() - self.assertEqual(len(vm.attrs), 0) - self.assertEqual(vm.attrs, OrderedDict()) + assert len(vm.attrs) == 0 + assert vm.attrs == OrderedDict() # Test kept attrs vm = self.va.mean(keep_attrs=True) - self.assertEqual(len(vm.attrs), len(self.attrs)) - self.assertEqual(vm.attrs, self.attrs) + assert len(vm.attrs) == len(self.attrs) + assert vm.attrs == self.attrs def test_assign_attrs(self): expected = DataArray([], attrs=dict(a=1, b=2)) @@ -1817,12 +1817,12 @@ def test_assign_attrs(self): new = DataArray([]) actual = DataArray([]).assign_attrs(a=1, b=2) self.assertDatasetIdentical(actual, expected) - self.assertEqual(new.attrs, {}) + assert new.attrs == {} expected.attrs['c'] = 3 new_actual = actual.assign_attrs({'c': 3}) self.assertDatasetIdentical(new_actual, expected) - self.assertEqual(actual.attrs, {'a': 1, 'b': 2}) + assert actual.attrs == {'a': 1, 'b': 2} def test_fillna(self): a = DataArray([np.nan, 1, np.nan, 3], coords={'x': range(4)}, dims='x') @@ -1865,7 +1865,7 @@ def test_fillna(self): def test_groupby_iter(self): for ((act_x, act_dv), (exp_x, exp_ds)) in \ zip(self.dv.groupby('y'), self.ds.groupby('y')): - self.assertEqual(exp_x, act_x) + assert exp_x == act_x self.assertDataArrayIdentical(exp_ds['foo'], act_dv) for ((_, exp_dv), act_dv) in zip(self.dv.groupby('x'), self.dv): self.assertDataArrayIdentical(exp_dv, act_dv) @@ -1882,7 +1882,7 @@ def test_groupby_properties(self): self.assertItemsEqual(expected_groups.keys(), grouped.groups.keys()) for key in expected_groups: self.assertArrayEqual(expected_groups[key], grouped.groups[key]) - self.assertEqual(3, len(grouped)) + assert 3 == len(grouped) def test_groupby_apply_identity(self): expected = self.make_groupby_example_array() @@ -2048,7 +2048,7 @@ def test_groupby_restore_dim_order(self): ('a', ('a', 'y')), ('b', ('x', 'b'))]: result = array.groupby(by).apply(lambda x: x.squeeze()) - self.assertEqual(result.dims, expected_dims) + assert result.dims == expected_dims def test_groupby_first_and_last(self): array = DataArray([1, 2, 3, 4, 5], dims='x') @@ -2109,7 +2109,7 @@ def test_groupby_bins(self): actual = array.groupby_bins('dim_0', bins).apply(lambda x: x.sum()) self.assertDataArrayIdentical(expected, actual) # make sure original array dims are unchanged - self.assertEqual(len(array.dim_0), 4) + assert len(array.dim_0) == 4 def test_groupby_bins_empty(self): array = DataArray(np.arange(4), [('x', range(4))]) @@ -2122,7 +2122,7 @@ def test_groupby_bins_empty(self): self.assertDataArrayIdentical(expected, actual) # make sure original array is unchanged # (was a problem in earlier versions) - self.assertEqual(len(array.x), 4) + assert len(array.x) == 4 def test_groupby_bins_multidim(self): array = self.make_groupby_multidim_example_array() @@ -2250,7 +2250,7 @@ def test_resample_old_vs_new_api(self): old_mean = attr_array.resample('1D', dim='time', how='mean', keep_attrs=True) new_mean = attr_array.resample(time='1D').mean(keep_attrs=True) - self.assertEqual(old_mean.attrs, new_mean.attrs) + assert old_mean.attrs == new_mean.attrs self.assertDatasetIdentical(old_mean, new_mean) # Mean, with NaN to skip @@ -2437,7 +2437,7 @@ def test_align_dtype(self): a = DataArray(np.random.random((30,)).astype(np.float32), [('x', x1)]) b = DataArray(np.random.random((30,)).astype(np.float32), [('x', x2)]) c, d = align(a, b, join='outer') - self.assertEqual(c.dtype, np.float32) + assert c.dtype == np.float32 def test_align_copy(self): x = DataArray([1, 2, 3], coords=[('a', [1, 2, 3])]) @@ -2668,19 +2668,19 @@ def test_to_pandas_name_matches_coordinate(self): series = arr.to_series() self.assertArrayEqual([1, 2, 3], series.values) self.assertArrayEqual([0, 1, 2], series.index.values) - self.assertEqual('x', series.name) - self.assertEqual('x', series.index.name) + assert 'x' == series.name + assert 'x' == series.index.name frame = arr.to_dataframe() expected = series.to_frame() - self.assertTrue(expected.equals(frame)) + assert expected.equals(frame) def test_to_and_from_series(self): expected = self.dv.to_dataframe()['foo'] actual = self.dv.to_series() self.assertArrayEqual(expected.values, actual.values) self.assertArrayEqual(expected.index.values, actual.index.values) - self.assertEqual('foo', actual.name) + assert 'foo' == actual.name # test roundtrip self.assertDataArrayIdentical( self.dv, @@ -2714,7 +2714,7 @@ def test_to_and_from_dict(self): actual = array.to_dict() # check that they are identical - self.assertEqual(expected, actual) + assert expected == actual # check roundtrip self.assertDataArrayIdentical(array, DataArray.from_dict(actual)) @@ -2779,7 +2779,7 @@ def test_to_dict_with_numpy_attrs(self): actual = da.to_dict() # check that they are identical - self.assertEqual(expected_attrs, actual['attrs']) + assert expected_attrs == actual['attrs'] def test_to_masked_array(self): rs = np.random.RandomState(44) @@ -2794,7 +2794,7 @@ def test_to_masked_array(self): self.assertDataArrayEqual(da, da_2) da_masked_array = da.to_masked_array(copy=True) - self.assertTrue(isinstance(da_masked_array, np.ma.MaskedArray)) + assert isinstance(da_masked_array, np.ma.MaskedArray) # Test masks self.assertArrayEqual(da_masked_array.mask, x_masked.mask) # Test that mask is unpacked correctly @@ -2805,23 +2805,23 @@ def test_to_masked_array(self): # Test that copy=False gives access to values masked_array = da.to_masked_array(copy=False) masked_array[0, 0] = 10. - self.assertEqual(masked_array[0, 0], 10.) - self.assertEqual(da[0, 0].values, 10.) - self.assertTrue(masked_array.base is da.values) - self.assertIsInstance(masked_array, np.ma.MaskedArray) + assert masked_array[0, 0] == 10. + assert da[0, 0].values == 10. + assert masked_array.base is da.values + assert isinstance(masked_array, np.ma.MaskedArray) # Test with some odd arrays for v in [4, np.nan, True, '4', 'four']: da = DataArray(v) ma = da.to_masked_array() - self.assertIsInstance(ma, np.ma.MaskedArray) + assert isinstance(ma, np.ma.MaskedArray) # Fix GH issue 684 - masked arrays mask should be an array not a scalar N = 4 v = range(N) da = DataArray(v) ma = da.to_masked_array() - self.assertEqual(len(ma.mask), N) + assert len(ma.mask) == N def test_to_and_from_cdms2(self): pytest.importorskip('cdms2') @@ -2835,16 +2835,16 @@ def test_to_and_from_cdms2(self): IndexVariable('time', [0, 1, 2])] actual = original.to_cdms2() self.assertArrayEqual(actual, original) - self.assertEqual(actual.id, original.name) + assert actual.id == original.name self.assertItemsEqual(actual.getAxisIds(), original.dims) for axis, coord in zip(actual.getAxisList(), expected_coords): - self.assertEqual(axis.id, coord.name) + assert axis.id == coord.name self.assertArrayEqual(axis, coord.values) - self.assertEqual(actual.baz, original.attrs['baz']) + assert actual.baz == original.attrs['baz'] component_times = actual.getAxis(1).asComponentTime() - self.assertEqual(len(component_times), 3) - self.assertEqual(str(component_times[0]), '2000-1-1 0:0:0.0') + assert len(component_times) == 3 + assert str(component_times[0]) == '2000-1-1 0:0:0.0' roundtripped = DataArray.from_cdms2(actual) self.assertDataArrayIdentical(original, roundtripped) @@ -2877,38 +2877,37 @@ def test_to_and_from_iris(self): 'height: mean (comment: A cell method)' actual = original.to_iris() self.assertArrayEqual(actual.data, original.data) - self.assertEqual(actual.var_name, original.name) + assert actual.var_name == original.name self.assertItemsEqual([d.var_name for d in actual.dim_coords], original.dims) - self.assertEqual(actual.cell_methods, + assert actual.cell_methods == \ (iris.coords.CellMethod(method='mean', coords=('height',), intervals=(), comments=('A cell method',)),) - ) for coord, orginal_key in zip((actual.coords()), original.coords): original_coord = original.coords[orginal_key] - self.assertEqual(coord.var_name, original_coord.name) + assert coord.var_name == original_coord.name self.assertArrayEqual( coord.points, CFDatetimeCoder().encode(original_coord).values) - self.assertEqual(actual.coord_dims(coord), - original.get_axis_num - (original.coords[coord.var_name].dims)) + assert actual.coord_dims(coord) == \ + original.get_axis_num \ + (original.coords[coord.var_name].dims) - self.assertEqual(actual.coord('distance2').attributes['foo'], - original.coords['distance2'].attrs['foo']) - self.assertEqual(actual.coord('distance').units, - cf_units.Unit(original.coords['distance'].units)) - self.assertEqual(actual.attributes['baz'], original.attrs['baz']) - self.assertEqual(actual.standard_name, original.attrs['standard_name']) + assert actual.coord('distance2').attributes['foo'] == \ + original.coords['distance2'].attrs['foo'] + assert actual.coord('distance').units == \ + cf_units.Unit(original.coords['distance'].units) + assert actual.attributes['baz'] == original.attrs['baz'] + assert actual.standard_name == original.attrs['standard_name'] roundtripped = DataArray.from_iris(actual) self.assertDataArrayIdentical(original, roundtripped) actual.remove_coord('time') auto_time_dimension = DataArray.from_iris(actual) - self.assertEqual(auto_time_dimension.dims, ('distance', 'dim_1')) + assert auto_time_dimension.dims == ('distance', 'dim_1') actual.coord('distance').var_name = None with raises_regex(ValueError, 'no var_name attribute'): @@ -2950,31 +2949,30 @@ def test_to_and_from_iris_dask(self): actual_data = actual.core_data() if \ hasattr(actual, 'core_data') else actual.data self.assertArrayEqual(actual_data, original.data) - self.assertEqual(actual.var_name, original.name) + assert actual.var_name == original.name self.assertItemsEqual([d.var_name for d in actual.dim_coords], original.dims) - self.assertEqual(actual.cell_methods, + assert actual.cell_methods == \ (iris.coords.CellMethod(method='mean', coords=('height',), intervals=(), comments=('A cell method',)),) - ) for coord, orginal_key in zip((actual.coords()), original.coords): original_coord = original.coords[orginal_key] - self.assertEqual(coord.var_name, original_coord.name) + assert coord.var_name == original_coord.name self.assertArrayEqual( coord.points, CFDatetimeCoder().encode(original_coord).values) - self.assertEqual(actual.coord_dims(coord), - original.get_axis_num - (original.coords[coord.var_name].dims)) + assert actual.coord_dims(coord) == \ + original.get_axis_num \ + (original.coords[coord.var_name].dims) - self.assertEqual(actual.coord('distance2').attributes['foo'], - original.coords['distance2'].attrs['foo']) - self.assertEqual(actual.coord('distance').units, - cf_units.Unit(original.coords['distance'].units)) - self.assertEqual(actual.attributes['baz'], original.attrs['baz']) - self.assertEqual(actual.standard_name, original.attrs['standard_name']) + assert actual.coord('distance2').attributes['foo'] == \ + original.coords['distance2'].attrs['foo'] + assert actual.coord('distance').units == \ + cf_units.Unit(original.coords['distance'].units) + assert actual.attributes['baz'] == original.attrs['baz'] + assert actual.standard_name == original.attrs['standard_name'] roundtripped = DataArray.from_iris(actual) self.assertDataArrayIdentical(original, roundtripped) @@ -2988,7 +2986,7 @@ def test_to_and_from_iris_dask(self): actual.remove_coord('time') auto_time_dimension = DataArray.from_iris(actual) - self.assertEqual(auto_time_dimension.dims, ('distance', 'dim_1')) + assert auto_time_dimension.dims == ('distance', 'dim_1') actual.coord('distance').var_name = None with raises_regex(ValueError, 'no var_name attribute'): @@ -3049,13 +3047,13 @@ def test_to_dataset_retains_keys(self): def test__title_for_slice(self): array = DataArray(np.ones((4, 3, 2)), dims=['a', 'b', 'c'], coords={'a': range(4), 'b': range(3), 'c': range(2)}) - self.assertEqual('', array._title_for_slice()) - self.assertEqual('c = 0', array.isel(c=0)._title_for_slice()) + assert '' == array._title_for_slice() + assert 'c = 0' == array.isel(c=0)._title_for_slice() title = array.isel(b=1, c=0)._title_for_slice() - self.assertTrue('b = 1, c = 0' == title or 'c = 0, b = 1' == title) + assert 'b = 1, c = 0' == title or 'c = 0, b = 1' == title a2 = DataArray(np.ones((4, 1)), dims=['a', 'b']) - self.assertEqual('', a2._title_for_slice()) + assert '' == a2._title_for_slice() def test__title_for_slice_truncate(self): array = DataArray(np.ones((4))) @@ -3065,8 +3063,8 @@ def test__title_for_slice_truncate(self): nchar = 80 title = array._title_for_slice(truncate=nchar) - self.assertEqual(nchar, len(title)) - self.assertTrue(title.endswith('...')) + assert nchar == len(title) + assert title.endswith('...') def test_dataarray_diff_n1(self): da = DataArray(np.random.randn(3, 4), dims=['x', 'y']) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 1a4214e1090..3f1a00438b7 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -106,7 +106,7 @@ def test_repr(self): foo: bar""") % data['dim3'].dtype # noqa: E501 actual = '\n'.join(x.rstrip() for x in repr(data).split('\n')) print(actual) - self.assertEqual(expected, actual) + assert expected == actual with set_options(display_width=100): max_len = max(map(len, repr(data).split('\n'))) @@ -119,7 +119,7 @@ def test_repr(self): *empty*""") actual = '\n'.join(x.rstrip() for x in repr(Dataset()).split('\n')) print(actual) - self.assertEqual(expected, actual) + assert expected == actual # verify that ... doesn't appear for scalar coordinates data = Dataset({'foo': ('x', np.ones(10))}).mean() @@ -130,11 +130,11 @@ def test_repr(self): foo float64 1.0""") actual = '\n'.join(x.rstrip() for x in repr(data).split('\n')) print(actual) - self.assertEqual(expected, actual) + assert expected == actual # verify long attributes are truncated data = Dataset(attrs={'foo': 'bar' * 1000}) - self.assertTrue(len(repr(data)) < 1000) + assert len(repr(data)) < 1000 def test_repr_multiindex(self): data = create_test_multiindex() @@ -149,7 +149,7 @@ def test_repr_multiindex(self): *empty*""") actual = '\n'.join(x.rstrip() for x in repr(data).split('\n')) print(actual) - self.assertEqual(expected, actual) + assert expected == actual # verify that long level names are not truncated mindex = pd.MultiIndex.from_product( @@ -167,7 +167,7 @@ def test_repr_multiindex(self): *empty*""") actual = '\n'.join(x.rstrip() for x in repr(data).split('\n')) print(actual) - self.assertEqual(expected, actual) + assert expected == actual def test_repr_period_index(self): data = create_test_data(seed=456) @@ -192,7 +192,7 @@ def test_unicode_data(self): Attributes: å: ∑""" % u'ba®') actual = unicode_type(data) - self.assertEqual(expected, actual) + assert expected == actual def test_info(self): ds = create_test_data(seed=123) @@ -227,7 +227,7 @@ def test_info(self): \t:string_attr = bar ; }''') actual = buf.getvalue() - self.assertEqual(expected, actual) + assert expected == actual buf.close() def test_constructor(self): @@ -298,7 +298,7 @@ def test_constructor_auto_align(self): self.assertDatasetIdentical(expected, actual) # regression test for GH346 - self.assertIsInstance(actual.variables['x'], IndexVariable) + assert isinstance(actual.variables['x'], IndexVariable) # variable with different dimensions c = ('y', [3, 4]) @@ -389,7 +389,7 @@ def test_constructor_with_coords(self): Dataset({'a': ('x', [1])}, {'a': ('x', [1])}) ds = Dataset({}, {'a': ('x', [1])}) - self.assertFalse(ds.data_vars) + assert not ds.data_vars self.assertItemsEqual(ds.coords.keys(), ['a']) mindex = pd.MultiIndex.from_product([['a', 'b'], [1, 2]], @@ -400,48 +400,48 @@ def test_constructor_with_coords(self): def test_properties(self): ds = create_test_data() - self.assertEqual(ds.dims, - {'dim1': 8, 'dim2': 9, 'dim3': 10, 'time': 20}) - self.assertEqual(list(ds.dims), sorted(ds.dims)) - self.assertEqual(ds.sizes, ds.dims) + assert ds.dims == \ + {'dim1': 8, 'dim2': 9, 'dim3': 10, 'time': 20} + assert list(ds.dims) == sorted(ds.dims) + assert ds.sizes == ds.dims # These exact types aren't public API, but this makes sure we don't # change them inadvertently: - self.assertIsInstance(ds.dims, utils.Frozen) - self.assertIsInstance(ds.dims.mapping, utils.SortedKeysDict) - self.assertIs(type(ds.dims.mapping.mapping), dict) + assert isinstance(ds.dims, utils.Frozen) + assert isinstance(ds.dims.mapping, utils.SortedKeysDict) + assert type(ds.dims.mapping.mapping) is dict with pytest.warns(FutureWarning): self.assertItemsEqual(ds, list(ds.variables)) with pytest.warns(FutureWarning): self.assertItemsEqual(ds.keys(), list(ds.variables)) - self.assertNotIn('aasldfjalskdfj', ds.variables) - self.assertIn('dim1', repr(ds.variables)) + assert 'aasldfjalskdfj' not in ds.variables + assert 'dim1' in repr(ds.variables) with pytest.warns(FutureWarning): - self.assertEqual(len(ds), 7) + assert len(ds) == 7 with pytest.warns(FutureWarning): - self.assertEqual(bool(ds), True) + assert bool(ds) == True self.assertItemsEqual(ds.data_vars, ['var1', 'var2', 'var3']) self.assertItemsEqual(ds.data_vars.keys(), ['var1', 'var2', 'var3']) - self.assertIn('var1', ds.data_vars) - self.assertNotIn('dim1', ds.data_vars) - self.assertNotIn('numbers', ds.data_vars) - self.assertEqual(len(ds.data_vars), 3) + assert 'var1' in ds.data_vars + assert 'dim1' not in ds.data_vars + assert 'numbers' not in ds.data_vars + assert len(ds.data_vars) == 3 self.assertItemsEqual(ds.indexes, ['dim2', 'dim3', 'time']) - self.assertEqual(len(ds.indexes), 3) - self.assertIn('dim2', repr(ds.indexes)) + assert len(ds.indexes) == 3 + assert 'dim2' in repr(ds.indexes) self.assertItemsEqual(ds.coords, ['time', 'dim2', 'dim3', 'numbers']) - self.assertIn('dim2', ds.coords) - self.assertIn('numbers', ds.coords) - self.assertNotIn('var1', ds.coords) - self.assertNotIn('dim1', ds.coords) - self.assertEqual(len(ds.coords), 4) + assert 'dim2' in ds.coords + assert 'numbers' in ds.coords + assert 'var1' not in ds.coords + assert 'dim1' not in ds.coords + assert len(ds.coords) == 4 - self.assertEqual(Dataset({'x': np.int64(1), - 'y': np.float32([1, 2])}).nbytes, 16) + assert Dataset({'x': np.int64(1), + 'y': np.float32([1, 2])}).nbytes == 16 def test_get_index(self): ds = Dataset({'foo': (('x', 'y'), np.zeros((2, 3)))}, @@ -457,26 +457,26 @@ def test_attr_access(self): self.assertDataArrayIdentical(ds.tmin, ds['tmin']) self.assertDataArrayIdentical(ds.tmin.x, ds.x) - self.assertEqual(ds.title, ds.attrs['title']) - self.assertEqual(ds.tmin.units, ds['tmin'].attrs['units']) + assert ds.title == ds.attrs['title'] + assert ds.tmin.units == ds['tmin'].attrs['units'] - self.assertLessEqual(set(['tmin', 'title']), set(dir(ds))) - self.assertIn('units', set(dir(ds.tmin))) + assert set(['tmin', 'title']) <= set(dir(ds)) + assert 'units' in set(dir(ds.tmin)) # should defer to variable of same name ds.attrs['tmin'] = -999 - self.assertEqual(ds.attrs['tmin'], -999) + assert ds.attrs['tmin'] == -999 self.assertDataArrayIdentical(ds.tmin, ds['tmin']) def test_variable(self): a = Dataset() d = np.random.random((10, 3)) a['foo'] = (('time', 'x',), d) - self.assertTrue('foo' in a.variables) - self.assertTrue('foo' in a) + assert 'foo' in a.variables + assert 'foo' in a a['bar'] = (('time', 'x',), d) # order of creation is preserved - self.assertEqual(list(a.variables), ['foo', 'bar']) + assert list(a.variables) == ['foo', 'bar'] self.assertArrayEqual(a['foo'].values, d) # try to add variable with dim (10,3) with data that's (3,10) with pytest.raises(ValueError): @@ -487,13 +487,13 @@ def test_modify_inplace(self): vec = np.random.random((10,)) attributes = {'foo': 'bar'} a['x'] = ('x', vec, attributes) - self.assertTrue('x' in a.coords) - self.assertIsInstance(a.coords['x'].to_index(), pd.Index) + assert 'x' in a.coords + assert isinstance(a.coords['x'].to_index(), pd.Index) self.assertVariableIdentical(a.coords['x'].variable, a.variables['x']) b = Dataset() b['x'] = ('x', vec, attributes) self.assertVariableIdentical(a['x'], b['x']) - self.assertEqual(a.dims, b.dims) + assert a.dims == b.dims # this should work a['x'] = ('x', vec[:5]) a['z'] = ('x', np.arange(5)) @@ -506,7 +506,7 @@ def test_modify_inplace(self): a['y'] = ('y', arr) with pytest.raises(ValueError): a['y'] = ('y', scal) - self.assertTrue('y' not in a.dims) + assert 'y' not in a.dims def test_coords_properties(self): # use an OrderedDict for coordinates to ensure order across python @@ -519,7 +519,7 @@ def test_coords_properties(self): OrderedDict([('a', ('x', np.array([4, 5], 'int64'))), ('b', np.int64(-10))])) - self.assertEqual(4, len(data.coords)) + assert 4 == len(data.coords) self.assertItemsEqual(['x', 'y', 'a', 'b'], list(data.coords)) @@ -528,10 +528,10 @@ def test_coords_properties(self): self.assertVariableIdentical(data.coords['y'].variable, data['y'].variable) - self.assertIn('x', data.coords) - self.assertIn('a', data.coords) - self.assertNotIn(0, data.coords) - self.assertNotIn('foo', data.coords) + assert 'x' in data.coords + assert 'a' in data.coords + assert 0 not in data.coords + assert 'foo' not in data.coords with pytest.raises(KeyError): data.coords['foo'] @@ -545,9 +545,9 @@ def test_coords_properties(self): a (x) int64 4 5 b int64 -10""") actual = repr(data.coords) - self.assertEqual(expected, actual) + assert expected == actual - self.assertEqual({'x': 2, 'y': 3}, data.coords.dims) + assert {'x': 2, 'y': 3} == data.coords.dims def test_coords_modify(self): data = Dataset({'x': ('x', [-1, -2]), @@ -705,9 +705,9 @@ def test_data_vars_properties(self): ds['foo'] = (('x',), [1.0]) ds['bar'] = 2.0 - self.assertEqual(set(ds.data_vars), {'foo', 'bar'}) - self.assertIn('foo', ds.data_vars) - self.assertNotIn('x', ds.data_vars) + assert set(ds.data_vars) == {'foo', 'bar'} + assert 'foo' in ds.data_vars + assert 'x' not in ds.data_vars self.assertDataArrayIdentical(ds['foo'], ds.data_vars['foo']) expected = dedent("""\ @@ -715,77 +715,77 @@ def test_data_vars_properties(self): foo (x) float64 1.0 bar float64 2.0""") actual = repr(ds.data_vars) - self.assertEqual(expected, actual) + assert expected == actual def test_equals_and_identical(self): data = create_test_data(seed=42) - self.assertTrue(data.equals(data)) - self.assertTrue(data.identical(data)) + assert data.equals(data) + assert data.identical(data) data2 = create_test_data(seed=42) data2.attrs['foobar'] = 'baz' - self.assertTrue(data.equals(data2)) - self.assertFalse(data.identical(data2)) + assert data.equals(data2) + assert not data.identical(data2) del data2['time'] - self.assertFalse(data.equals(data2)) + assert not data.equals(data2) data = create_test_data(seed=42).rename({'var1': None}) - self.assertTrue(data.equals(data)) - self.assertTrue(data.identical(data)) + assert data.equals(data) + assert data.identical(data) data2 = data.reset_coords() - self.assertFalse(data2.equals(data)) - self.assertFalse(data2.identical(data)) + assert not data2.equals(data) + assert not data2.identical(data) def test_equals_failures(self): data = create_test_data() - self.assertFalse(data.equals('foo')) - self.assertFalse(data.identical(123)) - self.assertFalse(data.broadcast_equals({1: 2})) + assert not data.equals('foo') + assert not data.identical(123) + assert not data.broadcast_equals({1: 2}) def test_broadcast_equals(self): data1 = Dataset(coords={'x': 0}) data2 = Dataset(coords={'x': [0]}) - self.assertTrue(data1.broadcast_equals(data2)) - self.assertFalse(data1.equals(data2)) - self.assertFalse(data1.identical(data2)) + assert data1.broadcast_equals(data2) + assert not data1.equals(data2) + assert not data1.identical(data2) def test_attrs(self): data = create_test_data(seed=42) data.attrs = {'foobar': 'baz'} - self.assertTrue(data.attrs['foobar'], 'baz') - self.assertIsInstance(data.attrs, OrderedDict) + assert data.attrs['foobar'], 'baz' + assert isinstance(data.attrs, OrderedDict) @requires_dask def test_chunk(self): data = create_test_data() for v in data.variables.values(): - self.assertIsInstance(v.data, np.ndarray) - self.assertEqual(data.chunks, {}) + assert isinstance(v.data, np.ndarray) + assert data.chunks == {} reblocked = data.chunk() for k, v in reblocked.variables.items(): if k in reblocked.dims: - self.assertIsInstance(v.data, np.ndarray) + assert isinstance(v.data, np.ndarray) else: - self.assertIsInstance(v.data, da.Array) + assert isinstance(v.data, da.Array) expected_chunks = {'dim1': (8,), 'dim2': (9,), 'dim3': (10,)} - self.assertEqual(reblocked.chunks, expected_chunks) + assert reblocked.chunks == expected_chunks reblocked = data.chunk({'time': 5, 'dim1': 5, 'dim2': 5, 'dim3': 5}) # time is not a dim in any of the data_vars, so it # doesn't get chunked expected_chunks = {'dim1': (5, 3), 'dim2': (5, 4), 'dim3': (5, 5)} - self.assertEqual(reblocked.chunks, expected_chunks) + assert reblocked.chunks == expected_chunks reblocked = data.chunk(expected_chunks) - self.assertEqual(reblocked.chunks, expected_chunks) + assert reblocked.chunks == expected_chunks # reblock on already blocked data reblocked = reblocked.chunk(expected_chunks) - self.assertEqual(reblocked.chunks, expected_chunks) + assert reblocked.chunks == expected_chunks self.assertDatasetIdentical(reblocked, data) with raises_regex(ValueError, 'some chunks'): @@ -822,14 +822,14 @@ def test_isel(self): self.assertItemsEqual(data.dims, ret.dims) for d in data.dims: if d in slicers: - self.assertEqual(ret.dims[d], - np.arange(data.dims[d])[slicers[d]].size) + assert ret.dims[d] == \ + np.arange(data.dims[d])[slicers[d]].size else: - self.assertEqual(data.dims[d], ret.dims[d]) + assert data.dims[d] == ret.dims[d] # Verify that the data is what we expect for v in data.variables: - self.assertEqual(data[v].dims, ret[v].dims) - self.assertEqual(data[v].attrs, ret[v].attrs) + assert data[v].dims == ret[v].dims + assert data[v].attrs == ret[v].attrs slice_list = [slice(None)] * data[v].values.ndim for d, s in iteritems(slicers): if d in data[v].dims: @@ -844,13 +844,13 @@ def test_isel(self): data.isel(not_a_dim=slice(0, 2)) ret = data.isel(dim1=0) - self.assertEqual({'time': 20, 'dim2': 9, 'dim3': 10}, ret.dims) + assert {'time': 20, 'dim2': 9, 'dim3': 10} == ret.dims self.assertItemsEqual(data.data_vars, ret.data_vars) self.assertItemsEqual(data.coords, ret.coords) self.assertItemsEqual(data.indexes, ret.indexes) ret = data.isel(time=slice(2), dim1=0, dim2=slice(5)) - self.assertEqual({'time': 2, 'dim2': 5, 'dim3': 10}, ret.dims) + assert {'time': 2, 'dim2': 5, 'dim3': 10} == ret.dims self.assertItemsEqual(data.data_vars, ret.data_vars) self.assertItemsEqual(data.coords, ret.coords) self.assertItemsEqual(data.indexes, ret.indexes) @@ -1462,7 +1462,7 @@ def test_sel(lab_indexer, pos_indexer, replaced_idx=False, self.assertDatasetIdentical(ds, expected_ds) else: if renamed_dim: - self.assertEqual(ds['var'].dims[0], renamed_dim) + assert ds['var'].dims[0] == renamed_dim ds = ds.rename({renamed_dim: 'x'}) self.assertVariableIdentical(ds['var'].variable, expected_ds['var'].variable) @@ -1633,8 +1633,8 @@ def test_align(self): self.assertArrayEqual(left2['dim3'], union) self.assertDatasetIdentical(left2.sel(dim3=intersection), right2.sel(dim3=intersection)) - self.assertTrue(np.isnan(left2['var3'][-2:]).all()) - self.assertTrue(np.isnan(right2['var3'][:2]).all()) + assert np.isnan(left2['var3'][-2:]).all() + assert np.isnan(right2['var3'][:2]).all() left2, right2 = align(left, right, join='left') self.assertVariableEqual(left2['dim3'].variable, @@ -1642,7 +1642,7 @@ def test_align(self): self.assertVariableEqual(left2['dim3'].variable, left['dim3'].variable) self.assertDatasetIdentical(left2.sel(dim3=intersection), right2.sel(dim3=intersection)) - self.assertTrue(np.isnan(right2['var3'][:2]).all()) + assert np.isnan(right2['var3'][:2]).all() left2, right2 = align(left, right, join='right') self.assertVariableEqual(left2['dim3'].variable, @@ -1651,7 +1651,7 @@ def test_align(self): right['dim3'].variable) self.assertDatasetIdentical(left2.sel(dim3=intersection), right2.sel(dim3=intersection)) - self.assertTrue(np.isnan(left2['var3'][-2:]).all()) + assert np.isnan(left2['var3'][-2:]).all() with raises_regex(ValueError, 'invalid value for join'): align(left, right, join='foobar') @@ -1856,7 +1856,7 @@ def test_copy(self): for copied in [data.copy(deep=False), copy(data)]: self.assertDatasetIdentical(data, copied) - self.assertEqual(data.encoding, copied.encoding) + assert data.encoding == copied.encoding # Note: IndexVariable objects with string dtype are always # copied because of xarray.core.util.safe_cast_to_index. # Limiting the test to data variables. @@ -1865,13 +1865,13 @@ def test_copy(self): v1 = copied.variables[k] assert source_ndarray(v0.data) is source_ndarray(v1.data) copied['foo'] = ('z', np.arange(5)) - self.assertNotIn('foo', data) + assert 'foo' not in data for copied in [data.copy(deep=True), deepcopy(data)]: self.assertDatasetIdentical(data, copied) for k, v0 in data.variables.items(): v1 = copied.variables[k] - self.assertIsNot(v0, v1) + assert v0 is not v1 def test_rename(self): data = create_test_data() @@ -1890,11 +1890,11 @@ def test_rename(self): self.assertVariableEqual(Variable(dims, v.values, v.attrs), renamed[k].variable.to_base_variable()) - self.assertEqual(v.encoding, renamed[k].encoding) - self.assertEqual(type(v), type(renamed.variables[k])) + assert v.encoding == renamed[k].encoding + assert type(v) == type(renamed.variables[k]) - self.assertTrue('var1' not in renamed) - self.assertTrue('dim2' not in renamed) + assert 'var1' not in renamed + assert 'dim2' not in renamed with raises_regex(ValueError, "cannot rename 'not_a_var'"): data.rename({'not_a_var': 'nada'}) @@ -1932,7 +1932,7 @@ def test_rename_inplace(self): renamed = data.rename({'x': 'y'}) data.rename({'x': 'y'}, inplace=True) self.assertDatasetIdentical(data, renamed) - self.assertFalse(data.equals(copied)) + assert not data.equals(copied) assert data.dims == {'y': 3, 't': 3} # check virtual variables self.assertArrayEqual(data['t.dayofyear'], [1, 2, 3]) @@ -1943,8 +1943,8 @@ def test_swap_dims(self): {'x': ('y', [1, 2, 3]), 'y': list('abc')}) actual = original.swap_dims({'x': 'y'}) self.assertDatasetIdentical(expected, actual) - self.assertIsInstance(actual.variables['y'], IndexVariable) - self.assertIsInstance(actual.variables['x'], Variable) + assert isinstance(actual.variables['y'], IndexVariable) + assert isinstance(actual.variables['x'], Variable) roundtripped = actual.swap_dims({'y': 'x'}) self.assertDatasetIdentical(original.set_coords('y'), roundtripped) @@ -2116,12 +2116,12 @@ def test_update(self): actual = data.copy() actual_result = actual.update(data, inplace=True) - self.assertIs(actual_result, actual) + assert actual_result is actual self.assertDatasetIdentical(expected, actual) actual = data.update(data, inplace=False) expected = data - self.assertIsNot(actual, expected) + assert actual is not expected self.assertDatasetIdentical(expected, actual) other = Dataset(attrs={'new': 'attr'}) @@ -2150,7 +2150,7 @@ def test_update_auto_align(self): def test_getitem(self): data = create_test_data() - self.assertIsInstance(data['var1'], DataArray) + assert isinstance(data['var1'], DataArray) self.assertVariableEqual(data['var1'].variable, data.variables['var1']) with pytest.raises(KeyError): data['notfound'] @@ -2186,7 +2186,7 @@ def test_virtual_variables_default_coords(self): expected = DataArray(range(10), dims='x', name='x') actual = dataset['x'] self.assertDataArrayIdentical(expected, actual) - self.assertIsInstance(actual.variable, IndexVariable) + assert isinstance(actual.variable, IndexVariable) actual = dataset[['x', 'foo']] expected = dataset.assign_coords(x=range(10)) @@ -2211,7 +2211,7 @@ def test_virtual_variables_time(self): self.assertDatasetEqual(expected, actual) # non-coordinate variables ds = Dataset({'t': ('x', pd.date_range('2000-01-01', periods=3))}) - self.assertTrue((ds['t.year'] == 2000).all()) + assert (ds['t.year'] == 2000).all() def test_virtual_variable_same_name(self): # regression test for GH367 @@ -2335,7 +2335,7 @@ def test_assign(self): actual = ds.assign(x=[0, 1, 2], y=2) expected = Dataset({'x': [0, 1, 2], 'y': 2}) self.assertDatasetIdentical(actual, expected) - self.assertEqual(list(actual.variables), ['x', 'y']) + assert list(actual.variables) == ['x', 'y'] self.assertDatasetIdentical(ds, Dataset()) actual = actual.assign(y=lambda ds: ds.x ** 2) @@ -2364,12 +2364,12 @@ def test_assign_attrs(self): new = Dataset() actual = new.assign_attrs(a=1, b=2) self.assertDatasetIdentical(actual, expected) - self.assertEqual(new.attrs, {}) + assert new.attrs == {} expected.attrs['c'] = 3 new_actual = actual.assign_attrs({'c': 3}) self.assertDatasetIdentical(new_actual, expected) - self.assertEqual(actual.attrs, dict(a=1, b=2)) + assert actual.attrs == dict(a=1, b=2) def test_assign_multiindex_level(self): data = create_test_multiindex() @@ -2420,7 +2420,7 @@ def test_delitem(self): del data['numbers'] self.assertItemsEqual(data.variables, all_items - set(['var1', 'numbers'])) - self.assertNotIn('numbers', data.coords) + assert 'numbers' not in data.coords def test_squeeze(self): data = Dataset({'foo': (['x', 'y', 'z'], [[[1], [2]]])}) @@ -2464,14 +2464,14 @@ def test_groupby(self): 'c': ('x', [0, 1, 0]), 'y': range(5)}) groupby = data.groupby('x') - self.assertEqual(len(groupby), 3) + assert len(groupby) == 3 expected_groups = {'a': 0, 'b': 1, 'c': 2} - self.assertEqual(groupby.groups, expected_groups) + assert groupby.groups == expected_groups expected_items = [('a', data.isel(x=0)), ('b', data.isel(x=1)), ('c', data.isel(x=2))] for actual, expected in zip(groupby, expected_items): - self.assertEqual(actual[0], expected[0]) + assert actual[0] == expected[0] self.assertDatasetEqual(actual[1], expected[1]) def identity(x): @@ -2495,7 +2495,7 @@ def test_groupby_returns_new_type(self): def test_groupby_iter(self): data = create_test_data() for n, (t, sub) in enumerate(list(data.groupby('dim1'))[:3]): - self.assertEqual(data['dim1'][n], t) + assert 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']) @@ -2607,8 +2607,12 @@ def test_groupby_order(self): data_vars_ref = list(ds.data_vars.keys()) ds = ds.groupby('t').mean() data_vars = list(ds.data_vars.keys()) +<<<<<<< HEAD self.assertEqual(data_vars, data_vars_ref) +======= + assert data_vars == data_vars_ref +>>>>>>> self_assert from unittest2pytest # coords are now at the end of the list, so the test below fails # all_vars = list(ds.variables.keys()) # all_vars_ref = list(ds.variables.keys()) @@ -2646,11 +2650,11 @@ def test_resample_by_mean_with_keep_attrs(self): resampled_ds = ds.resample(time='1D').mean(keep_attrs=True) actual = resampled_ds['bar'].attrs expected = ds['bar'].attrs - self.assertEqual(expected, actual) + assert expected == actual actual = resampled_ds.attrs expected = ds.attrs - self.assertEqual(expected, actual) + assert expected == actual def test_resample_by_mean_discarding_attrs(self): times = pd.date_range('2000-01-01', freq='6H', periods=10) @@ -2766,7 +2770,7 @@ def test_to_and_from_dataframe(self): names=['x', 'y']) expected = pd.DataFrame(w.reshape(-1), columns=['w'], index=exp_index) actual = ds.to_dataframe() - self.assertTrue(expected.equals(actual)) + assert expected.equals(actual) # check roundtrip self.assertDatasetIdentical(ds.assign_coords(x=[0, 1]), @@ -2864,7 +2868,7 @@ def test_to_and_from_dict(self): actual = ds.to_dict() # check that they are identical - self.assertEqual(expected, actual) + assert expected == actual # check roundtrip self.assertDatasetIdentical(ds, Dataset.from_dict(actual)) @@ -2943,14 +2947,14 @@ def test_to_dict_with_numpy_attrs(self): actual = ds.to_dict() # check that they are identical - self.assertEqual(expected_attrs, actual['data_vars']['a']['attrs']) + assert expected_attrs == actual['data_vars']['a']['attrs'] def test_pickle(self): data = create_test_data() roundtripped = pickle.loads(pickle.dumps(data)) self.assertDatasetIdentical(data, roundtripped) # regression test for #167: - self.assertEqual(data.dims, roundtripped.dims) + assert data.dims == roundtripped.dims def test_lazy_load(self): store = InaccessibleVariableDataStore() @@ -3094,20 +3098,20 @@ def test_fillna(self): ds.attrs['attr'] = 'ds' ds.a.attrs['attr'] = 'da' actual = ds.groupby('b').fillna(Dataset({'a': ('b', [0, 2])})) - self.assertEqual(actual.attrs, ds.attrs) - self.assertEqual(actual.a.name, 'a') - self.assertEqual(actual.a.attrs, ds.a.attrs) + assert actual.attrs == ds.attrs + assert actual.a.name == 'a' + assert actual.a.attrs == ds.a.attrs da = DataArray(range(5), name='a', attrs={'attr': 'da'}) actual = da.fillna(1) - self.assertEqual(actual.name, 'a') - self.assertEqual(actual.attrs, da.attrs) + assert actual.name == 'a' + assert actual.attrs == da.attrs ds = Dataset({'a': da}, attrs={'attr': 'ds'}) actual = ds.fillna({'a': 1}) - self.assertEqual(actual.attrs, ds.attrs) - self.assertEqual(actual.a.name, 'a') - self.assertEqual(actual.a.attrs, ds.a.attrs) + assert actual.attrs == ds.attrs + assert actual.a.name == 'a' + assert actual.a.attrs == ds.a.attrs def test_where(self): ds = Dataset({'a': ('x', range(5))}) @@ -3147,21 +3151,21 @@ def test_where(self): ds.attrs['attr'] = 'ds' ds.a.attrs['attr'] = 'da' actual = ds.groupby('c').where(cond) - self.assertEqual(actual.attrs, ds.attrs) - self.assertEqual(actual.a.name, 'a') - self.assertEqual(actual.a.attrs, ds.a.attrs) + assert actual.attrs == ds.attrs + assert actual.a.name == 'a' + assert actual.a.attrs == ds.a.attrs # attrs da = DataArray(range(5), name='a', attrs={'attr': 'da'}) actual = da.where(da.values > 1) - self.assertEqual(actual.name, 'a') - self.assertEqual(actual.attrs, da.attrs) + assert actual.name == 'a' + assert actual.attrs == da.attrs ds = Dataset({'a': da}, attrs={'attr': 'ds'}) actual = ds.where(ds > 0) - self.assertEqual(actual.attrs, ds.attrs) - self.assertEqual(actual.a.name, 'a') - self.assertEqual(actual.a.attrs, ds.a.attrs) + assert actual.attrs == ds.attrs + assert actual.a.name == 'a' + assert actual.a.attrs == ds.a.attrs def test_where_other(self): ds = Dataset({'a': ('x', range(5))}, {'x': range(5)}) @@ -3259,7 +3263,7 @@ def test_where_drop_no_indexes(self): def test_reduce(self): data = create_test_data() - self.assertEqual(len(data.mean().coords), 0) + assert len(data.mean().coords) == 0 actual = data.max() expected = Dataset(dict((k, v.max()) @@ -3325,7 +3329,7 @@ def test_reduce_non_numeric(self): data = np.random.randint(0, 100, size=size).astype(np.str_) data1[v] = (dims, data, {'foo': 'variable'}) - self.assertTrue('var4' not in data1.mean()) + assert 'var4' not in data1.mean() self.assertDatasetEqual(data1.mean(), data2.mean()) self.assertDatasetEqual(data1.mean(dim='dim1'), data2.mean(dim='dim1')) @@ -3382,15 +3386,15 @@ def test_reduce_keep_attrs(self): # Test dropped attrs ds = data.mean() - self.assertEqual(ds.attrs, {}) + assert ds.attrs == {} for v in ds.data_vars.values(): - self.assertEqual(v.attrs, {}) + assert v.attrs == {} # Test kept attrs ds = data.mean(keep_attrs=True) - self.assertEqual(ds.attrs, attrs) + assert ds.attrs == attrs for k, v in ds.data_vars.items(): - self.assertEqual(v.attrs, data[k].attrs) + assert v.attrs == data[k].attrs def test_reduce_argmin(self): # regression test for #205 @@ -3570,7 +3574,7 @@ def test_dataset_dataset_math(self): expected_id = id(actual) actual += ds self.assertDatasetIdentical(expected, actual) - self.assertEqual(expected_id, id(actual)) + assert expected_id == id(actual) self.assertDatasetIdentical(ds == ds, ds.notnull()) @@ -3647,13 +3651,13 @@ def test_dataset_transpose(self): ds = create_test_data() actual = ds.transpose() for k in ds.variables: - self.assertEqual(actual[k].dims[::-1], ds[k].dims) + assert actual[k].dims[::-1] == ds[k].dims new_order = ('dim2', 'dim3', 'dim1', 'time') actual = ds.transpose(*new_order) for k in ds.variables: expected_dims = tuple(d for d in new_order if d in ds[k].dims) - self.assertEqual(actual[k].dims, expected_dims) + assert actual[k].dims == expected_dims with raises_regex(ValueError, 'arguments to transpose'): ds.transpose('dim1', 'dim2', 'dim3') @@ -3669,7 +3673,7 @@ def test_dataset_retains_period_index_on_transpose(self): transposed = ds.transpose() - self.assertIsInstance(transposed.time.to_index(), pd.PeriodIndex) + assert isinstance(transposed.time.to_index(), pd.PeriodIndex) def test_dataset_diff_n1_simple(self): ds = Dataset({'foo': ('x', [5, 5, 6, 6])}) @@ -3782,33 +3786,46 @@ def test_filter_by_attrs(self): # Test return empty Dataset. ds.filter_by_attrs(standard_name='invalid_standard_name') new_ds = ds.filter_by_attrs(standard_name='invalid_standard_name') - self.assertFalse(bool(new_ds.data_vars)) + assert not bool(new_ds.data_vars) # Test return one DataArray. +<<<<<<< HEAD new_ds = ds.filter_by_attrs( standard_name='convective_precipitation_flux') self.assertEqual(new_ds['precipitation'].standard_name, 'convective_precipitation_flux') +======= + new_ds = ds.filter_by_attrs(standard_name='convective_precipitation_flux') + assert new_ds['precipitation'].standard_name == 'convective_precipitation_flux' +>>>>>>> self_assert from unittest2pytest self.assertDatasetEqual(new_ds['precipitation'], ds['precipitation']) # Test return more than one DataArray. new_ds = ds.filter_by_attrs(standard_name='air_potential_temperature') - self.assertEqual(len(new_ds.data_vars), 2) + assert len(new_ds.data_vars) == 2 for var in new_ds.data_vars: +<<<<<<< HEAD self.assertEqual(new_ds[var].standard_name, 'air_potential_temperature') +======= + assert new_ds[var].standard_name == 'air_potential_temperature' +>>>>>>> self_assert from unittest2pytest # Test callable. new_ds = ds.filter_by_attrs(height=lambda v: v is not None) - self.assertEqual(len(new_ds.data_vars), 2) + assert len(new_ds.data_vars) == 2 for var in new_ds.data_vars: +<<<<<<< HEAD self.assertEqual(new_ds[var].standard_name, 'air_potential_temperature') +======= + assert new_ds[var].standard_name == 'air_potential_temperature' +>>>>>>> self_assert from unittest2pytest new_ds = ds.filter_by_attrs(height='10 m') - self.assertEqual(len(new_ds.data_vars), 1) + assert len(new_ds.data_vars) == 1 for var in new_ds.data_vars: - self.assertEqual(new_ds[var].height, '10 m') + assert new_ds[var].height == '10 m' def test_binary_op_join_setting(self): # arithmetic_join applies to data array coordinates @@ -3853,8 +3870,8 @@ def test_full_like(self): expect = ds.copy(deep=True) expect['d1'].values = [2, 2, 2] expect['d2'].values = [2.0, 2.0, 2.0] - self.assertEqual(expect['d1'].dtype, int) - self.assertEqual(expect['d2'].dtype, float) + assert expect['d1'].dtype == int + assert expect['d2'].dtype == float self.assertDatasetIdentical(expect, actual) # override dtype @@ -3862,8 +3879,8 @@ def test_full_like(self): expect = ds.copy(deep=True) expect['d1'].values = [True, True, True] expect['d2'].values = [True, True, True] - self.assertEqual(expect['d1'].dtype, bool) - self.assertEqual(expect['d2'].dtype, bool) + assert expect['d1'].dtype == bool + assert expect['d2'].dtype == bool self.assertDatasetIdentical(expect, actual) def test_combine_first(self): diff --git a/xarray/tests/test_duck_array_ops.py b/xarray/tests/test_duck_array_ops.py index 121f401b1a0..6f5fa2f306b 100644 --- a/xarray/tests/test_duck_array_ops.py +++ b/xarray/tests/test_duck_array_ops.py @@ -70,7 +70,7 @@ def test_last(self): last(self.x, 3) def test_count(self): - self.assertEqual(12, count(self.x)) + assert 12 == count(self.x) expected = array([[1, 2, 3], [3, 2, 1]]) self.assertArrayEqual(expected, count(self.x, axis=-1)) diff --git a/xarray/tests/test_extensions.py b/xarray/tests/test_extensions.py index c3f89a5f533..15be5e7b622 100644 --- a/xarray/tests/test_extensions.py +++ b/xarray/tests/test_extensions.py @@ -9,6 +9,7 @@ import xarray as xr from . import TestCase, raises_regex +import pytest @xr.register_dataset_accessor('example_accessor') @@ -54,7 +55,7 @@ def foo(self): del xr.Dataset.demo assert not hasattr(xr.Dataset, 'demo') - with self.assertWarns('overriding a preexisting attribute'): + with pytest.warns('overriding a preexisting attribute'): @xr.register_dataarray_accessor('demo') class Foo(object): pass diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index f80326c3e84..53342825dcd 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -28,7 +28,7 @@ def test_get_indexer_at_least_n_items(self): ] for shape, expected in cases: actual = formatting._get_indexer_at_least_n_items(shape, 10) - self.assertEqual(expected, actual) + assert expected == actual def test_first_n_items(self): array = np.arange(100).reshape(10, 5, 2) @@ -48,7 +48,7 @@ def test_last_item(self): for r in reshape: result = formatting.last_item(array.reshape(r)) - self.assertEqual(result, expected) + assert result == expected def test_format_item(self): cases = [ @@ -67,7 +67,7 @@ def test_format_item(self): ] for item, expected in cases: actual = formatting.format_item(item) - self.assertEqual(expected, actual) + assert expected == actual def test_format_items(self): cases = [ @@ -85,62 +85,62 @@ def test_format_items(self): ] for item, expected in cases: actual = ' '.join(formatting.format_items(item)) - self.assertEqual(expected, actual) + assert expected == actual def test_format_array_flat(self): actual = formatting.format_array_flat(np.arange(100), 13) expected = '0 1 2 3 4 ...' - self.assertEqual(expected, actual) + assert expected == actual actual = formatting.format_array_flat(np.arange(100.0), 11) expected = '0.0 1.0 ...' - self.assertEqual(expected, actual) + assert expected == actual actual = formatting.format_array_flat(np.arange(100.0), 1) expected = '0.0 ...' - self.assertEqual(expected, actual) + assert expected == actual actual = formatting.format_array_flat(np.arange(3), 5) expected = '0 1 2' - self.assertEqual(expected, actual) + assert expected == actual actual = formatting.format_array_flat(np.arange(4.0), 11) expected = '0.0 1.0 ...' - self.assertEqual(expected, actual) + assert expected == actual actual = formatting.format_array_flat(np.arange(4), 0) expected = '0 ...' - self.assertEqual(expected, actual) + assert expected == actual def test_pretty_print(self): - self.assertEqual(formatting.pretty_print('abcdefghij', 8), 'abcde...') - self.assertEqual(formatting.pretty_print(u'ß', 1), u'ß') + assert formatting.pretty_print('abcdefghij', 8) == 'abcde...' + assert formatting.pretty_print(u'ß', 1) == u'ß' def test_maybe_truncate(self): - self.assertEqual(formatting.maybe_truncate(u'ß', 10), u'ß') + assert formatting.maybe_truncate(u'ß', 10) == u'ß' def test_format_timestamp_out_of_bounds(self): from datetime import datetime date = datetime(1300, 12, 1) expected = '1300-12-01' result = formatting.format_timestamp(date) - self.assertEqual(result, expected) + assert result == expected date = datetime(2300, 12, 1) expected = '2300-12-01' result = formatting.format_timestamp(date) - self.assertEqual(result, expected) + assert result == expected def test_attribute_repr(self): short = formatting.summarize_attr(u'key', u'Short string') long = formatting.summarize_attr(u'key', 100 * u'Very long string ') newlines = formatting.summarize_attr(u'key', u'\n\n\n') tabs = formatting.summarize_attr(u'key', u'\t\t\t') - self.assertEqual(short, ' key: Short string') - self.assertLessEqual(len(long), 80) - self.assertTrue(long.endswith(u'...')) - self.assertNotIn(u'\n', newlines) - self.assertNotIn(u'\t', tabs) + assert short == ' key: Short string' + assert len(long) <= 80 + assert long.endswith(u'...') + assert u'\n' not in newlines + assert u'\t' not in tabs def test_set_numpy_options(): diff --git a/xarray/tests/test_indexing.py b/xarray/tests/test_indexing.py index 3b5bbbdb55d..01bc1fb6816 100644 --- a/xarray/tests/test_indexing.py +++ b/xarray/tests/test_indexing.py @@ -86,7 +86,7 @@ def test_get_dim_indexers(self): mdata = DataArray(range(4), [('x', mindex)]) dim_indexers = indexing.get_dim_indexers(mdata, {'one': 'a', 'two': 1}) - self.assertEqual(dim_indexers, {'x': {'one': 'a', 'two': 1}}) + assert dim_indexers == {'x': {'one': 'a', 'two': 1}} with raises_regex(ValueError, 'cannot combine'): indexing.get_dim_indexers(mdata, {'x': 'a', 'two': 1}) @@ -169,7 +169,7 @@ def test_lazily_indexed_array(self): for actual in [v_lazy[i, j, k], v_lazy[:, j, k][i], v_lazy[:, :, k][:, j][i]]: - self.assertEqual(expected.shape, actual.shape) + assert expected.shape == actual.shape self.assertArrayEqual(expected, actual) assert isinstance(actual._data, indexing.LazilyIndexedArray) @@ -189,7 +189,7 @@ def test_lazily_indexed_array(self): for i, j in indexers: expected = np.asarray(v[i][j]) actual = v_lazy[i][j] - self.assertEqual(expected.shape, actual.shape) + assert expected.shape == actual.shape self.assertArrayEqual(expected, actual) assert isinstance(actual._data, indexing.LazilyIndexedArray) assert isinstance(actual._data.array, @@ -208,7 +208,7 @@ def test_sub_array(self): original = np.arange(10) wrapped = indexing.CopyOnWriteArray(original) child = wrapped[B[:5]] - self.assertIsInstance(child, indexing.CopyOnWriteArray) + assert isinstance(child, indexing.CopyOnWriteArray) child[B[:]] = 0 self.assertArrayEqual(original, np.arange(10)) self.assertArrayEqual(wrapped, np.arange(10)) @@ -225,16 +225,16 @@ def test_wrapper(self): original = indexing.LazilyIndexedArray(np.arange(10)) wrapped = indexing.MemoryCachedArray(original) self.assertArrayEqual(wrapped, np.arange(10)) - self.assertIsInstance(wrapped.array, indexing.NumpyIndexingAdapter) + assert isinstance(wrapped.array, indexing.NumpyIndexingAdapter) def test_sub_array(self): original = indexing.LazilyIndexedArray(np.arange(10)) wrapped = indexing.MemoryCachedArray(original) child = wrapped[B[:5]] - self.assertIsInstance(child, indexing.MemoryCachedArray) + assert isinstance(child, indexing.MemoryCachedArray) self.assertArrayEqual(child, np.arange(5)) - self.assertIsInstance(child.array, indexing.NumpyIndexingAdapter) - self.assertIsInstance(wrapped.array, indexing.LazilyIndexedArray) + assert isinstance(child.array, indexing.NumpyIndexingAdapter) + assert isinstance(wrapped.array, indexing.LazilyIndexedArray) def test_setitem(self): original = np.arange(10) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 09e77840f26..10c53221a6a 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -72,7 +72,7 @@ def tearDown(self): def pass_in_axis(self, plotmethod): fig, axes = plt.subplots(ncols=2) plotmethod(ax=axes[0]) - self.assertTrue(axes[0].has_data()) + assert axes[0].has_data() @pytest.mark.slow def imshow_called(self, plotmethod): @@ -105,42 +105,42 @@ def test_2d_line(self): def test_2d_line_accepts_legend_kw(self): self.darray[:, :, 0].plot.line(x='dim_0', add_legend=False) - self.assertFalse(plt.gca().get_legend()) + assert not plt.gca().get_legend() plt.cla() self.darray[:, :, 0].plot.line(x='dim_0', add_legend=True) - self.assertTrue(plt.gca().get_legend()) + assert plt.gca().get_legend() # check whether legend title is set - self.assertEqual(plt.gca().get_legend().get_title().get_text(), - 'dim_1') + assert plt.gca().get_legend().get_title().get_text() \ + == 'dim_1' def test_2d_line_accepts_x_kw(self): self.darray[:, :, 0].plot.line(x='dim_0') - self.assertTrue(plt.gca().get_xlabel() == 'dim_0') + assert plt.gca().get_xlabel() == 'dim_0' plt.cla() self.darray[:, :, 0].plot.line(x='dim_1') - self.assertTrue(plt.gca().get_xlabel() == 'dim_1') + assert plt.gca().get_xlabel() == 'dim_1' def test_2d_line_accepts_hue_kw(self): self.darray[:, :, 0].plot.line(hue='dim_0') - self.assertEqual(plt.gca().get_legend().get_title().get_text(), - 'dim_0') + assert plt.gca().get_legend().get_title().get_text() \ + == 'dim_0' plt.cla() self.darray[:, :, 0].plot.line(hue='dim_1') - self.assertEqual(plt.gca().get_legend().get_title().get_text(), - 'dim_1') + assert plt.gca().get_legend().get_title().get_text() \ + == 'dim_1' def test_2d_before_squeeze(self): a = DataArray(easy_array((1, 5))) a.plot() def test2d_uniform_calls_imshow(self): - self.assertTrue(self.imshow_called(self.darray[:, :, 0].plot.imshow)) + assert self.imshow_called(self.darray[:, :, 0].plot.imshow) @pytest.mark.slow def test2d_nonuniform_calls_contourf(self): a = self.darray[:, :, 0] a.coords['dim_1'] = [2, 1, 89] - self.assertTrue(self.contourf_called(a.plot.contourf)) + assert self.contourf_called(a.plot.contourf) def test2d_1d_2d_coordinates_contourf(self): sz = (20, 10) @@ -184,7 +184,7 @@ def test_datetime_dimension(self): coords=[('time', time), ('y', range(ncol))]) a.plot() ax = plt.gca() - self.assertTrue(ax.has_data()) + assert ax.has_data() @pytest.mark.slow def test_convenient_facetgrid(self): @@ -195,7 +195,7 @@ def test_convenient_facetgrid(self): self.assertArrayEqual(g.axes.shape, [2, 2]) for ax in g.axes.flat: - self.assertTrue(ax.has_data()) + assert ax.has_data() with raises_regex(ValueError, '[Ff]acet'): d.plot(x='x', y='y', col='z', ax=plt.gca()) @@ -213,10 +213,10 @@ def test_subplot_kws(self): for ax in g.axes.flat: try: # mpl V2 - self.assertEqual(ax.get_facecolor()[0:3], - mpl.colors.to_rgb('r')) + assert ax.get_facecolor()[0:3] == \ + mpl.colors.to_rgb('r') except AttributeError: - self.assertEqual(ax.get_axis_bgcolor(), 'r') + assert ax.get_axis_bgcolor() == 'r' @pytest.mark.slow def test_plot_size(self): @@ -252,7 +252,7 @@ def test_convenient_facetgrid_4d(self): self.assertArrayEqual(g.axes.shape, [3, 2]) for ax in g.axes.flat: - self.assertTrue(ax.has_data()) + assert ax.has_data() with raises_regex(ValueError, '[Ff]acet'): d.plot(x='x', y='y', col='columns', ax=plt.gca()) @@ -267,16 +267,16 @@ def setUp(self): def test_xlabel_is_index_name(self): self.darray.plot() - self.assertEqual('period', plt.gca().get_xlabel()) + assert 'period' == plt.gca().get_xlabel() def test_no_label_name_on_y_axis(self): self.darray.plot() - self.assertEqual('', plt.gca().get_ylabel()) + assert '' == plt.gca().get_ylabel() def test_ylabel_is_data_name(self): self.darray.name = 'temperature' self.darray.plot() - self.assertEqual(self.darray.name, plt.gca().get_ylabel()) + assert self.darray.name == plt.gca().get_ylabel() def test_format_string(self): self.darray.plot.line('ro') @@ -292,7 +292,7 @@ def test_nonnumeric_index_raises_typeerror(self): def test_primitive_returned(self): p = self.darray.plot.line() - self.assertTrue(isinstance(p[0], mpl.lines.Line2D)) + assert isinstance(p[0], mpl.lines.Line2D) @pytest.mark.slow def test_plot_nans(self): @@ -304,13 +304,13 @@ def test_x_ticks_are_rotated_for_time(self): a = DataArray(np.arange(len(time)), [('t', time)]) a.plot.line() rotation = plt.gca().get_xticklabels()[0].get_rotation() - self.assertNotEqual(rotation, 0) + assert rotation != 0 def test_slice_in_title(self): self.darray.coords['d'] = 10 self.darray.plot.line() title = plt.gca().get_title() - self.assertEqual('d = 10', title) + assert 'd = 10' == title class TestPlotHistogram(PlotTestCase): @@ -323,28 +323,28 @@ def test_3d_array(self): def test_title_no_name(self): self.darray.plot.hist() - self.assertEqual('', plt.gca().get_title()) + assert '' == plt.gca().get_title() def test_title_uses_name(self): self.darray.name = 'testpoints' self.darray.plot.hist() - self.assertIn(self.darray.name, plt.gca().get_title()) + assert self.darray.name in plt.gca().get_title() def test_ylabel_is_count(self): self.darray.plot.hist() - self.assertEqual('Count', plt.gca().get_ylabel()) + assert 'Count' == plt.gca().get_ylabel() def test_can_pass_in_kwargs(self): nbins = 5 self.darray.plot.hist(bins=nbins) - self.assertEqual(nbins, len(plt.gca().patches)) + assert nbins == len(plt.gca().patches) def test_can_pass_in_axis(self): self.pass_in_axis(self.darray.plot.hist) def test_primitive_returned(self): h = self.darray.plot.hist() - self.assertTrue(isinstance(h[-1][0], mpl.patches.Rectangle)) + assert isinstance(h[-1][0], mpl.patches.Rectangle) @pytest.mark.slow def test_plot_nans(self): @@ -360,20 +360,20 @@ def setUp(self): def test_robust(self): cmap_params = _determine_cmap_params(self.data, robust=True) - self.assertEqual(cmap_params['vmin'], np.percentile(self.data, 2)) - self.assertEqual(cmap_params['vmax'], np.percentile(self.data, 98)) - self.assertEqual(cmap_params['cmap'].name, 'viridis') - self.assertEqual(cmap_params['extend'], 'both') - self.assertIsNone(cmap_params['levels']) - self.assertIsNone(cmap_params['norm']) + assert cmap_params['vmin'] == np.percentile(self.data, 2) + assert cmap_params['vmax'] == np.percentile(self.data, 98) + assert cmap_params['cmap'].name == 'viridis' + assert cmap_params['extend'] == 'both' + assert cmap_params['levels'] is None + assert cmap_params['norm'] is None def test_center(self): cmap_params = _determine_cmap_params(self.data, center=0.5) - self.assertEqual(cmap_params['vmax'] - 0.5, 0.5 - cmap_params['vmin']) - self.assertEqual(cmap_params['cmap'], 'RdBu_r') - self.assertEqual(cmap_params['extend'], 'neither') - self.assertIsNone(cmap_params['levels']) - self.assertIsNone(cmap_params['norm']) + assert cmap_params['vmax'] - 0.5 == 0.5 - cmap_params['vmin'] + assert cmap_params['cmap'] == 'RdBu_r' + assert cmap_params['extend'] == 'neither' + assert cmap_params['levels'] is None + assert cmap_params['norm'] is None @pytest.mark.slow def test_integer_levels(self): @@ -382,36 +382,36 @@ def test_integer_levels(self): # default is to cover full data range but with no guarantee on Nlevels for level in np.arange(2, 10, dtype=int): cmap_params = _determine_cmap_params(data, levels=level) - self.assertEqual(cmap_params['vmin'], cmap_params['levels'][0]) - self.assertEqual(cmap_params['vmax'], cmap_params['levels'][-1]) - self.assertEqual(cmap_params['extend'], 'neither') + assert cmap_params['vmin'] == cmap_params['levels'][0] + assert cmap_params['vmax'] == cmap_params['levels'][-1] + assert cmap_params['extend'] == 'neither' # with min max we are more strict cmap_params = _determine_cmap_params(data, levels=5, vmin=0, vmax=5, cmap='Blues') - self.assertEqual(cmap_params['vmin'], 0) - self.assertEqual(cmap_params['vmax'], 5) - self.assertEqual(cmap_params['vmin'], cmap_params['levels'][0]) - self.assertEqual(cmap_params['vmax'], cmap_params['levels'][-1]) - self.assertEqual(cmap_params['cmap'].name, 'Blues') - self.assertEqual(cmap_params['extend'], 'neither') - self.assertEqual(cmap_params['cmap'].N, 4) - self.assertEqual(cmap_params['norm'].N, 5) + assert cmap_params['vmin'] == 0 + assert cmap_params['vmax'] == 5 + assert cmap_params['vmin'] == cmap_params['levels'][0] + assert cmap_params['vmax'] == cmap_params['levels'][-1] + assert cmap_params['cmap'].name == 'Blues' + assert cmap_params['extend'] == 'neither' + assert cmap_params['cmap'].N == 4 + assert cmap_params['norm'].N == 5 cmap_params = _determine_cmap_params(data, levels=5, vmin=0.5, vmax=1.5) - self.assertEqual(cmap_params['cmap'].name, 'viridis') - self.assertEqual(cmap_params['extend'], 'max') + assert cmap_params['cmap'].name == 'viridis' + assert cmap_params['extend'] == 'max' cmap_params = _determine_cmap_params(data, levels=5, vmin=1.5) - self.assertEqual(cmap_params['cmap'].name, 'viridis') - self.assertEqual(cmap_params['extend'], 'min') + assert cmap_params['cmap'].name == 'viridis' + assert cmap_params['extend'] == 'min' cmap_params = _determine_cmap_params(data, levels=5, vmin=1.3, vmax=1.5) - self.assertEqual(cmap_params['cmap'].name, 'viridis') - self.assertEqual(cmap_params['extend'], 'both') + assert cmap_params['cmap'].name == 'viridis' + assert cmap_params['extend'] == 'both' def test_list_levels(self): data = self.data + 1 @@ -420,10 +420,10 @@ def test_list_levels(self): # vmin and vmax should be ignored if levels are explicitly provided cmap_params = _determine_cmap_params(data, levels=orig_levels, vmin=0, vmax=3) - self.assertEqual(cmap_params['vmin'], 0) - self.assertEqual(cmap_params['vmax'], 5) - self.assertEqual(cmap_params['cmap'].N, 5) - self.assertEqual(cmap_params['norm'].N, 6) + assert cmap_params['vmin'] == 0 + assert cmap_params['vmax'] == 5 + assert cmap_params['cmap'].N == 5 + assert cmap_params['norm'].N == 6 for wrap_levels in [list, np.array, pd.Index, DataArray]: cmap_params = _determine_cmap_params( @@ -436,71 +436,71 @@ def test_divergentcontrol(self): # Default with positive data will be a normal cmap cmap_params = _determine_cmap_params(pos) - self.assertEqual(cmap_params['vmin'], 0) - self.assertEqual(cmap_params['vmax'], 1) - self.assertEqual(cmap_params['cmap'].name, "viridis") + assert cmap_params['vmin'] == 0 + assert cmap_params['vmax'] == 1 + assert cmap_params['cmap'].name == "viridis" # Default with negative data will be a divergent cmap cmap_params = _determine_cmap_params(neg) - self.assertEqual(cmap_params['vmin'], -0.9) - self.assertEqual(cmap_params['vmax'], 0.9) - self.assertEqual(cmap_params['cmap'], "RdBu_r") + assert cmap_params['vmin'] == -0.9 + assert cmap_params['vmax'] == 0.9 + assert cmap_params['cmap'] == "RdBu_r" # Setting vmin or vmax should prevent this only if center is false cmap_params = _determine_cmap_params(neg, vmin=-0.1, center=False) - self.assertEqual(cmap_params['vmin'], -0.1) - self.assertEqual(cmap_params['vmax'], 0.9) - self.assertEqual(cmap_params['cmap'].name, "viridis") + assert cmap_params['vmin'] == -0.1 + assert cmap_params['vmax'] == 0.9 + assert cmap_params['cmap'].name == "viridis" cmap_params = _determine_cmap_params(neg, vmax=0.5, center=False) - self.assertEqual(cmap_params['vmin'], -0.1) - self.assertEqual(cmap_params['vmax'], 0.5) - self.assertEqual(cmap_params['cmap'].name, "viridis") + assert cmap_params['vmin'] == -0.1 + assert cmap_params['vmax'] == 0.5 + assert cmap_params['cmap'].name == "viridis" # Setting center=False too cmap_params = _determine_cmap_params(neg, center=False) - self.assertEqual(cmap_params['vmin'], -0.1) - self.assertEqual(cmap_params['vmax'], 0.9) - self.assertEqual(cmap_params['cmap'].name, "viridis") + assert cmap_params['vmin'] == -0.1 + assert cmap_params['vmax'] == 0.9 + assert cmap_params['cmap'].name == "viridis" # However, I should still be able to set center and have a div cmap cmap_params = _determine_cmap_params(neg, center=0) - self.assertEqual(cmap_params['vmin'], -0.9) - self.assertEqual(cmap_params['vmax'], 0.9) - self.assertEqual(cmap_params['cmap'], "RdBu_r") + assert cmap_params['vmin'] == -0.9 + assert cmap_params['vmax'] == 0.9 + assert cmap_params['cmap'] == "RdBu_r" # Setting vmin or vmax alone will force symmetric bounds around center cmap_params = _determine_cmap_params(neg, vmin=-0.1) - self.assertEqual(cmap_params['vmin'], -0.1) - self.assertEqual(cmap_params['vmax'], 0.1) - self.assertEqual(cmap_params['cmap'], "RdBu_r") + assert cmap_params['vmin'] == -0.1 + assert cmap_params['vmax'] == 0.1 + assert cmap_params['cmap'] == "RdBu_r" cmap_params = _determine_cmap_params(neg, vmax=0.5) - self.assertEqual(cmap_params['vmin'], -0.5) - self.assertEqual(cmap_params['vmax'], 0.5) - self.assertEqual(cmap_params['cmap'], "RdBu_r") + assert cmap_params['vmin'] == -0.5 + assert cmap_params['vmax'] == 0.5 + assert cmap_params['cmap'] == "RdBu_r" cmap_params = _determine_cmap_params(neg, vmax=0.6, center=0.1) - self.assertEqual(cmap_params['vmin'], -0.4) - self.assertEqual(cmap_params['vmax'], 0.6) - self.assertEqual(cmap_params['cmap'], "RdBu_r") + assert cmap_params['vmin'] == -0.4 + assert cmap_params['vmax'] == 0.6 + assert cmap_params['cmap'] == "RdBu_r" # But this is only true if vmin or vmax are negative cmap_params = _determine_cmap_params(pos, vmin=-0.1) - self.assertEqual(cmap_params['vmin'], -0.1) - self.assertEqual(cmap_params['vmax'], 0.1) - self.assertEqual(cmap_params['cmap'], "RdBu_r") + assert cmap_params['vmin'] == -0.1 + assert cmap_params['vmax'] == 0.1 + assert cmap_params['cmap'] == "RdBu_r" cmap_params = _determine_cmap_params(pos, vmin=0.1) - self.assertEqual(cmap_params['vmin'], 0.1) - self.assertEqual(cmap_params['vmax'], 1) - self.assertEqual(cmap_params['cmap'].name, "viridis") + assert cmap_params['vmin'] == 0.1 + assert cmap_params['vmax'] == 1 + assert cmap_params['cmap'].name == "viridis" cmap_params = _determine_cmap_params(pos, vmax=0.5) - self.assertEqual(cmap_params['vmin'], 0) - self.assertEqual(cmap_params['vmax'], 0.5) - self.assertEqual(cmap_params['cmap'].name, "viridis") + assert cmap_params['vmin'] == 0 + assert cmap_params['vmax'] == 0.5 + assert cmap_params['cmap'].name == "viridis" # If both vmin and vmax are provided, output is non-divergent cmap_params = _determine_cmap_params(neg, vmin=-0.2, vmax=0.6) - self.assertEqual(cmap_params['vmin'], -0.2) - self.assertEqual(cmap_params['vmax'], 0.6) - self.assertEqual(cmap_params['cmap'].name, "viridis") + assert cmap_params['vmin'] == -0.2 + assert cmap_params['vmax'] == 0.6 + assert cmap_params['cmap'].name == "viridis" @requires_matplotlib @@ -518,24 +518,24 @@ def setUp(self): @pytest.mark.slow def test_recover_from_seaborn_jet_exception(self): pal = _color_palette('jet', 4) - self.assertTrue(type(pal) == np.ndarray) - self.assertEqual(len(pal), 4) + assert type(pal) == np.ndarray + assert len(pal) == 4 @pytest.mark.slow def test_build_discrete_cmap(self): for (cmap, levels, extend, filled) in [('jet', [0, 1], 'both', False), ('hot', [-4, 4], 'max', True)]: ncmap, cnorm = _build_discrete_cmap(cmap, levels, extend, filled) - self.assertEqual(ncmap.N, len(levels) - 1) - self.assertEqual(len(ncmap.colors), len(levels) - 1) - self.assertEqual(cnorm.N, len(levels)) + assert ncmap.N == len(levels) - 1 + assert len(ncmap.colors) == len(levels) - 1 + assert cnorm.N == len(levels) self.assertArrayEqual(cnorm.boundaries, levels) - self.assertEqual(max(levels), cnorm.vmax) - self.assertEqual(min(levels), cnorm.vmin) + assert max(levels) == cnorm.vmax + assert min(levels) == cnorm.vmin if filled: - self.assertEqual(ncmap.colorbar_extend, extend) + assert ncmap.colorbar_extend == extend else: - self.assertEqual(ncmap.colorbar_extend, 'max') + assert ncmap.colorbar_extend == 'max' @pytest.mark.slow def test_discrete_colormap_list_of_levels(self): @@ -546,13 +546,13 @@ def test_discrete_colormap_list_of_levels(self): for kind in ['imshow', 'pcolormesh', 'contourf', 'contour']: primitive = getattr(self.darray.plot, kind)(levels=levels) self.assertArrayEqual(levels, primitive.norm.boundaries) - self.assertEqual(max(levels), primitive.norm.vmax) - self.assertEqual(min(levels), primitive.norm.vmin) + assert max(levels) == primitive.norm.vmax + assert min(levels) == primitive.norm.vmin if kind != 'contour': - self.assertEqual(extend, primitive.cmap.colorbar_extend) + assert extend == primitive.cmap.colorbar_extend else: - self.assertEqual('max', primitive.cmap.colorbar_extend) - self.assertEqual(len(levels) - 1, len(primitive.cmap.colors)) + assert 'max' == primitive.cmap.colorbar_extend + assert len(levels) - 1 == len(primitive.cmap.colors) @pytest.mark.slow def test_discrete_colormap_int_levels(self): @@ -564,27 +564,27 @@ def test_discrete_colormap_int_levels(self): primitive = getattr(self.darray.plot, kind)(levels=levels, vmin=vmin, vmax=vmax) - self.assertGreaterEqual(levels, - len(primitive.norm.boundaries) - 1) + assert levels >= \ + len(primitive.norm.boundaries) - 1 if vmax is None: - self.assertGreaterEqual(primitive.norm.vmax, self.data_max) + assert primitive.norm.vmax >= self.data_max else: - self.assertGreaterEqual(primitive.norm.vmax, vmax) + assert primitive.norm.vmax >= vmax if vmin is None: - self.assertLessEqual(primitive.norm.vmin, self.data_min) + assert primitive.norm.vmin <= self.data_min else: - self.assertLessEqual(primitive.norm.vmin, vmin) + assert primitive.norm.vmin <= vmin if kind != 'contour': - self.assertEqual(extend, primitive.cmap.colorbar_extend) + assert extend == primitive.cmap.colorbar_extend else: - self.assertEqual('max', primitive.cmap.colorbar_extend) - self.assertGreaterEqual(levels, len(primitive.cmap.colors)) + assert 'max' == primitive.cmap.colorbar_extend + assert levels >= len(primitive.cmap.colors) def test_discrete_colormap_list_levels_and_vmin_or_vmax(self): levels = [0, 5, 10, 15] primitive = self.darray.plot(levels=levels, vmin=-3, vmax=20) - self.assertEqual(primitive.norm.vmax, max(levels)) - self.assertEqual(primitive.norm.vmin, min(levels)) + assert primitive.norm.vmax == max(levels) + assert primitive.norm.vmin == min(levels) class Common2dMixin: @@ -610,8 +610,8 @@ def setUp(self): def test_label_names(self): self.plotmethod() - self.assertEqual('x', plt.gca().get_xlabel()) - self.assertEqual('y', plt.gca().get_ylabel()) + assert 'x' == plt.gca().get_xlabel() + assert 'y' == plt.gca().get_ylabel() def test_1d_raises_valueerror(self): with raises_regex(ValueError, r'DataArray must be 2d'): @@ -638,14 +638,14 @@ def test_xyincrease_false_changes_axes(self): xlim = plt.gca().get_xlim() ylim = plt.gca().get_ylim() diffs = xlim[0] - 14, xlim[1] - 0, ylim[0] - 9, ylim[1] - 0 - self.assertTrue(all(abs(x) < 1 for x in diffs)) + assert all(abs(x) < 1 for x in diffs) def test_xyincrease_true_changes_axes(self): self.plotmethod(xincrease=True, yincrease=True) xlim = plt.gca().get_xlim() ylim = plt.gca().get_ylim() diffs = xlim[0] - 0, xlim[1] - 14, ylim[0] - 0, ylim[1] - 9 - self.assertTrue(all(abs(x) < 1 for x in diffs)) + assert all(abs(x) < 1 for x in diffs) def test_x_ticks_are_rotated_for_time(self): time = pd.date_range('2000-01-01', '2000-01-10') @@ -653,7 +653,7 @@ def test_x_ticks_are_rotated_for_time(self): [('xx', [1, 2]), ('t', time)]) a.plot(x='t') rotation = plt.gca().get_xticklabels()[0].get_rotation() - self.assertNotEqual(rotation, 0) + assert rotation != 0 def test_plot_nans(self): x1 = self.darray[:5] @@ -662,7 +662,7 @@ def test_plot_nans(self): clim1 = self.plotfunc(x1).get_clim() clim2 = self.plotfunc(x2).get_clim() - self.assertEqual(clim1, clim2) + assert clim1 == clim2 def test_can_plot_all_nans(self): # regression test for issue #1780 @@ -679,46 +679,46 @@ def test_disallows_rgb_arg(self): def test_viridis_cmap(self): cmap_name = self.plotmethod(cmap='viridis').get_cmap().name - self.assertEqual('viridis', cmap_name) + assert 'viridis' == cmap_name def test_default_cmap(self): cmap_name = self.plotmethod().get_cmap().name - self.assertEqual('RdBu_r', cmap_name) + assert 'RdBu_r' == cmap_name cmap_name = self.plotfunc(abs(self.darray)).get_cmap().name - self.assertEqual('viridis', cmap_name) + assert 'viridis' == cmap_name @requires_seaborn def test_seaborn_palette_as_cmap(self): cmap_name = self.plotmethod( levels=2, cmap='husl').get_cmap().name - self.assertEqual('husl', cmap_name) + assert 'husl' == cmap_name def test_can_change_default_cmap(self): cmap_name = self.plotmethod(cmap='Blues').get_cmap().name - self.assertEqual('Blues', cmap_name) + assert 'Blues' == cmap_name def test_diverging_color_limits(self): artist = self.plotmethod() vmin, vmax = artist.get_clim() - self.assertAlmostEqual(-vmin, vmax) + assert round(abs(-vmin-vmax), 7) == 0 def test_xy_strings(self): self.plotmethod('y', 'x') ax = plt.gca() - self.assertEqual('y', ax.get_xlabel()) - self.assertEqual('x', ax.get_ylabel()) + assert 'y' == ax.get_xlabel() + assert 'x' == ax.get_ylabel() def test_positional_coord_string(self): self.plotmethod(y='x') ax = plt.gca() - self.assertEqual('x', ax.get_ylabel()) - self.assertEqual('y', ax.get_xlabel()) + assert 'x' == ax.get_ylabel() + assert 'y' == ax.get_xlabel() self.plotmethod(x='x') ax = plt.gca() - self.assertEqual('x', ax.get_xlabel()) - self.assertEqual('y', ax.get_ylabel()) + assert 'x' == ax.get_xlabel() + assert 'y' == ax.get_ylabel() def test_bad_x_string_exception(self): with raises_regex( @@ -734,7 +734,7 @@ def test_bad_x_string_exception(self): def test_coord_strings(self): # 1d coords (same as dims) - self.assertEqual({'x', 'y'}, set(self.darray.dims)) + assert {'x', 'y'} == set(self.darray.dims) self.plotmethod(y='y', x='x') def test_non_linked_coords(self): @@ -743,11 +743,11 @@ def test_non_linked_coords(self): # Normal case, without transpose self.plotfunc(self.darray, x='x', y='newy') ax = plt.gca() - self.assertEqual('x', ax.get_xlabel()) - self.assertEqual('newy', ax.get_ylabel()) + assert 'x' == ax.get_xlabel() + assert 'newy' == ax.get_ylabel() # ax limits might change between plotfuncs # simply ensure that these high coords were passed over - self.assertTrue(np.min(ax.get_ylim()) > 100.) + assert np.min(ax.get_ylim()) > 100. def test_non_linked_coords_transpose(self): # plot with coordinate names that are not dimensions, @@ -757,11 +757,11 @@ def test_non_linked_coords_transpose(self): self.darray.coords['newy'] = self.darray.y + 150 self.plotfunc(self.darray, x='newy', y='x') ax = plt.gca() - self.assertEqual('newy', ax.get_xlabel()) - self.assertEqual('x', ax.get_ylabel()) + assert 'newy' == ax.get_xlabel() + assert 'x' == ax.get_ylabel() # ax limits might change between plotfuncs # simply ensure that these high coords were passed over - self.assertTrue(np.min(ax.get_xlim()) > 100.) + assert np.min(ax.get_xlim()) > 100. def test_default_title(self): a = DataArray(easy_array((4, 3, 2)), dims=['a', 'b', 'c']) @@ -769,53 +769,53 @@ def test_default_title(self): a.coords['d'] = u'foo' self.plotfunc(a.isel(c=1)) title = plt.gca().get_title() - self.assertTrue('c = 1, d = foo' == title or 'd = foo, c = 1' == title) + assert 'c = 1, d = foo' == title or 'd = foo, c = 1' == title def test_colorbar_default_label(self): self.darray.name = 'testvar' self.plotmethod(add_colorbar=True) - self.assertIn(self.darray.name, text_in_fig()) + assert self.darray.name in text_in_fig() def test_no_labels(self): self.darray.name = 'testvar' self.plotmethod(add_labels=False) alltxt = text_in_fig() for string in ['x', 'y', 'testvar']: - self.assertNotIn(string, alltxt) + assert string not in alltxt def test_colorbar_kwargs(self): # replace label self.darray.name = 'testvar' self.plotmethod(add_colorbar=True, cbar_kwargs={'label': 'MyLabel'}) alltxt = text_in_fig() - self.assertIn('MyLabel', alltxt) - self.assertNotIn('testvar', alltxt) + assert 'MyLabel' in alltxt + assert 'testvar' not in alltxt # you can use mapping types as well self.plotmethod(add_colorbar=True, cbar_kwargs=(('label', 'MyLabel'),)) alltxt = text_in_fig() - self.assertIn('MyLabel', alltxt) - self.assertNotIn('testvar', alltxt) + assert 'MyLabel' in alltxt + assert 'testvar' not in alltxt # change cbar ax fig, (ax, cax) = plt.subplots(1, 2) self.plotmethod(ax=ax, cbar_ax=cax, add_colorbar=True, - cbar_kwargs={'label': 'MyBar'}) - self.assertTrue(ax.has_data()) - self.assertTrue(cax.has_data()) + cbar_kwargs={'label':'MyBar'}) + assert ax.has_data() + assert cax.has_data() alltxt = text_in_fig() - self.assertIn('MyBar', alltxt) - self.assertNotIn('testvar', alltxt) + assert 'MyBar' in alltxt + assert 'testvar' not in alltxt # note that there are two ways to achieve this fig, (ax, cax) = plt.subplots(1, 2) self.plotmethod(ax=ax, add_colorbar=True, - cbar_kwargs={'label': 'MyBar', 'cax': cax}) - self.assertTrue(ax.has_data()) - self.assertTrue(cax.has_data()) + cbar_kwargs={'label':'MyBar', 'cax':cax}) + assert ax.has_data() + assert cax.has_data() alltxt = text_in_fig() - self.assertIn('MyBar', alltxt) - self.assertNotIn('testvar', alltxt) + assert 'MyBar' in alltxt + assert 'testvar' not in alltxt # see that no colorbar is respected self.plotmethod(add_colorbar=False) - self.assertNotIn('testvar', text_in_fig()) + assert 'testvar' not in text_in_fig() # check that error is raised pytest.raises(ValueError, self.plotmethod, add_colorbar=False, cbar_kwargs={'label': 'label'}) @@ -826,14 +826,14 @@ def test_verbose_facetgrid(self): g = xplt.FacetGrid(d, col='z') g.map_dataarray(self.plotfunc, 'x', 'y') for ax in g.axes.flat: - self.assertTrue(ax.has_data()) + assert ax.has_data() def test_2d_function_and_method_signature_same(self): func_sig = inspect.getcallargs(self.plotfunc, self.darray) method_sig = inspect.getcallargs(self.plotmethod) del method_sig['_PlotMethods_obj'] del func_sig['darray'] - self.assertEqual(func_sig, method_sig) + assert func_sig == method_sig def test_convenient_facetgrid(self): a = easy_array((10, 15, 4)) @@ -842,29 +842,29 @@ def test_convenient_facetgrid(self): self.assertArrayEqual(g.axes.shape, [2, 2]) for (y, x), ax in np.ndenumerate(g.axes): - self.assertTrue(ax.has_data()) + assert ax.has_data() if x == 0: - self.assertEqual('y', ax.get_ylabel()) + assert 'y' == ax.get_ylabel() else: - self.assertEqual('', ax.get_ylabel()) + assert '' == ax.get_ylabel() if y == 1: - self.assertEqual('x', ax.get_xlabel()) + assert 'x' == ax.get_xlabel() else: - self.assertEqual('', ax.get_xlabel()) + assert '' == ax.get_xlabel() # Infering labels g = self.plotfunc(d, col='z', col_wrap=2) self.assertArrayEqual(g.axes.shape, [2, 2]) for (y, x), ax in np.ndenumerate(g.axes): - self.assertTrue(ax.has_data()) + assert ax.has_data() if x == 0: - self.assertEqual('y', ax.get_ylabel()) + assert 'y' == ax.get_ylabel() else: - self.assertEqual('', ax.get_ylabel()) + assert '' == ax.get_ylabel() if y == 1: - self.assertEqual('x', ax.get_xlabel()) + assert 'x' == ax.get_xlabel() else: - self.assertEqual('', ax.get_xlabel()) + assert '' == ax.get_xlabel() def test_convenient_facetgrid_4d(self): a = easy_array((10, 15, 2, 3)) @@ -873,7 +873,7 @@ def test_convenient_facetgrid_4d(self): self.assertArrayEqual(g.axes.shape, [3, 2]) for ax in g.axes.flat: - self.assertTrue(ax.has_data()) + assert ax.has_data() def test_facetgrid_cmap(self): # Regression test for GH592 @@ -881,9 +881,9 @@ def test_facetgrid_cmap(self): d = DataArray(data, dims=['x', 'y', 'time']) fg = d.plot.pcolormesh(col='time') # check that all color limits are the same - self.assertEqual(len(set(m.get_clim() for m in fg._mappables)), 1) + assert len(set(m.get_clim() for m in fg._mappables)) == 1 # check that all colormaps are the same - self.assertEqual(len(set(m.get_cmap().name for m in fg._mappables)), 1) + assert len(set(m.get_cmap().name for m in fg._mappables)) == 1 @pytest.mark.slow @@ -894,46 +894,46 @@ class TestContourf(Common2dMixin, PlotTestCase): @pytest.mark.slow def test_contourf_called(self): # Having both statements ensures the test works properly - self.assertFalse(self.contourf_called(self.darray.plot.imshow)) - self.assertTrue(self.contourf_called(self.darray.plot.contourf)) + assert not self.contourf_called(self.darray.plot.imshow) + assert self.contourf_called(self.darray.plot.contourf) def test_primitive_artist_returned(self): artist = self.plotmethod() - self.assertTrue(isinstance(artist, mpl.contour.QuadContourSet)) + assert isinstance(artist, mpl.contour.QuadContourSet) @pytest.mark.slow def test_extend(self): artist = self.plotmethod() - self.assertEqual(artist.extend, 'neither') + assert artist.extend == 'neither' self.darray[0, 0] = -100 self.darray[-1, -1] = 100 artist = self.plotmethod(robust=True) - self.assertEqual(artist.extend, 'both') + assert artist.extend == 'both' self.darray[0, 0] = 0 self.darray[-1, -1] = 0 artist = self.plotmethod(vmin=-0, vmax=10) - self.assertEqual(artist.extend, 'min') + assert artist.extend == 'min' artist = self.plotmethod(vmin=-10, vmax=0) - self.assertEqual(artist.extend, 'max') + assert artist.extend == 'max' @pytest.mark.slow def test_2d_coord_names(self): self.plotmethod(x='x2d', y='y2d') # make sure labels came out ok ax = plt.gca() - self.assertEqual('x2d', ax.get_xlabel()) - self.assertEqual('y2d', ax.get_ylabel()) + assert 'x2d' == ax.get_xlabel() + assert 'y2d' == ax.get_ylabel() @pytest.mark.slow def test_levels(self): artist = self.plotmethod(levels=[-0.5, -0.4, 0.1]) - self.assertEqual(artist.extend, 'both') + assert artist.extend == 'both' artist = self.plotmethod(levels=3) - self.assertEqual(artist.extend, 'neither') + assert artist.extend == 'neither' @pytest.mark.slow @@ -947,27 +947,22 @@ def test_colors(self): def _color_as_tuple(c): return tuple(c[:3]) artist = self.plotmethod(colors='k') - self.assertEqual( - _color_as_tuple(artist.cmap.colors[0]), - (0.0, 0.0, 0.0)) + assert _color_as_tuple(artist.cmap.colors[0]) == \ + (0.0, 0.0, 0.0) artist = self.plotmethod(colors=['k', 'b']) - self.assertEqual( - _color_as_tuple(artist.cmap.colors[1]), - (0.0, 0.0, 1.0)) + assert _color_as_tuple(artist.cmap.colors[1]) == \ + (0.0, 0.0, 1.0) artist = self.darray.plot.contour(levels=[-0.5, 0., 0.5, 1.], colors=['k', 'r', 'w', 'b']) - self.assertEqual( - _color_as_tuple(artist.cmap.colors[1]), - (1.0, 0.0, 0.0)) - self.assertEqual( - _color_as_tuple(artist.cmap.colors[2]), - (1.0, 1.0, 1.0)) + assert _color_as_tuple(artist.cmap.colors[1]) == \ + (1.0, 0.0, 0.0) + assert _color_as_tuple(artist.cmap.colors[2]) == \ + (1.0, 1.0, 1.0) # the last color is now under "over" - self.assertEqual( - _color_as_tuple(artist.cmap._rgba_over), - (0.0, 0.0, 1.0)) + assert _color_as_tuple(artist.cmap._rgba_over) == \ + (0.0, 0.0, 1.0) def test_cmap_and_color_both(self): with pytest.raises(ValueError): @@ -982,8 +977,8 @@ def test_2d_coord_names(self): self.plotmethod(x='x2d', y='y2d') # make sure labels came out ok ax = plt.gca() - self.assertEqual('x2d', ax.get_xlabel()) - self.assertEqual('y2d', ax.get_ylabel()) + assert 'x2d' == ax.get_xlabel() + assert 'y2d' == ax.get_ylabel() def test_single_level(self): # this used to raise an error, but not anymore since @@ -998,19 +993,19 @@ class TestPcolormesh(Common2dMixin, PlotTestCase): def test_primitive_artist_returned(self): artist = self.plotmethod() - self.assertTrue(isinstance(artist, mpl.collections.QuadMesh)) + assert isinstance(artist, mpl.collections.QuadMesh) def test_everything_plotted(self): artist = self.plotmethod() - self.assertEqual(artist.get_array().size, self.darray.size) + assert artist.get_array().size == self.darray.size @pytest.mark.slow def test_2d_coord_names(self): self.plotmethod(x='x2d', y='y2d') # make sure labels came out ok ax = plt.gca() - self.assertEqual('x2d', ax.get_xlabel()) - self.assertEqual('y2d', ax.get_ylabel()) + assert 'x2d' == ax.get_xlabel() + assert 'y2d' == ax.get_ylabel() def test_dont_infer_interval_breaks_for_cartopy(self): # Regression for GH 781 @@ -1018,9 +1013,9 @@ def test_dont_infer_interval_breaks_for_cartopy(self): # Simulate a Cartopy Axis setattr(ax, 'projection', True) artist = self.plotmethod(x='x2d', y='y2d', ax=ax) - self.assertTrue(isinstance(artist, mpl.collections.QuadMesh)) + assert isinstance(artist, mpl.collections.QuadMesh) # Let cartopy handle the axis limits and artist size - self.assertTrue(artist.get_array().size <= self.darray.size) + assert artist.get_array().size <= self.darray.size @pytest.mark.slow @@ -1031,17 +1026,17 @@ class TestImshow(Common2dMixin, PlotTestCase): @pytest.mark.slow def test_imshow_called(self): # Having both statements ensures the test works properly - self.assertFalse(self.imshow_called(self.darray.plot.contourf)) - self.assertTrue(self.imshow_called(self.darray.plot.imshow)) + assert not self.imshow_called(self.darray.plot.contourf) + assert self.imshow_called(self.darray.plot.imshow) def test_xy_pixel_centered(self): self.darray.plot.imshow(yincrease=False) - self.assertTrue(np.allclose([-0.5, 14.5], plt.gca().get_xlim())) - self.assertTrue(np.allclose([9.5, -0.5], plt.gca().get_ylim())) + assert np.allclose([-0.5, 14.5], plt.gca().get_xlim()) + assert np.allclose([9.5, -0.5], plt.gca().get_ylim()) def test_default_aspect_is_auto(self): self.darray.plot.imshow() - self.assertEqual('auto', plt.gca().get_aspect()) + assert 'auto' == plt.gca().get_aspect() @pytest.mark.slow def test_cannot_change_mpl_aspect(self): @@ -1051,13 +1046,13 @@ def test_cannot_change_mpl_aspect(self): # with numbers we fall back to fig control self.darray.plot.imshow(size=5, aspect=2) - self.assertEqual('auto', plt.gca().get_aspect()) + assert 'auto' == plt.gca().get_aspect() assert tuple(plt.gcf().get_size_inches()) == (10, 5) @pytest.mark.slow def test_primitive_artist_returned(self): artist = self.plotmethod() - self.assertTrue(isinstance(artist, mpl.image.AxesImage)) + assert isinstance(artist, mpl.image.AxesImage) @pytest.mark.slow @requires_seaborn @@ -1074,21 +1069,21 @@ def test_plot_rgb_image(self): easy_array((10, 15, 3), start=0), dims=['y', 'x', 'band'], ).plot.imshow() - self.assertEqual(0, len(find_possible_colorbars())) + assert 0 == len(find_possible_colorbars()) def test_plot_rgb_image_explicit(self): DataArray( easy_array((10, 15, 3), start=0), dims=['y', 'x', 'band'], ).plot.imshow(y='y', x='x', rgb='band') - self.assertEqual(0, len(find_possible_colorbars())) + assert 0 == len(find_possible_colorbars()) def test_plot_rgb_faceted(self): DataArray( easy_array((2, 2, 10, 15, 3), start=0), dims=['a', 'b', 'y', 'x', 'band'], ).plot.imshow(row='a', col='b') - self.assertEqual(0, len(find_possible_colorbars())) + assert 0 == len(find_possible_colorbars()) def test_plot_rgba_image_transposed(self): # We can handle the color axis being in any position @@ -1130,22 +1125,22 @@ def test_no_args(self): # Don't want colorbar labeled with 'None' alltxt = text_in_fig() - self.assertNotIn('None', alltxt) + assert 'None' not in alltxt for ax in self.g.axes.flat: - self.assertTrue(ax.has_data()) + assert ax.has_data() @pytest.mark.slow def test_names_appear_somewhere(self): self.darray.name = 'testvar' self.g.map_dataarray(xplt.contourf, 'x', 'y') for k, ax in zip('abc', self.g.axes.flat): - self.assertEqual('z = {0}'.format(k), ax.get_title()) + assert 'z = {0}'.format(k) == ax.get_title() alltxt = text_in_fig() - self.assertIn(self.darray.name, alltxt) + assert self.darray.name in alltxt for label in ['x', 'y']: - self.assertIn(label, alltxt) + assert label in alltxt @pytest.mark.slow def test_text_not_super_long(self): @@ -1154,10 +1149,10 @@ def test_text_not_super_long(self): g.map_dataarray(xplt.contour, 'x', 'y') alltxt = text_in_fig() maxlen = max(len(txt) for txt in alltxt) - self.assertLess(maxlen, 50) + assert maxlen < 50 t0 = g.axes[0, 0].get_title() - self.assertTrue(t0.endswith('...')) + assert t0.endswith('...') @pytest.mark.slow def test_colorbar(self): @@ -1169,9 +1164,9 @@ def test_colorbar(self): for image in plt.gcf().findobj(mpl.image.AxesImage): clim = np.array(image.get_clim()) - self.assertTrue(np.allclose(expected, clim)) + assert np.allclose(expected, clim) - self.assertEqual(1, len(find_possible_colorbars())) + assert 1 == len(find_possible_colorbars()) @pytest.mark.slow def test_empty_cell(self): @@ -1179,8 +1174,8 @@ def test_empty_cell(self): g.map_dataarray(xplt.imshow, 'x', 'y') bottomright = g.axes[-1, -1] - self.assertFalse(bottomright.has_data()) - self.assertFalse(bottomright.get_visible()) + assert not bottomright.has_data() + assert not bottomright.get_visible() @pytest.mark.slow def test_norow_nocol_error(self): @@ -1228,7 +1223,7 @@ def test_robust(self): except ValueError: pass largest = max(abs(x) for x in numbers) - self.assertLess(largest, 21) + assert largest < 21 @pytest.mark.slow def test_can_set_vmin_vmax(self): @@ -1238,14 +1233,14 @@ def test_can_set_vmin_vmax(self): for image in plt.gcf().findobj(mpl.image.AxesImage): clim = np.array(image.get_clim()) - self.assertTrue(np.allclose(expected, clim)) + assert np.allclose(expected, clim) @pytest.mark.slow def test_can_set_norm(self): norm = mpl.colors.SymLogNorm(0.1) self.g.map_dataarray(xplt.imshow, 'x', 'y', norm=norm) for image in plt.gcf().findobj(mpl.image.AxesImage): - self.assertIs(image.norm, norm) + assert image.norm is norm @pytest.mark.slow def test_figure_size(self): @@ -1280,10 +1275,10 @@ def test_num_ticks(self): for ax in self.g.axes.flat: xticks = len(ax.get_xticks()) yticks = len(ax.get_yticks()) - self.assertLessEqual(xticks, maxticks) - self.assertLessEqual(yticks, maxticks) - self.assertGreaterEqual(xticks, nticks / 2.0) - self.assertGreaterEqual(yticks, nticks / 2.0) + assert xticks <= maxticks + assert yticks <= maxticks + assert xticks >= nticks / 2.0 + assert yticks >= nticks / 2.0 @pytest.mark.slow def test_map(self): @@ -1297,17 +1292,17 @@ def test_map_dataset(self): alltxt = text_in_fig() for label in ['x', 'y']: - self.assertIn(label, alltxt) + assert label in alltxt # everything has a label - self.assertNotIn('None', alltxt) + assert 'None' not in alltxt # colorbar can't be inferred automatically - self.assertNotIn('foo', alltxt) - self.assertEqual(0, len(find_possible_colorbars())) + assert 'foo' not in alltxt + assert 0 == len(find_possible_colorbars()) g.add_colorbar(label='colors!') - self.assertIn('colors!', text_in_fig()) - self.assertEqual(1, len(find_possible_colorbars())) + assert 'colors!' in text_in_fig() + assert 1 == len(find_possible_colorbars()) @pytest.mark.slow def test_set_axis_labels(self): @@ -1315,7 +1310,7 @@ def test_set_axis_labels(self): g.set_axis_labels('longitude', 'latitude') alltxt = text_in_fig() for label in ['longitude', 'latitude']: - self.assertIn(label, alltxt) + assert label in alltxt @pytest.mark.slow def test_facetgrid_colorbar(self): @@ -1323,13 +1318,13 @@ def test_facetgrid_colorbar(self): d = DataArray(a, dims=['y', 'x', 'z'], name='foo') d.plot.imshow(x='x', y='y', col='z') - self.assertEqual(1, len(find_possible_colorbars())) + assert 1 == len(find_possible_colorbars()) d.plot.imshow(x='x', y='y', col='z', add_colorbar=True) - self.assertEqual(1, len(find_possible_colorbars())) + assert 1 == len(find_possible_colorbars()) d.plot.imshow(x='x', y='y', col='z', add_colorbar=False) - self.assertEqual(0, len(find_possible_colorbars())) + assert 0 == len(find_possible_colorbars()) @pytest.mark.slow def test_facetgrid_polar(self): @@ -1354,17 +1349,17 @@ def setUp(self): @pytest.mark.slow def test_default_labels(self): g = xplt.FacetGrid(self.darray, col='col', row='row') - self.assertEqual((2, 3), g.axes.shape) + assert (2, 3) == g.axes.shape g.map_dataarray(xplt.imshow, 'x', 'y') # Rightmost column should be labeled for label, ax in zip(self.darray.coords['row'].values, g.axes[:, -1]): - self.assertTrue(substring_in_axes(label, ax)) + assert substring_in_axes(label, ax) # Top row should be labeled for label, ax in zip(self.darray.coords['col'].values, g.axes[0, :]): - self.assertTrue(substring_in_axes(label, ax)) + assert substring_in_axes(label, ax) class TestDatetimePlot(PlotTestCase): diff --git a/xarray/tests/test_utils.py b/xarray/tests/test_utils.py index 98dd21c5fa0..d806f5a895a 100644 --- a/xarray/tests/test_utils.py +++ b/xarray/tests/test_utils.py @@ -17,7 +17,7 @@ def new_method(): pass old_method = utils.alias(new_method, 'old_method') assert 'deprecated' in old_method.__doc__ - with self.assertWarns('deprecated'): + with pytest.warns('deprecated'): old_method() @@ -34,7 +34,7 @@ def test(self): ]: actual = utils.safe_cast_to_index(array) self.assertArrayEqual(expected, actual) - self.assertEqual(expected.dtype, actual.dtype) + assert expected.dtype == actual.dtype def test_multiindex_from_product_levels(): @@ -52,12 +52,9 @@ class TestArrayEquiv(TestCase): def test_0d(self): # verify our work around for pd.isnull not working for 0-dimensional # object arrays - self.assertTrue( - duck_array_ops.array_equiv(0, np.array(0, dtype=object))) - self.assertTrue( - duck_array_ops.array_equiv(np.nan, np.array(np.nan, dtype=object))) - self.assertFalse( - duck_array_ops.array_equiv(0, np.array(1, dtype=object))) + assert duck_array_ops.array_equiv(0, np.array(0, dtype=object)) + assert duck_array_ops.array_equiv(np.nan, np.array(np.nan, dtype=object)) + assert not duck_array_ops.array_equiv(0, np.array(1, dtype=object)) class TestDictionaries(TestCase): @@ -67,13 +64,13 @@ def setUp(self): self.z = {'a': 'Z'} def test_equivalent(self): - self.assertTrue(utils.equivalent(0, 0)) - self.assertTrue(utils.equivalent(np.nan, np.nan)) - self.assertTrue(utils.equivalent(0, np.array(0.0))) - self.assertTrue(utils.equivalent([0], np.array([0]))) - self.assertTrue(utils.equivalent(np.array([0]), [0])) - self.assertTrue(utils.equivalent(np.arange(3), 1.0 * np.arange(3))) - self.assertFalse(utils.equivalent(0, np.zeros(3))) + assert utils.equivalent(0, 0) + assert utils.equivalent(np.nan, np.nan) + assert utils.equivalent(0, np.array(0.0)) + assert utils.equivalent([0], np.array([0])) + assert utils.equivalent(np.array([0]), [0]) + assert utils.equivalent(np.arange(3), 1.0 * np.arange(3)) + assert not utils.equivalent(0, np.zeros(3)) def test_safe(self): # should not raise exception: @@ -84,9 +81,9 @@ def test_unsafe(self): utils.update_safety_check(self.x, self.z) def test_ordered_dict_intersection(self): - self.assertEqual({'b': 'B'}, - utils.ordered_dict_intersection(self.x, self.y)) - self.assertEqual({}, utils.ordered_dict_intersection(self.x, self.z)) + assert {'b': 'B'} == \ + utils.ordered_dict_intersection(self.x, self.y) + assert {} == utils.ordered_dict_intersection(self.x, self.z) def test_dict_equiv(self): x = OrderedDict() @@ -95,23 +92,23 @@ def test_dict_equiv(self): y = OrderedDict() y['b'] = np.array([1.0, 2.0, 3.0]) y['a'] = 3 - self.assertTrue(utils.dict_equiv(x, y)) # two nparrays are equal + assert utils.dict_equiv(x, y) # two nparrays are equal y['b'] = [1, 2, 3] # np.array not the same as a list - self.assertTrue(utils.dict_equiv(x, y)) # nparray == list + assert utils.dict_equiv(x, y) # nparray == list x['b'] = [1.0, 2.0, 3.0] - self.assertTrue(utils.dict_equiv(x, y)) # list vs. list + assert utils.dict_equiv(x, y) # list vs. list x['c'] = None - self.assertFalse(utils.dict_equiv(x, y)) # new key in x + assert not utils.dict_equiv(x, y) # new key in x x['c'] = np.nan y['c'] = np.nan - self.assertTrue(utils.dict_equiv(x, y)) # as intended, nan is nan + assert utils.dict_equiv(x, y) # as intended, nan is nan x['c'] = np.inf y['c'] = np.inf - self.assertTrue(utils.dict_equiv(x, y)) # inf == inf + assert utils.dict_equiv(x, y) # inf == inf y = dict(y) - self.assertTrue(utils.dict_equiv(x, y)) # different dict types are ok + assert utils.dict_equiv(x, y) # different dictionary types are fine y['b'] = 3 * np.arange(3) - self.assertFalse(utils.dict_equiv(x, y)) # unequal when arrays differ + assert not utils.dict_equiv(x, y) # not equal when arrays differ def test_frozen(self): x = utils.Frozen(self.x) @@ -121,28 +118,28 @@ def test_frozen(self): del x['a'] with pytest.raises(AttributeError): x.update(self.y) - self.assertEqual(x.mapping, self.x) - self.assertIn(repr(x), ("Frozen({'a': 'A', 'b': 'B'})", - "Frozen({'b': 'B', 'a': 'A'})")) + assert x.mapping == self.x + assert repr(x) in ("Frozen({'a': 'A', 'b': 'B'})", + "Frozen({'b': 'B', 'a': 'A'})") def test_sorted_keys_dict(self): x = {'a': 1, 'b': 2, 'c': 3} y = utils.SortedKeysDict(x) self.assertItemsEqual(y, ['a', 'b', 'c']) - self.assertEqual(repr(utils.SortedKeysDict()), - "SortedKeysDict({})") + assert repr(utils.SortedKeysDict()) == \ + "SortedKeysDict({})" def test_chain_map(self): m = utils.ChainMap({'x': 0, 'y': 1}, {'x': -100, 'z': 2}) - self.assertIn('x', m) - self.assertIn('y', m) - self.assertIn('z', m) - self.assertEqual(m['x'], 0) - self.assertEqual(m['y'], 1) - self.assertEqual(m['z'], 2) + assert 'x' in m + assert 'y' in m + assert 'z' in m + assert m['x'] == 0 + assert m['y'] == 1 + assert m['z'] == 2 m['x'] = 100 - self.assertEqual(m['x'], 100) - self.assertEqual(m.maps[0]['x'], 100) + assert m['x'] == 100 + assert m.maps[0]['x'] == 100 self.assertItemsEqual(['x', 'y', 'z'], m) @@ -154,31 +151,31 @@ def test_repr_object(): class Test_is_uniform_and_sorted(TestCase): def test_sorted_uniform(self): - self.assertTrue(utils.is_uniform_spaced(np.arange(5))) + assert utils.is_uniform_spaced(np.arange(5)) def test_sorted_not_uniform(self): - self.assertEqual(False, utils.is_uniform_spaced([-2, 1, 89])) + assert False == utils.is_uniform_spaced([-2, 1, 89]) def test_not_sorted_uniform(self): - self.assertEqual(False, utils.is_uniform_spaced([1, -1, 3])) + assert False == utils.is_uniform_spaced([1, -1, 3]) def test_not_sorted_not_uniform(self): - self.assertEqual(False, utils.is_uniform_spaced([4, 1, 89])) + assert False == utils.is_uniform_spaced([4, 1, 89]) def test_two_numbers(self): - self.assertTrue(utils.is_uniform_spaced([0, 1.7])) + assert utils.is_uniform_spaced([0, 1.7]) def test_relative_tolerance(self): - self.assertTrue(utils.is_uniform_spaced([0, 0.97, 2], rtol=0.1)) + assert utils.is_uniform_spaced([0, 0.97, 2], rtol=0.1) class Test_hashable(TestCase): def test_hashable(self): for v in [False, 1, (2, ), (3, 4), 'four']: - self.assertTrue(utils.hashable(v)) + assert utils.hashable(v) for v in [[5, 6], ['seven', '8'], {9: 'ten'}]: - self.assertFalse(utils.hashable(v)) + assert not utils.hashable(v) @requires_dask diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index c9c8f12486b..ebfda129250 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -34,26 +34,26 @@ class VariableSubclassTestCases(object): def test_properties(self): data = 0.5 * np.arange(10) v = self.cls(['time'], data, {'foo': 'bar'}) - self.assertEqual(v.dims, ('time',)) + assert v.dims == ('time',) self.assertArrayEqual(v.values, data) - self.assertEqual(v.dtype, float) - self.assertEqual(v.shape, (10,)) - self.assertEqual(v.size, 10) - self.assertEqual(v.sizes, {'time': 10}) - self.assertEqual(v.nbytes, 80) - self.assertEqual(v.ndim, 1) - self.assertEqual(len(v), 10) - self.assertEqual(v.attrs, {'foo': u'bar'}) + assert v.dtype == float + assert v.shape == (10,) + assert v.size == 10 + assert v.sizes == {'time': 10} + assert v.nbytes == 80 + assert v.ndim == 1 + assert len(v) == 10 + assert v.attrs == {'foo': u'bar'} def test_attrs(self): v = self.cls(['time'], 0.5 * np.arange(10)) - self.assertEqual(v.attrs, {}) + assert v.attrs == {} attrs = {'foo': 'bar'} v.attrs = attrs - self.assertEqual(v.attrs, attrs) - self.assertIsInstance(v.attrs, OrderedDict) + assert v.attrs == attrs + assert isinstance(v.attrs, OrderedDict) v.attrs['foo'] = 'baz' - self.assertEqual(v.attrs['foo'], 'baz') + assert v.attrs['foo'] == 'baz' def test_getitem_dict(self): v = self.cls(['x'], np.random.randn(5)) @@ -132,23 +132,23 @@ def _assertIndexedLikeNDArray(self, variable, expected_value0, """Given a 1-dimensional variable, verify that the variable is indexed like a numpy.ndarray. """ - self.assertEqual(variable[0].shape, ()) - self.assertEqual(variable[0].ndim, 0) - self.assertEqual(variable[0].size, 1) + assert variable[0].shape == () + assert variable[0].ndim == 0 + assert variable[0].size == 1 # test identity - self.assertTrue(variable.equals(variable.copy())) - self.assertTrue(variable.identical(variable.copy())) + assert variable.equals(variable.copy()) + assert variable.identical(variable.copy()) # check value is equal for both ndarray and Variable - self.assertEqual(variable.values[0], expected_value0) - self.assertEqual(variable[0].values, expected_value0) + assert variable.values[0] == expected_value0 + assert variable[0].values == expected_value0 # check type or dtype is consistent for both ndarray and Variable if expected_dtype is None: # check output type instead of array dtype - self.assertEqual(type(variable.values[0]), type(expected_value0)) - self.assertEqual(type(variable[0].values), type(expected_value0)) + assert type(variable.values[0]) == type(expected_value0) + assert type(variable[0].values) == type(expected_value0) elif expected_dtype is not False: - self.assertEqual(variable.values[0].dtype, expected_dtype) - self.assertEqual(variable[0].values.dtype, expected_dtype) + assert variable.values[0].dtype == expected_dtype + assert variable[0].values.dtype == expected_dtype def test_index_0d_int(self): for value, dtype in [(0, np.int_), @@ -232,14 +232,14 @@ def test_index_and_concat_datetime(self): [expected[i:(i + 1)] for i in range(10)], [expected[[i]] for i in range(10)]]: actual = Variable.concat(times, 't') - self.assertEqual(expected.dtype, actual.dtype) + assert expected.dtype == actual.dtype self.assertArrayEqual(expected, actual) def test_0d_time_data(self): # regression test for #105 x = self.cls('time', pd.date_range('2000-01-01', periods=5)) expected = np.datetime64('2000-01-01', 'ns') - self.assertEqual(x[0].values, expected) + assert x[0].values == expected def test_datetime64_conversion(self): times = pd.date_range('2000-01-01', periods=3) @@ -250,9 +250,9 @@ def test_datetime64_conversion(self): (times.to_pydatetime(), False), ]: v = self.cls(['t'], values) - self.assertEqual(v.dtype, np.dtype('datetime64[ns]')) + assert v.dtype == np.dtype('datetime64[ns]') self.assertArrayEqual(v.values, times.values) - self.assertEqual(v.values.dtype, np.dtype('datetime64[ns]')) + assert v.values.dtype == np.dtype('datetime64[ns]') same_source = source_ndarray(v.values) is source_ndarray(values) assert preserve_source == same_source @@ -265,28 +265,28 @@ def test_timedelta64_conversion(self): (times.to_pytimedelta(), False), ]: v = self.cls(['t'], values) - self.assertEqual(v.dtype, np.dtype('timedelta64[ns]')) + assert v.dtype == np.dtype('timedelta64[ns]') self.assertArrayEqual(v.values, times.values) - self.assertEqual(v.values.dtype, np.dtype('timedelta64[ns]')) + assert v.values.dtype == np.dtype('timedelta64[ns]') same_source = source_ndarray(v.values) is source_ndarray(values) assert preserve_source == same_source def test_object_conversion(self): data = np.arange(5).astype(str).astype(object) actual = self.cls('x', data) - self.assertEqual(actual.dtype, data.dtype) + assert actual.dtype == data.dtype def test_pandas_data(self): v = self.cls(['x'], pd.Series([0, 1, 2], index=[3, 2, 1])) self.assertVariableIdentical(v, v[[0, 1, 2]]) v = self.cls(['x'], pd.Index([0, 1, 2])) - self.assertEqual(v[0].values, v.values[0]) + assert v[0].values == v.values[0] def test_pandas_period_index(self): v = self.cls(['x'], pd.period_range(start='2000', periods=20, freq='B')) v = v.load() # for dask-based Variable - self.assertEqual(v[0], pd.Period('2000', freq='B')) + assert v[0] == pd.Period('2000', freq='B') assert "Period('2000-01-03', 'B')" in repr(v) def test_1d_math(self): @@ -326,15 +326,15 @@ def test_1d_math(self): # something complicated self.assertArrayEqual((v ** 2 * w - 1 + x).values, x ** 2 * y - 1 + x) # make sure dtype is preserved (for Index objects) - self.assertEqual(float, (+v).dtype) - self.assertEqual(float, (+v).values.dtype) - self.assertEqual(float, (0 + v).dtype) - self.assertEqual(float, (0 + v).values.dtype) + assert float == (+v).dtype + assert float == (+v).values.dtype + assert float == (0 + v).dtype + assert float == (0 + v).values.dtype # check types of returned data - self.assertIsInstance(+v, Variable) - self.assertNotIsInstance(+v, IndexVariable) - self.assertIsInstance(0 + v, Variable) - self.assertNotIsInstance(0 + v, IndexVariable) + assert isinstance(+v, Variable) + assert not isinstance(+v, IndexVariable) + assert isinstance(0 + v, Variable) + assert not isinstance(0 + v, IndexVariable) def test_1d_reduce(self): x = np.arange(5) @@ -342,7 +342,7 @@ def test_1d_reduce(self): actual = v.sum() expected = Variable((), 10) self.assertVariableIdentical(expected, actual) - self.assertIs(type(actual), Variable) + assert type(actual) is Variable def test_array_interface(self): x = np.arange(5) @@ -355,10 +355,16 @@ def test_array_interface(self): self.assertVariableIdentical( v.clip(2, 3), self.cls('x', x.clip(2, 3)).to_base_variable()) # test ufuncs +<<<<<<< HEAD self.assertVariableIdentical( np.sin(v), self.cls(['x'], np.sin(x)).to_base_variable()) self.assertIsInstance(np.sin(v), Variable) self.assertNotIsInstance(np.sin(v), IndexVariable) +======= + self.assertVariableIdentical(np.sin(v), self.cls(['x'], np.sin(x)).to_base_variable()) + assert isinstance(np.sin(v), Variable) + assert not isinstance(np.sin(v), IndexVariable) +>>>>>>> self_assert from unittest2pytest def example_1d_objects(self): for data in [range(3), @@ -372,21 +378,21 @@ def test___array__(self): for v, data in self.example_1d_objects(): self.assertArrayEqual(v.values, np.asarray(data)) self.assertArrayEqual(np.asarray(v), np.asarray(data)) - self.assertEqual(v[0].values, np.asarray(data)[0]) - self.assertEqual(np.asarray(v[0]), np.asarray(data)[0]) + assert v[0].values == np.asarray(data)[0] + assert np.asarray(v[0]) == np.asarray(data)[0] def test_equals_all_dtypes(self): for v, _ in self.example_1d_objects(): v2 = v.copy() - self.assertTrue(v.equals(v2)) - self.assertTrue(v.identical(v2)) - self.assertTrue(v.no_conflicts(v2)) - self.assertTrue(v[0].equals(v2[0])) - self.assertTrue(v[0].identical(v2[0])) - self.assertTrue(v[0].no_conflicts(v2[0])) - self.assertTrue(v[:2].equals(v2[:2])) - self.assertTrue(v[:2].identical(v2[:2])) - self.assertTrue(v[:2].no_conflicts(v2[:2])) + assert v.equals(v2) + assert v.identical(v2) + assert v.no_conflicts(v2) + assert v[0].equals(v2[0]) + assert v[0].identical(v2[0]) + assert v[0].no_conflicts(v2[0]) + assert v[:2].equals(v2[:2]) + assert v[:2].identical(v2[:2]) + assert v[:2].no_conflicts(v2[:2]) def test_eq_all_dtypes(self): # ensure that we don't choke on comparisons for which numpy returns @@ -410,7 +416,7 @@ def test_encoding_preserved(self): self.assertVariableIdentical(expected.to_base_variable(), actual.to_base_variable()) - self.assertEqual(expected.encoding, actual.encoding) + assert expected.encoding == actual.encoding def test_concat(self): x = np.arange(5) @@ -476,23 +482,23 @@ def test_concat_number_strings(self): actual = Variable.concat([a, b], dim='x') expected = Variable('x', np.arange(5).astype(str).astype(object)) self.assertVariableIdentical(expected, actual) - self.assertEqual(expected.dtype, object) - self.assertEqual(type(expected.values[0]), str) + assert expected.dtype == object + assert type(expected.values[0]) == str def test_copy(self): v = self.cls('x', 0.5 * np.arange(10), {'foo': 'bar'}) for deep in [True, False]: w = v.copy(deep=deep) - self.assertIs(type(v), type(w)) + assert type(v) is type(w) self.assertVariableIdentical(v, w) - self.assertEqual(v.dtype, w.dtype) + assert v.dtype == w.dtype if self.cls is Variable: if deep: - self.assertIsNot(source_ndarray(v.values), - source_ndarray(w.values)) + assert source_ndarray(v.values) is not \ + source_ndarray(w.values) else: - self.assertIs(source_ndarray(v.values), - source_ndarray(w.values)) + assert source_ndarray(v.values) is \ + source_ndarray(w.values) self.assertVariableIdentical(v, copy(v)) def test_copy_index(self): @@ -501,8 +507,8 @@ def test_copy_index(self): v = self.cls('x', midx) for deep in [True, False]: w = v.copy(deep=deep) - self.assertIsInstance(w._data, PandasIndexAdapter) - self.assertIsInstance(w.to_index(), pd.MultiIndex) + assert isinstance(w._data, PandasIndexAdapter) + assert isinstance(w.to_index(), pd.MultiIndex) self.assertArrayEqual(v._data.array, w._data.array) def test_real_and_imag(self): @@ -744,24 +750,24 @@ def test_data_and_values(self): v = Variable(['time', 'x'], self.d) self.assertArrayEqual(v.data, self.d) self.assertArrayEqual(v.values, self.d) - self.assertIs(source_ndarray(v.values), self.d) + assert source_ndarray(v.values) is self.d with pytest.raises(ValueError): # wrong size v.values = np.random.random(5) d2 = np.random.random((10, 3)) v.values = d2 - self.assertIs(source_ndarray(v.values), d2) + assert source_ndarray(v.values) is d2 d3 = np.random.random((10, 3)) v.data = d3 - self.assertIs(source_ndarray(v.data), d3) + assert source_ndarray(v.data) is d3 def test_numpy_same_methods(self): v = Variable([], np.float32(0.0)) - self.assertEqual(v.item(), 0) - self.assertIs(type(v.item()), float) + assert v.item() == 0 + assert type(v.item()) is float v = IndexVariable('x', np.arange(5)) - self.assertEqual(2, v.searchsorted(2)) + assert 2 == v.searchsorted(2) def test_datetime64_conversion_scalar(self): expected = np.datetime64('2000-01-01', 'ns') @@ -771,9 +777,9 @@ def test_datetime64_conversion_scalar(self): datetime(2000, 1, 1), ]: v = Variable([], values) - self.assertEqual(v.dtype, np.dtype('datetime64[ns]')) - self.assertEqual(v.values, expected) - self.assertEqual(v.values.dtype, np.dtype('datetime64[ns]')) + assert v.dtype == np.dtype('datetime64[ns]') + assert v.values == expected + assert v.values.dtype == np.dtype('datetime64[ns]') def test_timedelta64_conversion_scalar(self): expected = np.timedelta64(24 * 60 * 60 * 10 ** 9, 'ns') @@ -783,29 +789,29 @@ def test_timedelta64_conversion_scalar(self): timedelta(days=1), ]: v = Variable([], values) - self.assertEqual(v.dtype, np.dtype('timedelta64[ns]')) - self.assertEqual(v.values, expected) - self.assertEqual(v.values.dtype, np.dtype('timedelta64[ns]')) + assert v.dtype == np.dtype('timedelta64[ns]') + assert v.values == expected + assert v.values.dtype == np.dtype('timedelta64[ns]') def test_0d_str(self): v = Variable([], u'foo') - self.assertEqual(v.dtype, np.dtype('U3')) - self.assertEqual(v.values, 'foo') + assert v.dtype == np.dtype('U3') + assert v.values == 'foo' v = Variable([], np.string_('foo')) - self.assertEqual(v.dtype, np.dtype('S3')) - self.assertEqual(v.values, bytes('foo', 'ascii') if PY3 else 'foo') + assert v.dtype == np.dtype('S3') + assert v.values == bytes('foo', 'ascii') if PY3 else 'foo' def test_0d_datetime(self): v = Variable([], pd.Timestamp('2000-01-01')) - self.assertEqual(v.dtype, np.dtype('datetime64[ns]')) - self.assertEqual(v.values, np.datetime64('2000-01-01', 'ns')) + assert v.dtype == np.dtype('datetime64[ns]') + assert v.values == np.datetime64('2000-01-01', 'ns') def test_0d_timedelta(self): for td in [pd.to_timedelta('1s'), np.timedelta64(1, 's')]: v = Variable([], td) - self.assertEqual(v.dtype, np.dtype('timedelta64[ns]')) - self.assertEqual(v.values, np.timedelta64(10 ** 9, 'ns')) + assert v.dtype == np.dtype('timedelta64[ns]') + assert v.values == np.timedelta64(10 ** 9, 'ns') def test_equals_and_identical(self): d = np.random.rand(10, 3) @@ -814,62 +820,62 @@ def test_equals_and_identical(self): attrs={'att1': 3, 'att2': [1, 2, 3]}) v2 = Variable(('dim1', 'dim2'), data=d, attrs={'att1': 3, 'att2': [1, 2, 3]}) - self.assertTrue(v1.equals(v2)) - self.assertTrue(v1.identical(v2)) + assert v1.equals(v2) + assert v1.identical(v2) v3 = Variable(('dim1', 'dim3'), data=d) - self.assertFalse(v1.equals(v3)) + assert not v1.equals(v3) v4 = Variable(('dim1', 'dim2'), data=d) - self.assertTrue(v1.equals(v4)) - self.assertFalse(v1.identical(v4)) + assert v1.equals(v4) + assert not v1.identical(v4) v5 = deepcopy(v1) v5.values[:] = np.random.rand(10, 3) - self.assertFalse(v1.equals(v5)) + assert not v1.equals(v5) - self.assertFalse(v1.equals(None)) - self.assertFalse(v1.equals(d)) + assert not v1.equals(None) + assert not v1.equals(d) - self.assertFalse(v1.identical(None)) - self.assertFalse(v1.identical(d)) + assert not v1.identical(None) + assert not v1.identical(d) def test_broadcast_equals(self): v1 = Variable((), np.nan) v2 = Variable(('x'), [np.nan, np.nan]) - self.assertTrue(v1.broadcast_equals(v2)) - self.assertFalse(v1.equals(v2)) - self.assertFalse(v1.identical(v2)) + assert v1.broadcast_equals(v2) + assert not v1.equals(v2) + assert not v1.identical(v2) v3 = Variable(('x'), [np.nan]) - self.assertTrue(v1.broadcast_equals(v3)) - self.assertFalse(v1.equals(v3)) - self.assertFalse(v1.identical(v3)) + assert v1.broadcast_equals(v3) + assert not v1.equals(v3) + assert not v1.identical(v3) - self.assertFalse(v1.broadcast_equals(None)) + assert not v1.broadcast_equals(None) v4 = Variable(('x'), [np.nan] * 3) - self.assertFalse(v2.broadcast_equals(v4)) + assert not v2.broadcast_equals(v4) def test_no_conflicts(self): v1 = Variable(('x'), [1, 2, np.nan, np.nan]) v2 = Variable(('x'), [np.nan, 2, 3, np.nan]) - self.assertTrue(v1.no_conflicts(v2)) - self.assertFalse(v1.equals(v2)) - self.assertFalse(v1.broadcast_equals(v2)) - self.assertFalse(v1.identical(v2)) + assert v1.no_conflicts(v2) + assert not v1.equals(v2) + assert not v1.broadcast_equals(v2) + assert not v1.identical(v2) - self.assertFalse(v1.no_conflicts(None)) + assert not v1.no_conflicts(None) v3 = Variable(('y'), [np.nan, 2, 3, np.nan]) - self.assertFalse(v3.no_conflicts(v1)) + assert not v3.no_conflicts(v1) d = np.array([1, 2, np.nan, np.nan]) - self.assertFalse(v1.no_conflicts(d)) - self.assertFalse(v2.no_conflicts(d)) + assert not v1.no_conflicts(d) + assert not v2.no_conflicts(d) v4 = Variable(('w', 'x'), [d]) - self.assertTrue(v1.no_conflicts(v4)) + assert v1.no_conflicts(v4) def test_as_variable(self): data = np.arange(10) @@ -882,8 +888,8 @@ def test_as_variable(self): ds = Dataset({'x': expected}) var = as_variable(ds['x']).to_base_variable() self.assertVariableIdentical(expected, var) - self.assertNotIsInstance(ds['x'], Variable) - self.assertIsInstance(as_variable(ds['x']), Variable) + assert not isinstance(ds['x'], Variable) + assert isinstance(as_variable(ds['x']), Variable) FakeVariable = namedtuple('FakeVariable', 'values dims') fake_xarray = FakeVariable(expected.values, expected.dims) @@ -935,12 +941,12 @@ def test_repr(self): Attributes: foo: bar """).strip() - self.assertEqual(expected, repr(v)) + assert expected == repr(v) def test_repr_lazy_data(self): v = Variable('x', LazilyIndexedArray(np.arange(2e5))) - self.assertIn('200000 values with dtype', repr(v)) - self.assertIsInstance(v._data, LazilyIndexedArray) + assert '200000 values with dtype' in repr(v) + assert isinstance(v._data, LazilyIndexedArray) def test_detect_indexer_type(self): """ Tests indexer type was correctly detected. """ @@ -1041,7 +1047,7 @@ def test_items(self): iter(Variable([], 0)) # test setting v.values[:] = 0 - self.assertTrue(np.all(v.values == 0)) + assert np.all(v.values == 0) # test orthogonal setting v[range(10), range(11)] = 1 self.assertArrayEqual(v.values, np.ones((10, 11))) @@ -1103,7 +1109,7 @@ def test_shift(self): v = Variable('x', [1, 2, 3, 4, 5]) self.assertVariableIdentical(v, v.shift(x=0)) - self.assertIsNot(v, v.shift(x=0)) + assert v is not v.shift(x=0) expected = Variable('x', [np.nan, 1, 2, 3, 4]) self.assertVariableIdentical(expected, v.shift(x=1)) @@ -1136,7 +1142,7 @@ def test_roll(self): v = Variable('x', [1, 2, 3, 4, 5]) self.assertVariableIdentical(v, v.roll(x=0)) - self.assertIsNot(v, v.roll(x=0)) + assert v is not v.roll(x=0) expected = Variable('x', [5, 1, 2, 3, 4]) self.assertVariableIdentical(expected, v.roll(x=1)) @@ -1167,7 +1173,7 @@ def test_transpose(self): x = np.random.randn(2, 3, 4, 5) w = Variable(['a', 'b', 'c', 'd'], x) w2 = Variable(['d', 'b', 'c', 'a'], np.einsum('abcd->dbca', x)) - self.assertEqual(w2.shape, (5, 3, 4, 2)) + assert w2.shape == (5, 3, 4, 2) self.assertVariableIdentical(w2, w.transpose('d', 'b', 'c', 'a')) self.assertVariableIdentical(w, w2.transpose('a', 'b', 'c', 'd')) w3 = Variable(['b', 'c', 'd', 'a'], np.einsum('abcd->bcda', x)) @@ -1202,10 +1208,10 @@ def test_squeeze(self): def test_get_axis_num(self): v = Variable(['x', 'y', 'z'], np.random.randn(2, 3, 4)) - self.assertEqual(v.get_axis_num('x'), 0) - self.assertEqual(v.get_axis_num(['x']), (0,)) - self.assertEqual(v.get_axis_num(['x', 'y']), (0, 1)) - self.assertEqual(v.get_axis_num(['z', 'y', 'x']), (2, 1, 0)) + assert v.get_axis_num('x') == 0 + assert v.get_axis_num(['x']) == (0,) + assert v.get_axis_num(['x', 'y']) == (0, 1) + assert v.get_axis_num(['z', 'y', 'x']) == (2, 1, 0) with raises_regex(ValueError, 'not found in array dim'): v.get_axis_num('foobar') @@ -1346,9 +1352,9 @@ def test_inplace_math(self): v = Variable(['x'], x) v2 = v v2 += 1 - self.assertIs(v, v2) + assert v is v2 # since we provided an ndarray for data, it is also modified in-place - self.assertIs(source_ndarray(v.values), x) + assert source_ndarray(v.values) is x self.assertArrayEqual(v.values, np.arange(5) + 1) with raises_regex(ValueError, 'dimensions cannot change'): @@ -1470,13 +1476,13 @@ def test_reduce_keep_attrs(self): # Test dropped attrs vm = v.mean() - self.assertEqual(len(vm.attrs), 0) - self.assertEqual(vm.attrs, OrderedDict()) + assert len(vm.attrs) == 0 + assert vm.attrs == OrderedDict() # Test kept attrs vm = v.mean(keep_attrs=True) - self.assertEqual(len(vm.attrs), len(_attrs)) - self.assertEqual(vm.attrs, _attrs) + assert len(vm.attrs) == len(_attrs) + assert vm.attrs == _attrs def test_count(self): expected = Variable([], 3) @@ -1489,7 +1495,7 @@ def test_count(self): actual = Variable(['x'], [True, False, True]).count() self.assertVariableIdentical(expected, actual) - self.assertEqual(actual.dtype, int) + assert actual.dtype == int expected = Variable(['x'], [2, 3]) actual = Variable(['x', 'y'], [[1, 0, np.nan], [1, 1, 1]]).count('y') @@ -1498,7 +1504,7 @@ def test_count(self): def test_setitem(self): v = Variable(['x', 'y'], [[0, 3, 2], [3, 4, 5]]) v[0, 1] = 1 - self.assertTrue(v[0, 1] == 1) + assert v[0, 1] == 1 v = Variable(['x', 'y'], [[0, 3, 2], [3, 4, 5]]) v[dict(x=[0, 1])] = 1 @@ -1511,7 +1517,7 @@ def test_setitem(self): self.assertArrayEqual(v[0], np.ones_like(v[0])) v = Variable(['x', 'y'], [[0, 3, 2], [3, 4, 5]]) v[dict(x=[True, False], y=[False, True, False])] = 1 - self.assertTrue(v[0, 1] == 1) + assert v[0, 1] == 1 def test_setitem_fancy(self): # assignment which should work as np.ndarray does @@ -1644,26 +1650,26 @@ def test_init(self): def test_to_index(self): data = 0.5 * np.arange(10) v = IndexVariable(['time'], data, {'foo': 'bar'}) - self.assertTrue(pd.Index(data, name='time').identical(v.to_index())) + assert pd.Index(data, name='time').identical(v.to_index()) def test_multiindex_default_level_names(self): midx = pd.MultiIndex.from_product([['a', 'b'], [1, 2]]) v = IndexVariable(['x'], midx, {'foo': 'bar'}) - self.assertEqual(v.to_index().names, ('x_level_0', 'x_level_1')) + assert v.to_index().names == ('x_level_0', 'x_level_1') def test_data(self): x = IndexVariable('x', np.arange(3.0)) - self.assertIsInstance(x._data, PandasIndexAdapter) - self.assertIsInstance(x.data, np.ndarray) - self.assertEqual(float, x.dtype) + assert isinstance(x._data, PandasIndexAdapter) + assert isinstance(x.data, np.ndarray) + assert float == x.dtype self.assertArrayEqual(np.arange(3), x) - self.assertEqual(float, x.values.dtype) + assert float == x.values.dtype with raises_regex(TypeError, 'cannot be modified'): x[:] = 0 def test_name(self): coord = IndexVariable('x', [10.0]) - self.assertEqual(coord.name, 'x') + assert coord.name == 'x' with pytest.raises(AttributeError): coord.name = 'y' @@ -1672,9 +1678,9 @@ def test_level_names(self): midx = pd.MultiIndex.from_product([['a', 'b'], [1, 2]], names=['level_1', 'level_2']) x = IndexVariable('x', midx) - self.assertEqual(x.level_names, midx.names) + assert x.level_names == midx.names - self.assertIsNone(IndexVariable('y', [10.0]).level_names) + assert IndexVariable('y', [10.0]).level_names is None def test_get_level_variable(self): midx = pd.MultiIndex.from_product([['a', 'b'], [1, 2]], @@ -1709,9 +1715,9 @@ def test_concat_multiindex(self): assert isinstance(actual.to_index(), pd.MultiIndex) def test_coordinate_alias(self): - with self.assertWarns('deprecated'): + with pytest.warns('deprecated'): x = Coordinate('x', [1, 2, 3]) - self.assertIsInstance(x, IndexVariable) + assert isinstance(x, IndexVariable) # These tests make use of multi-dimensional variables, which are not valid # IndexVariable objects: @@ -1740,55 +1746,55 @@ def test_unchanged_types(self): pd.date_range('2000-01-01', periods=3), pd.date_range('2000-01-01', periods=3).values]: x = t(data) - self.assertIs(source_ndarray(x), - source_ndarray(as_compatible_data(x))) + assert source_ndarray(x) is \ + source_ndarray(as_compatible_data(x)) def test_converted_types(self): for input_array in [[[0, 1, 2]], pd.DataFrame([[0, 1, 2]])]: actual = as_compatible_data(input_array) self.assertArrayEqual(np.asarray(input_array), actual) - self.assertEqual(np.ndarray, type(actual)) - self.assertEqual(np.asarray(input_array).dtype, actual.dtype) + assert np.ndarray == type(actual) + assert np.asarray(input_array).dtype == actual.dtype def test_masked_array(self): original = np.ma.MaskedArray(np.arange(5)) expected = np.arange(5) actual = as_compatible_data(original) self.assertArrayEqual(expected, actual) - self.assertEqual(np.dtype(int), actual.dtype) + assert np.dtype(int) == actual.dtype original = np.ma.MaskedArray(np.arange(5), mask=4 * [False] + [True]) expected = np.arange(5.0) expected[-1] = np.nan actual = as_compatible_data(original) self.assertArrayEqual(expected, actual) - self.assertEqual(np.dtype(float), actual.dtype) + assert np.dtype(float) == actual.dtype def test_datetime(self): expected = np.datetime64('2000-01-01') actual = as_compatible_data(expected) - self.assertEqual(expected, actual) - self.assertEqual(np.ndarray, type(actual)) - self.assertEqual(np.dtype('datetime64[ns]'), actual.dtype) + assert expected == actual + assert np.ndarray == type(actual) + assert np.dtype('datetime64[ns]') == actual.dtype expected = np.array([np.datetime64('2000-01-01')]) actual = as_compatible_data(expected) - self.assertEqual(np.asarray(expected), actual) - self.assertEqual(np.ndarray, type(actual)) - self.assertEqual(np.dtype('datetime64[ns]'), actual.dtype) + assert np.asarray(expected) == actual + assert np.ndarray == type(actual) + assert np.dtype('datetime64[ns]') == actual.dtype expected = np.array([np.datetime64('2000-01-01', 'ns')]) actual = as_compatible_data(expected) - self.assertEqual(np.asarray(expected), actual) - self.assertEqual(np.ndarray, type(actual)) - self.assertEqual(np.dtype('datetime64[ns]'), actual.dtype) - self.assertIs(expected, source_ndarray(np.asarray(actual))) + assert np.asarray(expected) == actual + assert np.ndarray == type(actual) + assert np.dtype('datetime64[ns]') == actual.dtype + assert expected is source_ndarray(np.asarray(actual)) expected = np.datetime64('2000-01-01', 'ns') actual = as_compatible_data(datetime(2000, 1, 1)) - self.assertEqual(np.asarray(expected), actual) - self.assertEqual(np.ndarray, type(actual)) - self.assertEqual(np.dtype('datetime64[ns]'), actual.dtype) + assert np.asarray(expected) == actual + assert np.ndarray == type(actual) + assert np.dtype('datetime64[ns]') == actual.dtype def test_full_like(self): # For more thorough tests, see test_variable.py @@ -1810,11 +1816,11 @@ def test_full_like_dask(self): attrs={'foo': 'bar'}).chunk(((1, 1), (2,))) def check(actual, expect_dtype, expect_values): - self.assertEqual(actual.dtype, expect_dtype) - self.assertEqual(actual.shape, orig.shape) - self.assertEqual(actual.dims, orig.dims) - self.assertEqual(actual.attrs, orig.attrs) - self.assertEqual(actual.chunks, orig.chunks) + assert actual.dtype == expect_dtype + assert actual.shape == orig.shape + assert actual.dims == orig.dims + assert actual.attrs == orig.attrs + assert actual.chunks == orig.chunks self.assertArrayEqual(actual.values, expect_values) check(full_like(orig, 2), From 88ab9cc79ceafe4db4a0e5f39565a5c91465ed9b Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 00:14:52 -0500 Subject: [PATCH 04/41] pytest.warns needs a warning --- xarray/tests/test_coding_times.py | 2 +- xarray/tests/test_conventions.py | 2 +- xarray/tests/test_extensions.py | 2 +- xarray/tests/test_utils.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/xarray/tests/test_coding_times.py b/xarray/tests/test_coding_times.py index 5a7dda60922..305300d74cf 100644 --- a/xarray/tests/test_coding_times.py +++ b/xarray/tests/test_coding_times.py @@ -166,7 +166,7 @@ def test_decode_non_standard_calendar_single_element_fallback(self): dt = nc4.netcdftime.datetime(2001, 2, 29) for calendar in ['360_day', 'all_leap', '366_day']: num_time = nc4.date2num(dt, units, calendar) - with pytest.warns('Unable to decode time axis'): + with pytest.warns(Warning, 'Unable to decode time axis'): actual = coding.times.decode_cf_datetime(num_time, units, calendar=calendar) expected = np.asarray(nc4.num2date(num_time, units, calendar)) diff --git a/xarray/tests/test_conventions.py b/xarray/tests/test_conventions.py index 4ee1e8fb00f..68004158a85 100644 --- a/xarray/tests/test_conventions.py +++ b/xarray/tests/test_conventions.py @@ -196,7 +196,7 @@ def test_incompatible_attributes(self): def test_missing_fillvalue(self): v = Variable(['x'], np.array([np.nan, 1, 2, 3])) v.encoding = {'dtype': 'int16'} - with pytest.warns('floating point data as an integer'): + with pytest.warns(Warning, 'floating point data as an integer'): conventions.encode_cf_variable(v) def test_multidimensional_coordinates(self): diff --git a/xarray/tests/test_extensions.py b/xarray/tests/test_extensions.py index 15be5e7b622..818cc345731 100644 --- a/xarray/tests/test_extensions.py +++ b/xarray/tests/test_extensions.py @@ -55,7 +55,7 @@ def foo(self): del xr.Dataset.demo assert not hasattr(xr.Dataset, 'demo') - with pytest.warns('overriding a preexisting attribute'): + with pytest.warns(Warning, 'overriding a preexisting attribute'): @xr.register_dataarray_accessor('demo') class Foo(object): pass diff --git a/xarray/tests/test_utils.py b/xarray/tests/test_utils.py index d806f5a895a..d3c2c9a50f5 100644 --- a/xarray/tests/test_utils.py +++ b/xarray/tests/test_utils.py @@ -17,7 +17,7 @@ def new_method(): pass old_method = utils.alias(new_method, 'old_method') assert 'deprecated' in old_method.__doc__ - with pytest.warns('deprecated'): + with pytest.warns(Warning, 'deprecated'): old_method() From 51c1fd7e11d9b4d1b518923a67d8a4281089b03a Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 01:07:59 -0500 Subject: [PATCH 05/41] assert_equal --- xarray/tests/test_accessors.py | 18 +-- xarray/tests/test_backends.py | 37 +++-- xarray/tests/test_coding_times.py | 30 ++-- xarray/tests/test_combine.py | 8 +- xarray/tests/test_conventions.py | 28 ++-- xarray/tests/test_dask.py | 4 +- xarray/tests/test_dataarray.py | 178 +++++++++++------------ xarray/tests/test_dataset.py | 217 ++++++++++++++++------------ xarray/tests/test_duck_array_ops.py | 14 +- xarray/tests/test_indexing.py | 30 ++-- xarray/tests/test_plot.py | 34 ++--- xarray/tests/test_ufuncs.py | 2 +- xarray/tests/test_utils.py | 4 +- xarray/tests/test_variable.py | 154 ++++++++++---------- 14 files changed, 398 insertions(+), 360 deletions(-) diff --git a/xarray/tests/test_accessors.py b/xarray/tests/test_accessors.py index 4206b91db8c..e44f2aba826 100644 --- a/xarray/tests/test_accessors.py +++ b/xarray/tests/test_accessors.py @@ -36,10 +36,10 @@ def test_field_access(self): hours = xr.DataArray(self.times.hour, name='hour', coords=[self.times, ], dims=['time', ]) - self.assertDataArrayEqual(years, self.data.time.dt.year) - self.assertDataArrayEqual(months, self.data.time.dt.month) - self.assertDataArrayEqual(days, self.data.time.dt.day) - self.assertDataArrayEqual(hours, self.data.time.dt.hour) + assert_equal(years, self.data.time.dt.year) + assert_equal(months, self.data.time.dt.month) + assert_equal(days, self.data.time.dt.day) + assert_equal(hours, self.data.time.dt.hour) def test_not_datetime_type(self): nontime_data = self.data.copy() @@ -81,10 +81,10 @@ def test_dask_field_access(self): assert dask_hour.data.chunks == dask_chunks # Check the actual output from the accessors - self.assertDataArrayEqual(years, dask_year.compute()) - self.assertDataArrayEqual(months, dask_month.compute()) - self.assertDataArrayEqual(days, dask_day.compute()) - self.assertDataArrayEqual(hours, dask_hour.compute()) + assert_equal(years, dask_year.compute()) + assert_equal(months, dask_month.compute()) + assert_equal(days, dask_day.compute()) + assert_equal(hours, dask_hour.compute()) def test_seasons(self): dates = pd.date_range(start="2000/01/01", freq="M", periods=12) @@ -93,4 +93,4 @@ def test_seasons(self): "SON", "SON", "SON", "DJF"] seasons = xr.DataArray(seasons) - self.assertArrayEqual(seasons.values, dates.dt.season.values) + assert_equal(seasons.values, dates.dt.season.values) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index d50197d346f..e61625198bf 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -368,7 +368,7 @@ def test_roundtrip_example_1_netcdf(self): # will depend on the encoding used. For example, # without CF encoding 'actual' will end up with # a dtype attribute. - self.assertDatasetEqual(expected, actual) + assert_equal(expected, actual) def test_roundtrip_coordinates(self): original = Dataset({'foo': ('x', [0, 1])}, @@ -784,7 +784,7 @@ def test_open_group(self): # check equivalent ways to specify group for group in 'foo', '/foo', 'foo/', '/foo/': with open_dataset(tmp_file, group=group) as actual: - self.assertVariableEqual(actual['x'], expected['x']) + assert_equal(actual['x'], expected['x']) # check that missing group raises appropriate exception with pytest.raises(IOError): @@ -811,7 +811,7 @@ def test_open_subgroup(self): # check equivalent ways to specify group for group in 'foo/bar', '/foo/bar', 'foo/bar/', '/foo/bar/': with open_dataset(tmp_file, group=group) as actual: - self.assertVariableEqual(actual['x'], expected['x']) + assert_equal(actual['x'], expected['x']) def test_write_groups(self): data1 = create_test_data() @@ -885,7 +885,7 @@ def test_open_encodings(self): expected['time'] = ('time', time, {}, encoding) with open_dataset(tmp_file) as actual: - self.assertVariableEqual(actual['time'], expected['time']) + assert_equal(actual['time'], expected['time']) actual_encoding = dict((k, v) for k, v in iteritems(actual['time'].encoding) if k in expected['time'].encoding) @@ -932,7 +932,7 @@ def test_compression_encoding(self): # regression test for #156 expected = data.isel(dim1=0) with self.roundtrip(expected) as actual: - self.assertDatasetEqual(expected, actual) + assert_equal(expected, actual) def test_encoding_chunksizes_unlimited(self): # regression test for GH1225 @@ -947,7 +947,7 @@ def test_encoding_chunksizes_unlimited(self): 'original_shape': (3,), } with self.roundtrip(ds) as actual: - self.assertDatasetEqual(ds, actual) + assert_equal(ds, actual) def test_mask_and_scale(self): with create_tmp_file() as tmp_file: @@ -966,7 +966,7 @@ def test_mask_and_scale(self): expected = np.ma.array([-1, -1, 10, 10.1, 10.2], mask=[True, True, False, False, False]) actual = nc.variables['x'][:] - self.assertArrayEqual(expected, actual) + assert_equal(expected, actual) # now check xarray with open_dataset(tmp_file) as ds: @@ -1514,12 +1514,12 @@ def test_encoding_unlimited_dims(self): with self.roundtrip(ds, save_kwargs=dict(unlimited_dims=['y'])) as actual: assert actual.encoding['unlimited_dims'] == set('y') - self.assertDatasetEqual(ds, actual) + assert_equal(ds, actual) ds.encoding = {'unlimited_dims': ['y']} with self.roundtrip(ds) as actual: assert actual.encoding['unlimited_dims'] == set('y') - self.assertDatasetEqual(ds, actual) + assert_equal(ds, actual) class GenericNetCDFDataTestAutocloseTrue(GenericNetCDFDataTest): @@ -1547,7 +1547,7 @@ def test_array_type_after_indexing(self): def test_complex(self): expected = Dataset({'x': ('y', np.ones(5) + 1j * np.ones(5))}) with self.roundtrip(expected) as actual: - self.assertDatasetEqual(expected, actual) + assert_equal(expected, actual) @pytest.mark.xfail(reason='https://github.com/pydata/xarray/issues/535') def test_cross_engine_read_write_netcdf4(self): @@ -1577,11 +1577,11 @@ def test_encoding_unlimited_dims(self): with self.roundtrip(ds, save_kwargs=dict(unlimited_dims=['y'])) as actual: assert actual.encoding['unlimited_dims'] == set('y') - self.assertDatasetEqual(ds, actual) + assert_equal(ds, actual) ds.encoding = {'unlimited_dims': ['y']} with self.roundtrip(ds) as actual: assert actual.encoding['unlimited_dims'] == set('y') - self.assertDatasetEqual(ds, actual) + assert_equal(ds, actual) # tests pending h5netcdf fix @@ -2031,7 +2031,7 @@ def create_datasets(self, **kwargs): def test_cmp_local_file(self): with self.create_datasets() as (actual, expected): - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) # global attributes should be global attributes on the dataset assert 'NC_GLOBAL' not in actual.attrs @@ -2043,15 +2043,14 @@ def test_cmp_local_file(self): assert actual.attrs.keys() == expected.attrs.keys() with self.create_datasets() as (actual, expected): - self.assertDatasetEqual( - actual.isel(l=2), expected.isel(l=2)) # noqa: E741 + assert_equal(actual.isel(l=2), expected.isel(l=2)) with self.create_datasets() as (actual, expected): - self.assertDatasetEqual(actual.isel(i=0, j=-1), + assert_equal(actual.isel(i=0, j=-1), expected.isel(i=0, j=-1)) with self.create_datasets() as (actual, expected): - self.assertDatasetEqual(actual.isel(j=slice(1, 2)), + assert_equal(actual.isel(j=slice(1, 2)), expected.isel(j=slice(1, 2))) def test_compatible_to_netcdf(self): @@ -2061,12 +2060,12 @@ def test_compatible_to_netcdf(self): actual.to_netcdf(tmp_file) actual = open_dataset(tmp_file) actual['bears'] = actual['bears'].astype(str) - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) @requires_dask def test_dask(self): with self.create_datasets(chunks={'j': 2}) as (actual, expected): - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) @network diff --git a/xarray/tests/test_coding_times.py b/xarray/tests/test_coding_times.py index 305300d74cf..6155b311b32 100644 --- a/xarray/tests/test_coding_times.py +++ b/xarray/tests/test_coding_times.py @@ -59,7 +59,7 @@ def test_cf_datetime(self): calendar) if (isinstance(actual, np.ndarray) and np.issubdtype(actual.dtype, np.datetime64)): - # self.assertEqual(actual.dtype.kind, 'M') + # assert actual.dtype.kind == 'M' # For some reason, numpy 1.8 does not compare ns precision # datetime64 arrays as equal to arrays of datetime objects, # but it works for us precision. Thus, convert to us @@ -67,7 +67,7 @@ def test_cf_datetime(self): actual_cmp = actual.astype('M8[us]') else: actual_cmp = actual - self.assertArrayEqual(expected, actual_cmp) + assert_equal(expected, actual_cmp) encoded, _, _ = coding.times.encode_cf_datetime(actual, units, calendar) if '1-1-1' not in units: @@ -75,7 +75,7 @@ def test_cf_datetime(self): # units/encoding cannot be preserved in this case: # (Pdb) pd.to_datetime('1-1-1 00:00:0.0') # Timestamp('2001-01-01 00:00:00') - self.assertArrayEqual(num_dates, np.around(encoded, 1)) + assert_equal(num_dates, np.around(encoded, 1)) if (hasattr(num_dates, 'ndim') and num_dates.ndim == 1 and '1000' not in units): # verify that wrapping with a pandas.Index works @@ -83,7 +83,7 @@ def test_cf_datetime(self): # non-datetime64 compatible dates into a pandas.Index encoded, _, _ = coding.times.encode_cf_datetime( pd.Index(actual), units, calendar) - self.assertArrayEqual(num_dates, np.around(encoded, 1)) + assert_equal(num_dates, np.around(encoded, 1)) @requires_netCDF4 def test_decode_cf_datetime_overflow(self): @@ -108,7 +108,7 @@ def test_decode_cf_datetime_non_standard_units(self): # they cannot be parsed by netcdftime, but pd.Timestamp works units = 'hours since 1-1-1970' actual = coding.times.decode_cf_datetime(np.arange(100), units) - self.assertArrayEqual(actual, expected) + assert_equal(actual, expected) @requires_netCDF4 def test_decode_cf_datetime_non_iso_strings(self): @@ -120,7 +120,7 @@ def test_decode_cf_datetime_non_iso_strings(self): (np.arange(100), 'hours since 2000-01-01 0:00')] for num_dates, units in cases: actual = coding.times.decode_cf_datetime(num_dates, units) - self.assertArrayEqual(actual, expected) + assert_equal(actual, expected) @requires_netCDF4 def test_decode_non_standard_calendar(self): @@ -197,8 +197,8 @@ def test_decode_non_standard_calendar_multidim_time(self): actual = coding.times.decode_cf_datetime(mdim_time, units, calendar=calendar) assert actual.dtype == np.dtype('M8[ns]') - self.assertArrayEqual(actual[:, 0], expected1) - self.assertArrayEqual(actual[:, 1], expected2) + assert_equal(actual[:, 0], expected1) + assert_equal(actual[:, 1], expected2) @requires_netCDF4 def test_decode_non_standard_calendar_fallback(self): @@ -220,7 +220,7 @@ def test_decode_non_standard_calendar_fallback(self): str(w[0].message) assert actual.dtype == np.dtype('O') - self.assertArrayEqual(actual, expected) + assert_equal(actual, expected) @requires_netCDF4 def test_cf_datetime_nan(self): @@ -235,7 +235,7 @@ def test_cf_datetime_nan(self): warnings.filterwarnings('ignore', 'All-NaN') actual = coding.times.decode_cf_datetime(num_dates, units) expected = np.array(expected_list, dtype='datetime64[ns]') - self.assertArrayEqual(expected, actual) + assert_equal(expected, actual) @requires_netCDF4 def test_decoded_cf_datetime_array_2d(self): @@ -245,7 +245,7 @@ def test_decoded_cf_datetime_array_2d(self): result = coding.times.CFDatetimeCoder().decode(variable) assert result.dtype == 'datetime64[ns]' expected = pd.date_range('2000-01-01', periods=4).values.reshape(2, 2) - self.assertArrayEqual(np.asarray(result), expected) + assert_equal(np.asarray(result), expected) def test_infer_datetime_units(self): for dates, expected in [(pd.date_range('1900-01-01', periods=5), @@ -288,18 +288,18 @@ def test_cf_timedelta(self): expected = numbers actual, _ = coding.times.encode_cf_timedelta(timedeltas, units) - self.assertArrayEqual(expected, actual) + assert_equal(expected, actual) assert expected.dtype == actual.dtype if units is not None: expected = timedeltas actual = coding.times.decode_cf_timedelta(numbers, units) - self.assertArrayEqual(expected, actual) + assert_equal(expected, actual) assert expected.dtype == actual.dtype expected = np.timedelta64('NaT', 'ns') actual = coding.times.decode_cf_timedelta(np.array(np.nan), 'days') - self.assertArrayEqual(expected, actual) + assert_equal(expected, actual) def test_cf_timedelta_2d(self): timedeltas = ['1D', '2D', '3D'] @@ -310,7 +310,7 @@ def test_cf_timedelta_2d(self): expected = timedeltas actual = coding.times.decode_cf_timedelta(numbers, units) - self.assertArrayEqual(expected, actual) + assert_equal(expected, actual) assert expected.dtype == actual.dtype def test_infer_timedelta_units(self): diff --git a/xarray/tests/test_combine.py b/xarray/tests/test_combine.py index c6c3854936e..f154f151bf6 100644 --- a/xarray/tests/test_combine.py +++ b/xarray/tests/test_combine.py @@ -62,12 +62,12 @@ def rectify_dim_order(dataset): actual = concat(datasets, data[dim], coords='all') expected = np.array([data['extra'].values for _ in range(data.dims[dim])]) - self.assertArrayEqual(actual['extra'].values, expected) + assert_equal(actual['extra'].values, expected) actual = concat(datasets, data[dim], coords='different') - self.assertDataArrayEqual(data['extra'], actual['extra']) + assert_equal(data['extra'], actual['extra']) actual = concat(datasets, data[dim], coords='minimal') - self.assertDataArrayEqual(data['extra'], actual['extra']) + assert_equal(data['extra'], actual['extra']) # verify that the dim argument takes precedence over # concatenating dataset variables of the same name @@ -247,7 +247,7 @@ def test_concat(self): expected = DataArray(np.array([foo.values, bar.values]), dims=['w', 'x', 'y'], coords={'x': [0, 1]}) actual = concat([foo, bar], 'w') - self.assertDataArrayEqual(expected, actual) + assert_equal(expected, actual) # from iteration: grouped = [g for _, g in foo.groupby('x')] stacked = concat(grouped, ds['x']) diff --git a/xarray/tests/test_conventions.py b/xarray/tests/test_conventions.py index 68004158a85..816e918e31b 100644 --- a/xarray/tests/test_conventions.py +++ b/xarray/tests/test_conventions.py @@ -34,8 +34,8 @@ def test_wrapper_class(self): assert actual.size == expected.size assert actual.ndim == expected.ndim assert len(actual) == len(expected) - self.assertArrayEqual(expected, actual) - self.assertArrayEqual(expected[:1], actual[B[:1]]) + assert_equal(expected, actual) + assert_equal(expected[:1], actual[B[:1]]) with pytest.raises(IndexError): actual[B[:, :2]] @@ -59,34 +59,34 @@ def test_char_to_bytes(self): array = np.array([['a', 'b', 'c'], ['d', 'e', 'f']]) expected = np.array(['abc', 'def']) actual = conventions.char_to_bytes(array) - self.assertArrayEqual(actual, expected) + assert_equal(actual, expected) expected = np.array(['ad', 'be', 'cf']) actual = conventions.char_to_bytes(array.T) # non-contiguous - self.assertArrayEqual(actual, expected) + assert_equal(actual, expected) def test_char_to_bytes_ndim_zero(self): expected = np.array('a') actual = conventions.char_to_bytes(expected) - self.assertArrayEqual(actual, expected) + assert_equal(actual, expected) def test_char_to_bytes_size_zero(self): array = np.zeros((3, 0), dtype='S1') expected = np.array([b'', b'', b'']) actual = conventions.char_to_bytes(array) - self.assertArrayEqual(actual, expected) + assert_equal(actual, expected) def test_bytes_to_char(self): array = np.array([['ab', 'cd'], ['ef', 'gh']]) expected = np.array([[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]) actual = conventions.bytes_to_char(array) - self.assertArrayEqual(actual, expected) + assert_equal(actual, expected) expected = np.array([[['a', 'b'], ['e', 'f']], [['c', 'd'], ['g', 'h']]]) actual = conventions.bytes_to_char(array.T) - self.assertArrayEqual(actual, expected) + assert_equal(actual, expected) def test_vectorized_indexing(self): array = np.array([[b'a', b'b', b'c'], [b'd', b'e', b'f']], dtype='S') @@ -94,7 +94,7 @@ def test_vectorized_indexing(self): expected = np.array([[b'abc', b'def'], [b'def', b'abc']]) indexer = V[np.array([[0, 1], [1, 0]])] actual = stacked[indexer] - self.assertArrayEqual(actual, expected) + assert_equal(actual, expected) class TestBytesToStringArray(TestCase): @@ -109,8 +109,8 @@ def test_encoding(self): assert actual.shape == expected.shape assert actual.size == expected.size assert actual.ndim == expected.ndim - self.assertArrayEqual(expected, actual) - self.assertArrayEqual(expected[0], actual[B[0]]) + assert_equal(expected, actual) + assert_equal(expected[0], actual[B[0]]) def test_scalar(self): expected = np.array(u'abc', dtype=object) @@ -140,7 +140,7 @@ def test_booltype_array(self): x = np.array([1, 0, 1, 1, 0], dtype='i1') bx = conventions.BoolTypeArray(x) assert bx.dtype == np.bool - self.assertArrayEqual(bx, np.array([True, False, True, True, False], + assert_equal(bx, np.array([True, False, True, True, False], dtype=np.bool)) @@ -151,7 +151,7 @@ def test(self): a = conventions.NativeEndiannessArray(x) assert a.dtype == expected.dtype assert a.dtype == expected[:].dtype - self.assertArrayEqual(a, expected) + assert_equal(a, expected) def test_decode_cf_with_conflicting_fill_missing_value(): @@ -326,7 +326,7 @@ def test_decode_cf_datetime_transition_to_invalid(self): expected = [datetime(2000, 1, 1, 0, 0), datetime(2265, 10, 28, 0, 0)] - self.assertArrayEqual(ds_decoded.time.values, expected) + assert_equal(ds_decoded.time.values, expected) class CFEncodedInMemoryStore(WritableCFDataStore, InMemoryDataStore): diff --git a/xarray/tests/test_dask.py b/xarray/tests/test_dask.py index b64a37152f3..6cb882e58d9 100644 --- a/xarray/tests/test_dask.py +++ b/xarray/tests/test_dask.py @@ -64,7 +64,7 @@ def test_basics(self): v = self.lazy_var assert self.data is v.data assert self.data.chunks == v.chunks - self.assertArrayEqual(self.values, v) + assert_equal(self.values, v) def test_copy(self): self.assertLazyAndIdentical(self.eager_var, self.lazy_var.copy()) @@ -245,7 +245,7 @@ def assertLazyAndAllClose(self, expected, actual): self.assertLazyAnd(expected, actual, self.assertDataArrayAllClose) def assertLazyAndEqual(self, expected, actual): - self.assertLazyAnd(expected, actual, self.assertDataArrayEqual) + self.assertLazyAnd(expected, actual, assert_equal) def setUp(self): self.values = np.random.randn(4, 6) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 8428792c8dc..d8c0492aed4 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -63,16 +63,16 @@ def test_repr_multiindex(self): assert expected == repr(self.mda) def test_properties(self): - self.assertVariableEqual(self.dv.variable, self.v) - self.assertArrayEqual(self.dv.values, self.v.values) + assert_equal(self.dv.variable, self.v) + assert_equal(self.dv.values, self.v.values) for attr in ['dims', 'dtype', 'shape', 'size', 'nbytes', 'ndim', 'attrs']: assert getattr(self.dv, attr) == getattr(self.v, attr) assert len(self.dv) == len(self.v) - self.assertVariableEqual(self.dv.variable, self.v) + assert_equal(self.dv.variable, self.v) self.assertItemsEqual(list(self.dv.coords), list(self.ds.coords)) for k, v in iteritems(self.dv.coords): - self.assertArrayEqual(v, self.ds.coords[k]) + assert_equal(v, self.ds.coords[k]) with pytest.raises(AttributeError): self.dv.dataset assert isinstance(self.ds['x'].to_index(), pd.Index) @@ -85,10 +85,10 @@ def test_data_property(self): array = DataArray(np.zeros((3, 4))) actual = array.copy() actual.values = np.ones((3, 4)) - self.assertArrayEqual(np.ones((3, 4)), actual.values) + assert_equal(np.ones((3, 4)), actual.values) actual.data = 2 * np.ones((3, 4)) - self.assertArrayEqual(2 * np.ones((3, 4)), actual.data) - self.assertArrayEqual(actual.data, actual.values) + assert_equal(2 * np.ones((3, 4)), actual.data) + assert_equal(actual.data, actual.values) def test_indexes(self): array = DataArray(np.zeros((2, 3)), @@ -179,7 +179,7 @@ def test_name(self): copied = arr.copy() arr.name = 'bar' assert arr.name == 'bar' - self.assertDataArrayEqual(copied, arr) + assert_equal(copied, arr) actual = DataArray(IndexVariable('x', [3])) actual.name = 'y' @@ -311,16 +311,16 @@ def test_constructor_from_self_described(self): self.assertDataArrayIdentical(expected, actual) actual = DataArray(expected.values, actual.coords) - self.assertDataArrayEqual(expected, actual) + assert_equal(expected, actual) frame = pd.DataFrame(data, index=pd.Index(['a', 'b'], name='x'), columns=pd.Index([-1, -2], name='y')) actual = DataArray(frame) - self.assertDataArrayEqual(expected, actual) + assert_equal(expected, actual) series = pd.Series(data[0], index=pd.Index([-1, -2], name='y')) actual = DataArray(series) - self.assertDataArrayEqual(expected[0].reset_coords('x', drop=True), + assert_equal(expected[0].reset_coords('x', drop=True), actual) panel = pd.Panel({0: frame}) @@ -441,7 +441,7 @@ def test_getitem(self): for i in [I[:], I[...], I[x.values], I[x.variable], I[x], I[x, y], I[x.values > -1], I[x.variable > -1], I[x > -1], I[x > -1, y > -1]]: - self.assertVariableEqual(self.dv, self.dv[i]) + assert_equal(self.dv, self.dv[i]) for i in [I[0], I[:, 0], I[:3, :2], I[x.values[:3]], I[x.variable[:3]], I[x[:3]], I[x[:3], y[:4]], @@ -492,21 +492,21 @@ def test_getitem_dataarray(self): da = DataArray(np.arange(12).reshape((3, 4)), dims=['x', 'y']) ind = DataArray([[0, 1], [0, 1]], dims=['x', 'z']) actual = da[ind] - self.assertArrayEqual(actual, da.values[[[0, 1], [0, 1]], :]) + assert_equal(actual, da.values[[[0, 1], [0, 1]], :]) da = DataArray(np.arange(12).reshape((3, 4)), dims=['x', 'y'], coords={'x': [0, 1, 2], 'y': ['a', 'b', 'c', 'd']}) ind = xr.DataArray([[0, 1], [0, 1]], dims=['X', 'Y']) actual = da[ind] expected = da.values[[[0, 1], [0, 1]], :] - self.assertArrayEqual(actual, expected) + assert_equal(actual, expected) assert actual.dims == ('X', 'Y', 'y') # boolean indexing ind = xr.DataArray([True, True, False], dims=['x']) - self.assertDataArrayEqual(da[ind], da[[0, 1], :]) - self.assertDataArrayEqual(da[ind], da[[0, 1]]) - self.assertDataArrayEqual(da[ind], da[ind.values]) + assert_equal(da[ind], da[[0, 1], :]) + assert_equal(da[ind], da[[0, 1]]) + assert_equal(da[ind], da[ind.values]) def test_setitem(self): # basic indexing should work as numpy's indexing @@ -524,7 +524,7 @@ def test_setitem(self): dims=['x', 'y']) orig[t] = 1 expected[t] = 1 - self.assertArrayEqual(orig.values, expected) + assert_equal(orig.values, expected) def test_setitem_fancy(self): # vectorized indexing @@ -743,7 +743,7 @@ def test_isel_fancy(self): self.assertDataArrayIdentical(actual['b'], stations['b']) expected = da.variable[:, stations['dim2s'].variable, stations['dim1s'].variable] - self.assertArrayEqual(actual, expected) + assert_equal(actual, expected) def test_sel(self): self.ds['x'] = ('x', np.array(list('abcdefghij'))) @@ -770,17 +770,17 @@ def test_sel_dataarray(self): # along new dimension ind = DataArray(['a', 'b', 'c'], dims=['new_dim']) actual = da.sel(x=ind) - self.assertArrayEqual(actual, da.isel(x=[0, 1, 2])) + assert_equal(actual, da.isel(x=[0, 1, 2])) assert 'new_dim' in actual.dims # with coordinate ind = DataArray(['a', 'b', 'c'], dims=['new_dim'], coords={'new_dim': [0, 1, 2]}) actual = da.sel(x=ind) - self.assertArrayEqual(actual, da.isel(x=[0, 1, 2])) + assert_equal(actual, da.isel(x=[0, 1, 2])) assert 'new_dim' in actual.dims assert 'new_dim' in actual.coords - self.assertDataArrayEqual(actual['new_dim'].drop('x'), + assert_equal(actual['new_dim'].drop('x'), ind['new_dim']) def test_sel_no_index(self): @@ -1019,8 +1019,8 @@ def test_virtual_time_components(self): dates = pd.date_range('2000-01-01', periods=10) da = DataArray(np.arange(1, 11), [('time', dates)]) - self.assertArrayEqual(da['time.dayofyear'], da.values) - self.assertArrayEqual(da.coords['time.dayofyear'], da.values) + assert_equal(da['time.dayofyear'], da.values) + assert_equal(da.coords['time.dayofyear'], da.values) def test_coords(self): # use int64 to ensure repr() consistency on windows @@ -1435,18 +1435,18 @@ def test_dataset_getitem(self): self.assertDataArrayIdentical(dv, self.dv) def test_array_interface(self): - self.assertArrayEqual(np.asarray(self.dv), self.x) + assert_equal(np.asarray(self.dv), self.x) # test patched in methods - self.assertArrayEqual(self.dv.astype(float), self.v.astype(float)) + assert_equal(self.dv.astype(float), self.v.astype(float)) assert_array_equal(self.dv.argsort(), self.v.argsort()) assert_array_equal(self.dv.clip(2, 3), self.v.clip(2, 3)) # test ufuncs expected = deepcopy(self.ds) expected['foo'][:] = np.sin(self.x) - self.assertDataArrayEqual(expected['foo'], np.sin(self.dv)) + assert_equal(expected['foo'], np.sin(self.dv)) assert_array_equal(self.dv, np.maximum(self.v, self.dv)) bar = Variable(['x', 'y'], np.zeros((10, 20))) - self.assertDataArrayEqual(self.dv, np.maximum(self.dv, bar)) + assert_equal(self.dv, np.maximum(self.dv, bar)) def test_is_null(self): x = np.random.RandomState(42).randn(5, 6) @@ -1463,15 +1463,15 @@ def test_math(self): a = self.dv # variable math was already tested extensively, so let's just make sure # that all types are properly converted here - self.assertDataArrayEqual(a, +a) - self.assertDataArrayEqual(a, a + 0) - self.assertDataArrayEqual(a, 0 + a) - self.assertDataArrayEqual(a, a + 0 * v) - self.assertDataArrayEqual(a, 0 * v + a) - self.assertDataArrayEqual(a, a + 0 * x) - self.assertDataArrayEqual(a, 0 * x + a) - self.assertDataArrayEqual(a, a + 0 * a) - self.assertDataArrayEqual(a, 0 * a + a) + assert_equal(a, +a) + assert_equal(a, a + 0) + assert_equal(a, 0 + a) + assert_equal(a, a + 0 * v) + assert_equal(a, 0 * v + a) + assert_equal(a, a + 0 * x) + assert_equal(a, 0 * x + a) + assert_equal(a, a + 0 * a) + assert_equal(a, 0 * a + a) def test_math_automatic_alignment(self): a = DataArray(range(5), [('x', range(5))]) @@ -1499,7 +1499,7 @@ def test_inplace_math_basics(self): b += 1 assert b is a assert b.variable is v - self.assertArrayEqual(b.values, x) + assert_equal(b.values, x) assert source_ndarray(b.values) is x def test_inplace_math_automatic_alignment(self): @@ -1657,7 +1657,7 @@ def test_unstack_pandas_consistency(self): self.assertDataArrayIdentical(expected, actual) def test_transpose(self): - self.assertVariableEqual(self.dv.variable.transpose(), + assert_equal(self.dv.variable.transpose(), self.dv.transpose().variable) def test_squeeze(self): @@ -1772,7 +1772,7 @@ def test_reduce(self): expected = DataArray([0, 0], {'x': coords['x'], 'c': -999}, 'x') self.assertDataArrayIdentical(expected, actual) - self.assertVariableEqual(self.dv.reduce(np.mean, 'x').variable, + assert_equal(self.dv.reduce(np.mean, 'x').variable, self.v.reduce(np.mean, 'x')) orig = DataArray([[1, 0, np.nan], [3, 0, 3]], coords, dims=['x', 'y']) @@ -1787,7 +1787,7 @@ def test_reduce(self): actual = orig.mean(dim='x', skipna=True) expected = DataArray(orig.values.astype(int), dims=['x', 'y']).mean('x') - self.assertDataArrayEqual(actual, expected) + assert_equal(actual, expected) # skip due to bug in older versions of numpy.nanpercentile def test_quantile(self): @@ -1881,7 +1881,7 @@ def test_groupby_properties(self): expected_groups = {'a': range(0, 9), 'c': [9], 'b': range(10, 20)} self.assertItemsEqual(expected_groups.keys(), grouped.groups.keys()) for key in expected_groups: - self.assertArrayEqual(expected_groups[key], grouped.groups[key]) + assert_equal(expected_groups[key], grouped.groups[key]) assert 3 == len(grouped) def test_groupby_apply_identity(self): @@ -1971,7 +1971,7 @@ def test_groupby_apply_ndarray(self): array = self.make_groupby_example_array() grouped = array.groupby('abc') actual = grouped.apply(np.asarray) - self.assertDataArrayEqual(array, actual) + assert_equal(array, actual) def test_groupby_apply_changes_metadata(self): def change_metadata(x): @@ -1984,7 +1984,7 @@ def change_metadata(x): actual = grouped.apply(change_metadata) expected = array.copy() expected = change_metadata(expected) - self.assertDataArrayEqual(expected, actual) + assert_equal(expected, actual) def test_groupby_math(self): array = self.make_groupby_example_array() @@ -2608,25 +2608,25 @@ def test_to_pandas(self): # 0d actual = DataArray(42).to_pandas() expected = np.array(42) - self.assertArrayEqual(actual, expected) + assert_equal(actual, expected) # 1d values = np.random.randn(3) index = pd.Index(['a', 'b', 'c'], name='x') da = DataArray(values, coords=[index]) actual = da.to_pandas() - self.assertArrayEqual(actual.values, values) - self.assertArrayEqual(actual.index, index) - self.assertArrayEqual(actual.index.name, 'x') + assert_equal(actual.values, values) + assert_equal(actual.index, index) + assert_equal(actual.index.name, 'x') # 2d values = np.random.randn(3, 2) da = DataArray(values, coords=[('x', ['a', 'b', 'c']), ('y', [0, 1])], name='foo') actual = da.to_pandas() - self.assertArrayEqual(actual.values, values) - self.assertArrayEqual(actual.index, ['a', 'b', 'c']) - self.assertArrayEqual(actual.columns, [0, 1]) + assert_equal(actual.values, values) + assert_equal(actual.index, ['a', 'b', 'c']) + assert_equal(actual.columns, [0, 1]) # roundtrips for shape in [(3,), (3, 4), (3, 4, 5)]: @@ -2644,9 +2644,9 @@ def test_to_dataframe(self): [('B', [1, 2, 3]), ('A', list('cdef'))], name='foo') expected = arr.to_series() actual = arr.to_dataframe()['foo'] - self.assertArrayEqual(expected.values, actual.values) - self.assertArrayEqual(expected.name, actual.name) - self.assertArrayEqual(expected.index.values, actual.index.values) + assert_equal(expected.values, actual.values) + assert_equal(expected.name, actual.name) + assert_equal(expected.index.values, actual.index.values) # regression test for coords with different dimensions arr.coords['C'] = ('B', [-1, -2, -3]) @@ -2654,9 +2654,9 @@ def test_to_dataframe(self): expected['C'] = [-1] * 4 + [-2] * 4 + [-3] * 4 expected = expected[['C', 'foo']] actual = arr.to_dataframe() - self.assertArrayEqual(expected.values, actual.values) - self.assertArrayEqual(expected.columns.values, actual.columns.values) - self.assertArrayEqual(expected.index.values, actual.index.values) + assert_equal(expected.values, actual.values) + assert_equal(expected.columns.values, actual.columns.values) + assert_equal(expected.index.values, actual.index.values) arr.name = None # unnamed with raises_regex(ValueError, 'unnamed'): @@ -2666,8 +2666,8 @@ def test_to_pandas_name_matches_coordinate(self): # coordinate with same name as array arr = DataArray([1, 2, 3], dims='x', name='x') series = arr.to_series() - self.assertArrayEqual([1, 2, 3], series.values) - self.assertArrayEqual([0, 1, 2], series.index.values) + assert_equal([1, 2, 3], series.values) + assert_equal([0, 1, 2], series.index.values) assert 'x' == series.name assert 'x' == series.index.name @@ -2678,8 +2678,8 @@ def test_to_pandas_name_matches_coordinate(self): def test_to_and_from_series(self): expected = self.dv.to_dataframe()['foo'] actual = self.dv.to_series() - self.assertArrayEqual(expected.values, actual.values) - self.assertArrayEqual(expected.index.values, actual.index.values) + assert_equal(expected.values, actual.values) + assert_equal(expected.index.values, actual.index.values) assert 'foo' == actual.name # test roundtrip self.assertDataArrayIdentical( @@ -2790,17 +2790,17 @@ def test_to_masked_array(self): # Test round trip x_masked_2 = da.to_masked_array() da_2 = DataArray(x_masked_2) - self.assertArrayEqual(x_masked, x_masked_2) - self.assertDataArrayEqual(da, da_2) + assert_equal(x_masked, x_masked_2) + assert_equal(da, da_2) da_masked_array = da.to_masked_array(copy=True) assert isinstance(da_masked_array, np.ma.MaskedArray) # Test masks - self.assertArrayEqual(da_masked_array.mask, x_masked.mask) + assert_equal(da_masked_array.mask, x_masked.mask) # Test that mask is unpacked correctly - self.assertArrayEqual(da.values, x_masked.filled(np.nan)) + assert_equal(da.values, x_masked.filled(np.nan)) # Test that the underlying data (including nans) hasn't changed - self.assertArrayEqual(da_masked_array, x_masked.filled(np.nan)) + assert_equal(da_masked_array, x_masked.filled(np.nan)) # Test that copy=False gives access to values masked_array = da.to_masked_array(copy=False) @@ -2834,12 +2834,12 @@ def test_to_and_from_cdms2(self): expected_coords = [IndexVariable('distance', [-2, 2]), IndexVariable('time', [0, 1, 2])] actual = original.to_cdms2() - self.assertArrayEqual(actual, original) + assert_equal(actual, original) assert actual.id == original.name self.assertItemsEqual(actual.getAxisIds(), original.dims) for axis, coord in zip(actual.getAxisList(), expected_coords): assert axis.id == coord.name - self.assertArrayEqual(axis, coord.values) + assert_equal(axis, coord.values) assert actual.baz == original.attrs['baz'] component_times = actual.getAxis(1).asComponentTime() @@ -2876,7 +2876,7 @@ def test_to_and_from_iris(self): original.attrs['cell_methods'] = \ 'height: mean (comment: A cell method)' actual = original.to_iris() - self.assertArrayEqual(actual.data, original.data) + assert_equal(actual.data, original.data) assert actual.var_name == original.name self.assertItemsEqual([d.var_name for d in actual.dim_coords], original.dims) @@ -2948,7 +2948,7 @@ def test_to_and_from_iris_dask(self): # Be careful not to trigger the loading of the iris data actual_data = actual.core_data() if \ hasattr(actual, 'core_data') else actual.data - self.assertArrayEqual(actual_data, original.data) + assert_equal(actual_data, original.data) assert actual.var_name == original.name self.assertItemsEqual([d.var_name for d in actual.dim_coords], original.dims) @@ -3042,7 +3042,7 @@ def test_to_dataset_retains_keys(self): # convert to dateset and back again result = array.to_dataset('x').to_array(dim='x') - self.assertDatasetEqual(array, result) + assert_equal(array, result) def test__title_for_slice(self): array = DataArray(np.ones((4, 3, 2)), dims=['a', 'b', 'c'], @@ -3070,7 +3070,7 @@ def test_dataarray_diff_n1(self): da = DataArray(np.random.randn(3, 4), dims=['x', 'y']) actual = da.diff('y') expected = DataArray(np.diff(da.values, axis=1), dims=['x', 'y']) - self.assertDataArrayEqual(expected, actual) + assert_equal(expected, actual) def test_coordinate_diff(self): # regression test for GH634 @@ -3079,7 +3079,7 @@ def test_coordinate_diff(self): expected = DataArray([1] * 9, dims=['lon'], coords=[range(1, 10)], name='lon') actual = lon.diff('lon') - self.assertDataArrayEqual(expected, actual) + assert_equal(expected, actual) def test_shift(self): arr = DataArray([1, 2, 3], dims='x') @@ -3146,14 +3146,14 @@ def test_dot(self): actual = da.dot(dm) expected_vals = np.tensordot(da_vals, dm_vals, [2, 0]) expected = DataArray(expected_vals, coords=[x, y], dims=['x', 'y']) - self.assertDataArrayEqual(expected, actual) + assert_equal(expected, actual) # all shared dims actual = da.dot(da) expected_vals = np.tensordot(da_vals, da_vals, axes=([0, 1, 2], [0, 1, 2])) expected = DataArray(expected_vals) - self.assertDataArrayEqual(expected, actual) + assert_equal(expected, actual) # multiple shared dims dm_vals = np.arange(20 * 5 * 4).reshape((20, 5, 4)) @@ -3162,7 +3162,7 @@ def test_dot(self): actual = da.dot(dm) expected_vals = np.tensordot(da_vals, dm_vals, axes=([1, 2], [1, 2])) expected = DataArray(expected_vals, coords=[x, j], dims=['x', 'j']) - self.assertDataArrayEqual(expected, actual) + assert_equal(expected, actual) with pytest.raises(NotImplementedError): da.dot(dm.to_dataset(name='dm')) @@ -3183,7 +3183,7 @@ def test_binary_op_join_setting(self): missing_3, join=align_type) expected = xr.DataArray([np.nan, 2, 4, np.nan], [(dim, [0, 1, 2, 3])]) - self.assertDataArrayEqual(actual, expected) + assert_equal(actual, expected) def test_combine_first(self): ar0 = DataArray([[0, 0], [0, 0]], [('x', ['a', 'b']), ('y', [-1, 0])]) @@ -3193,17 +3193,17 @@ def test_combine_first(self): actual = ar0.combine_first(ar1) expected = DataArray([[0, 0, np.nan], [0, 0, 1], [np.nan, 1, 1]], [('x', ['a', 'b', 'c']), ('y', [-1, 0, 1])]) - self.assertDataArrayEqual(actual, expected) + assert_equal(actual, expected) actual = ar1.combine_first(ar0) expected = DataArray([[0, 0, np.nan], [0, 1, 1], [np.nan, 1, 1]], [('x', ['a', 'b', 'c']), ('y', [-1, 0, 1])]) - self.assertDataArrayEqual(actual, expected) + assert_equal(actual, expected) actual = ar0.combine_first(ar2) expected = DataArray([[0, 0], [0, 0], [2, 2]], [('x', ['a', 'b', 'd']), ('y', [-1, 0])]) - self.assertDataArrayEqual(actual, expected) + assert_equal(actual, expected) def test_sortby(self): da = DataArray([[1, 2], [3, 4], [5, 6]], @@ -3218,34 +3218,34 @@ def test_sortby(self): expected = sorted1d dax = DataArray([100, 99, 98], [('x', ['c', 'b', 'a'])]) actual = da.sortby(dax) - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) # test descending order sort actual = da.sortby(dax, ascending=False) - self.assertDatasetEqual(actual, da) + assert_equal(actual, da) # test alignment (fills in nan for 'c') dax_short = DataArray([98, 97], [('x', ['b', 'a'])]) actual = da.sortby(dax_short) - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) # test multi-dim sort by 1D dataarray values expected = sorted2d dax = DataArray([100, 99, 98], [('x', ['c', 'b', 'a'])]) day = DataArray([90, 80], [('y', [1, 0])]) actual = da.sortby([day, dax]) - self.assertDataArrayEqual(actual, expected) + assert_equal(actual, expected) if LooseVersion(np.__version__) < LooseVersion('1.11.0'): pytest.skip('numpy 1.11.0 or later to support object data-type.') expected = sorted1d actual = da.sortby('x') - self.assertDataArrayEqual(actual, expected) + assert_equal(actual, expected) expected = sorted2d actual = da.sortby(['x', 'y']) - self.assertDataArrayEqual(actual, expected) + assert_equal(actual, expected) @requires_bottleneck def test_rank(self): @@ -3253,8 +3253,8 @@ def test_rank(self): ar = DataArray([[3, 4, np.nan, 1]]) expect_0 = DataArray([[1, 1, np.nan, 1]]) expect_1 = DataArray([[2, 3, np.nan, 1]]) - self.assertDataArrayEqual(ar.rank('dim_0'), expect_0) - self.assertDataArrayEqual(ar.rank('dim_1'), expect_1) + assert_equal(ar.rank('dim_0'), expect_0) + assert_equal(ar.rank('dim_1'), expect_1) # int x = DataArray([3, 2, 1]) self.assertDataArrayEqual(x.rank('dim_0'), x) @@ -3264,7 +3264,7 @@ def test_rank(self): x = DataArray([3.0, 1.0, np.nan, 2.0, 4.0], dims=('z',)) y = DataArray([0.75, 0.25, np.nan, 0.5, 1.0], dims=('z',)) - self.assertDataArrayEqual(y.rank('z', pct=True), y) + assert_equal(y.rank('z', pct=True), y) @pytest.fixture(params=[1]) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 3f1a00438b7..c48b55476a2 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -324,14 +324,14 @@ def test_constructor_pandas_sequence(self): ) ds_based_on_pandas = Dataset(pandas_objs, ds.coords, attrs=ds.attrs) del ds_based_on_pandas['x'] - self.assertDatasetEqual(ds, ds_based_on_pandas) + assert_equal(ds, ds_based_on_pandas) # reindex pandas obj, check align works rearranged_index = reversed(pandas_objs['foo'].index) pandas_objs['foo'] = pandas_objs['foo'].reindex(rearranged_index) ds_based_on_pandas = Dataset(pandas_objs, ds.coords, attrs=ds.attrs) del ds_based_on_pandas['x'] - self.assertDatasetEqual(ds, ds_based_on_pandas) + assert_equal(ds, ds_based_on_pandas) def test_constructor_pandas_single(self): @@ -345,7 +345,7 @@ def test_constructor_pandas_single(self): pandas_obj = a.to_pandas() ds_based_on_pandas = Dataset(pandas_obj) for dim in ds_based_on_pandas.data_vars: - self.assertArrayEqual(ds_based_on_pandas[dim], pandas_obj[dim]) + assert_equal(ds_based_on_pandas[dim], pandas_obj[dim]) def test_constructor_compat(self): data = OrderedDict([('x', DataArray(0, coords={'y': 1})), @@ -477,7 +477,7 @@ def test_variable(self): a['bar'] = (('time', 'x',), d) # order of creation is preserved assert list(a.variables) == ['foo', 'bar'] - self.assertArrayEqual(a['foo'].values, d) + assert_equal(a['foo'].values, d) # try to add variable with dim (10,3) with data that's (3,10) with pytest.raises(ValueError): a['qux'] = (('time', 'x'), d.T) @@ -557,11 +557,11 @@ def test_coords_modify(self): actual = data.copy(deep=True) actual.coords['x'] = ('x', ['a', 'b']) - self.assertArrayEqual(actual['x'], ['a', 'b']) + assert_equal(actual['x'], ['a', 'b']) actual = data.copy(deep=True) actual.coords['z'] = ('z', ['a', 'b']) - self.assertArrayEqual(actual['z'], ['a', 'b']) + assert_equal(actual['z'], ['a', 'b']) actual = data.copy(deep=True) with raises_regex(ValueError, 'conflicting sizes'): @@ -986,10 +986,10 @@ def test_isel_fancy(self): stations['dim2s'].variable] expected_var3 = data['var3'].variable[slice(None), stations['dim1s'].variable] - self.assertDataArrayEqual(actual['a'].drop('dim2'), stations['a']) - self.assertArrayEqual(actual['var1'], expected_var1) - self.assertArrayEqual(actual['var2'], expected_var2) - self.assertArrayEqual(actual['var3'], expected_var3) + assert_equal(actual['a'].drop('dim2'), stations['a']) + assert_equal(actual['var1'], expected_var1) + assert_equal(actual['var2'], expected_var2) + assert_equal(actual['var3'], expected_var3) def test_isel_dataarray(self): """ Test for indexing by DataArray """ @@ -1076,30 +1076,30 @@ def test_sel(self): loc_slicers = {'dim1': slice(None, None, 2), 'dim2': slice(0, 0.5), 'dim3': slice('a', 'c')} - self.assertDatasetEqual(data.isel(**int_slicers), + assert_equal(data.isel(**int_slicers), data.sel(**loc_slicers)) data['time'] = ('time', pd.date_range('2000-01-01', periods=20)) - self.assertDatasetEqual(data.isel(time=0), + assert_equal(data.isel(time=0), data.sel(time='2000-01-01')) - self.assertDatasetEqual(data.isel(time=slice(10)), + assert_equal(data.isel(time=slice(10)), data.sel(time=slice('2000-01-01', '2000-01-10'))) - self.assertDatasetEqual(data, data.sel(time=slice('1999', '2005'))) + assert_equal(data, data.sel(time=slice('1999', '2005'))) times = pd.date_range('2000-01-01', periods=3) - self.assertDatasetEqual(data.isel(time=slice(3)), + assert_equal(data.isel(time=slice(3)), data.sel(time=times)) - self.assertDatasetEqual(data.isel(time=slice(3)), + assert_equal(data.isel(time=slice(3)), data.sel(time=(data['time.dayofyear'] <= 3))) td = pd.to_timedelta(np.arange(3), unit='days') data = Dataset({'x': ('td', np.arange(3)), 'td': td}) - self.assertDatasetEqual(data, data.sel(td=td)) - self.assertDatasetEqual(data, data.sel(td=slice('3 days'))) - self.assertDatasetEqual(data.isel(td=0), + assert_equal(data, data.sel(td=td)) + assert_equal(data, data.sel(td=slice('3 days'))) + assert_equal(data.isel(td=0), data.sel(td=pd.Timedelta('0 days'))) - self.assertDatasetEqual(data.isel(td=0), + assert_equal(data.isel(td=0), data.sel(td=pd.Timedelta('0h'))) - self.assertDatasetEqual(data.isel(td=slice(1, 3)), + assert_equal(data.isel(td=slice(1, 3)), data.sel(td=slice('1 days', '2 days'))) def test_sel_dataarray(self): @@ -1107,14 +1107,14 @@ def test_sel_dataarray(self): ind = DataArray([0.0, 0.5, 1.0], dims=['dim2']) actual = data.sel(dim2=ind) - self.assertDatasetEqual(actual, data.isel(dim2=[0, 1, 2])) + assert_equal(actual, data.isel(dim2=[0, 1, 2])) # with different dimension ind = DataArray([0.0, 0.5, 1.0], dims=['new_dim']) actual = data.sel(dim2=ind) expected = data.isel(dim2=Variable('new_dim', [0, 1, 2])) assert 'new_dim' in actual.dims - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) # Multi-dimensional ind = DataArray([[0.0], [0.5], [1.0]], dims=['new_dim', 'new_dim2']) @@ -1123,7 +1123,7 @@ def test_sel_dataarray(self): [[0], [1], [2]])) assert 'new_dim' in actual.dims assert 'new_dim2' in actual.dims - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) # with coordinate ind = DataArray([0.0, 0.5, 1.0], dims=['new_dim'], @@ -1132,9 +1132,9 @@ def test_sel_dataarray(self): expected = data.isel(dim2=[0, 1, 2]).rename({'dim2': 'new_dim'}) assert 'new_dim' in actual.dims assert 'new_dim' in actual.coords - self.assertDatasetEqual(actual.drop('new_dim').drop('dim2'), + assert_equal(actual.drop('new_dim').drop('dim2'), expected.drop('new_dim')) - self.assertDataArrayEqual(actual['new_dim'].drop('dim2'), + assert_equal(actual['new_dim'].drop('dim2'), ind['new_dim']) # with conflicted coordinate (silently ignored) @@ -1142,18 +1142,18 @@ def test_sel_dataarray(self): coords={'dim2': ['a', 'b', 'c']}) actual = data.sel(dim2=ind) expected = data.isel(dim2=[0, 1, 2]) - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) # with conflicted coordinate (silently ignored) ind = DataArray([0.0, 0.5, 1.0], dims=['new_dim'], coords={'new_dim': ['a', 'b', 'c'], 'dim2': 3}) actual = data.sel(dim2=ind) - self.assertDataArrayEqual(actual['new_dim'].drop('dim2'), + assert_equal(actual['new_dim'].drop('dim2'), ind['new_dim'].drop('dim2')) expected = data.isel(dim2=[0, 1, 2]) expected['dim2'] = (('new_dim'), expected['dim2'].values) - self.assertDataArrayEqual(actual['dim2'].drop('new_dim'), + assert_equal(actual['dim2'].drop('new_dim'), expected['dim2']) assert actual['var1'].dims == ('dim1', 'new_dim') @@ -1164,7 +1164,7 @@ def test_sel_dataarray(self): 'new_dim': ('dim2', [1.1, 1.2, 1.3])}) actual = data.sel(dim2=ind) expected = data.isel(dim2=[0, 1, 2]) - self.assertDatasetEqual(actual.drop('new_dim'), expected) + assert_equal(actual.drop('new_dim'), expected) assert np.allclose(actual['new_dim'].values, ind['new_dim'].values) def test_sel_dataarray_mindex(self): @@ -1403,7 +1403,7 @@ def test_sel_fancy(self): idx_y = DataArray([0, 2, 1], dims=['b'], coords={'b': [0, 3, 6]}) expected_ary = data['foo'][[0, 1, 2], [0, 2, 1]] actual = data.sel(x=idx_x, y=idx_y) - self.assertArrayEqual(expected_ary, actual['foo']) + assert_equal(expected_ary, actual['foo']) self.assertDataArrayIdentical(actual['a'].drop('x'), idx_x['a']) self.assertDataArrayIdentical(actual['b'].drop('y'), idx_y['b']) @@ -1624,31 +1624,46 @@ def test_align(self): union = list('abcdefghijkl') left2, right2 = align(left, right, join='inner') - self.assertArrayEqual(left2['dim3'], intersection) + assert_equal(left2['dim3'], intersection) self.assertDatasetIdentical(left2, right2) left2, right2 = align(left, right, join='outer') +<<<<<<< HEAD self.assertVariableEqual(left2['dim3'].variable, right2['dim3'].variable) self.assertArrayEqual(left2['dim3'], union) +======= + assert_equal(left2['dim3'].variable, right2['dim3'].variable) + assert_equal(left2['dim3'], union) +>>>>>>> assert_equal self.assertDatasetIdentical(left2.sel(dim3=intersection), right2.sel(dim3=intersection)) assert np.isnan(left2['var3'][-2:]).all() assert np.isnan(right2['var3'][:2]).all() left2, right2 = align(left, right, join='left') +<<<<<<< HEAD self.assertVariableEqual(left2['dim3'].variable, right2['dim3'].variable) self.assertVariableEqual(left2['dim3'].variable, left['dim3'].variable) +======= + assert_equal(left2['dim3'].variable, right2['dim3'].variable) + assert_equal(left2['dim3'].variable, left['dim3'].variable) +>>>>>>> assert_equal self.assertDatasetIdentical(left2.sel(dim3=intersection), right2.sel(dim3=intersection)) assert np.isnan(right2['var3'][:2]).all() left2, right2 = align(left, right, join='right') +<<<<<<< HEAD self.assertVariableEqual(left2['dim3'].variable, right2['dim3'].variable) self.assertVariableEqual(left2['dim3'].variable, right['dim3'].variable) +======= + assert_equal(left2['dim3'].variable, right2['dim3'].variable) + assert_equal(left2['dim3'].variable, right['dim3'].variable) +>>>>>>> assert_equal self.assertDatasetIdentical(left2.sel(dim3=intersection), right2.sel(dim3=intersection)) assert np.isnan(left2['var3'][-2:]).all() @@ -1808,13 +1823,13 @@ def test_variable_indexing(self): v = data['var1'] d1 = data['dim1'] d2 = data['dim2'] - self.assertVariableEqual(v, v[d1.values]) - self.assertVariableEqual(v, v[d1]) - self.assertVariableEqual(v[:3], v[d1 < 3]) - self.assertVariableEqual(v[:, 3:], v[:, d2 >= 1.5]) - self.assertVariableEqual(v[:3, 3:], v[d1 < 3, d2 >= 1.5]) - self.assertVariableEqual(v[:3, :2], v[range(3), range(2)]) - self.assertVariableEqual(v[:3, :2], v.loc[d1[:3], d2[:2]]) + assert_equal(v, v[d1.values]) + assert_equal(v, v[d1]) + assert_equal(v[:3], v[d1 < 3]) + assert_equal(v[:, 3:], v[:, d2 >= 1.5]) + assert_equal(v[:3, 3:], v[d1 < 3, d2 >= 1.5]) + assert_equal(v[:3, :2], v[range(3), range(2)]) + assert_equal(v[:3, :2], v.loc[d1[:3], d2[:2]]) def test_drop_variables(self): data = create_test_data() @@ -1888,7 +1903,7 @@ def test_rename(self): if name in dims: dims[dims.index(name)] = newname - self.assertVariableEqual(Variable(dims, v.values, v.attrs), + assert_equal(Variable(dims, v.values, v.attrs), renamed[k].variable.to_base_variable()) assert v.encoding == renamed[k].encoding assert type(v) == type(renamed.variables[k]) @@ -1935,7 +1950,7 @@ def test_rename_inplace(self): assert not data.equals(copied) assert data.dims == {'y': 3, 't': 3} # check virtual variables - self.assertArrayEqual(data['t.dayofyear'], [1, 2, 3]) + assert_equal(data['t.dayofyear'], [1, 2, 3]) def test_swap_dims(self): original = Dataset({'x': [1, 2, 3], 'y': ('x', list('abc')), 'z': 42}) @@ -2151,7 +2166,7 @@ def test_update_auto_align(self): def test_getitem(self): data = create_test_data() assert isinstance(data['var1'], DataArray) - self.assertVariableEqual(data['var1'].variable, data.variables['var1']) + assert_equal(data['var1'].variable, data.variables['var1']) with pytest.raises(KeyError): data['notfound'] with pytest.raises(KeyError): @@ -2159,7 +2174,7 @@ def test_getitem(self): actual = data[['var1', 'var2']] expected = Dataset({'var1': data['var1'], 'var2': data['var2']}) - self.assertDatasetEqual(expected, actual) + assert_equal(expected, actual) actual = data['numbers'] expected = DataArray(data['numbers'].variable, @@ -2198,17 +2213,17 @@ def test_virtual_variables_time(self): expected = DataArray(1 + np.arange(20), coords=[data['time']], dims='time', name='dayofyear') self.assertDataArrayIdentical(expected, data['time.dayofyear']) - self.assertArrayEqual(data['time.month'].values, + assert_equal(data['time.month'].values, data.variables['time'].to_index().month) - self.assertArrayEqual(data['time.season'].values, 'DJF') + assert_equal(data['time.season'].values, 'DJF') # test virtual variable math - self.assertArrayEqual(data['time.dayofyear'] + 1, 2 + np.arange(20)) - self.assertArrayEqual(np.sin(data['time.dayofyear']), + assert_equal(data['time.dayofyear'] + 1, 2 + np.arange(20)) + assert_equal(np.sin(data['time.dayofyear']), np.sin(1 + np.arange(20))) # ensure they become coordinates expected = Dataset({}, {'dayofyear': data['time.dayofyear']}) actual = data[['time.dayofyear']] - self.assertDatasetEqual(expected, actual) + assert_equal(expected, actual) # non-coordinate variables ds = Dataset({'t': ('x', pd.date_range('2000-01-01', periods=3))}) assert (ds['t.year'] == 2000).all() @@ -2242,15 +2257,24 @@ def test_virtual_variable_multiindex(self): def test_time_season(self): ds = Dataset({'t': pd.date_range('2000-01-01', periods=12, freq='M')}) +<<<<<<< HEAD seas = ['DJF'] * 2 + ['MAM'] * 3 + ['JJA'] * 3 + ['SON'] * 3 + ['DJF'] self.assertArrayEqual(seas, ds['t.season']) +======= + expected = ['DJF'] * 2 + ['MAM'] * 3 + ['JJA'] * 3 + ['SON'] * 3 + ['DJF'] + assert_equal(expected, ds['t.season']) +>>>>>>> assert_equal def test_slice_virtual_variable(self): data = create_test_data() - self.assertVariableEqual(data['time.dayofyear'][:10].variable, + assert_equal(data['time.dayofyear'][:10].variable, Variable(['time'], 1 + np.arange(10))) +<<<<<<< HEAD self.assertVariableEqual(data['time.dayofyear'][0].variable, Variable([], 1)) +======= + assert_equal(data['time.dayofyear'][0].variable, Variable([], 1)) +>>>>>>> assert_equal def test_setitem(self): # assign a variable @@ -2285,7 +2309,7 @@ def test_setitem(self): data1['dim1'] = data1['dim1'][:5] # override an existing value data1['A'] = 3 * data2['A'] - self.assertVariableEqual(data1['A'], 3 * data2['A']) + assert_equal(data1['A'], 3 * data2['A']) with pytest.raises(NotImplementedError): data1[{'x': 0}] = 0 @@ -2297,7 +2321,7 @@ def test_setitem_pandas(self): ds_copy = ds.copy() ds_copy['bar'] = ds['bar'].to_pandas() - self.assertDatasetEqual(ds, ds_copy) + assert_equal(ds, ds_copy) def test_setitem_auto_align(self): ds = Dataset() @@ -2472,14 +2496,14 @@ def test_groupby(self): ('c', data.isel(x=2))] for actual, expected in zip(groupby, expected_items): assert actual[0] == expected[0] - self.assertDatasetEqual(actual[1], expected[1]) + assert_equal(actual[1], expected[1]) def identity(x): return x for k in ['x', 'c', 'y']: actual = data.groupby(k, squeeze=False).apply(identity) - self.assertDatasetEqual(data, actual) + assert_equal(data, actual) def test_groupby_returns_new_type(self): data = Dataset({'z': (['x', 'y'], np.random.randn(3, 5))}) @@ -2496,9 +2520,9 @@ def test_groupby_iter(self): data = create_test_data() for n, (t, sub) in enumerate(list(data.groupby('dim1'))[:3]): assert 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']) + assert_equal(data['var1'][n], sub['var1']) + assert_equal(data['var2'][n], sub['var2']) + assert_equal(data['var3'][:, n], sub['var3']) def test_groupby_errors(self): data = create_test_data() @@ -2560,10 +2584,10 @@ def reorder_dims(x): expected = ((ds + Variable('dim3', np.zeros(10))) .transpose('dim3', 'dim1', 'dim2', 'time')) actual = grouped + zeros - self.assertDatasetEqual(expected, actual) + assert_equal(expected, actual) actual = zeros + grouped - self.assertDatasetEqual(expected, actual) + assert_equal(expected, actual) with raises_regex(ValueError, 'incompat.* grouped binary'): grouped + ds @@ -2635,10 +2659,10 @@ def test_resample_and_first(self): for how in ['mean', 'sum', 'first', 'last', ]: method = getattr(actual, how) result = method() - self.assertDatasetEqual(expected, result) + assert_equal(expected, result) for method in [np.mean, ]: result = actual.reduce(method) - self.assertDatasetEqual(expected, result) + assert_equal(expected, result) def test_resample_by_mean_with_keep_attrs(self): times = pd.date_range('2000-01-01', freq='6H', periods=10) @@ -3268,9 +3292,9 @@ def test_reduce(self): actual = data.max() expected = Dataset(dict((k, v.max()) for k, v in iteritems(data.data_vars))) - self.assertDatasetEqual(expected, actual) + assert_equal(expected, actual) - self.assertDatasetEqual(data.min(dim=['dim1']), + assert_equal(data.min(dim=['dim1']), data.min(dim='dim1')) for reduct, expected in [('dim2', ['dim1', 'dim3', 'time']), @@ -3281,7 +3305,7 @@ def test_reduce(self): print(reduct, actual, expected) self.assertItemsEqual(actual, expected) - self.assertDatasetEqual(data.mean(dim=[]), data) + assert_equal(data.mean(dim=[]), data) # uint support data = xr.Dataset({'a': (('x', 'y'), @@ -3330,8 +3354,8 @@ def test_reduce_non_numeric(self): data1[v] = (dims, data, {'foo': 'variable'}) assert 'var4' not in data1.mean() - self.assertDatasetEqual(data1.mean(), data2.mean()) - self.assertDatasetEqual(data1.mean(dim='dim1'), + assert_equal(data1.mean(), data2.mean()) + assert_equal(data1.mean(dim='dim1'), data2.mean(dim='dim1')) def test_reduce_strings(self): @@ -3456,7 +3480,7 @@ def test_rank(self): # same as dataarray version x = z.var3 y = ds.var3.rank('dim3') - self.assertDataArrayEqual(x, y) + assert_equal(x, y) # coordinates stick self.assertItemsEqual(list(z.coords), list(ds.coords)) self.assertItemsEqual(list(x.coords), list(y.coords)) @@ -3487,12 +3511,17 @@ def scale(x, multiple=1): return multiple * x actual = data.apply(scale, multiple=2) - self.assertDataArrayEqual(actual['var1'], 2 * data['var1']) + assert_equal(actual['var1'], 2 * data['var1']) self.assertDataArrayIdentical(actual['numbers'], data['numbers']) actual = data.apply(np.asarray) +<<<<<<< HEAD expected = data.drop('time') # time is not used on a data var self.assertDatasetEqual(expected, actual) +======= + expected = data.drop('time') # time is not used on a data var + assert_equal(expected, actual) +>>>>>>> assert_equal def make_example_math_dataset(self): variables = OrderedDict( @@ -3592,7 +3621,7 @@ def test_dataset_math_auto_align(self): actual = ds.isel(y=slice(1)) + ds.isel(y=slice(1, None)) expected = 2 * ds.drop(ds.y, dim='y') - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) actual = ds + ds[['bar']] expected = (2 * ds[['bar']]).merge(ds.coords) @@ -3679,17 +3708,17 @@ def test_dataset_diff_n1_simple(self): ds = Dataset({'foo': ('x', [5, 5, 6, 6])}) actual = ds.diff('x') expected = Dataset({'foo': ('x', [0, 1, 0])}) - self.assertDatasetEqual(expected, actual) + assert_equal(expected, actual) def test_dataset_diff_n1_label(self): ds = Dataset({'foo': ('x', [5, 5, 6, 6])}, {'x': [0, 1, 2, 3]}) actual = ds.diff('x', label='lower') expected = Dataset({'foo': ('x', [0, 1, 0])}, {'x': [0, 1, 2]}) - self.assertDatasetEqual(expected, actual) + assert_equal(expected, actual) actual = ds.diff('x', label='upper') expected = Dataset({'foo': ('x', [0, 1, 0])}, {'x': [1, 2, 3]}) - self.assertDatasetEqual(expected, actual) + assert_equal(expected, actual) def test_dataset_diff_n1(self): ds = create_test_data(seed=1) @@ -3704,7 +3733,7 @@ def test_dataset_diff_n1(self): expected['var3'] = ds['var3'] expected = Dataset(expected, coords={'time': ds['time'].values}) expected.coords['numbers'] = ('dim3', ds['numbers'].values) - self.assertDatasetEqual(expected, actual) + assert_equal(expected, actual) def test_dataset_diff_n2(self): ds = create_test_data(seed=1) @@ -3719,7 +3748,7 @@ def test_dataset_diff_n2(self): expected['var3'] = ds['var3'] expected = Dataset(expected, coords={'time': ds['time'].values}) expected.coords['numbers'] = ('dim3', ds['numbers'].values) - self.assertDatasetEqual(expected, actual) + assert_equal(expected, actual) def test_dataset_diff_exception_n_neg(self): ds = create_test_data(seed=1) @@ -3797,8 +3826,12 @@ def test_filter_by_attrs(self): ======= new_ds = ds.filter_by_attrs(standard_name='convective_precipitation_flux') assert new_ds['precipitation'].standard_name == 'convective_precipitation_flux' +<<<<<<< HEAD >>>>>>> self_assert from unittest2pytest self.assertDatasetEqual(new_ds['precipitation'], ds['precipitation']) +======= + assert_equal(new_ds['precipitation'], ds['precipitation']) +>>>>>>> assert_equal # Test return more than one DataArray. new_ds = ds.filter_by_attrs(standard_name='air_potential_temperature') @@ -3834,29 +3867,29 @@ def test_binary_op_join_setting(self): with xr.set_options(arithmetic_join='outer'): actual = missing_2 + missing_0 expected = xr.Dataset({'x': [0, 1, 2]}) - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) # arithmetic join also applies to data_vars ds1 = xr.Dataset({'foo': 1, 'bar': 2}) ds2 = xr.Dataset({'bar': 2, 'baz': 3}) expected = xr.Dataset({'bar': 4}) # default is inner joining actual = ds1 + ds2 - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) with xr.set_options(arithmetic_join='outer'): expected = xr.Dataset({'foo': np.nan, 'bar': 4, 'baz': np.nan}) actual = ds1 + ds2 - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) with xr.set_options(arithmetic_join='left'): expected = xr.Dataset({'foo': np.nan, 'bar': 4}) actual = ds1 + ds2 - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) with xr.set_options(arithmetic_join='right'): expected = xr.Dataset({'bar': 4, 'baz': np.nan}) actual = ds1 + ds2 - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) def test_full_like(self): # For more thorough tests, see test_variable.py @@ -3891,15 +3924,15 @@ def test_combine_first(self): expected = Dataset({'dsx0': ('x', [0, 0, np.nan]), 'dsx1': ('x', [np.nan, 1, 1])}, coords={'x': ['a', 'b', 'c']}) - self.assertDatasetEqual(actual, expected) - self.assertDatasetEqual(actual, xr.merge([dsx0, dsx1])) + assert_equal(actual, expected) + assert_equal(actual, xr.merge([dsx0, dsx1])) # works just like xr.merge([self, other]) dsy2 = DataArray([2, 2, 2], [('x', ['b', 'c', 'd'])]).to_dataset(name='dsy2') actual = dsx0.combine_first(dsy2) expected = xr.merge([dsy2, dsx0]) - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) def test_sortby(self): ds = Dataset({'A': DataArray([[1, 2], [3, 4], [5, 6]], @@ -3923,16 +3956,16 @@ def test_sortby(self): expected = sorted1d dax = DataArray([100, 99, 98], [('x', ['c', 'b', 'a'])]) actual = ds.sortby(dax) - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) # test descending order sort actual = ds.sortby(dax, ascending=False) - self.assertDatasetEqual(actual, ds) + assert_equal(actual, ds) # test alignment (fills in nan for 'c') dax_short = DataArray([98, 97], [('x', ['b', 'a'])]) actual = ds.sortby(dax_short) - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) # test 1-D lexsort # dax0 is sorted first to give indices of [1, 2, 0] @@ -3940,13 +3973,13 @@ def test_sortby(self): dax0 = DataArray([100, 95, 95], [('x', ['c', 'b', 'a'])]) dax1 = DataArray([0, 1, 0], [('x', ['c', 'b', 'a'])]) actual = ds.sortby([dax0, dax1]) # lexsort underneath gives [2, 1, 0] - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) expected = sorted2d # test multi-dim sort by 1D dataarray values day = DataArray([90, 80], [('y', [1, 0])]) actual = ds.sortby([day, dax]) - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) # test exception-raising with pytest.raises(KeyError) as excinfo: @@ -3961,7 +3994,7 @@ def test_sortby(self): expected = sorted1d actual = ds.sortby('x') - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) # test pandas.MultiIndex indices = (('b', 1), ('b', 0), ('a', 1), ('a', 0)) @@ -3978,26 +4011,26 @@ def test_sortby(self): ('y', [1, 0])]), 'B': DataArray([[11, 12], [9, 10], [7, 8], [5, 6]], dims=['x', 'y'])}) - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) # multi-dim sort by coordinate objects expected = sorted2d actual = ds.sortby(['x', 'y']) - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) # test descending order sort actual = ds.sortby(['x', 'y'], ascending=False) - self.assertDatasetEqual(actual, ds) + assert_equal(actual, ds) def test_attribute_access(self): ds = create_test_data(seed=1) for key in ['var1', 'var2', 'var3', 'time', 'dim1', 'dim2', 'dim3', 'numbers']: - self.assertDataArrayEqual(ds[key], getattr(ds, key)) + assert_equal(ds[key], getattr(ds, key)) assert key in dir(ds) for key in ['dim3', 'dim1', 'numbers']: - self.assertDataArrayEqual(ds['var3'][key], getattr(ds.var3, key)) + assert_equal(ds['var3'][key], getattr(ds.var3, key)) assert key in dir(ds['var3']) # attrs assert ds['var3'].attrs['foo'] == ds.var3.foo diff --git a/xarray/tests/test_duck_array_ops.py b/xarray/tests/test_duck_array_ops.py index 6f5fa2f306b..82e5cdd3020 100644 --- a/xarray/tests/test_duck_array_ops.py +++ b/xarray/tests/test_duck_array_ops.py @@ -32,15 +32,15 @@ def test_first(self): for axis, expected in zip([0, 1, 2, -3, -2, -1], 2 * expected_results): actual = first(self.x, axis) - self.assertArrayEqual(expected, actual) + assert_equal(expected, actual) expected = self.x[0] actual = first(self.x, axis=0, skipna=False) - self.assertArrayEqual(expected, actual) + assert_equal(expected, actual) expected = self.x[..., 0] actual = first(self.x, axis=-1, skipna=False) - self.assertArrayEqual(expected, actual) + assert_equal(expected, actual) with raises_regex(IndexError, 'out of bounds'): first(self.x, 3) @@ -56,15 +56,15 @@ def test_last(self): for axis, expected in zip([0, 1, 2, -3, -2, -1], 2 * expected_results): actual = last(self.x, axis) - self.assertArrayEqual(expected, actual) + assert_equal(expected, actual) expected = self.x[-1] actual = last(self.x, axis=0, skipna=False) - self.assertArrayEqual(expected, actual) + assert_equal(expected, actual) expected = self.x[..., -1] actual = last(self.x, axis=-1, skipna=False) - self.assertArrayEqual(expected, actual) + assert_equal(expected, actual) with raises_regex(IndexError, 'out of bounds'): last(self.x, 3) @@ -73,7 +73,7 @@ def test_count(self): assert 12 == count(self.x) expected = array([[1, 2, 3], [3, 2, 1]]) - self.assertArrayEqual(expected, count(self.x, axis=-1)) + assert_equal(expected, count(self.x, axis=-1)) def test_all_nan_arrays(self): assert np.isnan(mean([np.nan, np.nan])) diff --git a/xarray/tests/test_indexing.py b/xarray/tests/test_indexing.py index 01bc1fb6816..cb2428e9282 100644 --- a/xarray/tests/test_indexing.py +++ b/xarray/tests/test_indexing.py @@ -32,8 +32,8 @@ def test_expanded_indexer(self): I[..., 0, :], I[y], I[y, y], I[..., y, y], I[..., 0, 1, 2, 3, 4]]: j = indexing.expanded_indexer(i, x.ndim) - self.assertArrayEqual(x[i], x[j]) - self.assertArrayEqual(self.set_to_zero(x, i), + assert_equal(x[i], x[j]) + assert_equal(self.set_to_zero(x, i), self.set_to_zero(x, j)) with raises_regex(IndexError, 'too many indices'): indexing.expanded_indexer(I[1, 2, 3], 2) @@ -100,8 +100,8 @@ def test_get_dim_indexers(self): def test_remap_label_indexers(self): def test_indexer(data, x, expected_pos, expected_idx=None): pos, idx = indexing.remap_label_indexers(data, {'x': x}) - self.assertArrayEqual(pos.get('x'), expected_pos) - self.assertArrayEqual(idx.get('x'), expected_idx) + assert_equal(pos.get('x'), expected_pos) + assert_equal(idx.get('x'), expected_idx) data = Dataset({'x': ('x', [1, 2, 3])}) mindex = pd.MultiIndex.from_product([['a', 'b'], [1, 2], [-1, -2]], @@ -147,7 +147,7 @@ def test_slice_slice(self): expected = x[i][j] new_slice = indexing.slice_slice(i, j, size=100) actual = x[new_slice] - self.assertArrayEqual(expected, actual) + assert_equal(expected, actual) def test_lazily_indexed_array(self): original = np.random.rand(10, 20, 30) @@ -170,7 +170,7 @@ def test_lazily_indexed_array(self): v_lazy[:, j, k][i], v_lazy[:, :, k][:, j][i]]: assert expected.shape == actual.shape - self.assertArrayEqual(expected, actual) + assert_equal(expected, actual) assert isinstance(actual._data, indexing.LazilyIndexedArray) @@ -190,7 +190,7 @@ def test_lazily_indexed_array(self): expected = np.asarray(v[i][j]) actual = v_lazy[i][j] assert expected.shape == actual.shape - self.assertArrayEqual(expected, actual) + assert_equal(expected, actual) assert isinstance(actual._data, indexing.LazilyIndexedArray) assert isinstance(actual._data.array, indexing.NumpyIndexingAdapter) @@ -201,8 +201,8 @@ def test_setitem(self): original = np.arange(10) wrapped = indexing.CopyOnWriteArray(original) wrapped[B[:]] = 0 - self.assertArrayEqual(original, np.arange(10)) - self.assertArrayEqual(wrapped, np.zeros(10)) + assert_equal(original, np.arange(10)) + assert_equal(wrapped, np.zeros(10)) def test_sub_array(self): original = np.arange(10) @@ -210,9 +210,9 @@ def test_sub_array(self): child = wrapped[B[:5]] assert isinstance(child, indexing.CopyOnWriteArray) child[B[:]] = 0 - self.assertArrayEqual(original, np.arange(10)) - self.assertArrayEqual(wrapped, np.arange(10)) - self.assertArrayEqual(child, np.zeros(5)) + assert_equal(original, np.arange(10)) + assert_equal(wrapped, np.arange(10)) + assert_equal(child, np.zeros(5)) def test_index_scalar(self): # regression test for GH1374 @@ -224,7 +224,7 @@ class TestMemoryCachedArray(TestCase): def test_wrapper(self): original = indexing.LazilyIndexedArray(np.arange(10)) wrapped = indexing.MemoryCachedArray(original) - self.assertArrayEqual(wrapped, np.arange(10)) + assert_equal(wrapped, np.arange(10)) assert isinstance(wrapped.array, indexing.NumpyIndexingAdapter) def test_sub_array(self): @@ -232,7 +232,7 @@ def test_sub_array(self): wrapped = indexing.MemoryCachedArray(original) child = wrapped[B[:5]] assert isinstance(child, indexing.MemoryCachedArray) - self.assertArrayEqual(child, np.arange(5)) + assert_equal(child, np.arange(5)) assert isinstance(child.array, indexing.NumpyIndexingAdapter) assert isinstance(wrapped.array, indexing.LazilyIndexedArray) @@ -240,7 +240,7 @@ def test_setitem(self): original = np.arange(10) wrapped = indexing.MemoryCachedArray(original) wrapped[B[:]] = 0 - self.assertArrayEqual(original, np.zeros(10)) + assert_equal(original, np.zeros(10)) def test_index_scalar(self): # regression test for GH1374 diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 10c53221a6a..7884e5a3202 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -158,10 +158,10 @@ def test_can_pass_in_axis(self): self.pass_in_axis(self.darray.plot) def test__infer_interval_breaks(self): - self.assertArrayEqual([-0.5, 0.5, 1.5], _infer_interval_breaks([0, 1])) - self.assertArrayEqual([-0.5, 0.5, 5.0, 9.5, 10.5], + assert_equal([-0.5, 0.5, 1.5], _infer_interval_breaks([0, 1])) + assert_equal([-0.5, 0.5, 5.0, 9.5, 10.5], _infer_interval_breaks([0, 1, 9, 10])) - self.assertArrayEqual( + assert_equal( pd.date_range('20000101', periods=4) - np.timedelta64(12, 'h'), _infer_interval_breaks(pd.date_range('20000101', periods=3))) @@ -193,7 +193,7 @@ def test_convenient_facetgrid(self): d.coords['z'] = list('abcd') g = d.plot(x='x', y='y', col='z', col_wrap=2, cmap='cool') - self.assertArrayEqual(g.axes.shape, [2, 2]) + assert_equal(g.axes.shape, [2, 2]) for ax in g.axes.flat: assert ax.has_data() @@ -250,7 +250,7 @@ def test_convenient_facetgrid_4d(self): d = DataArray(a, dims=['y', 'x', 'columns', 'rows']) g = d.plot(x='x', y='y', col='columns', row='rows') - self.assertArrayEqual(g.axes.shape, [3, 2]) + assert_equal(g.axes.shape, [3, 2]) for ax in g.axes.flat: assert ax.has_data() @@ -428,7 +428,7 @@ def test_list_levels(self): for wrap_levels in [list, np.array, pd.Index, DataArray]: cmap_params = _determine_cmap_params( data, levels=wrap_levels(orig_levels)) - self.assertArrayEqual(cmap_params['levels'], orig_levels) + assert_equal(cmap_params['levels'], orig_levels) def test_divergentcontrol(self): neg = self.data - 0.1 @@ -529,7 +529,7 @@ def test_build_discrete_cmap(self): assert ncmap.N == len(levels) - 1 assert len(ncmap.colors) == len(levels) - 1 assert cnorm.N == len(levels) - self.assertArrayEqual(cnorm.boundaries, levels) + assert_equal(cnorm.boundaries, levels) assert max(levels) == cnorm.vmax assert min(levels) == cnorm.vmin if filled: @@ -545,7 +545,7 @@ def test_discrete_colormap_list_of_levels(self): ('min', [2, 5, 10, 15])]: for kind in ['imshow', 'pcolormesh', 'contourf', 'contour']: primitive = getattr(self.darray.plot, kind)(levels=levels) - self.assertArrayEqual(levels, primitive.norm.boundaries) + assert_equal(levels, primitive.norm.boundaries) assert max(levels) == primitive.norm.vmax assert min(levels) == primitive.norm.vmin if kind != 'contour': @@ -840,7 +840,7 @@ def test_convenient_facetgrid(self): d = DataArray(a, dims=['y', 'x', 'z']) g = self.plotfunc(d, x='x', y='y', col='z', col_wrap=2) - self.assertArrayEqual(g.axes.shape, [2, 2]) + assert_equal(g.axes.shape, [2, 2]) for (y, x), ax in np.ndenumerate(g.axes): assert ax.has_data() if x == 0: @@ -854,7 +854,7 @@ def test_convenient_facetgrid(self): # Infering labels g = self.plotfunc(d, col='z', col_wrap=2) - self.assertArrayEqual(g.axes.shape, [2, 2]) + assert_equal(g.axes.shape, [2, 2]) for (y, x), ax in np.ndenumerate(g.axes): assert ax.has_data() if x == 0: @@ -871,7 +871,7 @@ def test_convenient_facetgrid_4d(self): d = DataArray(a, dims=['y', 'x', 'columns', 'rows']) g = self.plotfunc(d, x='x', y='y', col='columns', row='rows') - self.assertArrayEqual(g.axes.shape, [3, 2]) + assert_equal(g.axes.shape, [3, 2]) for ax in g.axes.flat: assert ax.has_data() @@ -1189,7 +1189,7 @@ def test_groups(self): upperleft_array = self.darray.loc[upperleft_dict] z0 = self.darray.isel(z=0) - self.assertDataArrayEqual(upperleft_array, z0) + assert_equal(upperleft_array, z0) @pytest.mark.slow def test_float_index(self): @@ -1245,19 +1245,19 @@ def test_can_set_norm(self): @pytest.mark.slow def test_figure_size(self): - self.assertArrayEqual(self.g.fig.get_size_inches(), (10, 3)) + assert_equal(self.g.fig.get_size_inches(), (10, 3)) g = xplt.FacetGrid(self.darray, col='z', size=6) - self.assertArrayEqual(g.fig.get_size_inches(), (19, 6)) + assert_equal(g.fig.get_size_inches(), (19, 6)) g = self.darray.plot.imshow(col='z', size=6) - self.assertArrayEqual(g.fig.get_size_inches(), (19, 6)) + assert_equal(g.fig.get_size_inches(), (19, 6)) g = xplt.FacetGrid(self.darray, col='z', size=4, aspect=0.5) - self.assertArrayEqual(g.fig.get_size_inches(), (7, 4)) + assert_equal(g.fig.get_size_inches(), (7, 4)) g = xplt.FacetGrid(self.darray, col='z', figsize=(9, 4)) - self.assertArrayEqual(g.fig.get_size_inches(), (9, 4)) + assert_equal(g.fig.get_size_inches(), (9, 4)) with raises_regex(ValueError, "cannot provide both"): g = xplt.plot(self.darray, row=2, col='z', figsize=(6, 4), size=6) diff --git a/xarray/tests/test_ufuncs.py b/xarray/tests/test_ufuncs.py index af7d6e105cc..0fc704a03f5 100644 --- a/xarray/tests/test_ufuncs.py +++ b/xarray/tests/test_ufuncs.py @@ -17,7 +17,7 @@ def assertIdentical(self, a, b): try: assert a.identical(b), (a, b) except AttributeError: - self.assertArrayEqual(a, b) + assert_equal(a, b) def test_unary(self): args = [0, diff --git a/xarray/tests/test_utils.py b/xarray/tests/test_utils.py index d3c2c9a50f5..159cd708291 100644 --- a/xarray/tests/test_utils.py +++ b/xarray/tests/test_utils.py @@ -8,7 +8,7 @@ from xarray.core import duck_array_ops, utils from xarray.core.pycompat import OrderedDict -from . import TestCase, requires_dask +from . import TestCase, requires_dask, assert_equal class TestAlias(TestCase): @@ -33,7 +33,7 @@ def test(self): (pd.Index(td, dtype=object), td.astype(object)), ]: actual = utils.safe_cast_to_index(array) - self.assertArrayEqual(expected, actual) + assert_equal(expected, actual) assert expected.dtype == actual.dtype diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index ebfda129250..b31669acd1a 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -25,7 +25,8 @@ from xarray.core.utils import NDArrayMixin from . import ( - TestCase, source_ndarray, requires_dask, raises_regex, assert_identical) + TestCase, source_ndarray, requires_dask, raises_regex, assert_identical, + assert_equal) from xarray.tests import requires_bottleneck @@ -35,7 +36,7 @@ def test_properties(self): data = 0.5 * np.arange(10) v = self.cls(['time'], data, {'foo': 'bar'}) assert v.dims == ('time',) - self.assertArrayEqual(v.values, data) + assert_equal(v.values, data) assert v.dtype == float assert v.shape == (10,) assert v.size == 10 @@ -67,24 +68,24 @@ def test_getitem_1d(self): v_new = v[dict(x=[0, 1])] assert v_new.dims == ('x', ) - self.assertArrayEqual(v_new, data[[0, 1]]) + assert_equal(v_new, data[[0, 1]]) v_new = v[dict(x=slice(None))] assert v_new.dims == ('x', ) - self.assertArrayEqual(v_new, data) + assert_equal(v_new, data) v_new = v[dict(x=Variable('a', [0, 1]))] assert v_new.dims == ('a', ) - self.assertArrayEqual(v_new, data[[0, 1]]) + assert_equal(v_new, data[[0, 1]]) v_new = v[dict(x=1)] assert v_new.dims == () - self.assertArrayEqual(v_new, data[1]) + assert_equal(v_new, data[1]) # tuple argument v_new = v[slice(None)] assert v_new.dims == ('x', ) - self.assertArrayEqual(v_new, data) + assert_equal(v_new, data) def test_getitem_1d_fancy(self): v = self.cls(['x'], [0, 1, 2]) @@ -93,7 +94,7 @@ def test_getitem_1d_fancy(self): v_new = v[ind] assert v_new.dims == ('a', 'b') expected = np.array(v._data)[([0, 1], [0, 1]), ] - self.assertArrayEqual(v_new, expected) + assert_equal(v_new, expected) # boolean indexing ind = Variable(('x', ), [True, False, True]) @@ -218,9 +219,9 @@ def test_0d_object_array_with_list(self): listarray = np.empty((1,), dtype=object) listarray[0] = [1, 2, 3] x = self.cls('x', listarray) - self.assertArrayEqual(x.data, listarray) - self.assertArrayEqual(x[0].data, listarray.squeeze()) - self.assertArrayEqual(x.squeeze().data, listarray.squeeze()) + assert_equal(x.data, listarray) + assert_equal(x[0].data, listarray.squeeze()) + assert_equal(x.squeeze().data, listarray.squeeze()) def test_index_and_concat_datetime(self): # regression test for #125 @@ -233,7 +234,7 @@ def test_index_and_concat_datetime(self): [expected[[i]] for i in range(10)]]: actual = Variable.concat(times, 't') assert expected.dtype == actual.dtype - self.assertArrayEqual(expected, actual) + assert_equal(expected, actual) def test_0d_time_data(self): # regression test for #105 @@ -251,7 +252,7 @@ def test_datetime64_conversion(self): ]: v = self.cls(['t'], values) assert v.dtype == np.dtype('datetime64[ns]') - self.assertArrayEqual(v.values, times.values) + assert_equal(v.values, times.values) assert v.values.dtype == np.dtype('datetime64[ns]') same_source = source_ndarray(v.values) is source_ndarray(values) assert preserve_source == same_source @@ -266,7 +267,7 @@ def test_timedelta64_conversion(self): ]: v = self.cls(['t'], values) assert v.dtype == np.dtype('timedelta64[ns]') - self.assertArrayEqual(v.values, times.values) + assert_equal(v.values, times.values) assert v.values.dtype == np.dtype('timedelta64[ns]') same_source = source_ndarray(v.values) is source_ndarray(values) assert preserve_source == same_source @@ -300,31 +301,36 @@ def test_1d_math(self): # unary ops self.assertVariableIdentical(base_v, +v) self.assertVariableIdentical(base_v, abs(v)) - self.assertArrayEqual((-v).values, -x) + assert_equal((-v).values, -x) # binary ops with numbers self.assertVariableIdentical(base_v, v + 0) self.assertVariableIdentical(base_v, 0 + v) self.assertVariableIdentical(base_v, v * 1) - self.assertArrayEqual((v > 2).values, x > 2) - self.assertArrayEqual((0 == v).values, 0 == x) - self.assertArrayEqual((v - 1).values, x - 1) - self.assertArrayEqual((1 - v).values, 1 - x) + assert_equal((v > 2).values, x > 2) + assert_equal((0 == v).values, 0 == x) + assert_equal((v - 1).values, x - 1) + assert_equal((1 - v).values, 1 - x) # binary ops with numpy arrays - self.assertArrayEqual((v * x).values, x ** 2) - self.assertArrayEqual((x * v).values, x ** 2) - self.assertArrayEqual(v - y, v - 1) - self.assertArrayEqual(y - v, 1 - v) + assert_equal((v * x).values, x ** 2) + assert_equal((x * v).values, x ** 2) + assert_equal(v - y, v - 1) + assert_equal(y - v, 1 - v) # verify attributes are dropped v2 = self.cls(['x'], x, {'units': 'meters'}) self.assertVariableIdentical(base_v, +v2) # binary ops with all variables - self.assertArrayEqual(v + v, 2 * v) + assert_equal(v + v, 2 * v) w = self.cls(['x'], y, {'foo': 'bar'}) +<<<<<<< HEAD self.assertVariableIdentical( v + w, self.cls(['x'], x + y).to_base_variable()) self.assertArrayEqual((v * w).values, x * y) +======= + self.assertVariableIdentical(v + w, self.cls(['x'], x + y).to_base_variable()) + assert_equal((v * w).values, x * y) +>>>>>>> assert_equal # something complicated - self.assertArrayEqual((v ** 2 * w - 1 + x).values, x ** 2 * y - 1 + x) + assert_equal((v ** 2 * w - 1 + x).values, x ** 2 * y - 1 + x) # make sure dtype is preserved (for Index objects) assert float == (+v).dtype assert float == (+v).values.dtype @@ -347,9 +353,9 @@ def test_1d_reduce(self): def test_array_interface(self): x = np.arange(5) v = self.cls(['x'], x) - self.assertArrayEqual(np.asarray(v), x) + assert_equal(np.asarray(v), x) # test patched in methods - self.assertArrayEqual(v.astype(float), x.astype(float)) + assert_equal(v.astype(float), x.astype(float)) # think this is a break, that argsort changes the type self.assertVariableIdentical(v.argsort(), v.to_base_variable()) self.assertVariableIdentical( @@ -376,8 +382,8 @@ def example_1d_objects(self): def test___array__(self): for v, data in self.example_1d_objects(): - self.assertArrayEqual(v.values, np.asarray(data)) - self.assertArrayEqual(np.asarray(v), np.asarray(data)) + assert_equal(v.values, np.asarray(data)) + assert_equal(np.asarray(v), np.asarray(data)) assert v[0].values == np.asarray(data)[0] assert np.asarray(v[0]) == np.asarray(data)[0] @@ -473,7 +479,7 @@ def test_concat_fixed_len_str(self): actual = Variable.concat([x, y], 'animal') expected = Variable( 'animal', np.array(['horse', 'aardvark'], dtype=kind)) - self.assertVariableEqual(expected, actual) + assert_equal(expected, actual) def test_concat_number_strings(self): # regression test for #305 @@ -509,7 +515,7 @@ def test_copy_index(self): w = v.copy(deep=deep) assert isinstance(w._data, PandasIndexAdapter) assert isinstance(w.to_index(), pd.MultiIndex) - self.assertArrayEqual(v._data.array, w._data.array) + assert_equal(v._data.array, w._data.array) def test_real_and_imag(self): v = self.cls('x', np.arange(3) - 1j * np.arange(3), {'foo': 'bar'}) @@ -568,28 +574,28 @@ def test_getitem_advanced(self): # orthogonal indexing v_new = v[([0, 1], [1, 0])] assert v_new.dims == ('x', 'y') - self.assertArrayEqual(v_new, v_data[[0, 1]][:, [1, 0]]) + assert_equal(v_new, v_data[[0, 1]][:, [1, 0]]) v_new = v[[0, 1]] assert v_new.dims == ('x', 'y') - self.assertArrayEqual(v_new, v_data[[0, 1]]) + assert_equal(v_new, v_data[[0, 1]]) # with mixed arguments ind = Variable(['a'], [0, 1]) v_new = v[dict(x=[0, 1], y=ind)] assert v_new.dims == ('x', 'a') - self.assertArrayEqual(v_new, v_data[[0, 1]][:, [0, 1]]) + assert_equal(v_new, v_data[[0, 1]][:, [0, 1]]) # boolean indexing v_new = v[dict(x=[True, False], y=[False, True, False])] assert v_new.dims == ('x', 'y') - self.assertArrayEqual(v_new, v_data[0][1]) + assert_equal(v_new, v_data[0][1]) # with scalar variable ind = Variable((), 2) v_new = v[dict(y=ind)] expected = v[dict(y=2)] - self.assertArrayEqual(v_new, expected) + assert_equal(v_new, expected) # with boolean variable with wrong shape ind = np.array([True, False]) @@ -607,9 +613,9 @@ def test_getitem_uint_1d(self): v_data = v.compute().data v_new = v[np.array([0])] - self.assertArrayEqual(v_new, v_data[0]) + assert_equal(v_new, v_data[0]) v_new = v[np.array([0], dtype="uint64")] - self.assertArrayEqual(v_new, v_data[0]) + assert_equal(v_new, v_data[0]) def test_getitem_uint(self): # regression test for #1405 @@ -617,12 +623,12 @@ def test_getitem_uint(self): v_data = v.compute().data v_new = v[np.array([0])] - self.assertArrayEqual(v_new, v_data[[0], :]) + assert_equal(v_new, v_data[[0], :]) v_new = v[np.array([0], dtype="uint64")] - self.assertArrayEqual(v_new, v_data[[0], :]) + assert_equal(v_new, v_data[[0], :]) v_new = v[np.uint64(0)] - self.assertArrayEqual(v_new, v_data[0, :]) + assert_equal(v_new, v_data[0, :]) def test_getitem_0d_array(self): # make sure 0d-np.array can be used as an indexer @@ -630,7 +636,7 @@ def test_getitem_0d_array(self): v_data = v.compute().data v_new = v[np.array([0])[0]] - self.assertArrayEqual(v_new, v_data[0]) + assert_equal(v_new, v_data[0]) def test_getitem_fancy(self): v = self.cls(['x', 'y'], [[0, 1, 2], [3, 4, 5]]) @@ -639,59 +645,59 @@ def test_getitem_fancy(self): ind = Variable(['a', 'b'], [[0, 1, 1], [1, 1, 0]]) v_new = v[ind] assert v_new.dims == ('a', 'b', 'y') - self.assertArrayEqual(v_new, v_data[[[0, 1, 1], [1, 1, 0]], :]) + assert_equal(v_new, v_data[[[0, 1, 1], [1, 1, 0]], :]) # It would be ok if indexed with the multi-dimensional array including # the same name ind = Variable(['x', 'b'], [[0, 1, 1], [1, 1, 0]]) v_new = v[ind] assert v_new.dims == ('x', 'b', 'y') - self.assertArrayEqual(v_new, v_data[[[0, 1, 1], [1, 1, 0]], :]) + assert_equal(v_new, v_data[[[0, 1, 1], [1, 1, 0]], :]) ind = Variable(['a', 'b'], [[0, 1, 2], [2, 1, 0]]) v_new = v[dict(y=ind)] assert v_new.dims == ('x', 'a', 'b') - self.assertArrayEqual(v_new, v_data[:, ([0, 1, 2], [2, 1, 0])]) + assert_equal(v_new, v_data[:, ([0, 1, 2], [2, 1, 0])]) ind = Variable(['a', 'b'], [[0, 0], [1, 1]]) v_new = v[dict(x=[1, 0], y=ind)] assert v_new.dims == ('x', 'a', 'b') - self.assertArrayEqual(v_new, v_data[[1, 0]][:, ind]) + assert_equal(v_new, v_data[[1, 0]][:, ind]) # along diagonal ind = Variable(['a'], [0, 1]) v_new = v[ind, ind] assert v_new.dims == ('a',) - self.assertArrayEqual(v_new, v_data[[0, 1], [0, 1]]) + assert_equal(v_new, v_data[[0, 1], [0, 1]]) # with integer ind = Variable(['a', 'b'], [[0, 0], [1, 1]]) v_new = v[dict(x=0, y=ind)] assert v_new.dims == ('a', 'b') - self.assertArrayEqual(v_new[0], v_data[0][[0, 0]]) - self.assertArrayEqual(v_new[1], v_data[0][[1, 1]]) + assert_equal(v_new[0], v_data[0][[0, 0]]) + assert_equal(v_new[1], v_data[0][[1, 1]]) # with slice ind = Variable(['a', 'b'], [[0, 0], [1, 1]]) v_new = v[dict(x=slice(None), y=ind)] assert v_new.dims == ('x', 'a', 'b') - self.assertArrayEqual(v_new, v_data[:, [[0, 0], [1, 1]]]) + assert_equal(v_new, v_data[:, [[0, 0], [1, 1]]]) ind = Variable(['a', 'b'], [[0, 0], [1, 1]]) v_new = v[dict(x=ind, y=slice(None))] assert v_new.dims == ('a', 'b', 'y') - self.assertArrayEqual(v_new, v_data[[[0, 0], [1, 1]], :]) + assert_equal(v_new, v_data[[[0, 0], [1, 1]], :]) ind = Variable(['a', 'b'], [[0, 0], [1, 1]]) v_new = v[dict(x=ind, y=slice(None, 1))] assert v_new.dims == ('a', 'b', 'y') - self.assertArrayEqual(v_new, v_data[[[0, 0], [1, 1]], slice(None, 1)]) + assert_equal(v_new, v_data[[[0, 0], [1, 1]], slice(None, 1)]) # slice matches explicit dimension ind = Variable(['y'], [0, 1]) v_new = v[ind, :2] assert v_new.dims == ('y',) - self.assertArrayEqual(v_new, v_data[[0, 1], [0, 1]]) + assert_equal(v_new, v_data[[0, 1], [0, 1]]) # with multiple slices v = self.cls(['x', 'y', 'z'], [[[1, 2, 3], [4, 5, 6]]]) @@ -748,8 +754,8 @@ def setUp(self): def test_data_and_values(self): v = Variable(['time', 'x'], self.d) - self.assertArrayEqual(v.data, self.d) - self.assertArrayEqual(v.values, self.d) + assert_equal(v.data, self.d) + assert_equal(v.values, self.d) assert source_ndarray(v.values) is self.d with pytest.raises(ValueError): # wrong size @@ -1050,31 +1056,31 @@ def test_items(self): assert np.all(v.values == 0) # test orthogonal setting v[range(10), range(11)] = 1 - self.assertArrayEqual(v.values, np.ones((10, 11))) + assert_equal(v.values, np.ones((10, 11))) def test_getitem_basic(self): v = self.cls(['x', 'y'], [[0, 1, 2], [3, 4, 5]]) v_new = v[dict(x=0)] assert v_new.dims == ('y', ) - self.assertArrayEqual(v_new, v._data[0]) + assert_equal(v_new, v._data[0]) v_new = v[dict(x=0, y=slice(None))] assert v_new.dims == ('y', ) - self.assertArrayEqual(v_new, v._data[0]) + assert_equal(v_new, v._data[0]) v_new = v[dict(x=0, y=1)] assert v_new.dims == () - self.assertArrayEqual(v_new, v._data[0, 1]) + assert_equal(v_new, v._data[0, 1]) v_new = v[dict(y=1)] assert v_new.dims == ('x', ) - self.assertArrayEqual(v_new, v._data[:, 1]) + assert_equal(v_new, v._data[:, 1]) # tuple argument v_new = v[(slice(None), 1)] assert v_new.dims == ('x', ) - self.assertArrayEqual(v_new, v._data[:, 1]) + assert_equal(v_new, v._data[:, 1]) def test_getitem_with_mask_2d_input(self): v = Variable(('x', 'y'), [[0, 1, 2], [3, 4, 5]]) @@ -1163,7 +1169,7 @@ def test_roll_consistency(self): for shift in [-3, 0, 1, 7, 11]: expected = np.roll(v.values, shift, axis=axis) actual = v.roll(**{dim: shift}).values - self.assertArrayEqual(expected, actual) + assert_equal(expected, actual) def test_transpose(self): v = Variable(['time', 'x'], self.d) @@ -1355,7 +1361,7 @@ def test_inplace_math(self): assert v is v2 # since we provided an ndarray for data, it is also modified in-place assert source_ndarray(v.values) is x - self.assertArrayEqual(v.values, np.arange(5) + 1) + assert_equal(v.values, np.arange(5) + 1) with raises_regex(ValueError, 'dimensions cannot change'): v += Variable('y', np.arange(5)) @@ -1426,7 +1432,7 @@ def test_rank(self): # pct v = Variable(['x'], [3.0, 1.0, np.nan, 2.0, 4.0]) v_expect = Variable(['x'], [0.75, 0.25, np.nan, 0.5, 1.0]) - self.assertVariableEqual(v.rank('x', pct=True), v_expect) + assert_equal(v.rank('x', pct=True), v_expect) # invalid dim with raises_regex(ValueError, 'not found'): v.rank('y') @@ -1508,13 +1514,13 @@ def test_setitem(self): v = Variable(['x', 'y'], [[0, 3, 2], [3, 4, 5]]) v[dict(x=[0, 1])] = 1 - self.assertArrayEqual(v[[0, 1]], np.ones_like(v[[0, 1]])) + assert_equal(v[[0, 1]], np.ones_like(v[[0, 1]])) # boolean indexing v = Variable(['x', 'y'], [[0, 3, 2], [3, 4, 5]]) v[dict(x=[True, False])] = 1 - self.assertArrayEqual(v[0], np.ones_like(v[0])) + assert_equal(v[0], np.ones_like(v[0])) v = Variable(['x', 'y'], [[0, 3, 2], [3, 4, 5]]) v[dict(x=[True, False], y=[False, True, False])] = 1 assert v[0, 1] == 1 @@ -1526,7 +1532,7 @@ def assert_assigned_2d(array, key_x, key_y, values): expected[key_x, key_y] = values v = Variable(['x', 'y'], array) v[dict(x=key_x, y=key_y)] = values - self.assertArrayEqual(expected, v) + assert_equal(expected, v) # 1d vectorized indexing assert_assigned_2d(np.random.randn(4, 3), @@ -1582,8 +1588,8 @@ def assert_assigned_2d(array, key_x, key_y, values): v = Variable(['x', 'y'], [[0, 3, 2], [3, 4, 5]]) ind = Variable(['a'], [0, 1]) v[dict(x=ind)] = Variable(['a', 'y'], np.ones((2, 3), dtype=int) * 10) - self.assertArrayEqual(v[0], np.ones_like(v[0]) * 10) - self.assertArrayEqual(v[1], np.ones_like(v[1]) * 10) + assert_equal(v[0], np.ones_like(v[0]) * 10) + assert_equal(v[1], np.ones_like(v[1]) * 10) assert v.dims == ('x', 'y') # dimension should not change # increment @@ -1662,7 +1668,7 @@ def test_data(self): assert isinstance(x._data, PandasIndexAdapter) assert isinstance(x.data, np.ndarray) assert float == x.dtype - self.assertArrayEqual(np.arange(3), x) + assert_equal(np.arange(3), x) assert float == x.values.dtype with raises_regex(TypeError, 'cannot be modified'): x[:] = 0 @@ -1752,7 +1758,7 @@ def test_unchanged_types(self): def test_converted_types(self): for input_array in [[[0, 1, 2]], pd.DataFrame([[0, 1, 2]])]: actual = as_compatible_data(input_array) - self.assertArrayEqual(np.asarray(input_array), actual) + assert_equal(np.asarray(input_array), actual) assert np.ndarray == type(actual) assert np.asarray(input_array).dtype == actual.dtype @@ -1760,14 +1766,14 @@ def test_masked_array(self): original = np.ma.MaskedArray(np.arange(5)) expected = np.arange(5) actual = as_compatible_data(original) - self.assertArrayEqual(expected, actual) + assert_equal(expected, actual) assert np.dtype(int) == actual.dtype original = np.ma.MaskedArray(np.arange(5), mask=4 * [False] + [True]) expected = np.arange(5.0) expected[-1] = np.nan actual = as_compatible_data(original) - self.assertArrayEqual(expected, actual) + assert_equal(expected, actual) assert np.dtype(float) == actual.dtype def test_datetime(self): @@ -1821,7 +1827,7 @@ def check(actual, expect_dtype, expect_values): assert actual.dims == orig.dims assert actual.attrs == orig.attrs assert actual.chunks == orig.chunks - self.assertArrayEqual(actual.values, expect_values) + assert_equal(actual.values, expect_values) check(full_like(orig, 2), orig.dtype, np.full_like(orig.values, 2)) From 7bdb888d6dbef7ae5029bca3917f7e95c1ee2e60 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 13:52:00 -0500 Subject: [PATCH 06/41] future warnings --- xarray/conventions.py | 2 +- xarray/core/dtypes.py | 6 +++--- xarray/core/indexing.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/xarray/conventions.py b/xarray/conventions.py index bdb4931081a..fe75d9e3e6a 100644 --- a/xarray/conventions.py +++ b/xarray/conventions.py @@ -355,7 +355,7 @@ def ensure_dtype_not_object(var, name=None): fill_value = u'' else: # insist on using float for numeric values - if not np.issubdtype(inferred_dtype, float): + if not np.issubdtype(inferred_dtype, np.floating): inferred_dtype = np.dtype(float) fill_value = inferred_dtype.type(np.nan) diff --git a/xarray/core/dtypes.py b/xarray/core/dtypes.py index 057861194a7..eeb60243362 100644 --- a/xarray/core/dtypes.py +++ b/xarray/core/dtypes.py @@ -20,13 +20,13 @@ def maybe_promote(dtype): fill_value : Valid missing value for the promoted dtype. """ # N.B. these casting rules should match pandas - if np.issubdtype(dtype, float): + if np.issubdtype(dtype, np.floating): fill_value = np.nan - elif np.issubdtype(dtype, int): + elif np.issubdtype(dtype, np.signedinteger): # convert to floating point so NaN is valid dtype = float fill_value = np.nan - elif np.issubdtype(dtype, complex): + elif np.issubdtype(dtype, np.complexfloating): fill_value = np.nan + np.nan * 1j elif np.issubdtype(dtype, np.datetime64): fill_value = np.datetime64('NaT') diff --git a/xarray/core/indexing.py b/xarray/core/indexing.py index 4f1bdb415c1..e06b045ad88 100644 --- a/xarray/core/indexing.py +++ b/xarray/core/indexing.py @@ -879,7 +879,7 @@ def __array__(self, dtype=None): if isinstance(array, pd.PeriodIndex): with suppress(AttributeError): # this might not be public API - array = array.asobject + array = array.astype('object') return np.asarray(array.values, dtype=dtype) @property From 5e3a857a3c09df2a04776eaeb8e71c0ef7dfecbf Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 14:30:07 -0500 Subject: [PATCH 07/41] assert identical --- xarray/tests/test_accessors.py | 3 +- xarray/tests/test_backends.py | 145 +++--- xarray/tests/test_coding_times.py | 2 +- xarray/tests/test_combine.py | 73 +-- xarray/tests/test_conventions.py | 12 +- xarray/tests/test_dask.py | 10 +- xarray/tests/test_dataarray.py | 614 ++++++++++++------------ xarray/tests/test_dataset.py | 756 ++++++++++++++---------------- xarray/tests/test_tutorial.py | 2 +- xarray/tests/test_ufuncs.py | 28 +- xarray/tests/test_variable.py | 322 ++++++------- 11 files changed, 951 insertions(+), 1016 deletions(-) diff --git a/xarray/tests/test_accessors.py b/xarray/tests/test_accessors.py index e44f2aba826..90b8753ce09 100644 --- a/xarray/tests/test_accessors.py +++ b/xarray/tests/test_accessors.py @@ -6,7 +6,8 @@ import numpy as np import pandas as pd -from . import TestCase, requires_dask, raises_regex +from . import (TestCase, requires_dask, raises_regex, assert_equal, + assert_identical, assert_allclose) class TestDatetimeAccessor(TestCase): diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index e61625198bf..77b51f0b229 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -29,7 +29,8 @@ requires_scipy_or_netCDF4, requires_dask, requires_h5netcdf, requires_pynio, requires_pathlib, requires_zarr, requires_rasterio, has_netCDF4, has_scipy, assert_allclose, - flaky, network, assert_identical, raises_regex) + flaky, network, assert_identical, raises_regex, assert_equal, + assert_identical) from .test_dataset import create_test_data @@ -173,7 +174,7 @@ def test_zero_dimensional_variable(self): expected['bytes_var'] = ([], b'foobar') expected['string_var'] = ([], u'foobar') with self.roundtrip(expected) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_write_store(self): expected = create_test_data() @@ -201,7 +202,7 @@ def test_roundtrip_test_data(self): expected = create_test_data() with self.roundtrip(expected) as actual: self.check_dtypes_roundtripped(expected, actual) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_load(self): expected = create_test_data() @@ -218,7 +219,7 @@ def assert_loads(vars=None): for k, v in actual.variables.items(): if k in vars: assert v._in_memory - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) with pytest.raises(AssertionError): # make sure the contextmanager works! @@ -234,7 +235,7 @@ def assert_loads(vars=None): # verify we can read data even after closing the file with self.roundtrip(expected) as ds: actual = ds.load() - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_dataset_compute(self): expected = create_test_data() @@ -252,8 +253,8 @@ def test_dataset_compute(self): for v in computed.variables.values(): assert v._in_memory - self.assertDatasetIdentical(expected, actual) - self.assertDatasetIdentical(expected, computed) + assert_identical(expected, actual) + assert_identical(expected, computed) def test_pickle(self): expected = Dataset({'foo': ('x', [42])}) @@ -263,14 +264,14 @@ def test_pickle(self): # windows doesn't like opening the same file twice roundtripped.close() unpickled_ds = pickle.loads(raw_pickle) - self.assertDatasetIdentical(expected, unpickled_ds) + assert_identical(expected, unpickled_ds) def test_pickle_dataarray(self): expected = Dataset({'foo': ('x', [42])}) with self.roundtrip( expected, allow_cleanup_failure=ON_WINDOWS) as roundtripped: unpickled_array = pickle.loads(pickle.dumps(roundtripped['foo'])) - self.assertDatasetIdentical(expected['foo'], unpickled_array) + assert_identical(expected['foo'], unpickled_array) def test_dataset_caching(self): expected = Dataset({'foo': ('x', [5, 6, 7])}) @@ -291,7 +292,7 @@ def test_dataset_caching(self): def test_roundtrip_None_variable(self): expected = Dataset({None: (('x', 'y'), [[0, 1], [2, 3]])}) with self.roundtrip(expected) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_roundtrip_object_dtype(self): floats = np.array([0.0, 0.0, 1.0, 2.0, 3.0], dtype=object) @@ -312,7 +313,7 @@ def test_roundtrip_object_dtype(self): expected = original.copy(deep=True) with self.roundtrip(original) as actual: try: - self.assertDatasetIdentical(expected, actual) + assert_identical(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) @@ -322,23 +323,23 @@ def test_roundtrip_object_dtype(self): # https://github.com/pydata/xarray/issues/1647 expected['bytes_nans'][-1] = b'' expected['strings_nans'][-1] = u'' - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_roundtrip_string_data(self): expected = Dataset({'x': ('t', ['ab', 'cdef'])}) with self.roundtrip(expected) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_roundtrip_string_encoded_characters(self): expected = Dataset({'x': ('t', [u'ab', u'cdef'])}) expected['x'].encoding['dtype'] = 'S1' with self.roundtrip(expected) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) assert actual['x'].encoding['_Encoding'] == 'utf-8' expected['x'].encoding['_Encoding'] = 'ascii' with self.roundtrip(expected) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) assert actual['x'].encoding['_Encoding'] == 'ascii' def test_roundtrip_datetime_data(self): @@ -346,20 +347,20 @@ def test_roundtrip_datetime_data(self): expected = Dataset({'t': ('t', times), 't0': times[0]}) kwds = {'encoding': {'t0': {'units': 'days since 1950-01-01'}}} with self.roundtrip(expected, save_kwargs=kwds) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) assert actual.t0.encoding['units'] == 'days since 1950-01-01' def test_roundtrip_timedelta_data(self): time_deltas = pd.to_timedelta(['1h', '2h', 'NaT']) expected = Dataset({'td': ('td', time_deltas), 'td0': time_deltas[0]}) with self.roundtrip(expected) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_roundtrip_float64_data(self): expected = Dataset({'x': ('y', np.array([1.0, 2.0, np.pi], dtype='float64'))}) with self.roundtrip(expected) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_roundtrip_example_1_netcdf(self): expected = open_example_dataset('example_1.nc') @@ -375,25 +376,25 @@ def test_roundtrip_coordinates(self): {'x': [2, 3], 'y': ('a', [42]), 'z': ('x', [4, 5])}) with self.roundtrip(original) as actual: - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) def test_roundtrip_global_coordinates(self): original = Dataset({'x': [2, 3], 'y': ('a', [42]), 'z': ('x', [4, 5])}) with self.roundtrip(original) as actual: - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) def test_roundtrip_coordinates_with_space(self): original = Dataset(coords={'x': 0, 'y z': 1}) expected = Dataset({'y z': 1}, {'x': 0}) with pytest.warns(xr.SerializationWarning): with self.roundtrip(original) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_roundtrip_boolean_dtype(self): original = create_boolean_data() assert original['x'].dtype == 'bool' with self.roundtrip(original) as actual: - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) assert actual['x'].dtype == 'bool' def test_orthogonal_indexing(self): @@ -438,7 +439,7 @@ def test_isel_dataarray(self): with self.roundtrip(in_memory) as on_disk: expected = in_memory.isel(dim2=in_memory['dim2'] < 3) actual = on_disk.isel(dim2=on_disk['dim2'] < 3) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def validate_array_type(self, ds): # Make sure that only NumpyIndexingAdapter stores a bare np.ndarray. @@ -510,11 +511,11 @@ def test_roundtrip_bytes_with_fill_value(self): expected = original.copy(deep=True) print(original) with self.roundtrip(original) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) original = Dataset({'x': ('t', values, {}, {'_FillValue': b''})}) with self.roundtrip(original) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_roundtrip_string_with_fill_value_nchar(self): values = np.array([u'ab', u'cdef', np.nan], dtype=object) @@ -525,7 +526,7 @@ def test_roundtrip_string_with_fill_value_nchar(self): # Not supported yet. with pytest.raises(NotImplementedError): with self.roundtrip(original) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_unsigned_roundtrip_mask_and_scale(self): decoded = create_unsigned_masked_scaled_data() @@ -594,7 +595,7 @@ def equals_latlon(obj): 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) + assert_identical(actual, original) with create_tmp_file() as tmp_file: original.to_netcdf(tmp_file) with open_dataset(tmp_file, decode_coords=False) as ds: @@ -606,7 +607,7 @@ def equals_latlon(obj): modified = original.drop(['temp', 'precip']) with self.roundtrip(modified) as actual: - self.assertDatasetIdentical(actual, modified) + assert_identical(actual, modified) with create_tmp_file() as tmp_file: modified.to_netcdf(tmp_file) with open_dataset(tmp_file, decode_coords=False) as ds: @@ -625,7 +626,7 @@ def test_roundtrip_endian(self): # one hold mixed endian data (ds) the other should be # all big endian (actual). assertDatasetIdentical # should still pass though. - self.assertDatasetIdentical(ds, actual) + assert_identical(ds, actual) if type(self) is NetCDF4DataTest: ds['z'].encoding['endian'] = 'big' @@ -671,7 +672,7 @@ def test_encoding_kwarg(self): kwargs = dict(encoding={'t': {'units': units}}) with self.roundtrip(ds, save_kwargs=kwargs) as actual: assert actual.t.encoding['units'] == units - self.assertDatasetIdentical(actual, ds) + assert_identical(actual, ds) def test_default_fill_value(self): # Test default encoding for float: @@ -712,7 +713,7 @@ def test_append_write(self): # regression for GH1215 data = create_test_data() with self.roundtrip_append(data) as actual: - self.assertDatasetIdentical(data, actual) + assert_identical(data, actual) def test_append_overwrite_values(self): # regression for GH1215 @@ -723,7 +724,7 @@ def test_append_overwrite_values(self): data['var9'] = data['var2'] * 3 self.save(data[['var2', 'var9']], tmp_file, mode='a') with self.open(tmp_file) as actual: - self.assertDatasetIdentical(data, actual) + assert_identical(data, actual) def test_vectorized_indexing(self): self._test_vectorized_indexing(vindex_support=False) @@ -820,9 +821,9 @@ def test_write_groups(self): data1.to_netcdf(tmp_file, group='data/1') data2.to_netcdf(tmp_file, group='data/2', mode='a') with open_dataset(tmp_file, group='data/1') as actual1: - self.assertDatasetIdentical(data1, actual1) + assert_identical(data1, actual1) with open_dataset(tmp_file, group='data/2') as actual2: - self.assertDatasetIdentical(data2, actual2) + assert_identical(data2, actual2) def test_roundtrip_string_with_fill_value_vlen(self): values = np.array([u'ab', u'cdef', np.nan], dtype=object) @@ -835,12 +836,12 @@ def test_roundtrip_string_with_fill_value_vlen(self): original = Dataset({'x': ('t', values, {}, {'_FillValue': u'XXX'})}) with pytest.raises(NotImplementedError): with self.roundtrip(original) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) original = Dataset({'x': ('t', values, {}, {'_FillValue': u''})}) with pytest.raises(NotImplementedError): with self.roundtrip(original) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_roundtrip_character_array(self): with create_tmp_file() as tmp_file: @@ -855,15 +856,15 @@ def test_roundtrip_character_array(self): values = np.array(['abc', 'def'], dtype='S') expected = Dataset({'x': ('x', values)}) with open_dataset(tmp_file) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # regression test for #157 with self.roundtrip(actual) as roundtripped: - self.assertDatasetIdentical(expected, roundtripped) + assert_identical(expected, roundtripped) def test_default_to_char_arrays(self): data = Dataset({'x': np.array(['foo', 'zzzz'], dtype='S')}) with self.roundtrip(data) as actual: - self.assertDatasetIdentical(data, actual) + assert_identical(data, actual) assert actual['x'].dtype == np.dtype('S4') def test_open_encodings(self): @@ -971,7 +972,7 @@ def test_mask_and_scale(self): # now check xarray with open_dataset(tmp_file) as ds: expected = create_masked_and_scaled_data() - self.assertDatasetIdentical(expected, ds) + assert_identical(expected, ds) def test_0dimensional_variable(self): # This fix verifies our work-around to this netCDF4-python bug: @@ -983,7 +984,7 @@ def test_0dimensional_variable(self): with open_dataset(tmp_file) as ds: expected = Dataset({'x': ((), 123)}) - self.assertDatasetIdentical(expected, ds) + assert_identical(expected, ds) def test_already_open_dataset(self): with create_tmp_file() as tmp_file: @@ -995,7 +996,7 @@ def test_already_open_dataset(self): with backends.NetCDF4DataStore(nc, autoclose=False) as store: with open_dataset(store) as ds: expected = Dataset({'x': ((), 42)}) - self.assertDatasetIdentical(expected, ds) + assert_identical(expected, ds) def test_variable_len_strings(self): with create_tmp_file() as tmp_file: @@ -1009,7 +1010,7 @@ def test_variable_len_strings(self): expected = Dataset({'x': ('x', values)}) for kwargs in [{}, {'decode_cf': True}]: with open_dataset(tmp_file, **kwargs) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) @requires_netCDF4 @@ -1225,11 +1226,11 @@ def test_write_persistence_modes(self): # overwrite mode with self.roundtrip(original, save_kwargs={'mode': 'w'}) as actual: - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) # don't overwrite mode with self.roundtrip(original, save_kwargs={'mode': 'w-'}) as actual: - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) # make sure overwriting works as expected with self.create_zarr_target() as store: @@ -1237,7 +1238,7 @@ def test_write_persistence_modes(self): # should overwrite with no error original.to_zarr(store, mode='w') actual = xr.open_zarr(store) - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) with pytest.raises(ValueError): original.to_zarr(store, mode='w-') @@ -1261,11 +1262,11 @@ def test_group(self): group = 'some/random/path' with self.roundtrip(original, save_kwargs={'group': group}, open_kwargs={'group': group}) as actual: - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) with pytest.raises(KeyError): with self.roundtrip(original, save_kwargs={'group': group}) as actual: - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) # TODO: implement zarr object encoding and make these tests pass @pytest.mark.xfail(reason="Zarr object encoding not implemented") @@ -1347,7 +1348,7 @@ def test_bytesio_pickle(self): fobj = BytesIO(data.to_netcdf()) with open_dataset(fobj, autoclose=self.autoclose) as ds: unpickled = pickle.loads(pickle.dumps(ds)) - self.assertDatasetIdentical(unpickled, data) + assert_identical(unpickled, data) class ScipyInMemoryDataTestAutocloseTrue(ScipyInMemoryDataTest): @@ -1401,7 +1402,7 @@ def test_array_attrs(self): def test_roundtrip_example_1_netcdf_gz(self): with open_example_dataset('example_1.nc.gz') as expected: with open_example_dataset('example_1.nc') as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_netcdf3_endianness(self): # regression test for GH416 @@ -1562,7 +1563,7 @@ def test_cross_engine_read_write_netcdf4(self): data.to_netcdf(tmp_file, engine=write_engine) for read_engine in valid_engines: with open_dataset(tmp_file, engine=read_engine) as actual: - self.assertDatasetIdentical(data, actual) + assert_identical(data, actual) def test_read_byte_attrs_as_unicode(self): with create_tmp_file() as tmp_file: @@ -1570,7 +1571,7 @@ def test_read_byte_attrs_as_unicode(self): nc.foo = b'bar' with open_dataset(tmp_file) as actual: expected = Dataset(attrs={'foo': 'bar'}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_encoding_unlimited_dims(self): ds = Dataset({'x': ('y', np.arange(10.0))}) @@ -1726,12 +1727,12 @@ def test_open_mfdataset_does_same_as_concat(self): with open_mfdataset(files, data_vars=opt) as ds: kwargs = dict(data_vars=opt, dim='t') ds_expect = xr.concat([ds1, ds2], **kwargs) - self.assertDatasetIdentical(ds, ds_expect) + assert_identical(ds, ds_expect) with open_mfdataset(files, coords=opt) as ds: kwargs = dict(coords=opt, dim='t') ds_expect = xr.concat([ds1, ds2], **kwargs) - self.assertDatasetIdentical(ds, ds_expect) + assert_identical(ds, ds_expect) def test_common_coord_when_datavars_all(self): opt = 'all' @@ -1806,7 +1807,7 @@ def test_roundtrip_datetime_data(self): times = pd.to_datetime(['2000-01-01', '2000-01-02', 'NaT']) expected = Dataset({'t': ('t', times), 't0': times[0]}) with self.roundtrip(expected) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_write_store(self): # Override method in DatasetIOTestCases - not applicable to dask @@ -1830,7 +1831,7 @@ def test_open_mfdataset(self): assert isinstance(actual.foo.variable.data, da.Array) assert actual.foo.variable.data.chunks == \ ((5, 5),) - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) with open_mfdataset([tmp1, tmp2], chunks={'x': 3}, autoclose=self.autoclose) as actual: assert actual.foo.variable.data.chunks == \ @@ -1850,7 +1851,7 @@ def test_open_mfdataset_pathlib(self): original.isel(x=slice(5, 10)).to_netcdf(tmp2) with open_mfdataset([tmp1, tmp2], autoclose=self.autoclose) as actual: - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) def test_attrs_mfdataset(self): original = Dataset({'foo': ('x', np.random.randn(10))}) @@ -1882,7 +1883,7 @@ def preprocess(ds): expected = preprocess(original) with open_mfdataset(tmp, preprocess=preprocess, autoclose=self.autoclose) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_save_mfdataset_roundtrip(self): original = Dataset({'foo': ('x', np.random.randn(10))}) @@ -1893,7 +1894,7 @@ def test_save_mfdataset_roundtrip(self): save_mfdataset(datasets, [tmp1, tmp2]) with open_mfdataset([tmp1, tmp2], autoclose=self.autoclose) as actual: - self.assertDatasetIdentical(actual, original) + assert_identical(actual, original) def test_save_mfdataset_invalid(self): ds = Dataset() @@ -1920,7 +1921,7 @@ def test_save_mfdataset_pathlib_roundtrip(self): save_mfdataset(datasets, [tmp1, tmp2]) with open_mfdataset([tmp1, tmp2], autoclose=self.autoclose) as actual: - self.assertDatasetIdentical(actual, original) + assert_identical(actual, original) def test_open_and_do_math(self): original = Dataset({'foo': ('x', np.random.randn(10))}) @@ -1939,7 +1940,7 @@ def test_open_mfdataset_concat_dim_none(self): Dataset({'x': np.nan}).to_netcdf(tmp2) with open_mfdataset([tmp1, tmp2], concat_dim=None, autoclose=self.autoclose) as actual: - self.assertDatasetIdentical(data, actual) + assert_identical(data, actual) def test_open_dataset(self): original = Dataset({'foo': ('x', np.random.randn(10))}) @@ -1948,12 +1949,12 @@ def test_open_dataset(self): with open_dataset(tmp, chunks={'x': 5}) as actual: assert isinstance(actual.foo.variable.data, da.Array) assert actual.foo.variable.data.chunks == ((5, 5),) - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) with open_dataset(tmp, chunks=5) as actual: - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) with open_dataset(tmp) as actual: assert isinstance(actual.foo.variable.data, np.ndarray) - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) def test_dask_roundtrip(self): with create_tmp_file() as tmp: @@ -1961,11 +1962,11 @@ def test_dask_roundtrip(self): data.to_netcdf(tmp) chunks = {'dim1': 4, 'dim2': 4, 'dim3': 4, 'time': 10} with open_dataset(tmp, chunks=chunks) as dask_ds: - self.assertDatasetIdentical(data, dask_ds) + assert_identical(data, dask_ds) with create_tmp_file() as tmp2: dask_ds.to_netcdf(tmp2) with open_dataset(tmp2) as on_disk: - self.assertDatasetIdentical(data, on_disk) + assert_identical(data, on_disk) def test_deterministic_names(self): with create_tmp_file() as tmp: @@ -2128,7 +2129,7 @@ def test_weakrefs(self): on_disk = open_dataset(tmp_file, engine='pynio') actual = on_disk.rename({'foo': 'bar', 'x': 'y'}) del on_disk # trigger garbage collection - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) class TestPyNioAutocloseTrue(TestPyNio): @@ -2608,7 +2609,7 @@ def test_dataarray_to_netcdf_no_name(self): original_da.to_netcdf(tmp) with open_dataarray(tmp) as loaded_da: - self.assertDataArrayIdentical(original_da, loaded_da) + assert_identical(original_da, loaded_da) def test_dataarray_to_netcdf_with_name(self): original_da = DataArray(np.arange(12).reshape((3, 4)), @@ -2618,7 +2619,7 @@ def test_dataarray_to_netcdf_with_name(self): original_da.to_netcdf(tmp) with open_dataarray(tmp) as loaded_da: - self.assertDataArrayIdentical(original_da, loaded_da) + assert_identical(original_da, loaded_da) def test_dataarray_to_netcdf_coord_name_clash(self): original_da = DataArray(np.arange(12).reshape((3, 4)), @@ -2629,7 +2630,7 @@ def test_dataarray_to_netcdf_coord_name_clash(self): original_da.to_netcdf(tmp) with open_dataarray(tmp) as loaded_da: - self.assertDataArrayIdentical(original_da, loaded_da) + assert_identical(original_da, loaded_da) def test_open_dataarray_options(self): data = DataArray( @@ -2640,7 +2641,7 @@ def test_open_dataarray_options(self): expected = data.drop('y') with open_dataarray(tmp, drop_variables=['y']) as loaded: - self.assertDataArrayIdentical(expected, loaded) + assert_identical(expected, loaded) def test_dataarray_to_netcdf_return_bytes(self): # regression test for GH1410 @@ -2657,4 +2658,4 @@ def test_dataarray_to_netcdf_no_name_pathlib(self): original_da.to_netcdf(tmp) with open_dataarray(tmp) as loaded_da: - self.assertDataArrayIdentical(original_da, loaded_da) + assert_identical(original_da, loaded_da) diff --git a/xarray/tests/test_coding_times.py b/xarray/tests/test_coding_times.py index 6155b311b32..36d7af1f7a1 100644 --- a/xarray/tests/test_coding_times.py +++ b/xarray/tests/test_coding_times.py @@ -8,7 +8,7 @@ import pandas as pd from xarray import Variable, coding -from . import TestCase, requires_netCDF4 +from . import TestCase, requires_netCDF4, assert_equal, assert_identical import pytest diff --git a/xarray/tests/test_combine.py b/xarray/tests/test_combine.py index f154f151bf6..b59a4260646 100644 --- a/xarray/tests/test_combine.py +++ b/xarray/tests/test_combine.py @@ -11,7 +11,8 @@ from xarray import Dataset, DataArray, auto_combine, concat, Variable from xarray.core.pycompat import iteritems, OrderedDict -from . import TestCase, InaccessibleArray, requires_dask, raises_regex +from . import (TestCase, InaccessibleArray, requires_dask, raises_regex, + assert_equal, assert_identical) from .test_dataset import create_test_data @@ -27,7 +28,7 @@ def test_concat(self): split_data = [data.isel(dim1=slice(3)), data.isel(dim1=slice(3, None))] - self.assertDatasetIdentical(data, concat(split_data, 'dim1')) + assert_identical(data, concat(split_data, 'dim1')) def rectify_dim_order(dataset): # return a new dataset with all variable dimensions transposed into @@ -38,22 +39,22 @@ def rectify_dim_order(dataset): for dim in ['dim1', 'dim2']: datasets = [g for _, g in data.groupby(dim, squeeze=False)] - self.assertDatasetIdentical(data, concat(datasets, dim)) + assert_identical(data, concat(datasets, dim)) dim = 'dim2' - self.assertDatasetIdentical( + assert_identical( data, concat(datasets, data[dim])) - self.assertDatasetIdentical( + assert_identical( data, concat(datasets, data[dim], coords='minimal')) datasets = [g for _, g in data.groupby(dim, squeeze=True)] concat_over = [k for k, v in iteritems(data.coords) if dim in v.dims and k != dim] actual = concat(datasets, data[dim], coords=concat_over) - self.assertDatasetIdentical(data, rectify_dim_order(actual)) + assert_identical(data, rectify_dim_order(actual)) actual = concat(datasets, data[dim], coords='different') - self.assertDatasetIdentical(data, rectify_dim_order(actual)) + assert_identical(data, rectify_dim_order(actual)) # make sure the coords argument behaves as expected data.coords['extra'] = ('dim4', np.arange(3)) @@ -75,14 +76,14 @@ def rectify_dim_order(dataset): datasets = [g for _, g in data.groupby('dim1', squeeze=False)] expected = data.copy() expected['dim1'] = dim - self.assertDatasetIdentical(expected, concat(datasets, dim)) + assert_identical(expected, concat(datasets, dim)) def test_concat_data_vars(self): data = Dataset({'foo': ('x', np.random.randn(10))}) objs = [data.isel(x=slice(5)), data.isel(x=slice(5, None))] for data_vars in ['minimal', 'different', 'all', [], ['foo']]: actual = concat(objs, dim='x', data_vars=data_vars) - self.assertDatasetIdentical(data, actual) + assert_identical(data, actual) def test_concat_coords(self): data = Dataset({'foo': ('x', np.random.randn(10))}) @@ -91,7 +92,7 @@ def test_concat_coords(self): data.isel(x=slice(5, None)).assign_coords(c=1)] for coords in ['different', 'all', ['c']]: actual = concat(objs, dim='x', coords=coords) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) for coords in ['minimal', []]: with raises_regex(ValueError, 'not equal across'): concat(objs, dim='x', coords=coords) @@ -103,7 +104,7 @@ def test_concat_constant_index(self): 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) + assert_identical(expected, actual) with raises_regex(ValueError, 'not equal across datasets'): concat([ds1, ds2], 'y', data_vars='minimal') @@ -111,10 +112,10 @@ def test_concat_size0(self): data = create_test_data() split_data = [data.isel(dim1=slice(0, 0)), data] actual = concat(split_data, 'dim1') - self.assertDatasetIdentical(data, actual) + assert_identical(data, actual) actual = concat(split_data[::-1], 'dim1') - self.assertDatasetIdentical(data, actual) + assert_identical(data, actual) def test_concat_autoalign(self): ds1 = Dataset({'foo': DataArray([1, 2], coords=[('x', [1, 2])])}) @@ -123,7 +124,7 @@ def test_concat_autoalign(self): expected = Dataset({'foo': DataArray([[1, 2, np.nan], [1, np.nan, 2]], dims=['y', 'x'], coords={'x': [1, 2, 3]})}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_concat_errors(self): data = create_test_data() @@ -140,7 +141,7 @@ def test_concat_errors(self): data0, data1 = deepcopy(split_data) data1.attrs['foo'] = 'bar' concat([data0, data1], 'dim1', compat='identical') - self.assertDatasetIdentical( + assert_identical( data, concat([data0, data1], 'dim1', compat='equals')) with raises_regex(ValueError, 'encountered unexpected'): @@ -172,31 +173,31 @@ def test_concat_promote_shape(self): objs = [Dataset({}, {'x': 0}), Dataset({'x': [1]})] actual = concat(objs, 'x') expected = Dataset({'x': [0, 1]}) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) objs = [Dataset({'x': [0]}), Dataset({}, {'x': 1})] actual = concat(objs, 'x') - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) # mixed dims between variables objs = [Dataset({'x': [2], 'y': 3}), Dataset({'x': [4], 'y': 5})] actual = concat(objs, 'x') expected = Dataset({'x': [2, 4], 'y': ('x', [3, 5])}) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) # mixed dims in coord variable objs = [Dataset({'x': [0]}, {'y': -1}), Dataset({'x': [1]}, {'y': ('x', [-2])})] actual = concat(objs, 'x') expected = Dataset({'x': [0, 1]}, {'y': ('x', [-1, -2])}) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) # scalars with mixed lengths along concat dim -- values should repeat objs = [Dataset({'x': [0]}, {'y': -1}), Dataset({'x': [1, 2]}, {'y': -2})] actual = concat(objs, 'x') expected = Dataset({'x': [0, 1, 2]}, {'y': ('x', [-1, -2, -2])}) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) # broadcast 1d x 1d -> 2d objs = [Dataset({'z': ('x', [-1])}, {'x': [0], 'y': [0]}), @@ -204,7 +205,7 @@ def test_concat_promote_shape(self): actual = concat(objs, 'x') expected = Dataset({'z': (('x', 'y'), [[-1], [1]])}, {'x': [0, 1], 'y': [0]}) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) def test_concat_do_not_promote(self): # GH438 @@ -212,7 +213,7 @@ def test_concat_do_not_promote(self): Dataset({'y': ('t', [2])}, {'x': 1, 't': [0]})] expected = Dataset({'y': ('t', [1, 2])}, {'x': 1, 't': [0, 0]}) actual = concat(objs, 't') - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) objs = [Dataset({'y': ('t', [1])}, {'x': 1, 't': [0]}), Dataset({'y': ('t', [2])}, {'x': 2, 't': [0]})] @@ -224,7 +225,7 @@ def test_concat_dim_is_variable(self): coord = Variable('y', [3, 4]) expected = Dataset({'x': ('y', [0, 1]), 'y': [3, 4]}) actual = concat(objs, coord) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) def test_concat_multiindex(self): x = pd.MultiIndex.from_product([[1, 2, 3], ['a', 'b']]) @@ -251,19 +252,19 @@ def test_concat(self): # from iteration: grouped = [g for _, g in foo.groupby('x')] stacked = concat(grouped, ds['x']) - self.assertDataArrayIdentical(foo, stacked) + assert_identical(foo, stacked) # with an index as the 'dim' argument stacked = concat(grouped, ds.indexes['x']) - self.assertDataArrayIdentical(foo, stacked) + assert_identical(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) + assert_identical(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) + assert_identical(expected, actual) with raises_regex(ValueError, 'not identical'): concat([foo, bar], dim='w', compat='identical') @@ -302,22 +303,22 @@ def test_auto_combine(self): objs = [Dataset({'x': [0]}), Dataset({'x': [1]})] actual = auto_combine(objs) expected = Dataset({'x': [0, 1]}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = auto_combine([actual]) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) objs = [Dataset({'x': [0, 1]}), Dataset({'x': [2]})] actual = auto_combine(objs) expected = Dataset({'x': [0, 1, 2]}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # ensure auto_combine handles non-sorted variables objs = [Dataset(OrderedDict([('x', ('a', [0])), ('y', ('a', [0]))])), Dataset(OrderedDict([('y', ('a', [1])), ('x', ('a', [1]))]))] actual = auto_combine(objs) expected = Dataset({'x': ('a', [0, 1]), 'y': ('a', [0, 1])}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) objs = [Dataset({'x': [0], 'y': [0]}), Dataset({'y': [1], 'x': [1]})] with raises_regex(ValueError, 'too many .* dimensions'): @@ -341,7 +342,7 @@ def test_auto_combine_previously_failed(self): expected = Dataset({'a': ('x', [0, 1]), 'b': ('x', [0, np.nan])}, {'x': [0, 1]}) actual = auto_combine(datasets) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # Your data includes "time" and "station" dimensions, and each year's # data has a different set of stations. @@ -351,7 +352,7 @@ def test_auto_combine_previously_failed(self): [[np.nan, 2, 3], [1, 2, np.nan]])}, {'x': [0, 1, 2]}) actual = auto_combine(datasets, concat_dim='t') - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) @requires_dask # only for toolz def test_auto_combine_still_fails(self): @@ -367,13 +368,13 @@ def test_auto_combine_no_concat(self): objs = [Dataset({'x': 0}), Dataset({'y': 1})] actual = auto_combine(objs) expected = Dataset({'x': 0, 'y': 1}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) objs = [Dataset({'x': 0, 'y': 1}), Dataset({'y': np.nan, 'z': 2})] actual = auto_combine(objs) expected = Dataset({'x': 0, 'y': 1, 'z': 2}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) data = Dataset({'x': 0}) actual = auto_combine([data, data, data], concat_dim=None) - self.assertDatasetIdentical(data, actual) + assert_identical(data, actual) diff --git a/xarray/tests/test_conventions.py b/xarray/tests/test_conventions.py index 816e918e31b..8e69f1a6690 100644 --- a/xarray/tests/test_conventions.py +++ b/xarray/tests/test_conventions.py @@ -247,13 +247,13 @@ def test_dataset(self): {'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) + assert_identical(expected, actual) def test_invalid_coordinates(self): # regression test for GH308 original = Dataset({'foo': ('t', [1, 2], {'coordinates': 'invalid'})}) actual = conventions.decode_cf(original) - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) def test_decode_coordinates(self): # regression test for GH610 @@ -266,7 +266,7 @@ def test_0d_int32_encoding(self): original = Variable((), np.int32(0), encoding={'dtype': 'int64'}) expected = Variable((), np.int64(0)) actual = conventions.maybe_encode_nonstring_dtype(original) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_decode_cf_with_multiple_missing_values(self): original = Variable(['t'], [0, 1, 2], @@ -274,7 +274,7 @@ def test_decode_cf_with_multiple_missing_values(self): expected = Variable(['t'], [np.nan, np.nan, 2], {}) with warnings.catch_warnings(record=True) as w: actual = conventions.decode_cf_variable('t', original) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) assert 'has multiple fill' in str(w[0].message) def test_decode_cf_with_drop_variables(self): @@ -293,8 +293,8 @@ def test_decode_cf_with_drop_variables(self): }) actual = conventions.decode_cf(original, drop_variables=("x",)) actual2 = conventions.decode_cf(original, drop_variables="x") - self.assertDatasetIdentical(expected, actual) - self.assertDatasetIdentical(expected, actual2) + assert_identical(expected, actual) + assert_identical(expected, actual2) def test_invalid_time_units_raises_eagerly(self): ds = Dataset({'time': ('time', [0, 1], {'units': 'foobar since 123'})}) diff --git a/xarray/tests/test_dask.py b/xarray/tests/test_dask.py index 6cb882e58d9..5650f5a218a 100644 --- a/xarray/tests/test_dask.py +++ b/xarray/tests/test_dask.py @@ -48,7 +48,7 @@ def assertLazyAnd(self, expected, actual, test): class TestVariable(DaskTestCase): def assertLazyAndIdentical(self, expected, actual): - self.assertLazyAnd(expected, actual, self.assertVariableIdentical) + self.assertLazyAnd(expected, actual, assert_identical) def assertLazyAndAllClose(self, expected, actual): self.assertLazyAnd(expected, actual, self.assertVariableAllClose) @@ -149,7 +149,7 @@ def test_pickle(self): assert kernel_call_count == 1 a2 = pickle.loads(pickle.dumps(a1)) assert kernel_call_count == 1 - self.assertVariableIdentical(a1, a2) + assert_identical(a1, a2) assert not a1._in_memory assert not a2._in_memory @@ -239,7 +239,7 @@ def test_persist(self): class TestDataArrayAndDataset(DaskTestCase): def assertLazyAndIdentical(self, expected, actual): - self.assertLazyAnd(expected, actual, self.assertDataArrayIdentical) + self.assertLazyAnd(expected, actual, assert_identical) def assertLazyAndAllClose(self, expected, actual): self.assertLazyAnd(expected, actual, self.assertDataArrayAllClose) @@ -523,7 +523,7 @@ def test_dataarray_pickle(self): assert kernel_call_count == 2 a2 = pickle.loads(pickle.dumps(a1)) assert kernel_call_count == 2 - self.assertDataArrayIdentical(a1, a2) + assert_identical(a1, a2) assert not a1._in_memory assert not a2._in_memory assert not a1.coords['y']._in_memory @@ -542,7 +542,7 @@ def test_dataset_pickle(self): assert kernel_call_count == 2 ds2 = pickle.loads(pickle.dumps(ds1)) assert kernel_call_count == 2 - self.assertDatasetIdentical(ds1, ds2) + assert_identical(ds1, ds2) assert not ds1['a']._in_memory assert not ds2['a']._in_memory assert not ds1['y']._in_memory diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index d8c0492aed4..1244efc7590 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -70,7 +70,7 @@ def test_properties(self): assert getattr(self.dv, attr) == getattr(self.v, attr) assert len(self.dv) == len(self.v) assert_equal(self.dv.variable, self.v) - self.assertItemsEqual(list(self.dv.coords), list(self.ds.coords)) + assert set(self.dv.coords) == set(self.ds.coords) for k, v in iteritems(self.dv.coords): assert_equal(v, self.ds.coords[k]) with pytest.raises(AttributeError): @@ -141,7 +141,7 @@ def test_struct_array_dims(self): expected = DataArray([1, -4, -5], dims=['participant'], coords={'participant': p_data}) - self.assertDataArrayIdentical(actual, expected) + assert_identical(actual, expected) # checking array subraction when dims are not the same p_data_1 = np.array([('John', 180), ('Stacy', 151), ('Dick', 200)], @@ -155,7 +155,7 @@ def test_struct_array_dims(self): expected = DataArray([1, -5], dims=['participant'], coords={'participant': p_data_2}) - self.assertDataArrayIdentical(actual, expected) + assert_identical(actual, expected) # checking array subraction when dims are not the same and one # is np.nan @@ -170,7 +170,7 @@ def test_struct_array_dims(self): expected = DataArray([1, -5], dims=['participant'], coords={'participant': p_data_2}) - self.assertDataArrayIdentical(actual, expected) + assert_identical(actual, expected) def test_name(self): arr = self.dv @@ -184,7 +184,7 @@ def test_name(self): actual = DataArray(IndexVariable('x', [3])) actual.name = 'y' expected = DataArray([3], [('x', [3])], name='y') - self.assertDataArrayIdentical(actual, expected) + assert_identical(actual, expected) def test_dims(self): arr = self.dv @@ -215,62 +215,62 @@ def test_constructor(self): actual = DataArray(data) expected = Dataset({None: (['dim_0', 'dim_1'], data)})[None] - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = DataArray(data, [['a', 'b'], [-1, -2, -3]]) expected = Dataset({None: (['dim_0', 'dim_1'], data), 'dim_0': ('dim_0', ['a', 'b']), 'dim_1': ('dim_1', [-1, -2, -3])})[None] - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = DataArray(data, [pd.Index(['a', 'b'], name='x'), pd.Index([-1, -2, -3], name='y')]) expected = Dataset({None: (['x', 'y'], data), 'x': ('x', ['a', 'b']), 'y': ('y', [-1, -2, -3])})[None] - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) coords = [['a', 'b'], [-1, -2, -3]] actual = DataArray(data, coords, ['x', 'y']) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) coords = [pd.Index(['a', 'b'], name='A'), pd.Index([-1, -2, -3], name='B')] actual = DataArray(data, coords, ['x', 'y']) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) coords = {'x': ['a', 'b'], 'y': [-1, -2, -3]} actual = DataArray(data, coords, ['x', 'y']) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) coords = [('x', ['a', 'b']), ('y', [-1, -2, -3])] actual = DataArray(data, coords) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) expected = Dataset({None: (['x', 'y'], data), 'x': ('x', ['a', 'b'])})[None] actual = DataArray(data, {'x': ['a', 'b']}, ['x', 'y']) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = DataArray(data, dims=['x', 'y']) expected = Dataset({None: (['x', 'y'], data)})[None] - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = DataArray(data, dims=['x', 'y'], name='foo') expected = Dataset({'foo': (['x', 'y'], data)})['foo'] - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = DataArray(data, name='foo') expected = Dataset({'foo': (['dim_0', 'dim_1'], data)})['foo'] - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = DataArray(data, dims=['x', 'y'], attrs={'bar': 2}) expected = Dataset({None: (['x', 'y'], data, {'bar': 2})})[None] - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = DataArray(data, dims=['x', 'y'], encoding={'bar': 2}) expected = Dataset({None: (['x', 'y'], data, {}, {'bar': 2})})[None] - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_constructor_invalid(self): data = np.random.randn(3, 2) @@ -308,7 +308,7 @@ def test_constructor_from_self_described(self): dims=['x', 'y'], name='foobar', attrs={'bar': 2}, encoding={'foo': 3}) actual = DataArray(expected) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = DataArray(expected.values, actual.coords) assert_equal(expected, actual) @@ -327,29 +327,29 @@ def test_constructor_from_self_described(self): actual = DataArray(panel) expected = DataArray([data], expected.coords, ['dim_0', 'x', 'y']) expected['dim_0'] = [0] - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) expected = DataArray(data, coords={'x': ['a', 'b'], 'y': [-1, -2], 'a': 0, 'z': ('x', [-0.5, 0.5])}, dims=['x', 'y']) actual = DataArray(expected) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = DataArray(expected.values, expected.coords) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) expected = Dataset({'foo': ('foo', ['a', 'b'])})['foo'] actual = DataArray(pd.Index(['a', 'b'], name='foo')) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = DataArray(IndexVariable('foo', ['a', 'b'])) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_constructor_from_0d(self): expected = Dataset({None: ([], 0)})[None] actual = DataArray(0) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) @requires_dask def test_constructor_dask_coords(self): @@ -431,11 +431,11 @@ def test_broadcast_equals(self): def test_getitem(self): # strings pull out dataarrays - self.assertDataArrayIdentical(self.dv, self.ds['foo']) + assert_identical(self.dv, self.ds['foo']) x = self.dv['x'] y = self.dv['y'] - self.assertDataArrayIdentical(self.ds['x'], x) - self.assertDataArrayIdentical(self.ds['y'], y) + assert_identical(self.ds['x'], x) + assert_identical(self.ds['y'], y) I = ReturnItem() # noqa: E741 # allow ambiguous name for i in [I[:], I[...], I[x.values], I[x.variable], I[x], I[x, y], @@ -452,7 +452,7 @@ def test_getitem(self): def test_getitem_dict(self): actual = self.dv[{'x': slice(3), 'y': 0}] expected = self.dv.isel(x=slice(3), y=0) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_getitem_coords(self): orig = DataArray([[10], [20]], @@ -462,37 +462,37 @@ def test_getitem_coords(self): 'xy': (['y', 'x'], [['d', 'e']])}, dims=['x', 'y']) - self.assertDataArrayIdentical(orig, orig[:]) - self.assertDataArrayIdentical(orig, orig[:, :]) - self.assertDataArrayIdentical(orig, orig[...]) - self.assertDataArrayIdentical(orig, orig[:2, :1]) - self.assertDataArrayIdentical(orig, orig[[0, 1], [0]]) + assert_identical(orig, orig[:]) + assert_identical(orig, orig[:, :]) + assert_identical(orig, orig[...]) + assert_identical(orig, orig[:2, :1]) + assert_identical(orig, orig[[0, 1], [0]]) actual = orig[0, 0] expected = DataArray( 10, {'x': 1, 'y': 3, 'z': 4, 'x2': 'a', 'y2': 'c', 'xy': 'd'}) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = orig[0, :] expected = DataArray( [10], {'x': 1, 'y': [3], 'z': 4, 'x2': 'a', 'y2': ('y', ['c']), 'xy': ('y', ['d'])}, dims='y') - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = orig[:, 0] expected = DataArray( [10, 20], {'x': [1, 2], 'y': 3, 'z': 4, 'x2': ('x', ['a', 'b']), 'y2': 'c', 'xy': ('x', ['d', 'e'])}, dims='x') - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_getitem_dataarray(self): # It should not conflict da = DataArray(np.arange(12).reshape((3, 4)), dims=['x', 'y']) ind = DataArray([[0, 1], [0, 1]], dims=['x', 'z']) actual = da[ind] - assert_equal(actual, da.values[[[0, 1], [0, 1]], :]) + assert_equal(actual.values, da.values[[[0, 1], [0, 1]], :]) da = DataArray(np.arange(12).reshape((3, 4)), dims=['x', 'y'], coords={'x': [0, 1, 2], 'y': ['a', 'b', 'c', 'd']}) @@ -532,15 +532,15 @@ def test_setitem_fancy(self): ind = Variable(['a'], [0, 1]) da[dict(x=ind, y=ind)] = 0 expected = DataArray([[0, 1], [1, 0], [1, 1]], dims=['x', 'y']) - self.assertDataArrayIdentical(expected, da) + assert_identical(expected, da) # assign another 0d-variable da[dict(x=ind, y=ind)] = Variable((), 0) expected = DataArray([[0, 1], [1, 0], [1, 1]], dims=['x', 'y']) - self.assertDataArrayIdentical(expected, da) + assert_identical(expected, da) # assign another 1d-variable da[dict(x=ind, y=ind)] = Variable(['a'], [2, 3]) expected = DataArray([[2, 1], [1, 3], [1, 1]], dims=['x', 'y']) - self.assertVariableIdentical(expected, da) + assert_identical(expected, da) # 2d-vectorized indexing da = DataArray(np.ones((3, 2)), dims=['x', 'y']) @@ -548,13 +548,13 @@ def test_setitem_fancy(self): ind_y = DataArray([[1, 0]], dims=['a', 'b']) da[dict(x=ind_x, y=ind_y)] = 0 expected = DataArray([[1, 0], [0, 1], [1, 1]], dims=['x', 'y']) - self.assertVariableIdentical(expected, da) + assert_identical(expected, da) da = DataArray(np.ones((3, 2)), dims=['x', 'y']) ind = Variable(['a'], [0, 1]) da[ind] = 0 expected = DataArray([[0, 0], [0, 0], [1, 1]], dims=['x', 'y']) - self.assertVariableIdentical(expected, da) + assert_identical(expected, da) def test_setitem_dataarray(self): def get_data(): @@ -574,8 +574,8 @@ def get_data(): coords={'x': np.arange(1, 4)}) da[dict(x=ind)] = 0 # should not raise assert np.allclose(da[dict(x=ind)].values, 0) - self.assertDataArrayIdentical(da['x'], get_data()['x']) - self.assertDataArrayIdentical(da['non-dim'], get_data()['non-dim']) + assert_identical(da['x'], get_data()['x']) + assert_identical(da['non-dim'], get_data()['non-dim']) da = get_data() # conflict in the assigning values @@ -591,8 +591,8 @@ def get_data(): 'non-dim': ('x', [0, 2, 4])}) da[dict(x=ind)] = value assert np.allclose(da[dict(x=ind)].values, 0) - self.assertDataArrayIdentical(da['x'], get_data()['x']) - self.assertDataArrayIdentical(da['non-dim'], get_data()['non-dim']) + assert_identical(da['x'], get_data()['x']) + assert_identical(da['non-dim'], get_data()['non-dim']) # Conflict in the non-dimension coordinate value = xr.DataArray(np.zeros((3, 3, 2)), dims=['x', 'y', 'z'], @@ -626,7 +626,7 @@ def test_attr_sources_multiindex(self): def test_pickle(self): data = DataArray(np.random.random((3, 3)), dims=('id', 'time')) roundtripped = pickle.loads(pickle.dumps(data)) - self.assertDataArrayIdentical(data, roundtripped) + assert_identical(data, roundtripped) @requires_dask def test_chunk(self): @@ -651,23 +651,23 @@ def test_chunk(self): assert 'testname_' in blocked.data.name def test_isel(self): - self.assertDataArrayIdentical(self.dv[0], self.dv.isel(x=0)) - self.assertDataArrayIdentical(self.dv, self.dv.isel(x=slice(None))) - self.assertDataArrayIdentical(self.dv[:3], self.dv.isel(x=slice(3))) - self.assertDataArrayIdentical(self.dv[:3, :5], + assert_identical(self.dv[0], self.dv.isel(x=0)) + assert_identical(self.dv, self.dv.isel(x=slice(None))) + assert_identical(self.dv[:3], self.dv.isel(x=slice(3))) + assert_identical(self.dv[:3, :5], self.dv.isel(x=slice(3), y=slice(5))) def test_isel_types(self): # regression test for #1405 da = DataArray([1, 2, 3], dims='x') # uint64 - self.assertDataArrayIdentical(da.isel(x=np.array([0], dtype="uint64")), + assert_identical(da.isel(x=np.array([0], dtype="uint64")), da.isel(x=np.array([0]))) # uint32 - self.assertDataArrayIdentical(da.isel(x=np.array([0], dtype="uint32")), + assert_identical(da.isel(x=np.array([0], dtype="uint32")), da.isel(x=np.array([0]))) # int64 - self.assertDataArrayIdentical(da.isel(x=np.array([0], dtype="int64")), + assert_identical(da.isel(x=np.array([0], dtype="int64")), da.isel(x=np.array([0]))) def test_isel_fancy(self): @@ -701,7 +701,7 @@ def test_isel_fancy(self): np.testing.assert_equal(actual, expected) # test that the order of the indexers doesn't matter - self.assertDataArrayIdentical( + assert_identical( da.isel(y=(('points', ), y), x=(('points', ), x)), da.isel(x=(('points', ), x), y=(('points', ), y))) @@ -719,7 +719,7 @@ def test_isel_fancy(self): actual = da.isel(x=stations['dim1s'], y=stations['dim2s']) assert 'station' in actual.coords assert 'station' in actual.dims - self.assertDataArrayIdentical(actual['station'], stations['station']) + assert_identical(actual['station'], stations['station']) with raises_regex(ValueError, 'conflicting values for '): da.isel(x=DataArray([0, 1, 2], dims='station', @@ -739,24 +739,24 @@ def test_isel_fancy(self): assert 'a' in actual.dims assert 'b' in actual.coords assert 'b' in actual.dims - self.assertDataArrayIdentical(actual['a'], stations['a']) - self.assertDataArrayIdentical(actual['b'], stations['b']) + assert_identical(actual['a'], stations['a']) + assert_identical(actual['b'], stations['b']) expected = da.variable[:, stations['dim2s'].variable, stations['dim1s'].variable] - assert_equal(actual, expected) + assert_equal(actual.values, expected) def test_sel(self): self.ds['x'] = ('x', np.array(list('abcdefghij'))) da = self.ds['foo'] - self.assertDataArrayIdentical(da, da.sel(x=slice(None))) - self.assertDataArrayIdentical(da[1], da.sel(x='b')) - self.assertDataArrayIdentical(da[:3], da.sel(x=slice('c'))) - self.assertDataArrayIdentical(da[:3], da.sel(x=['a', 'b', 'c'])) - self.assertDataArrayIdentical(da[:, :4], da.sel(y=(self.ds['y'] < 4))) + assert_identical(da, da.sel(x=slice(None))) + assert_identical(da[1], da.sel(x='b')) + assert_identical(da[:3], da.sel(x=slice('c'))) + assert_identical(da[:3], da.sel(x=['a', 'b', 'c'])) + assert_identical(da[:, :4], da.sel(y=(self.ds['y'] < 4))) # verify that indexing with a dataarray works b = DataArray('b') - self.assertDataArrayIdentical(da[1], da.sel(x=b)) - self.assertDataArrayIdentical(da[[1]], da.sel(x=slice(b, b))) + assert_identical(da[1], da.sel(x=b)) + assert_identical(da[[1]], da.sel(x=slice(b, b))) def test_sel_dataarray(self): # indexing with DataArray @@ -765,7 +765,7 @@ def test_sel_dataarray(self): ind = DataArray(['a', 'b', 'c'], dims=['x']) actual = da.sel(x=ind) - self.assertDataArrayIdentical(actual, da.isel(x=[0, 1, 2])) + assert_identical(actual, da.isel(x=[0, 1, 2])) # along new dimension ind = DataArray(['a', 'b', 'c'], dims=['new_dim']) @@ -785,10 +785,10 @@ def test_sel_dataarray(self): def test_sel_no_index(self): array = DataArray(np.arange(10), dims='x') - self.assertDataArrayIdentical(array[0], array.sel(x=0)) - self.assertDataArrayIdentical(array[:5], array.sel(x=slice(5))) - self.assertDataArrayIdentical(array[[0, -1]], array.sel(x=[0, -1])) - self.assertDataArrayIdentical( + assert_identical(array[0], array.sel(x=0)) + assert_identical(array[:5], array.sel(x=slice(5))) + assert_identical(array[[0, -1]], array.sel(x=[0, -1])) + assert_identical( array[array < 5], array.sel(x=(array < 5))) def test_sel_method(self): @@ -797,36 +797,36 @@ def test_sel_method(self): expected = data.sel(y=['a', 'b']) actual = data.sel(y=['ab', 'ba'], method='pad') - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) expected = data.sel(x=[1, 2]) actual = data.sel(x=[0.9, 1.9], method='backfill', tolerance=1) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_sel_drop(self): data = DataArray([1, 2, 3], [('x', [0, 1, 2])]) expected = DataArray(1) selected = data.sel(x=0, drop=True) - self.assertDataArrayIdentical(expected, selected) + assert_identical(expected, selected) expected = DataArray(1, {'x': 0}) selected = data.sel(x=0, drop=False) - self.assertDataArrayIdentical(expected, selected) + assert_identical(expected, selected) data = DataArray([1, 2, 3], dims=['x']) expected = DataArray(1) selected = data.sel(x=0, drop=True) - self.assertDataArrayIdentical(expected, selected) + assert_identical(expected, selected) def test_isel_drop(self): data = DataArray([1, 2, 3], [('x', [0, 1, 2])]) expected = DataArray(1) selected = data.isel(x=0, drop=True) - self.assertDataArrayIdentical(expected, selected) + assert_identical(expected, selected) expected = DataArray(1, {'x': 0}) selected = data.isel(x=0, drop=False) - self.assertDataArrayIdentical(expected, selected) + assert_identical(expected, selected) def test_isel_points(self): shape = (10, 5, 6) @@ -863,7 +863,7 @@ def test_isel_points(self): np.testing.assert_equal(actual.T, expected) # test that the order of the indexers doesn't matter - self.assertDataArrayIdentical( + assert_identical( da.isel_points(y=y, x=x), da.isel_points(x=x, y=y)) @@ -892,14 +892,14 @@ def test_isel_points(self): def test_loc(self): self.ds['x'] = ('x', np.array(list('abcdefghij'))) da = self.ds['foo'] - self.assertDataArrayIdentical(da[:3], da.loc[:'c']) - self.assertDataArrayIdentical(da[1], da.loc['b']) - self.assertDataArrayIdentical(da[1], da.loc[{'x': 'b'}]) - self.assertDataArrayIdentical(da[1], da.loc['b', ...]) - self.assertDataArrayIdentical(da[:3], da.loc[['a', 'b', 'c']]) - self.assertDataArrayIdentical(da[:3, :4], + assert_identical(da[:3], da.loc[:'c']) + assert_identical(da[1], da.loc['b']) + assert_identical(da[1], da.loc[{'x': 'b'}]) + assert_identical(da[1], da.loc['b', ...]) + assert_identical(da[:3], da.loc[['a', 'b', 'c']]) + assert_identical(da[:3, :4], da.loc[['a', 'b', 'c'], np.arange(4)]) - self.assertDataArrayIdentical(da[:, :4], da.loc[:, self.ds['y'] < 4]) + assert_identical(da[:, :4], da.loc[:, self.ds['y'] < 4]) def test_loc_assign(self): self.ds['x'] = ('x', np.array(list('abcdefghij'))) @@ -942,8 +942,8 @@ def get_data(): coords={'x': np.arange(1, 4)}) da.loc[dict(x=ind)] = 0 # should not raise assert np.allclose(da[dict(x=ind)].values, 0) - self.assertDataArrayIdentical(da['x'], get_data()['x']) - self.assertDataArrayIdentical(da['non-dim'], get_data()['non-dim']) + assert_identical(da['x'], get_data()['x']) + assert_identical(da['non-dim'], get_data()['non-dim']) da = get_data() # conflict in the assigning values @@ -959,8 +959,8 @@ def get_data(): 'non-dim': ('x', [0, 2, 4])}) da.loc[dict(x=ind)] = value assert np.allclose(da[dict(x=ind)].values, 0) - self.assertDataArrayIdentical(da['x'], get_data()['x']) - self.assertDataArrayIdentical(da['non-dim'], get_data()['non-dim']) + assert_identical(da['x'], get_data()['x']) + assert_identical(da['non-dim'], get_data()['non-dim']) def test_loc_single_boolean(self): data = DataArray([0, 1], coords=[[True, False]]) @@ -977,12 +977,12 @@ def test_sel(lab_indexer, pos_indexer, replaced_idx=False, da = mdata.sel(x=lab_indexer) expected_da = mdata.isel(x=pos_indexer) if not replaced_idx: - self.assertDataArrayIdentical(da, expected_da) + assert_identical(da, expected_da) else: if renamed_dim: assert da.dims[0] == renamed_dim da = da.rename({renamed_dim: 'x'}) - self.assertVariableIdentical(da.variable, expected_da.variable) + assert_identical(da.variable, expected_da.variable) self.assertVariableNotEqual(da['x'], expected_da['x']) test_sel(('a', 1, -1), 0) @@ -998,22 +998,22 @@ def test_sel(lab_indexer, pos_indexer, replaced_idx=False, renamed_dim='three') test_sel({'one': 'a'}, range(4), replaced_idx=True) - self.assertDataArrayIdentical(mdata.loc['a'], mdata.sel(x='a')) - self.assertDataArrayIdentical(mdata.loc[('a', 1), ...], + assert_identical(mdata.loc['a'], mdata.sel(x='a')) + assert_identical(mdata.loc[('a', 1), ...], mdata.sel(x=('a', 1))) - self.assertDataArrayIdentical(mdata.loc[{'one': 'a'}, ...], + assert_identical(mdata.loc[{'one': 'a'}, ...], mdata.sel(x={'one': 'a'})) with pytest.raises(IndexError): mdata.loc[('a', 1)] - self.assertDataArrayIdentical(mdata.sel(x={'one': 'a', 'two': 1}), + assert_identical(mdata.sel(x={'one': 'a', 'two': 1}), mdata.sel(one='a', two=1)) def test_virtual_default_coords(self): array = DataArray(np.zeros((5,)), dims='x') expected = DataArray(range(5), dims='x', name='x') - self.assertDataArrayIdentical(expected, array['x']) - self.assertDataArrayIdentical(expected, array.coords['x']) + assert_identical(expected, array['x']) + assert_identical(expected, array.coords['x']) def test_virtual_time_components(self): dates = pd.date_range('2000-01-01', periods=10) @@ -1054,7 +1054,7 @@ def test_coords(self): del da.coords['x'] expected = DataArray(da.values, {'y': [0, 1, 2]}, dims=['x', 'y'], name='foo') - self.assertDataArrayIdentical(da, expected) + assert_identical(da, expected) with raises_regex(ValueError, 'conflicting MultiIndex'): self.mda['level_1'] = np.arange(4) @@ -1092,17 +1092,17 @@ def test_coord_coords(self): expected = DataArray([1, 2], {'z': 4, 'x2': ('x', ['a', 'b']), 'x': [1, 2]}, dims='x', name='x') - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) del actual.coords['x2'] - self.assertDataArrayIdentical( + assert_identical( expected.reset_coords('x2', drop=True), actual) actual.coords['x3'] = ('x', ['a', 'b']) expected = DataArray([1, 2], {'z': 4, 'x3': ('x', ['a', 'b']), 'x': [1, 2]}, dims='x', name='x') - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_reset_coords(self): data = DataArray(np.zeros((3, 4)), @@ -1117,34 +1117,34 @@ def test_reset_coords(self): 'bar': ('x', ['a', 'b', 'c']), 'baz': ('y', range(4)), 'y': range(4)}) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) actual = data.reset_coords(['bar', 'baz']) - self.assertDatasetIdentical(actual, expected) + assert_identical(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)), 'y': range(4)}) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) actual = data.reset_coords(['bar']) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) actual = data.reset_coords(drop=True) expected = DataArray(np.zeros((3, 4)), coords={'y': range(4)}, dims=['x', 'y'], name='foo') - self.assertDataArrayIdentical(actual, expected) + assert_identical(actual, expected) actual = data.copy() actual.reset_coords(drop=True, inplace=True) - self.assertDataArrayIdentical(actual, expected) + assert_identical(actual, expected) actual = data.reset_coords('bar', drop=True) expected = DataArray(np.zeros((3, 4)), {'baz': ('y', range(4)), 'y': range(4)}, dims=['x', 'y'], name='foo') - self.assertDataArrayIdentical(actual, expected) + assert_identical(actual, expected) with raises_regex(ValueError, 'cannot reset coord'): data.reset_coords(inplace=True) @@ -1159,13 +1159,13 @@ def test_assign_coords(self): array = DataArray(10) actual = array.assign_coords(c=42) expected = DataArray(10, {'c': 42}) - self.assertDataArrayIdentical(actual, expected) + assert_identical(actual, expected) array = DataArray([1, 2, 3, 4], {'c': ('x', [0, 0, 1, 1])}, dims='x') actual = array.groupby('c').assign_coords(d=lambda a: a.mean()) expected = array.copy() expected.coords['d'] = ('x', [1.5, 1.5, 3.5, 3.5]) - self.assertDataArrayIdentical(actual, expected) + assert_identical(actual, expected) with raises_regex(ValueError, 'conflicting MultiIndex'): self.mda.assign_coords(level_1=range(4)) @@ -1179,7 +1179,7 @@ def test_coords_alignment(self): coords={'rhs': ('x', [np.nan, 2, 3]), 'x': [0, 1, 2]}, dims='x') - self.assertDataArrayIdentical(lhs, expected) + assert_identical(lhs, expected) def test_coords_replacement_alignment(self): # regression test for GH725 @@ -1187,28 +1187,28 @@ def test_coords_replacement_alignment(self): new_coord = DataArray([1, 2, 3], dims=['abc'], coords=[[1, 2, 3]]) arr['abc'] = new_coord expected = DataArray([0, 1, 2], coords=[('abc', [1, 2, 3])]) - self.assertDataArrayIdentical(arr, expected) + assert_identical(arr, expected) def test_coords_non_string(self): arr = DataArray(0, coords={1: 2}) actual = arr.coords[1] expected = DataArray(2, coords={1: 2}, name=1) - self.assertDataArrayIdentical(actual, expected) + assert_identical(actual, expected) def test_reindex_like(self): foo = DataArray(np.random.randn(5, 6), [('x', range(5)), ('y', range(6))]) bar = foo[:2, :2] - self.assertDataArrayIdentical(foo.reindex_like(bar), bar) + assert_identical(foo.reindex_like(bar), bar) expected = foo.copy() expected[:] = np.nan expected[:2, :2] = bar - self.assertDataArrayIdentical(bar.reindex_like(foo), expected) + assert_identical(bar.reindex_like(foo), expected) def test_reindex_like_no_index(self): foo = DataArray(np.random.randn(5, 6), dims=['x', 'y']) - self.assertDatasetIdentical(foo, foo.reindex_like(foo)) + assert_identical(foo, foo.reindex_like(foo)) bar = foo[:4] with raises_regex( @@ -1220,7 +1220,7 @@ def test_reindex_regressions(self): expected = DataArray(np.random.randn(5), coords=[("time", range(5))]) time2 = DataArray(np.arange(5), dims="time2") actual = expected.reindex(time=time2) - self.assertDataArrayIdentical(actual, expected) + assert_identical(actual, expected) # regression test for #736, reindex can not change complex nums dtype x = np.array([1, 2, 3], dtype=np.complex) @@ -1234,21 +1234,21 @@ def test_reindex_method(self): y = [-0.1, 0.5, 1.1] actual = x.reindex(y=y, method='backfill', tolerance=0.2) expected = DataArray([10, np.nan, np.nan], coords=[('y', y)]) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) alt = Dataset({'y': y}) actual = x.reindex_like(alt, method='backfill') expected = DataArray([10, 20, np.nan], coords=[('y', y)]) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_rename(self): renamed = self.dv.rename('bar') - self.assertDatasetIdentical( + assert_identical( renamed.to_dataset(), self.ds.rename({'foo': 'bar'})) assert renamed.name == 'bar' renamed = self.dv.x.rename({'x': 'z'}).rename('z') - self.assertDatasetIdentical( + assert_identical( renamed, self.ds.rename({'x': 'z'}).z) assert renamed.name == 'z' assert renamed.dims == ('z',) @@ -1257,7 +1257,7 @@ def test_swap_dims(self): array = DataArray(np.random.randn(3), {'y': ('x', list('abc'))}, 'x') expected = DataArray(array.values, {'y': list('abc')}, dims='y') actual = array.swap_dims({'x': 'y'}) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_expand_dims_error(self): array = DataArray(np.random.randn(3, 4), dims=['x', 'dim_0'], @@ -1299,9 +1299,9 @@ def test_expand_dims(self): dims=['y', 'x', 'dim_0'], coords={'x': np.linspace(0.0, 1.0, 3)}, attrs={'key': 'entry'}) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) roundtripped = actual.squeeze('y', drop=True) - self.assertDatasetIdentical(array, roundtripped) + assert_identical(array, roundtripped) # pass multiple dims actual = array.expand_dims(dim=['y', 'z']) @@ -1310,9 +1310,9 @@ def test_expand_dims(self): dims=['y', 'z', 'x', 'dim_0'], coords={'x': np.linspace(0.0, 1.0, 3)}, attrs={'key': 'entry'}) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) roundtripped = actual.squeeze(['y', 'z'], drop=True) - self.assertDatasetIdentical(array, roundtripped) + assert_identical(array, roundtripped) # pass multiple dims and axis. Axis is out of order actual = array.expand_dims(dim=['z', 'y'], axis=[2, 1]) @@ -1321,11 +1321,11 @@ def test_expand_dims(self): dims=['x', 'y', 'z', 'dim_0'], coords={'x': np.linspace(0.0, 1.0, 3)}, attrs={'key': 'entry'}) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) # make sure the attrs are tracked assert actual.attrs['key'] == 'entry' roundtripped = actual.squeeze(['z', 'y'], drop=True) - self.assertDatasetIdentical(array, roundtripped) + assert_identical(array, roundtripped) # Negative axis and they are out of order actual = array.expand_dims(dim=['y', 'z'], axis=[-1, -2]) @@ -1334,10 +1334,10 @@ def test_expand_dims(self): dims=['x', 'dim_0', 'z', 'y'], coords={'x': np.linspace(0.0, 1.0, 3)}, attrs={'key': 'entry'}) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) assert actual.attrs['key'] == 'entry' roundtripped = actual.squeeze(['y', 'z'], drop=True) - self.assertDatasetIdentical(array, roundtripped) + assert_identical(array, roundtripped) def test_expand_dims_with_scalar_coordinate(self): array = DataArray(np.random.randn(3, 4), dims=['x', 'dim_0'], @@ -1349,9 +1349,9 @@ def test_expand_dims_with_scalar_coordinate(self): coords={'x': np.linspace(0.0, 1.0, 3), 'z': np.ones(1)}, attrs={'key': 'entry'}) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) roundtripped = actual.squeeze(['z'], drop=False) - self.assertDatasetIdentical(array, roundtripped) + assert_identical(array, roundtripped) def test_set_index(self): indexes = [self.mindex.get_level_values(n) for n in self.mindex.names] @@ -1363,14 +1363,14 @@ def test_set_index(self): expected['level_3'] = level_3 obj = array.set_index(x=self.mindex.names) - self.assertDataArrayIdentical(obj, expected) + assert_identical(obj, expected) obj = obj.set_index(x='level_3', append=True) expected = array.set_index(x=['level_1', 'level_2', 'level_3']) - self.assertDataArrayIdentical(obj, expected) + assert_identical(obj, expected) array.set_index(x=['level_1', 'level_2', 'level_3'], inplace=True) - self.assertDataArrayIdentical(array, expected) + assert_identical(array, expected) array2d = DataArray(np.random.rand(2, 2), coords={'x': ('x', [0, 1]), @@ -1385,42 +1385,42 @@ def test_reset_index(self): expected = DataArray(self.mda.values, coords=coords, dims='x') obj = self.mda.reset_index('x') - self.assertDataArrayIdentical(obj, expected) + assert_identical(obj, expected) obj = self.mda.reset_index(self.mindex.names) - self.assertDataArrayIdentical(obj, expected) + assert_identical(obj, expected) obj = self.mda.reset_index(['x', 'level_1']) - self.assertDataArrayIdentical(obj, expected) + assert_identical(obj, expected) coords = {'x': ('x', self.mindex.droplevel('level_1')), 'level_1': ('x', self.mindex.get_level_values('level_1'))} expected = DataArray(self.mda.values, coords=coords, dims='x') obj = self.mda.reset_index(['level_1']) - self.assertDataArrayIdentical(obj, expected) + assert_identical(obj, expected) expected = DataArray(self.mda.values, dims='x') obj = self.mda.reset_index('x', drop=True) - self.assertDataArrayIdentical(obj, expected) + assert_identical(obj, expected) array = self.mda.copy() array.reset_index(['x'], drop=True, inplace=True) - self.assertDataArrayIdentical(array, expected) + assert_identical(array, expected) # single index array = DataArray([1, 2], coords={'x': ['a', 'b']}, dims='x') expected = DataArray([1, 2], coords={'x_': ('x', ['a', 'b'])}, dims='x') - self.assertDataArrayIdentical(array.reset_index('x'), expected) + assert_identical(array.reset_index('x'), expected) def test_reorder_levels(self): midx = self.mindex.reorder_levels(['level_2', 'level_1']) expected = DataArray(self.mda.values, coords={'x': midx}, dims='x') obj = self.mda.reorder_levels(x=['level_2', 'level_1']) - self.assertDataArrayIdentical(obj, expected) + assert_identical(obj, expected) array = self.mda.copy() array.reorder_levels(x=['level_2', 'level_1'], inplace=True) - self.assertDataArrayIdentical(array, expected) + assert_identical(array, expected) array = DataArray([1, 2], dims='x') with pytest.raises(KeyError): @@ -1432,12 +1432,12 @@ def test_reorder_levels(self): def test_dataset_getitem(self): dv = self.ds['foo'] - self.assertDataArrayIdentical(dv, self.dv) + assert_identical(dv, self.dv) def test_array_interface(self): assert_equal(np.asarray(self.dv), self.x) # test patched in methods - assert_equal(self.dv.astype(float), self.v.astype(float)) + assert_array_equal(self.dv.astype(float), self.v.astype(float)) assert_array_equal(self.dv.argsort(), self.v.argsort()) assert_array_equal(self.dv.clip(2, 3), self.v.clip(2, 3)) # test ufuncs @@ -1454,8 +1454,8 @@ def test_is_null(self): original = DataArray(x, [-np.arange(5), np.arange(6)], ['x', 'y']) expected = DataArray(pd.isnull(x), [-np.arange(5), np.arange(6)], ['x', 'y']) - self.assertDataArrayIdentical(expected, original.isnull()) - self.assertDataArrayIdentical(~expected, original.notnull()) + assert_identical(expected, original.isnull()) + assert_identical(~expected, original.notnull()) def test_math(self): x = self.x @@ -1477,7 +1477,7 @@ def test_math_automatic_alignment(self): a = DataArray(range(5), [('x', range(5))]) b = DataArray(range(5), [('x', range(1, 6))]) expected = DataArray(np.ones(4), [('x', [1, 2, 3, 4])]) - self.assertDataArrayIdentical(a - b, expected) + assert_identical(a - b, expected) def test_non_overlapping_dataarrays_return_empty_result(self): @@ -1533,63 +1533,63 @@ def test_math_with_coords(self): actual = orig + 1 expected = DataArray(orig.values + 1, orig.coords) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = 1 + orig - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = orig + orig[0, 0] exp_coords = dict((k, v) for k, v in coords.items() if k != 'lat') expected = DataArray(orig.values + orig.values[0, 0], exp_coords, dims=['x', 'y']) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = orig[0, 0] + orig - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = orig[0, 0] + orig[-1, -1] expected = DataArray(orig.values[0, 0] + orig.values[-1, -1], {'c': -999}) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = orig[:, 0] + orig[0, :] exp_values = orig[:, 0].values[:, None] + orig[0, :].values[None, :] expected = DataArray(exp_values, exp_coords, dims=['x', 'y']) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = orig[0, :] + orig[:, 0] - self.assertDataArrayIdentical(expected.T, actual) + assert_identical(expected.T, actual) actual = orig - orig.T expected = DataArray(np.zeros((2, 3)), orig.coords) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = orig.T - orig - self.assertDataArrayIdentical(expected.T, actual) + assert_identical(expected.T, actual) alt = DataArray([1, 1], {'x': [-1, -2], 'c': 'foo', 'd': 555}, 'x') actual = orig + alt expected = orig + 1 expected.coords['d'] = 555 del expected.coords['c'] - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = alt + orig - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_index_math(self): orig = DataArray(range(3), dims='x', name='x') actual = orig + 1 expected = DataArray(1 + np.arange(3), dims='x', name='x') - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) # regression tests for #254 actual = orig[0] < orig expected = DataArray([False, True, True], dims='x', name='x') - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = orig > orig[0] - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_dataset_math(self): # more comprehensive tests with multiple dataset variables @@ -1600,11 +1600,11 @@ def test_dataset_math(self): actual = 2 * obs['tmax'] expected = DataArray(2 * (10 + np.arange(5)), obs.coords, name='tmax') - self.assertDataArrayIdentical(actual, expected) + assert_identical(actual, expected) actual = obs['tmax'] - obs['tmin'] expected = DataArray(10 * np.ones(5), obs.coords) - self.assertDataArrayIdentical(actual, expected) + assert_identical(actual, expected) sim = Dataset({'tmin': ('x', 1 + np.arange(5)), 'tmax': ('x', 11 + np.arange(5)), @@ -1613,30 +1613,30 @@ def test_dataset_math(self): actual = sim['tmin'] - obs['tmin'] expected = DataArray(np.ones(5), obs.coords, name='tmin') - self.assertDataArrayIdentical(actual, expected) + assert_identical(actual, expected) actual = -obs['tmin'] + sim['tmin'] - self.assertDataArrayIdentical(actual, expected) + assert_identical(actual, expected) actual = sim['tmin'].copy() actual -= obs['tmin'] - self.assertDataArrayIdentical(actual, expected) + assert_identical(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) + assert_identical(actual, expected) actual = sim.copy() actual['tmin'] -= obs['tmin'] - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) def test_stack_unstack(self): orig = DataArray([[0, 1], [2, 3]], dims=['x', 'y'], attrs={'foo': 2}) actual = orig.stack(z=['x', 'y']).unstack('z').drop(['x', 'y']) - self.assertDataArrayIdentical(orig, actual) + assert_identical(orig, actual) def test_stack_unstack_decreasing_coordinate(self): # regression test for GH980 @@ -1645,7 +1645,7 @@ def test_stack_unstack_decreasing_coordinate(self): 'y': np.arange(3, 0, -1)}) stacked = orig.stack(allpoints=['y', 'x']) actual = stacked.unstack('allpoints') - self.assertDataArrayIdentical(orig, actual) + assert_identical(orig, actual) def test_unstack_pandas_consistency(self): df = pd.DataFrame({'foo': range(3), @@ -1654,7 +1654,7 @@ def test_unstack_pandas_consistency(self): s = df.set_index(['x', 'y'])['foo'] expected = DataArray(s.unstack(), name='foo') actual = DataArray(s, dims='z').unstack('z') - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_transpose(self): assert_equal(self.dv.variable.transpose(), @@ -1667,18 +1667,18 @@ def test_squeeze_drop(self): array = DataArray([1], [('x', [0])]) expected = DataArray(1) actual = array.squeeze(drop=True) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) expected = DataArray(1, {'x': 0}) actual = array.squeeze(drop=False) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_drop_coordinates(self): expected = DataArray(np.random.randn(2, 3), dims=['x', 'y']) arr = expected.copy() arr.coords['z'] = 2 actual = arr.drop('z') - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) with pytest.raises(ValueError): arr.drop('not found') @@ -1695,7 +1695,7 @@ def test_drop_index_labels(self): dims=['x', 'y']) actual = arr.drop([0, 1], dim='y') expected = arr[:, 2:] - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_dropna(self): x = np.random.randn(4, 4) @@ -1704,23 +1704,23 @@ def test_dropna(self): actual = arr.dropna('a') expected = arr[1::2] - self.assertDataArrayIdentical(actual, expected) + assert_identical(actual, expected) actual = arr.dropna('b', how='all') - self.assertDataArrayIdentical(actual, arr) + assert_identical(actual, arr) actual = arr.dropna('a', thresh=1) - self.assertDataArrayIdentical(actual, arr) + assert_identical(actual, arr) actual = arr.dropna('b', thresh=3) expected = arr[:, 1:] - self.assertDataArrayIdentical(actual, expected) + assert_identical(actual, expected) def test_where(self): arr = DataArray(np.arange(4), dims='x') expected = arr.sel(x=slice(2)) actual = arr.where(arr.x < 2, drop=True) - self.assertDataArrayIdentical(actual, expected) + assert_identical(actual, expected) def test_cumops(self): coords = {'x': [-1, -2], 'y': ['ab', 'cd', 'ef'], @@ -1732,21 +1732,21 @@ def test_cumops(self): actual = orig.cumsum('x') expected = DataArray([[-1, 0, 1], [-4, 0, 4]], coords, dims=['x', 'y']) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = orig.cumsum('y') expected = DataArray([[-1, -1, 0], [-3, -3, 0]], coords, dims=['x', 'y']) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = orig.cumprod('x') expected = DataArray([[-1, 0, 1], [3, 0, 3]], coords, dims=['x', 'y']) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = orig.cumprod('y') expected = DataArray([[-1, 0, 0], [-3, 0, 0]], coords, dims=['x', 'y']) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_reduce(self): coords = {'x': [-1, -2], 'y': ['ab', 'cd', 'ef'], @@ -1756,21 +1756,21 @@ def test_reduce(self): actual = orig.mean() expected = DataArray(0, {'c': -999}) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = orig.mean(['x', 'y']) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = orig.mean('x') expected = DataArray([-2, 0, 2], {'y': coords['y'], 'c': -999}, 'y') - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = orig.mean(['x']) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = orig.mean('y') expected = DataArray([0, 0], {'x': coords['x'], 'c': -999}, 'x') - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) assert_equal(self.dv.reduce(np.mean, 'x').variable, self.v.reduce(np.mean, 'x')) @@ -1778,7 +1778,7 @@ def test_reduce(self): orig = DataArray([[1, 0, np.nan], [3, 0, 3]], coords, dims=['x', 'y']) actual = orig.count() expected = DataArray(5, {'c': -999}) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) # uint support orig = DataArray(np.arange(6).reshape(3, 2).astype('uint'), @@ -1816,33 +1816,33 @@ def test_assign_attrs(self): expected.attrs['b'] = 2 new = DataArray([]) actual = DataArray([]).assign_attrs(a=1, b=2) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) assert new.attrs == {} expected.attrs['c'] = 3 new_actual = actual.assign_attrs({'c': 3}) - self.assertDatasetIdentical(new_actual, expected) + assert_identical(new_actual, expected) assert actual.attrs == {'a': 1, 'b': 2} def test_fillna(self): a = DataArray([np.nan, 1, np.nan, 3], coords={'x': range(4)}, dims='x') actual = a.fillna(-1) expected = DataArray([-1, 1, -1, 3], coords={'x': range(4)}, dims='x') - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) b = DataArray(range(4), coords={'x': range(4)}, dims='x') actual = a.fillna(b) expected = b.copy() - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = a.fillna(range(4)) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = a.fillna(b[:3]) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = a.fillna(b[:0]) - self.assertDataArrayIdentical(a, actual) + assert_identical(a, actual) with raises_regex(TypeError, 'fillna on a DataArray'): a.fillna({0: 0}) @@ -1854,21 +1854,21 @@ def test_fillna(self): actual = a.fillna(fill_value) expected = DataArray([[0, 1], [1, 1], [0, 1], [3, 3]], coords={'x': range(4)}, dims=('x', 'y')) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) expected = b.copy() for target in [a, expected]: target.coords['b'] = ('x', [0, 0, 1, 1]) actual = a.groupby('b').fillna(DataArray([0, 2], dims='b')) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_groupby_iter(self): for ((act_x, act_dv), (exp_x, exp_ds)) in \ zip(self.dv.groupby('y'), self.ds.groupby('y')): assert exp_x == act_x - self.assertDataArrayIdentical(exp_ds['foo'], act_dv) + assert_identical(exp_ds['foo'], act_dv) for ((_, exp_dv), act_dv) in zip(self.dv.groupby('x'), self.dv): - self.assertDataArrayIdentical(exp_dv, act_dv) + assert_identical(exp_dv, act_dv) def make_groupby_example_array(self): da = self.dv.copy() @@ -1879,7 +1879,7 @@ def make_groupby_example_array(self): def test_groupby_properties(self): grouped = self.make_groupby_example_array().groupby('abc') expected_groups = {'a': range(0, 9), 'c': [9], 'b': range(10, 20)} - self.assertItemsEqual(expected_groups.keys(), grouped.groups.keys()) + assert expected_groups.keys() == grouped.groups.keys() for key in expected_groups: assert_equal(expected_groups[key], grouped.groups[key]) assert 3 == len(grouped) @@ -1896,7 +1896,7 @@ def identity(x): for squeeze in [False, True]: grouped = expected.groupby(g, squeeze=squeeze) actual = grouped.apply(identity, shortcut=shortcut) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_groupby_sum(self): array = self.make_groupby_example_array() @@ -1934,7 +1934,7 @@ def test_groupby_count(self): dims='x') actual = array.groupby('cat').count() expected = DataArray([1, 1, 2], coords=[('cat', ['a', 'b', 'c'])]) - self.assertDataArrayIdentical(actual, expected) + assert_identical(actual, expected) @unittest.skip('needs to be fixed for shortcut=False, keep_attrs=False') def test_groupby_reduce_attrs(self): @@ -1949,7 +1949,7 @@ def test_groupby_reduce_attrs(self): expected = array.groupby('abc').mean() if keep_attrs: expected.attrs['foo'] = 'bar' - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_groupby_apply_center(self): def center(x): @@ -1993,18 +1993,18 @@ def test_groupby_math(self): expected = array + array.coords['x'] actual = grouped + array.coords['x'] - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = array.coords['x'] + grouped - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) ds = array.coords['x'].to_dataset('X') expected = array + ds actual = grouped + ds - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = ds + grouped - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) grouped = array.groupby('abc') expected_agg = (grouped.mean() - np.arange(3)).rename(None) @@ -2026,18 +2026,18 @@ def test_groupby_math_not_aligned(self): other = DataArray([10], coords={'b': [0]}, dims='b') actual = array.groupby('b') + other expected = DataArray([10, 11, np.nan, np.nan], array.coords) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) other = DataArray([10], coords={'c': 123, 'b': [0]}, dims='b') actual = array.groupby('b') + other expected.coords['c'] = (['x'], [123] * 2 + [np.nan] * 2) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) other = Dataset({'a': ('b', [10])}, {'b': [0]}) actual = array.groupby('b') + other expected = Dataset({'a': ('x', [10, 11, np.nan, np.nan])}, array.coords) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_groupby_restore_dim_order(self): array = DataArray(np.random.randn(5, 3), @@ -2056,20 +2056,20 @@ def test_groupby_first_and_last(self): expected = DataArray([1, 3], [('ab', ['a', 'b'])]) actual = array.groupby(by).first() - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) expected = DataArray([2, 5], [('ab', ['a', 'b'])]) actual = array.groupby(by).last() - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) array = DataArray(np.random.randn(5, 3), dims=['x', 'y']) expected = DataArray(array[[0, 2]], {'ab': ['a', 'b']}, ['ab', 'y']) actual = array.groupby(by).first() - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = array.groupby('x').first() expected = array # should be a no-op - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def make_groupby_multidim_example_array(self): return DataArray([[[0, 1], [2, 3]], [[5, 10], [15, 20]]], @@ -2084,7 +2084,7 @@ def test_groupby_multidim(self): coords=[('lon', [30., 40., 50.])])), ('lat', DataArray([16, 40], coords=[('lat', [10., 20.])]))]: actual_sum = array.groupby(dim).sum() - self.assertDataArrayIdentical(expected_sum, actual_sum) + assert_identical(expected_sum, actual_sum) def test_groupby_multidim_apply(self): array = self.make_groupby_multidim_example_array() @@ -2092,7 +2092,7 @@ def test_groupby_multidim_apply(self): expected = DataArray([[[-2.5, -6.], [-5., -8.5]], [[2.5, 3.], [8., 8.5]]], coords=array.coords, dims=array.dims) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_groupby_bins(self): array = DataArray(np.arange(4), dims='dim_0') @@ -2107,7 +2107,7 @@ def test_groupby_bins(self): # the problem with this is that it overwrites the dimensions of array! # actual = array.groupby('dim_0', bins=bins).sum() actual = array.groupby_bins('dim_0', bins).apply(lambda x: x.sum()) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) # make sure original array dims are unchanged assert len(array.dim_0) == 4 @@ -2119,7 +2119,7 @@ def test_groupby_bins_empty(self): actual = array.groupby_bins('x', bins).sum() expected = DataArray([6, np.nan], dims='x_bins', coords={'x_bins': bin_coords}) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) # make sure original array is unchanged # (was a problem in earlier versions) assert len(array.x) == 4 @@ -2131,13 +2131,13 @@ def test_groupby_bins_multidim(self): expected = DataArray([16, 40], dims='lat_bins', coords={'lat_bins': bin_coords}) actual = array.groupby_bins('lat', bins).apply(lambda x: x.sum()) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) # modify the array coordinates to be non-monotonic after unstacking array['lat'].data = np.array([[10., 20.], [20., 10.]]) expected = DataArray([28, 28], dims='lat_bins', coords={'lat_bins': bin_coords}) actual = array.groupby_bins('lat', bins).apply(lambda x: x.sum()) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_groupby_bins_sort(self): data = xr.DataArray( @@ -2152,10 +2152,10 @@ def test_resample(self): actual = array.resample(time='24H').mean() expected = DataArray(array.to_series().resample('24H').mean()) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = array.resample(time='24H').reduce(np.mean) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) with raises_regex(ValueError, 'index must be monotonic'): array[[2, 0, 1]].resample(time='1D') @@ -2166,23 +2166,23 @@ def test_resample_first(self): actual = array.resample(time='1D').first() expected = DataArray([0, 4, 8], [('time', times[::4])]) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) # verify that labels don't use the first value actual = array.resample(time='24H').first() expected = DataArray(array.to_series().resample('24H').first()) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) # missing values array = array.astype(float) array[:2] = np.nan actual = array.resample(time='1D').first() expected = DataArray([2, 4, 8], [('time', times[::4])]) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = array.resample(time='1D').first(skipna=False) expected = DataArray([np.nan, 4, 8], [('time', times[::4])]) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) # regression test for http://stackoverflow.com/questions/33158558/ array = Dataset({'time': times})['time'] @@ -2191,7 +2191,7 @@ def test_resample_first(self): '2000-01-03T06']) expected = DataArray(expected_times, [('time', times[::4])], name='time') - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_resample_bad_resample_dim(self): times = pd.date_range('2000-01-01', freq='6H', periods=10) @@ -2240,7 +2240,7 @@ def test_resample_old_vs_new_api(self): with pytest.warns(DeprecationWarning): old_mean = array.resample('1D', 'time', how='mean') new_mean = array.resample(time='1D').mean() - self.assertDataArrayIdentical(old_mean, new_mean) + assert_identical(old_mean, new_mean) # Mean, while keeping attributes attr_array = array.copy() @@ -2251,7 +2251,7 @@ def test_resample_old_vs_new_api(self): keep_attrs=True) new_mean = attr_array.resample(time='1D').mean(keep_attrs=True) assert old_mean.attrs == new_mean.attrs - self.assertDatasetIdentical(old_mean, new_mean) + assert_identical(old_mean, new_mean) # Mean, with NaN to skip nan_array = array.copy() @@ -2262,8 +2262,8 @@ def test_resample_old_vs_new_api(self): skipna=False) new_mean = nan_array.resample(time='1D').mean(skipna=False) expected = DataArray([np.nan, 1, 1], [('time', times[::4])]) - self.assertDataArrayIdentical(old_mean, expected) - self.assertDataArrayIdentical(new_mean, expected) + assert_identical(old_mean, expected) + assert_identical(new_mean, expected) # Try other common resampling methods resampler = array.resample(time='1D') @@ -2273,12 +2273,12 @@ def test_resample_old_vs_new_api(self): new_api = getattr(resampler, method)(keep_attrs=False) with pytest.warns(DeprecationWarning): old_api = array.resample('1D', dim='time', how=method) - self.assertDatasetIdentical(new_api, old_api) + assert_identical(new_api, old_api) for method in [np.mean, np.sum, np.max, np.min]: new_api = resampler.reduce(method) with pytest.warns(DeprecationWarning): old_api = array.resample('1D', dim='time', how=method) - self.assertDatasetIdentical(new_api, old_api) + assert_identical(new_api, old_api) def test_upsample(self): times = pd.date_range('2000-01-01', freq='6H', periods=5) @@ -2287,22 +2287,22 @@ def test_upsample(self): # Forward-fill actual = array.resample(time='3H').ffill() expected = DataArray(array.to_series().resample('3H').ffill()) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) # Backward-fill actual = array.resample(time='3H').bfill() expected = DataArray(array.to_series().resample('3H').bfill()) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) # As frequency actual = array.resample(time='3H').asfreq() expected = DataArray(array.to_series().resample('3H').asfreq()) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) # Pad actual = array.resample(time='3H').pad() expected = DataArray(array.to_series().resample('3H').pad()) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) # Nearest rs = array.resample(time='3H') @@ -2311,7 +2311,7 @@ def test_upsample(self): expected = DataArray( array.reindex(time=new_times, method='nearest') ) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_upsample_nd(self): # Same as before, but now we try on multi-dimensional DataArrays. @@ -2331,7 +2331,7 @@ def test_upsample_nd(self): expected = DataArray(expected_data, {'time': expected_times, 'x': xs, 'y': ys}, ('x', 'y', 'time')) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) # Backward-fill actual = array.resample(time='3H').ffill() @@ -2342,7 +2342,7 @@ def test_upsample_nd(self): expected = DataArray(expected_data, {'time': expected_times, 'x': xs, 'y': ys}, ('x', 'y', 'time')) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) # As frequency actual = array.resample(time='3H').asfreq() @@ -2352,7 +2352,7 @@ def test_upsample_nd(self): expected = DataArray(expected_data, {'time': expected_times, 'x': xs, 'y': ys}, ('x', 'y', 'time')) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) # Pad actual = array.resample(time='3H').pad() @@ -2363,7 +2363,7 @@ def test_upsample_nd(self): expected = DataArray(expected_data, {'time': expected_times, 'x': xs, 'y': ys}, ('x', 'y', 'time')) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) @requires_scipy def test_upsample_interpolate(self): @@ -2427,8 +2427,8 @@ def test_align(self): array = DataArray(np.random.random((6, 8)), coords={'x': list('abcdef')}, dims=['x', 'y']) array1, array2 = align(array, array[:5], join='inner') - self.assertDataArrayIdentical(array1, array[:5]) - self.assertDataArrayIdentical(array2, array[:5]) + assert_identical(array1, array[:5]) + assert_identical(array2, array[:5]) def test_align_dtype(self): # regression test for #264 @@ -2447,23 +2447,23 @@ def test_align_copy(self): expected_y2 = DataArray([2, np.nan, 1], coords=[('a', [1, 2, 3])]) x2, y2 = align(x, y, join='outer', copy=False) - self.assertDataArrayIdentical(expected_x2, x2) - self.assertDataArrayIdentical(expected_y2, y2) + assert_identical(expected_x2, x2) + assert_identical(expected_y2, y2) assert source_ndarray(x2.data) is source_ndarray(x.data) x2, y2 = align(x, y, join='outer', copy=True) - self.assertDataArrayIdentical(expected_x2, x2) - self.assertDataArrayIdentical(expected_y2, y2) + assert_identical(expected_x2, x2) + assert_identical(expected_y2, y2) assert source_ndarray(x2.data) is not source_ndarray(x.data) # Trivial align - 1 element x = DataArray([1, 2, 3], coords=[('a', [1, 2, 3])]) x2, = align(x, copy=False) - self.assertDataArrayIdentical(x, x2) + assert_identical(x, x2) assert source_ndarray(x2.data) is source_ndarray(x.data) x2, = align(x, copy=True) - self.assertDataArrayIdentical(x, x2) + assert_identical(x, x2) assert source_ndarray(x2.data) is not source_ndarray(x.data) def test_align_exclude(self): @@ -2480,9 +2480,9 @@ def test_align_exclude(self): coords=[('a', [-2, -1, 20]), ('b', [5, 6])]) expected_z2 = DataArray([np.nan, np.nan, 1], dims=['a'], coords={'a': [-2, -1, 20], 'b': 7}) - self.assertDataArrayIdentical(expected_x2, x2) - self.assertDataArrayIdentical(expected_y2, y2) - self.assertDataArrayIdentical(expected_z2, z2) + assert_identical(expected_x2, x2) + assert_identical(expected_y2, y2) + assert_identical(expected_z2, z2) def test_align_indexes(self): x = DataArray([1, 2, 3], coords=[('a', [-1, 10, -2])]) @@ -2491,32 +2491,32 @@ def test_align_indexes(self): x2, y2 = align(x, y, join='outer', indexes={'a': [10, -1, -2]}) expected_x2 = DataArray([2, 1, 3], coords=[('a', [10, -1, -2])]) expected_y2 = DataArray([np.nan, 2, 1], coords=[('a', [10, -1, -2])]) - self.assertDataArrayIdentical(expected_x2, x2) - self.assertDataArrayIdentical(expected_y2, y2) + assert_identical(expected_x2, x2) + assert_identical(expected_y2, y2) x2, = align(x, join='outer', indexes={'a': [-2, 7, 10, -1]}) expected_x2 = DataArray([3, np.nan, 2, 1], coords=[('a', [-2, 7, 10, -1])]) - self.assertDataArrayIdentical(expected_x2, x2) + assert_identical(expected_x2, x2) def test_align_without_indexes_exclude(self): arrays = [DataArray([1, 2, 3], dims=['x']), DataArray([1, 2], dims=['x'])] result0, result1 = align(*arrays, exclude=['x']) - self.assertDatasetIdentical(result0, arrays[0]) - self.assertDatasetIdentical(result1, arrays[1]) + assert_identical(result0, arrays[0]) + assert_identical(result1, arrays[1]) def test_align_mixed_indexes(self): array_no_coord = DataArray([1, 2], dims=['x']) array_with_coord = DataArray([1, 2], coords=[('x', ['a', 'b'])]) result0, result1 = align(array_no_coord, array_with_coord) - self.assertDatasetIdentical(result0, array_with_coord) - self.assertDatasetIdentical(result1, array_with_coord) + assert_identical(result0, array_with_coord) + assert_identical(result1, array_with_coord) result0, result1 = align(array_no_coord, array_with_coord, exclude=['x']) - self.assertDatasetIdentical(result0, array_no_coord) - self.assertDatasetIdentical(result1, array_with_coord) + assert_identical(result0, array_no_coord) + assert_identical(result1, array_with_coord) def test_align_without_indexes_errors(self): with raises_regex(ValueError, 'cannot be aligned'): @@ -2534,16 +2534,16 @@ def test_broadcast_arrays(self): expected_coords = [('a', [-1, -2]), ('b', [3, 4])] expected_x2 = DataArray([[1, 1], [2, 2]], expected_coords, name='x') expected_y2 = DataArray([[1, 2], [1, 2]], expected_coords, name='y') - self.assertDataArrayIdentical(expected_x2, x2) - self.assertDataArrayIdentical(expected_y2, y2) + assert_identical(expected_x2, x2) + assert_identical(expected_y2, y2) x = DataArray(np.random.randn(2, 3), dims=['a', 'b']) y = DataArray(np.random.randn(3, 2), dims=['b', 'a']) x2, y2 = broadcast(x, y) expected_x2 = x expected_y2 = y.T - self.assertDataArrayIdentical(expected_x2, x2) - self.assertDataArrayIdentical(expected_y2, y2) + assert_identical(expected_x2, x2) + assert_identical(expected_y2, y2) def test_broadcast_arrays_misaligned(self): # broadcast on misaligned coords must auto-align @@ -2555,8 +2555,8 @@ def test_broadcast_arrays_misaligned(self): expected_y2 = DataArray([[np.nan, np.nan], [1, 1], [2, 2]], coords=[('a', [-2, -1, 20]), ('b', [3, 4])]) x2, y2 = broadcast(x, y) - self.assertDataArrayIdentical(expected_x2, x2) - self.assertDataArrayIdentical(expected_y2, y2) + assert_identical(expected_x2, x2) + assert_identical(expected_y2, y2) def test_broadcast_arrays_nocopy(self): # Test that input data is not copied over in case @@ -2567,13 +2567,13 @@ def test_broadcast_arrays_nocopy(self): expected_y2 = DataArray([3, 3], coords=[('a', [-1, -2])], name='y') x2, y2 = broadcast(x, y) - self.assertDataArrayIdentical(expected_x2, x2) - self.assertDataArrayIdentical(expected_y2, y2) + assert_identical(expected_x2, x2) + assert_identical(expected_y2, y2) assert source_ndarray(x2.data) is source_ndarray(x.data) # single-element broadcast (trivial case) x2, = broadcast(x) - self.assertDataArrayIdentical(x, x2) + assert_identical(x, x2) assert source_ndarray(x2.data) is source_ndarray(x.data) def test_broadcast_arrays_exclude(self): @@ -2588,21 +2588,21 @@ def test_broadcast_arrays_exclude(self): expected_y2 = DataArray([np.nan, 1, 2], coords=[('a', [-2, -1, 20])]) expected_z2 = DataArray([5, 5, 5], dims=['a'], coords={'a': [-2, -1, 20], 'b': 5}) - self.assertDataArrayIdentical(expected_x2, x2) - self.assertDataArrayIdentical(expected_y2, y2) - self.assertDataArrayIdentical(expected_z2, z2) + assert_identical(expected_x2, x2) + assert_identical(expected_y2, y2) + assert_identical(expected_z2, z2) def test_broadcast_coordinates(self): # regression test for GH649 ds = Dataset({'a': (['x', 'y'], np.ones((5, 6)))}) x_bc, y_bc, a_bc = broadcast(ds.x, ds.y, ds.a) - self.assertDataArrayIdentical(ds.a, a_bc) + assert_identical(ds.a, a_bc) X, Y = np.meshgrid(np.arange(5), np.arange(6), indexing='ij') exp_x = DataArray(X, dims=['x', 'y'], name='x') exp_y = DataArray(Y, dims=['x', 'y'], name='y') - self.assertDataArrayIdentical(exp_x, x_bc) - self.assertDataArrayIdentical(exp_y, y_bc) + assert_identical(exp_x, x_bc) + assert_identical(exp_y, y_bc) def test_to_pandas(self): # 0d @@ -2633,7 +2633,7 @@ def test_to_pandas(self): dims = list('abc')[:len(shape)] da = DataArray(np.random.randn(*shape), dims=dims) roundtripped = DataArray(da.to_pandas()).drop(dims) - self.assertDataArrayIdentical(da, roundtripped) + assert_identical(da, roundtripped) with raises_regex(ValueError, 'cannot convert'): DataArray(np.random.randn(1, 2, 3, 4, 5)).to_pandas() @@ -2682,13 +2682,13 @@ def test_to_and_from_series(self): assert_equal(expected.index.values, actual.index.values) assert 'foo' == actual.name # test roundtrip - self.assertDataArrayIdentical( + assert_identical( self.dv, DataArray.from_series(actual).drop(['x', 'y'])) # test name is None actual.name = None expected_da = self.dv.rename(None) - self.assertDataArrayIdentical( + assert_identical( expected_da, DataArray.from_series(actual).drop(['x', 'y'])) @@ -2717,18 +2717,18 @@ def test_to_and_from_dict(self): assert expected == actual # check roundtrip - self.assertDataArrayIdentical(array, DataArray.from_dict(actual)) + assert_identical(array, DataArray.from_dict(actual)) # a more bare bones representation still roundtrips d = {'name': 'foo', 'dims': ('x', 'y'), 'data': array.values.tolist(), 'coords': {'x': {'dims': 'x', 'data': ['a', 'b']}}} - self.assertDataArrayIdentical(array, DataArray.from_dict(d)) + assert_identical(array, DataArray.from_dict(d)) # and the most bare bones representation still roundtrips d = {'name': 'foo', 'dims': ('x', 'y'), 'data': array.values} - self.assertDataArrayIdentical(array.drop('x'), DataArray.from_dict(d)) + assert_identical(array.drop('x'), DataArray.from_dict(d)) # missing a dims in the coords d = {'dims': ('x', 'y'), @@ -2751,7 +2751,7 @@ def test_to_and_from_dict_with_time_dim(self): lat = [77.7, 83.2, 76] da = DataArray(x, {'t': t, 'lat': lat}, dims=['t', 'lat']) roundtripped = DataArray.from_dict(da.to_dict()) - self.assertDataArrayIdentical(da, roundtripped) + assert_identical(da, roundtripped) def test_to_and_from_dict_with_nan_nat(self): y = np.random.randn(10, 3) @@ -2761,7 +2761,7 @@ def test_to_and_from_dict_with_nan_nat(self): lat = [77.7, 83.2, 76] da = DataArray(y, {'t': t, 'lat': lat}, dims=['t', 'lat']) roundtripped = DataArray.from_dict(da.to_dict()) - self.assertDataArrayIdentical(da, roundtripped) + assert_identical(da, roundtripped) def test_to_dict_with_numpy_attrs(self): # this doesn't need to roundtrip @@ -2847,7 +2847,7 @@ def test_to_and_from_cdms2(self): assert str(component_times[0]) == '2000-1-1 0:0:0.0' roundtripped = DataArray.from_cdms2(actual) - self.assertDataArrayIdentical(original, roundtripped) + assert_identical(original, roundtripped) def test_to_and_from_iris(self): try: @@ -2903,7 +2903,7 @@ def test_to_and_from_iris(self): assert actual.standard_name == original.attrs['standard_name'] roundtripped = DataArray.from_iris(actual) - self.assertDataArrayIdentical(original, roundtripped) + assert_identical(original, roundtripped) actual.remove_coord('time') auto_time_dimension = DataArray.from_iris(actual) @@ -2975,7 +2975,7 @@ def test_to_and_from_iris_dask(self): assert actual.standard_name == original.attrs['standard_name'] roundtripped = DataArray.from_iris(actual) - self.assertDataArrayIdentical(original, roundtripped) + assert_identical(original, roundtripped) # If the Iris version supports it then we should get a dask array back if hasattr(actual, 'core_data'): @@ -2999,17 +2999,17 @@ def test_to_dataset_whole(self): actual = unnamed.to_dataset(name='foo') expected = Dataset({'foo': ('x', [1, 2])}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) named = DataArray([1, 2], dims='x', name='foo') actual = named.to_dataset() expected = Dataset({'foo': ('x', [1, 2])}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) expected = Dataset({'bar': ('x', [1, 2])}) with pytest.warns(FutureWarning): actual = named.to_dataset('bar') - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_to_dataset_split(self): array = DataArray([1, 2, 3], coords=[('x', list('abc'))], @@ -3017,18 +3017,18 @@ def test_to_dataset_split(self): expected = Dataset(OrderedDict([('a', 1), ('b', 2), ('c', 3)]), attrs={'a': 1}) actual = array.to_dataset('x') - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) with pytest.raises(TypeError): array.to_dataset('x', name='foo') roundtripped = actual.to_array(dim='x') - self.assertDataArrayIdentical(array, roundtripped) + assert_identical(array, roundtripped) array = DataArray([1, 2, 3], dims='x') expected = Dataset(OrderedDict([(0, 1), (1, 2), (2, 3)])) actual = array.to_dataset('x') - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_to_dataset_retains_keys(self): @@ -3085,24 +3085,24 @@ def test_shift(self): arr = DataArray([1, 2, 3], dims='x') actual = arr.shift(x=1) expected = DataArray([np.nan, 1, 2], dims='x') - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) arr = DataArray([1, 2, 3], [('x', ['a', 'b', 'c'])]) for offset in [-5, -2, -1, 0, 1, 2, 5]: expected = DataArray(arr.to_pandas().shift(offset)) actual = arr.shift(x=offset) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_roll(self): arr = DataArray([1, 2, 3], coords={'x': range(3)}, dims='x') actual = arr.roll(x=1) expected = DataArray([3, 1, 2], coords=[('x', [2, 0, 1])]) - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_real_and_imag(self): array = DataArray(1 + 2j) - self.assertDataArrayIdentical(array.real, DataArray(1)) - self.assertDataArrayIdentical(array.imag, DataArray(2)) + assert_identical(array.real, DataArray(1)) + assert_identical(array.imag, DataArray(2)) def test_setattr_raises(self): array = DataArray(0, coords={'scalar': 1}, attrs={'foo': 'bar'}) @@ -3124,13 +3124,13 @@ def test_full_like(self): actual = full_like(da, 2) expect = da.copy(deep=True) expect.values = [[2.0, 2.0], [2.0, 2.0]] - self.assertDataArrayIdentical(expect, actual) + assert_identical(expect, actual) # override dtype actual = full_like(da, fill_value=True, dtype=bool) expect.values = [[True, True], [True, True]] assert expect.dtype == bool - self.assertDataArrayIdentical(expect, actual) + assert_identical(expect, actual) def test_dot(self): x = np.linspace(-3, 3, 6) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index c48b55476a2..33ffce571ac 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -247,7 +247,7 @@ def test_constructor(self): # verify handling of DataArrays expected = Dataset({'x': x1, 'z': z}) actual = Dataset({'z': expected['z']}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_constructor_invalid_dims(self): # regression for GH1120 @@ -258,16 +258,16 @@ def test_constructor_invalid_dims(self): def test_constructor_1d(self): expected = Dataset({'x': (['x'], 5.0 + np.arange(5))}) actual = Dataset({'x': 5.0 + np.arange(5)}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = Dataset({'x': [5, 6, 7, 8, 9]}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_constructor_0d(self): expected = Dataset({'x': ([], 1)}) for arg in [1, np.array(1), expected['x']]: actual = Dataset({'x': arg}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) class Arbitrary(object): pass @@ -280,7 +280,7 @@ class Arbitrary(object): print(arg) expected = Dataset({'x': ([], arg)}) actual = Dataset({'x': arg}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_constructor_deprecated(self): with raises_regex(ValueError, 'DataArray dimensions'): @@ -295,7 +295,7 @@ def test_constructor_auto_align(self): 'b': ('x', [np.nan, 3, 4])}, {'x': [0, 1, 2]}) actual = Dataset({'a': a, 'b': b}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # regression test for GH346 assert isinstance(actual.variables['x'], IndexVariable) @@ -304,13 +304,13 @@ def test_constructor_auto_align(self): c = ('y', [3, 4]) expected2 = expected.merge({'c': c}) actual = Dataset({'a': a, 'b': b, 'c': c}) - self.assertDatasetIdentical(expected2, actual) + assert_identical(expected2, actual) # variable that is only aligned against the aligned variables d = ('x', [3, 2, 1]) expected3 = expected.merge({'d': d}) actual = Dataset({'a': a, 'b': b, 'd': d}) - self.assertDatasetIdentical(expected3, actual) + assert_identical(expected3, actual) e = ('x', [0, 0]) with raises_regex(ValueError, 'conflicting sizes'): @@ -354,14 +354,14 @@ def test_constructor_compat(self): Dataset(data, compat='equals') expected = Dataset({'x': 0}, {'y': ('z', [1, 1, 1])}) actual = Dataset(data) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = Dataset(data, compat='broadcast_equals') - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) data = OrderedDict([('y', ('z', [1, 1, 1])), ('x', DataArray(0, coords={'y': 1}))]) actual = Dataset(data) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) original = Dataset({'a': (('x', 'y'), np.ones((2, 3)))}, {'c': (('x', 'y'), np.zeros((2, 3))), 'x': [0, 1]}) @@ -373,7 +373,7 @@ def test_constructor_compat(self): # dimensions in 'c' actual = Dataset(OrderedDict([('a', original['a'][:, 0]), ('b', original['a'][0].drop('x'))])) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) data = {'x': DataArray(0, coords={'y': 3}), 'y': ('z', [1, 1, 1])} with pytest.raises(MergeError): @@ -382,7 +382,7 @@ def test_constructor_compat(self): data = {'x': DataArray(0, coords={'y': 1}), 'y': [1, 1]} actual = Dataset(data) expected = Dataset({'x': 0}, {'y': [1, 1]}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_constructor_with_coords(self): with raises_regex(ValueError, 'found in both data_vars and'): @@ -454,8 +454,8 @@ def test_get_index(self): def test_attr_access(self): ds = Dataset({'tmin': ('x', [42], {'units': 'Celcius'})}, attrs={'title': 'My test data'}) - self.assertDataArrayIdentical(ds.tmin, ds['tmin']) - self.assertDataArrayIdentical(ds.tmin.x, ds.x) + assert_identical(ds.tmin, ds['tmin']) + assert_identical(ds.tmin.x, ds.x) assert ds.title == ds.attrs['title'] assert ds.tmin.units == ds['tmin'].attrs['units'] @@ -466,7 +466,7 @@ def test_attr_access(self): # should defer to variable of same name ds.attrs['tmin'] = -999 assert ds.attrs['tmin'] == -999 - self.assertDataArrayIdentical(ds.tmin, ds['tmin']) + assert_identical(ds.tmin, ds['tmin']) def test_variable(self): a = Dataset() @@ -489,10 +489,10 @@ def test_modify_inplace(self): a['x'] = ('x', vec, attributes) assert 'x' in a.coords assert isinstance(a.coords['x'].to_index(), pd.Index) - self.assertVariableIdentical(a.coords['x'].variable, a.variables['x']) + assert_identical(a.coords['x'].variable, a.variables['x']) b = Dataset() b['x'] = ('x', vec, attributes) - self.assertVariableIdentical(a['x'], b['x']) + assert_identical(a['x'], b['x']) assert a.dims == b.dims # this should work a['x'] = ('x', vec[:5]) @@ -523,10 +523,8 @@ def test_coords_properties(self): self.assertItemsEqual(['x', 'y', 'a', 'b'], list(data.coords)) - self.assertVariableIdentical(data.coords['x'].variable, - data['x'].variable) - self.assertVariableIdentical(data.coords['y'].variable, - data['y'].variable) + assert_identical(data.coords['x'].variable, data['x'].variable) + assert_identical(data.coords['y'].variable, data['y'].variable) assert 'x' in data.coords assert 'a' in data.coords @@ -566,12 +564,12 @@ def test_coords_modify(self): actual = data.copy(deep=True) with raises_regex(ValueError, 'conflicting sizes'): actual.coords['x'] = ('x', [-1]) - self.assertDatasetIdentical(actual, data) # should not be modified + assert_identical(actual, data) # should not be modified actual = data.copy() del actual.coords['b'] expected = data.reset_coords('b', drop=True) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) with pytest.raises(KeyError): del data.coords['not_found'] @@ -582,13 +580,13 @@ def test_coords_modify(self): actual = data.copy(deep=True) actual.coords.update({'c': 11}) expected = data.merge({'c': 11}).set_coords('c') - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_coords_setitem_with_new_dimension(self): actual = Dataset() actual.coords['foo'] = ('x', [1, 2, 3]) expected = Dataset(coords={'foo': ('x', [1, 2, 3])}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_coords_setitem_multiindex(self): data = create_test_multiindex() @@ -607,42 +605,42 @@ def test_coords_set(self): 'zzz': ('x', [2])}) actual = one_coord.set_coords('x') - self.assertDatasetIdentical(one_coord, actual) + assert_identical(one_coord, actual) actual = one_coord.set_coords(['x']) - self.assertDatasetIdentical(one_coord, actual) + assert_identical(one_coord, actual) actual = one_coord.set_coords('yy') - self.assertDatasetIdentical(two_coords, actual) + assert_identical(two_coords, actual) actual = one_coord.set_coords(['yy', 'zzz']) - self.assertDatasetIdentical(all_coords, actual) + assert_identical(all_coords, actual) actual = one_coord.reset_coords() - self.assertDatasetIdentical(one_coord, actual) + assert_identical(one_coord, actual) actual = two_coords.reset_coords() - self.assertDatasetIdentical(one_coord, actual) + assert_identical(one_coord, actual) actual = all_coords.reset_coords() - self.assertDatasetIdentical(one_coord, actual) + assert_identical(one_coord, actual) actual = all_coords.reset_coords(['yy', 'zzz']) - self.assertDatasetIdentical(one_coord, actual) + assert_identical(one_coord, actual) actual = all_coords.reset_coords('zzz') - self.assertDatasetIdentical(two_coords, actual) + assert_identical(two_coords, actual) with raises_regex(ValueError, 'cannot remove index'): one_coord.reset_coords('x') actual = all_coords.reset_coords('zzz', drop=True) expected = all_coords.drop('zzz') - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) expected = two_coords.drop('zzz') - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_coords_to_dataset(self): orig = Dataset({'foo': ('y', [-1, 0, 1])}, {'x': 10, 'y': [2, 3, 4]}) expected = Dataset(coords={'x': 10, 'y': [2, 3, 4]}) actual = orig.coords.to_dataset() - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_coords_merge(self): orig_coords = Dataset(coords={'a': ('x', [1, 2]), 'x': [0, 1]}).coords @@ -652,9 +650,9 @@ def test_coords_merge(self): 'b': ('x', ['a', 'b']), 'x': [0, 1]}) actual = orig_coords.merge(other_coords) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = other_coords.merge(orig_coords) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) other_coords = Dataset(coords={'x': ('x', ['a'])}).coords with pytest.raises(MergeError): @@ -669,36 +667,36 @@ def test_coords_merge(self): other_coords = Dataset(coords={'a': ('x', [8, 9])}).coords expected = Dataset(coords={'x': range(2)}) actual = orig_coords.merge(other_coords) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = other_coords.merge(orig_coords) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) other_coords = Dataset(coords={'x': np.nan}).coords actual = orig_coords.merge(other_coords) - self.assertDatasetIdentical(orig_coords.to_dataset(), actual) + assert_identical(orig_coords.to_dataset(), actual) actual = other_coords.merge(orig_coords) - self.assertDatasetIdentical(orig_coords.to_dataset(), actual) + assert_identical(orig_coords.to_dataset(), actual) def test_coords_merge_mismatched_shape(self): orig_coords = Dataset(coords={'a': ('x', [1, 1])}).coords other_coords = Dataset(coords={'a': 1}).coords expected = orig_coords.to_dataset() actual = orig_coords.merge(other_coords) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) other_coords = Dataset(coords={'a': ('y', [1])}).coords expected = Dataset(coords={'a': (['x', 'y'], [[1], [1]])}) actual = orig_coords.merge(other_coords) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = other_coords.merge(orig_coords) - self.assertDatasetIdentical(expected.transpose(), actual) + assert_identical(expected.transpose(), actual) orig_coords = Dataset(coords={'a': ('x', [np.nan])}).coords other_coords = Dataset(coords={'a': np.nan}).coords expected = orig_coords.to_dataset() actual = orig_coords.merge(other_coords) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_data_vars_properties(self): ds = Dataset() @@ -708,7 +706,7 @@ def test_data_vars_properties(self): assert set(ds.data_vars) == {'foo', 'bar'} assert 'foo' in ds.data_vars assert 'x' not in ds.data_vars - self.assertDataArrayIdentical(ds['foo'], ds.data_vars['foo']) + assert_identical(ds['foo'], ds.data_vars['foo']) expected = dedent("""\ Data variables: @@ -786,7 +784,7 @@ def test_chunk(self): # reblock on already blocked data reblocked = reblocked.chunk(expected_chunks) assert reblocked.chunks == expected_chunks - self.assertDatasetIdentical(reblocked, data) + assert_identical(reblocked, data) with raises_regex(ValueError, 'some chunks'): data.chunk({'foo': 10}) @@ -883,7 +881,7 @@ def test_isel_fancy(self): expected = data.isel(dim1=(('test_coord', ), pdim1), dim2=(('test_coord', ), pdim2), dim3=(('test_coord', ), pdim3)) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) # DataArray with coordinate idx1 = DataArray(pdim1, dims=['a'], coords={'a': np.random.randn(3)}) @@ -902,7 +900,7 @@ def test_isel_fancy(self): dim3=(('c', ), pdim3)) expected = expected.assign_coords(a=idx1['a'], b=idx2['b'], c=idx3['c']) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) idx1 = DataArray(pdim1, dims=['a'], coords={'a': np.random.randn(3)}) idx2 = DataArray(pdim2, dims=['a']) @@ -917,7 +915,7 @@ def test_isel_fancy(self): dim2=(('a', ), pdim2), dim3=(('a', ), pdim3)) expected = expected.assign_coords(a=idx1['a']) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) actual = data.isel(dim1=(('points', ), pdim1), dim2=(('points', ), pdim2)) @@ -927,7 +925,7 @@ def test_isel_fancy(self): np.testing.assert_array_equal(data['dim2'][pdim2], actual['dim2']) # test that the order of the indexers doesn't matter - self.assertDatasetIdentical(data.isel(dim1=(('points', ), pdim1), + assert_identical(data.isel(dim1=(('points', ), pdim1), dim2=(('points', ), pdim2)), data.isel(dim2=(('points', ), pdim2), dim1=(('points', ), pdim1))) @@ -942,7 +940,7 @@ def test_isel_fancy(self): # test to be sure we keep around variables that were not indexed ds = Dataset({'x': [1, 2, 3, 4], 'y': 0}) actual = ds.isel(x=(('points', ), [0, 1, 2])) - self.assertDataArrayIdentical(ds['y'], actual['y']) + assert_identical(ds['y'], actual['y']) # tests using index or DataArray as indexers stations = Dataset() @@ -954,7 +952,7 @@ def test_isel_fancy(self): dim2=stations['dim2s']) assert 'station' in actual.coords assert 'station' in actual.dims - self.assertDataArrayIdentical(actual['station'].drop(['dim2']), + assert_identical(actual['station'].drop(['dim2']), stations['station']) with raises_regex(ValueError, 'conflicting values for '): @@ -977,9 +975,9 @@ def test_isel_fancy(self): assert 'dim2' in actual.coords assert 'a' in actual['dim2'].dims - self.assertDataArrayIdentical(actual['a'].drop(['dim2']), + assert_identical(actual['a'].drop(['dim2']), stations['a']) - self.assertDataArrayIdentical(actual['b'], stations['b']) + assert_identical(actual['b'], stations['b']) expected_var1 = data['var1'].variable[stations['dim1s'].variable, stations['dim2s'].variable] expected_var2 = data['var2'].variable[stations['dim1s'].variable, @@ -998,8 +996,8 @@ def test_isel_dataarray(self): indexing_da = DataArray(np.arange(1, 4), dims=['dim1'], coords={'dim1': np.random.randn(3)}) actual = data.isel(dim1=indexing_da) - self.assertDataArrayIdentical(indexing_da['dim1'], actual['dim1']) - self.assertDataArrayIdentical(data['dim2'], actual['dim2']) + assert_identical(indexing_da['dim1'], actual['dim1']) + assert_identical(data['dim2'], actual['dim2']) # Conflict in the dimension coordinate indexing_da = DataArray(np.arange(1, 4), dims=['dim2'], @@ -1016,21 +1014,21 @@ def test_isel_dataarray(self): indexing_da = DataArray(np.arange(1, 4), dims=['dim2'], coords={'dim2': data['dim2'].values[1:4]}) actual = data.isel(dim2=indexing_da) - self.assertDataArrayIdentical(actual['dim2'], indexing_da['dim2']) + assert_identical(actual['dim2'], indexing_da['dim2']) # Silently drop conflicted (non-dimensional) coordinate of indexer indexing_da = DataArray(np.arange(1, 4), dims=['dim2'], coords={'dim2': data['dim2'].values[1:4], 'numbers': ('dim2', np.arange(2, 5))}) actual = data.isel(dim2=indexing_da) - self.assertDataArrayIdentical(actual['numbers'], data['numbers']) + assert_identical(actual['numbers'], data['numbers']) # boolean data array with coordinate with the same name indexing_da = DataArray(np.arange(1, 10), dims=['dim2'], coords={'dim2': data['dim2'].values}) indexing_da = (indexing_da < 3) actual = data.isel(dim2=indexing_da) - self.assertDataArrayIdentical(actual['dim2'], data['dim2'][:2]) + assert_identical(actual['dim2'], data['dim2'][:2]) # boolean data array with non-dimensioncoordinate indexing_da = DataArray(np.arange(1, 10), dims=['dim2'], @@ -1040,11 +1038,11 @@ def test_isel_dataarray(self): 'non_dim2': 0}) indexing_da = (indexing_da < 3) actual = data.isel(dim2=indexing_da) - self.assertDataArrayIdentical( + assert_identical( actual['dim2'].drop('non_dim').drop('non_dim2'), data['dim2'][:2]) - self.assertDataArrayIdentical( + assert_identical( actual['non_dim'], indexing_da['non_dim'][:2]) - self.assertDataArrayIdentical( + assert_identical( actual['non_dim2'], indexing_da['non_dim2']) # non-dimension coordinate will be also attached @@ -1177,13 +1175,13 @@ def test_sel_dataarray_mindex(self): actual_sel = mds.sel(x=DataArray(mds.indexes['x'][:3], dims='x')) assert actual_isel['x'].dims == ('x', ) assert actual_sel['x'].dims == ('x', ) - self.assertDatasetIdentical(actual_isel, actual_sel) + assert_identical(actual_isel, actual_sel) actual_isel = mds.isel(x=xr.DataArray(np.arange(3), dims='z')) actual_sel = mds.sel(x=Variable('z', mds.indexes['x'][:3])) assert actual_isel['x'].dims == ('z', ) assert actual_sel['x'].dims == ('z', ) - self.assertDatasetIdentical(actual_isel, actual_sel) + assert_identical(actual_isel, actual_sel) # with coordinate actual_isel = mds.isel(x=xr.DataArray(np.arange(3), dims='z', @@ -1192,7 +1190,7 @@ def test_sel_dataarray_mindex(self): coords={'z': [0, 1, 2]})) assert actual_isel['x'].dims == ('z', ) assert actual_sel['x'].dims == ('z', ) - self.assertDatasetIdentical(actual_isel, actual_sel) + assert_identical(actual_isel, actual_sel) # Vectorized indexing with level-variables raises an error with raises_regex(ValueError, 'Vectorized selection is '): @@ -1208,26 +1206,26 @@ def test_sel_drop(self): data = Dataset({'foo': ('x', [1, 2, 3])}, {'x': [0, 1, 2]}) expected = Dataset({'foo': 1}) selected = data.sel(x=0, drop=True) - self.assertDatasetIdentical(expected, selected) + assert_identical(expected, selected) expected = Dataset({'foo': 1}, {'x': 0}) selected = data.sel(x=0, drop=False) - self.assertDatasetIdentical(expected, selected) + assert_identical(expected, selected) data = Dataset({'foo': ('x', [1, 2, 3])}) expected = Dataset({'foo': 1}) selected = data.sel(x=0, drop=True) - self.assertDatasetIdentical(expected, selected) + assert_identical(expected, selected) def test_isel_drop(self): data = Dataset({'foo': ('x', [1, 2, 3])}, {'x': [0, 1, 2]}) expected = Dataset({'foo': 1}) selected = data.isel(x=0, drop=True) - self.assertDatasetIdentical(expected, selected) + assert_identical(expected, selected) expected = Dataset({'foo': 1}, {'x': 0}) selected = data.isel(x=0, drop=False) - self.assertDatasetIdentical(expected, selected) + assert_identical(expected, selected) def test_isel_points(self): data = create_test_data() @@ -1247,7 +1245,7 @@ def test_isel_points(self): np.testing.assert_array_equal(data['dim2'][pdim2], actual['dim2']) # test that the order of the indexers doesn't matter - self.assertDatasetIdentical(data.isel_points(dim1=pdim1, dim2=pdim2), + assert_identical(data.isel_points(dim1=pdim1, dim2=pdim2), data.isel_points(dim2=pdim2, dim1=pdim1)) # make sure we're raising errors in the right places @@ -1271,7 +1269,7 @@ def test_isel_points(self): # test to be sure we keep around variables that were not indexed ds = Dataset({'x': [1, 2, 3, 4], 'y': 0}) actual = ds.isel_points(x=[0, 1, 2]) - self.assertDataArrayIdentical(ds['y'], actual['y']) + assert_identical(ds['y'], actual['y']) # tests using index or DataArray as a dim stations = Dataset() @@ -1284,7 +1282,7 @@ def test_isel_points(self): dim=stations['station']) assert 'station' in actual.coords assert 'station' in actual.dims - self.assertDataArrayIdentical(actual['station'].drop(['dim2']), + assert_identical(actual['station'].drop(['dim2']), stations['station']) # make sure we get the default 'points' coordinate when passed a list @@ -1319,19 +1317,19 @@ def test_sel_points(self): dim='test_coord') actual = data.sel_points(dim1=data.dim1[pdim1], dim2=data.dim2[pdim2], dim3=data.dim3[pdim3], dim='test_coord') - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) data = Dataset({'foo': (('x', 'y'), np.arange(9).reshape(3, 3))}) expected = Dataset({'foo': ('points', [0, 4, 8])}) actual = data.sel_points(x=[0, 1, 2], y=[0, 1, 2]) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) data.coords.update({'x': [0, 1, 2], 'y': [0, 1, 2]}) expected.coords.update({'x': ('points', [0, 1, 2]), 'y': ('points', [0, 1, 2])}) actual = data.sel_points(x=[0.1, 1.1, 2.5], y=[0, 1.2, 2.0], method='pad') - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) with pytest.raises(KeyError): data.sel_points(x=[2.5], y=[2.0], method='pad', tolerance=1e-3) @@ -1351,7 +1349,7 @@ def test_sel_fancy(self): actual = data.sel(dim1=Variable(('test_coord', ), data.dim1[pdim1]), dim2=Variable(('test_coord', ), data.dim2[pdim2]), dim3=Variable(('test_coord', ), data.dim3[pdim3])) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # DataArray Indexer idx_t = DataArray(data['time'][[3, 2, 1]].values, dims=['a'], @@ -1365,7 +1363,7 @@ def test_sel_fancy(self): dim2=Variable(('a', ), [3, 2, 1]), dim3=Variable(('a', ), [3, 2, 1])) expected = expected.assign_coords(a=idx_t['a']) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) idx_t = DataArray(data['time'][[3, 2, 1]].values, dims=['a'], coords={'a': ['a', 'b', 'c']}) @@ -1379,7 +1377,7 @@ def test_sel_fancy(self): dim3=Variable(('c', ), [1, 2, 1])) expected = expected.assign_coords(a=idx_t['a'], b=idx_2['b'], c=idx_3['c']) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # test from sel_points data = Dataset({'foo': (('x', 'y'), np.arange(9).reshape(3, 3))}) @@ -1390,22 +1388,22 @@ def test_sel_fancy(self): 'y': Variable(('points', ), [0, 1, 2])}) actual = data.sel(x=Variable(('points', ), [0, 1, 2]), y=Variable(('points', ), [0, 1, 2])) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) expected.coords.update({'x': ('points', [0, 1, 2]), 'y': ('points', [0, 1, 2])}) actual = data.sel(x=Variable(('points', ), [0.1, 1.1, 2.5]), y=Variable(('points', ), [0, 1.2, 2.0]), method='pad') - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) idx_x = DataArray([0, 1, 2], dims=['a'], coords={'a': ['a', 'b', 'c']}) idx_y = DataArray([0, 2, 1], dims=['b'], coords={'b': [0, 3, 6]}) expected_ary = data['foo'][[0, 1, 2], [0, 2, 1]] actual = data.sel(x=idx_x, y=idx_y) assert_equal(expected_ary, actual['foo']) - self.assertDataArrayIdentical(actual['a'].drop('x'), idx_x['a']) - self.assertDataArrayIdentical(actual['b'].drop('y'), idx_y['b']) + assert_identical(actual['a'].drop('x'), idx_x['a']) + assert_identical(actual['b'].drop('y'), idx_y['b']) with pytest.raises(KeyError): data.sel_points(x=[2.5], y=[2.0], method='pad', tolerance=1e-3) @@ -1415,17 +1413,17 @@ def test_sel_method(self): expected = data.sel(dim2=1) actual = data.sel(dim2=0.95, method='nearest') - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = data.sel(dim2=0.95, method='nearest', tolerance=1) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) with pytest.raises(KeyError): actual = data.sel(dim2=np.pi, method='nearest', tolerance=0) expected = data.sel(dim2=[1.5]) actual = data.sel(dim2=[1.45], method='backfill') - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) with raises_regex(NotImplementedError, 'slice objects'): data.sel(dim2=slice(1, 3), method='ffill') @@ -1442,7 +1440,7 @@ def test_loc(self): data = create_test_data() expected = data.sel(dim3='a') actual = data.loc[dict(dim3='a')] - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) with raises_regex(TypeError, 'can only lookup dict'): data.loc['a'] with pytest.raises(TypeError): @@ -1459,12 +1457,12 @@ def test_sel(lab_indexer, pos_indexer, replaced_idx=False, ds = mdata.sel(x=lab_indexer) expected_ds = mdata.isel(x=pos_indexer) if not replaced_idx: - self.assertDatasetIdentical(ds, expected_ds) + assert_identical(ds, expected_ds) else: if renamed_dim: assert ds['var'].dims[0] == renamed_dim ds = ds.rename({renamed_dim: 'x'}) - self.assertVariableIdentical(ds['var'].variable, + assert_identical(ds['var'].variable, expected_ds['var'].variable) self.assertVariableNotEqual(ds['x'], expected_ds['x']) @@ -1481,16 +1479,16 @@ def test_sel(lab_indexer, pos_indexer, replaced_idx=False, renamed_dim='three') test_sel({'one': 'a'}, range(4), replaced_idx=True) - self.assertDatasetIdentical(mdata.loc[{'x': {'one': 'a'}}], + assert_identical(mdata.loc[{'x': {'one': 'a'}}], mdata.sel(x={'one': 'a'})) - self.assertDatasetIdentical(mdata.loc[{'x': 'a'}], + assert_identical(mdata.loc[{'x': 'a'}], mdata.sel(x='a')) - self.assertDatasetIdentical(mdata.loc[{'x': ('a', 1)}], + assert_identical(mdata.loc[{'x': ('a', 1)}], mdata.sel(x=('a', 1))) - self.assertDatasetIdentical(mdata.loc[{'x': ('a', 1, -1)}], + assert_identical(mdata.loc[{'x': ('a', 1, -1)}], mdata.sel(x=('a', 1, -1))) - self.assertDatasetIdentical(mdata.sel(x={'one': 'a', 'two': 1}), + assert_identical(mdata.sel(x={'one': 'a', 'two': 1}), mdata.sel(one='a', two=1)) def test_reindex_like(self): @@ -1499,7 +1497,7 @@ def test_reindex_like(self): expected = data.isel(dim1=slice(10), time=slice(13)) actual = data.reindex_like(expected) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) expected = data.copy(deep=True) expected['dim3'] = ('dim3', list('cdefghijkl')) @@ -1511,21 +1509,21 @@ def test_reindex_like(self): expected['numbers'][:-2] = expected['numbers'][2:].values expected['numbers'][-2:] = np.nan actual = data.reindex_like(expected) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) def test_reindex(self): data = create_test_data() - self.assertDatasetIdentical(data, data.reindex()) + assert_identical(data, data.reindex()) expected = data.assign_coords(dim1=data['dim1']) actual = data.reindex(dim1=data['dim1']) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) actual = data.reindex(dim1=data['dim1'].values) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) actual = data.reindex(dim1=data['dim1'].to_index()) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) with raises_regex( ValueError, 'cannot reindex or align along dimension'): @@ -1533,12 +1531,12 @@ def test_reindex(self): expected = data.isel(dim2=slice(5)) actual = data.reindex(dim2=data['dim2'][:5]) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) # test dict-like argument actual = data.reindex({'dim2': data['dim2']}) expected = data - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) with raises_regex(ValueError, 'cannot specify both'): data.reindex({'x': 0}, x=0) with raises_regex(ValueError, 'dictionary'): @@ -1551,7 +1549,7 @@ def test_reindex(self): # out of order expected = data.sel(dim2=data['dim2'][:5:-1]) actual = data.reindex(dim2=data['dim2'][:5:-1]) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) # regression test for #279 expected = Dataset({'x': ('time', np.random.randn(5))}, @@ -1559,7 +1557,7 @@ def test_reindex(self): time2 = DataArray(np.arange(5), dims="time2") with pytest.warns(FutureWarning): actual = expected.reindex(time=time2) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) # another regression test ds = Dataset({'foo': (['x', 'y'], np.zeros((3, 4)))}, @@ -1568,7 +1566,7 @@ def test_reindex(self): {'x': [0, 1, 3], 'y': [0, 1]}) expected['foo'][-1] = np.nan actual = ds.reindex(x=[0, 1, 3], y=[0, 1]) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_reindex_warning(self): data = create_test_data() @@ -1597,19 +1595,19 @@ def test_reindex_method(self): y = [-0.5, 0.5, 1.5] actual = ds.reindex(y=y, method='backfill') expected = Dataset({'x': ('y', [10, 20, np.nan]), 'y': y}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = ds.reindex(y=y, method='backfill', tolerance=0.1) expected = Dataset({'x': ('y', 3 * [np.nan]), 'y': y}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = ds.reindex(y=y, method='pad') expected = Dataset({'x': ('y', [np.nan, 10, 20]), 'y': y}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) alt = Dataset({'y': y}) actual = ds.reindex_like(alt, method='pad') - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_align(self): left = create_test_data() @@ -1625,46 +1623,33 @@ def test_align(self): left2, right2 = align(left, right, join='inner') assert_equal(left2['dim3'], intersection) - self.assertDatasetIdentical(left2, right2) + assert_identical(left2, right2) left2, right2 = align(left, right, join='outer') -<<<<<<< HEAD - self.assertVariableEqual(left2['dim3'].variable, - right2['dim3'].variable) + self.assertArrayEqual(left2['dim3'], union) -======= assert_equal(left2['dim3'].variable, right2['dim3'].variable) - assert_equal(left2['dim3'], union) ->>>>>>> assert_equal - self.assertDatasetIdentical(left2.sel(dim3=intersection), + + assert_identical(left2.sel(dim3=intersection), right2.sel(dim3=intersection)) assert np.isnan(left2['var3'][-2:]).all() assert np.isnan(right2['var3'][:2]).all() left2, right2 = align(left, right, join='left') -<<<<<<< HEAD - self.assertVariableEqual(left2['dim3'].variable, - right2['dim3'].variable) - self.assertVariableEqual(left2['dim3'].variable, left['dim3'].variable) -======= assert_equal(left2['dim3'].variable, right2['dim3'].variable) assert_equal(left2['dim3'].variable, left['dim3'].variable) ->>>>>>> assert_equal + self.assertDatasetIdentical(left2.sel(dim3=intersection), + + assert_identical(left2.sel(dim3=intersection), right2.sel(dim3=intersection)) assert np.isnan(right2['var3'][:2]).all() left2, right2 = align(left, right, join='right') -<<<<<<< HEAD - self.assertVariableEqual(left2['dim3'].variable, - right2['dim3'].variable) - self.assertVariableEqual(left2['dim3'].variable, - right['dim3'].variable) -======= assert_equal(left2['dim3'].variable, right2['dim3'].variable) assert_equal(left2['dim3'].variable, right['dim3'].variable) ->>>>>>> assert_equal self.assertDatasetIdentical(left2.sel(dim3=intersection), + assert_identical(left2.sel(dim3=intersection), right2.sel(dim3=intersection)) assert np.isnan(left2['var3'][-2:]).all() @@ -1678,8 +1663,8 @@ def test_align_exact(self): right = xr.Dataset(coords={'x': [1, 2]}) left1, left2 = xr.align(left, left, join='exact') - self.assertDatasetIdentical(left1, left) - self.assertDatasetIdentical(left2, left) + assert_identical(left1, left) + assert_identical(left2, left) with raises_regex(ValueError, 'indexes .* not equal'): xr.align(left, right, join='exact') @@ -1699,8 +1684,8 @@ def test_align_exclude(self): {'bar': DataArray([[1, 2], [np.nan, np.nan], [3, 4]], dims=['x', 'y'], coords={'x': [1, 2, 3], 'y': [5, 6]})}) - self.assertDatasetIdentical(expected_x2, x2) - self.assertDatasetIdentical(expected_y2, y2) + assert_identical(expected_x2, x2) + assert_identical(expected_y2, y2) def test_align_nocopy(self): x = Dataset({'foo': DataArray([1, 2, 3], coords=[('x', [1, 2, 3])])}) @@ -1710,15 +1695,15 @@ def test_align_nocopy(self): coords=[('x', [1, 2, 3])])}) x2, y2 = align(x, y, copy=False, join='outer') - self.assertDatasetIdentical(expected_x2, x2) - self.assertDatasetIdentical(expected_y2, y2) + assert_identical(expected_x2, x2) + assert_identical(expected_y2, y2) assert source_ndarray(x['foo'].data) is source_ndarray(x2['foo'].data) x2, y2 = align(x, y, copy=True, join='outer') - self.assertDatasetIdentical(expected_x2, x2) - self.assertDatasetIdentical(expected_y2, y2) assert source_ndarray(x['foo'].data) is not \ source_ndarray(x2['foo'].data) + assert_identical(expected_x2, x2) + assert_identical(expected_y2, y2) def test_align_indexes(self): x = Dataset({'foo': DataArray([1, 2, 3], dims='x', @@ -1726,7 +1711,9 @@ def test_align_indexes(self): x2, = align(x, indexes={'x': [2, 3, 1]}) expected_x2 = Dataset({'foo': DataArray([2, 3, 1], dims='x', coords={'x': [2, 3, 1]})}) - self.assertDatasetIdentical(expected_x2, x2) + + assert_identical(expected_x2, x2) + def test_align_non_unique(self): x = Dataset({'foo': ('x', [3, 4, 5]), 'x': [0, 0, 1]}) @@ -1745,21 +1732,21 @@ def test_broadcast(self): 'baz': (('x', 'y'), [[2, 3]])}, {'c': ('x', [4])}) actual, = broadcast(ds) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) ds_x = Dataset({'foo': ('x', [1])}) ds_y = Dataset({'bar': ('y', [2, 3])}) expected_x = Dataset({'foo': (('x', 'y'), [[1, 1]])}) expected_y = Dataset({'bar': (('x', 'y'), [[2, 3]])}) actual_x, actual_y = broadcast(ds_x, ds_y) - self.assertDatasetIdentical(expected_x, actual_x) - self.assertDatasetIdentical(expected_y, actual_y) + assert_identical(expected_x, actual_x) + assert_identical(expected_y, actual_y) array_y = ds_y['bar'] expected_y = expected_y['bar'] actual_x, actual_y = broadcast(ds_x, array_y) - self.assertDatasetIdentical(expected_x, actual_x) - self.assertDataArrayIdentical(expected_y, actual_y) + assert_identical(expected_x, actual_x) + assert_identical(expected_y, actual_y) def test_broadcast_nocopy(self): # Test that data is not copied if not needed @@ -1767,14 +1754,14 @@ def test_broadcast_nocopy(self): y = Dataset({'bar': ('y', [2, 3])}) actual_x, = broadcast(x) - self.assertDatasetIdentical(x, actual_x) - assert source_ndarray(actual_x['foo'].data) is \ - source_ndarray(x['foo'].data) + assert_identical(x, actual_x) + assert source_ndarray(actual_x['foo'].data) is source_ndarray( + x['foo'].data) actual_x, actual_y = broadcast(x, y) - self.assertDatasetIdentical(x, actual_x) - assert source_ndarray(actual_x['foo'].data) is \ - source_ndarray(x['foo'].data) + assert_identical(x, actual_x) + assert source_ndarray(actual_x['foo'].data) is source_ndarray( + x['foo'].data) def test_broadcast_exclude(self): x = Dataset({ @@ -1798,8 +1785,8 @@ def test_broadcast_exclude(self): 'foo': DataArray([[[1, 2]], [[1, 2]]], dims=['x', 'z', 'y'], coords={'z': [1], 'x': [1, 2], 'y': [5, 6]}), }) - self.assertDatasetIdentical(expected_x2, x2) - self.assertDatasetIdentical(expected_y2, y2) + assert_identical(expected_x2, x2) + assert_identical(expected_y2, y2) def test_broadcast_misaligned(self): x = Dataset({'foo': DataArray([1, 2, 3], @@ -1815,8 +1802,8 @@ def test_broadcast_misaligned(self): {'bar': DataArray( [[2, 4], [np.nan, np.nan], [np.nan, np.nan], [1, 3]], dims=['x', 'y'], coords={'y': [1, 2], 'x': [-3, -2, -1, 10]})}) - self.assertDatasetIdentical(expected_x2, x2) - self.assertDatasetIdentical(expected_y2, y2) + assert_identical(expected_x2, x2) + assert_identical(expected_y2, y2) def test_variable_indexing(self): data = create_test_data() @@ -1834,14 +1821,14 @@ def test_variable_indexing(self): def test_drop_variables(self): data = create_test_data() - self.assertDatasetIdentical(data, data.drop([])) + assert_identical(data, data.drop([])) expected = Dataset(dict((k, data[k]) for k in data.variables if k != 'time')) actual = data.drop('time') - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = data.drop(['time']) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) with raises_regex(ValueError, 'cannot be found'): data.drop('not_found_here') @@ -1852,11 +1839,11 @@ def test_drop_index_labels(self): actual = data.drop(['a'], 'x') expected = data.isel(x=[1]) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = data.drop(['a', 'b'], 'x') expected = data.isel(x=slice(0, 0)) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) with pytest.raises(ValueError): # not contained in axis @@ -1870,7 +1857,7 @@ def test_copy(self): data = create_test_data() for copied in [data.copy(deep=False), copy(data)]: - self.assertDatasetIdentical(data, copied) + assert_identical(data, copied) assert data.encoding == copied.encoding # Note: IndexVariable objects with string dtype are always # copied because of xarray.core.util.safe_cast_to_index. @@ -1883,7 +1870,7 @@ def test_copy(self): assert 'foo' not in data for copied in [data.copy(deep=True), deepcopy(data)]: - self.assertDatasetIdentical(data, copied) + assert_identical(data, copied) for k, v0 in data.variables.items(): v1 = copied.variables[k] assert v0 is not v1 @@ -1938,7 +1925,7 @@ def test_rename_same_name(self): data = create_test_data() newnames = {'var1': 'var1', 'dim2': 'dim2'} renamed = data.rename(newnames) - self.assertDatasetIdentical(renamed, data) + assert_identical(renamed, data) def test_rename_inplace(self): times = pd.date_range('2000-01-01', periods=3) @@ -1946,7 +1933,7 @@ def test_rename_inplace(self): copied = data.copy() renamed = data.rename({'x': 'y'}) data.rename({'x': 'y'}, inplace=True) - self.assertDatasetIdentical(data, renamed) + assert_identical(data, renamed) assert not data.equals(copied) assert data.dims == {'y': 3, 't': 3} # check virtual variables @@ -1957,16 +1944,16 @@ def test_swap_dims(self): expected = Dataset({'z': 42}, {'x': ('y', [1, 2, 3]), 'y': list('abc')}) actual = original.swap_dims({'x': 'y'}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) assert isinstance(actual.variables['y'], IndexVariable) assert isinstance(actual.variables['x'], Variable) roundtripped = actual.swap_dims({'y': 'x'}) - self.assertDatasetIdentical(original.set_coords('y'), roundtripped) + assert_identical(original.set_coords('y'), roundtripped) actual = original.copy() actual.swap_dims({'x': 'y'}, inplace=True) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) with raises_regex(ValueError, 'cannot swap'): original.swap_dims({'y': 'x'}) @@ -2006,10 +1993,10 @@ def test_expand_dims(self): 'b': np.linspace(0, 1, 4), 'c': np.linspace(0, 1, 5)}, attrs={'key': 'entry'}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # make sure squeeze restores the original data set. roundtripped = actual.squeeze('z') - self.assertDatasetIdentical(original, roundtripped) + assert_identical(original, roundtripped) # another test with a negative axis actual = original.expand_dims(['z'], [-1]) @@ -2019,10 +2006,10 @@ def test_expand_dims(self): 'b': np.linspace(0, 1, 4), 'c': np.linspace(0, 1, 5)}, attrs={'key': 'entry'}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # make sure squeeze restores the original data set. roundtripped = actual.squeeze('z') - self.assertDatasetIdentical(original, roundtripped) + assert_identical(original, roundtripped) def test_set_index(self): expected = create_test_multiindex() @@ -2032,16 +2019,16 @@ def test_set_index(self): ds = Dataset({}, coords=coords) obj = ds.set_index(x=mindex.names) - self.assertDatasetIdentical(obj, expected) + assert_identical(obj, expected) ds.set_index(x=mindex.names, inplace=True) - self.assertDatasetIdentical(ds, expected) + assert_identical(ds, expected) # ensure set_index with no existing index and a single data var given # doesn't return multi-index ds = Dataset(data_vars={'x_var': ('x', [0, 1, 2])}) expected = Dataset(coords={'x': [0, 1, 2]}) - self.assertDataArrayIdentical(ds.set_index(x='x_var'), expected) + assert_identical(ds.set_index(x='x_var'), expected) def test_reset_index(self): ds = create_test_multiindex() @@ -2051,10 +2038,10 @@ def test_reset_index(self): expected = Dataset({}, coords=coords) obj = ds.reset_index('x') - self.assertDatasetIdentical(obj, expected) + assert_identical(obj, expected) ds.reset_index('x', inplace=True) - self.assertDatasetIdentical(ds, expected) + assert_identical(ds, expected) def test_reorder_levels(self): ds = create_test_multiindex() @@ -2063,10 +2050,10 @@ def test_reorder_levels(self): expected = Dataset({}, coords={'x': midx}) reindexed = ds.reorder_levels(x=['level_2', 'level_1']) - self.assertDatasetIdentical(reindexed, expected) + assert_identical(reindexed, expected) ds.reorder_levels(x=['level_2', 'level_1'], inplace=True) - self.assertDatasetIdentical(ds, expected) + assert_identical(ds, expected) ds = Dataset({}, coords={'x': [1, 2]}) with raises_regex(ValueError, 'has no MultiIndex'): @@ -2083,7 +2070,7 @@ def test_stack(self): 'b': ('z', [0, 1, 2, 3]), 'z': exp_index}) actual = ds.stack(z=['x', 'y']) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) exp_index = pd.MultiIndex.from_product([['a', 'b'], [0, 1]], names=['y', 'x']) @@ -2091,7 +2078,7 @@ def test_stack(self): 'b': ('z', [0, 2, 1, 3]), 'z': exp_index}) actual = ds.stack(z=['y', 'x']) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_unstack(self): index = pd.MultiIndex.from_product([[0, 1], ['a', 'b']], @@ -2101,7 +2088,7 @@ def test_unstack(self): 'x': [0, 1], 'y': ['a', 'b']}) actual = ds.unstack('z') - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) def test_unstack_errors(self): ds = Dataset({'x': [1, 2, 3]}) @@ -2127,22 +2114,22 @@ def test_update(self): var2 = Variable('dim1', np.arange(8)) actual = data.update({'var2': var2}) expected['var2'] = var2 - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = data.copy() actual_result = actual.update(data, inplace=True) assert actual_result is actual - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = data.update(data, inplace=False) expected = data assert actual is not expected - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) other = Dataset(attrs={'new': 'attr'}) actual = data.copy() actual.update(other) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_update_auto_align(self): ds = Dataset({'x': ('t', [3, 4])}, {'t': [0, 1]}) @@ -2154,14 +2141,14 @@ def test_update_auto_align(self): with raises_regex(ValueError, 'conflicting sizes'): actual.update(other) actual.update(Dataset(other)) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = ds.copy() other = Dataset({'y': ('t', [5]), 't': [100]}) actual.update(other) expected = Dataset({'x': ('t', [3, 4]), 'y': ('t', [np.nan] * 2)}, {'t': [0, 1]}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_getitem(self): data = create_test_data() @@ -2181,18 +2168,18 @@ def test_getitem(self): {'dim3': data['dim3'], 'numbers': data['numbers']}, dims='dim3', name='numbers') - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = data[dict(dim1=0)] expected = data.isel(dim1=0) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_getitem_hashable(self): data = create_test_data() data[(3, 4)] = data['var1'] + 1 expected = data['var1'] + 1 expected.name = (3, 4) - self.assertDataArrayIdentical(expected, data[(3, 4)]) + assert_identical(expected, data[(3, 4)]) with raises_regex(KeyError, "('var1', 'var2')"): data[('var1', 'var2')] @@ -2200,19 +2187,19 @@ def test_virtual_variables_default_coords(self): dataset = Dataset({'foo': ('x', range(10))}) expected = DataArray(range(10), dims='x', name='x') actual = dataset['x'] - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) assert isinstance(actual.variable, IndexVariable) actual = dataset[['x', 'foo']] expected = dataset.assign_coords(x=range(10)) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_virtual_variables_time(self): # access virtual variables data = create_test_data() expected = DataArray(1 + np.arange(20), coords=[data['time']], dims='time', name='dayofyear') - self.assertDataArrayIdentical(expected, data['time.dayofyear']) + assert_identical(expected, data['time.dayofyear']) assert_equal(data['time.month'].values, data.variables['time'].to_index().month) assert_equal(data['time.season'].values, 'DJF') @@ -2234,14 +2221,14 @@ def test_virtual_variable_same_name(self): data = Dataset({'time': times}) actual = data['time.time'] expected = DataArray(times.time, [('time', times)], name='time') - self.assertDataArrayIdentical(actual, expected) + assert_identical(actual, expected) def test_virtual_variable_multiindex(self): # access multi-index levels as virtual variables data = create_test_multiindex() expected = DataArray(['a', 'a', 'b', 'b'], name='level_1', coords=[data['x'].to_index()], dims='x') - self.assertDataArrayIdentical(expected, data['level_1']) + assert_identical(expected, data['level_1']) # combine multi-index level and datetime dr_index = pd.date_range('1/1/2011', periods=4, freq='H') @@ -2250,31 +2237,21 @@ def test_virtual_variable_multiindex(self): data = Dataset({}, {'x': mindex}) expected = DataArray(mindex.get_level_values('level_date').hour, name='hour', coords=[mindex], dims='x') - self.assertDataArrayIdentical(expected, data['level_date.hour']) + assert_identical(expected, data['level_date.hour']) # attribute style access - self.assertDataArrayIdentical(data.level_str, data['level_str']) + assert_identical(data.level_str, data['level_str']) def test_time_season(self): ds = Dataset({'t': pd.date_range('2000-01-01', periods=12, freq='M')}) -<<<<<<< HEAD seas = ['DJF'] * 2 + ['MAM'] * 3 + ['JJA'] * 3 + ['SON'] * 3 + ['DJF'] self.assertArrayEqual(seas, ds['t.season']) -======= - expected = ['DJF'] * 2 + ['MAM'] * 3 + ['JJA'] * 3 + ['SON'] * 3 + ['DJF'] - assert_equal(expected, ds['t.season']) ->>>>>>> assert_equal def test_slice_virtual_variable(self): data = create_test_data() assert_equal(data['time.dayofyear'][:10].variable, Variable(['time'], 1 + np.arange(10))) -<<<<<<< HEAD - self.assertVariableEqual(data['time.dayofyear'][0].variable, - Variable([], 1)) -======= assert_equal(data['time.dayofyear'][0].variable, Variable([], 1)) ->>>>>>> assert_equal def test_setitem(self): # assign a variable @@ -2283,12 +2260,12 @@ def test_setitem(self): data1['A'] = var data2 = data1.copy() data2['A'] = var - self.assertDatasetIdentical(data1, data2) + assert_identical(data1, data2) # assign a dataset array dv = 2 * data2['A'] data1['B'] = dv.variable data2['B'] = dv - self.assertDatasetIdentical(data1, data2) + assert_identical(data1, data2) # can't assign an ND array without dimensions with raises_regex(ValueError, 'without explicit dimension names'): @@ -2296,11 +2273,11 @@ def test_setitem(self): # but can assign a 1D array data1['C'] = var.values data2['C'] = ('C', var.values) - self.assertDatasetIdentical(data1, data2) + assert_identical(data1, data2) # can assign a scalar data1['scalar'] = 0 data2['scalar'] = ([], 0) - self.assertDatasetIdentical(data1, data2) + assert_identical(data1, data2) # can't use the same dimension name as a scalar var with raises_regex(ValueError, 'already exists as a scalar'): data1['newvar'] = ('scalar', [3, 4, 5]) @@ -2328,23 +2305,23 @@ def test_setitem_auto_align(self): ds['x'] = ('y', range(3)) ds['y'] = 1 + np.arange(3) expected = Dataset({'x': ('y', range(3)), 'y': 1 + np.arange(3)}) - self.assertDatasetIdentical(ds, expected) + assert_identical(ds, expected) ds['y'] = DataArray(range(3), dims='y') expected = Dataset({'x': ('y', range(3))}, {'y': range(3)}) - self.assertDatasetIdentical(ds, expected) + assert_identical(ds, expected) ds['x'] = DataArray([1, 2], coords=[('y', [0, 1])]) expected = Dataset({'x': ('y', [1, 2, np.nan])}, {'y': range(3)}) - self.assertDatasetIdentical(ds, expected) + assert_identical(ds, expected) ds['x'] = 42 expected = Dataset({'x': 42, 'y': range(3)}) - self.assertDatasetIdentical(ds, expected) + assert_identical(ds, expected) ds['x'] = DataArray([4, 5, 6, 7], coords=[('y', [0, 1, 2, 3])]) expected = Dataset({'x': ('y', [4, 5, 6])}, {'y': range(3)}) - self.assertDatasetIdentical(ds, expected) + assert_identical(ds, expected) def test_setitem_align_new_indexes(self): ds = Dataset({'foo': ('x', [1, 2, 3])}, {'x': [0, 1, 2]}) @@ -2352,47 +2329,47 @@ def test_setitem_align_new_indexes(self): expected = Dataset({'foo': ('x', [1, 2, 3]), 'bar': ('x', [np.nan, 2, 3])}, {'x': [0, 1, 2]}) - self.assertDatasetIdentical(ds, expected) + assert_identical(ds, expected) def test_assign(self): ds = Dataset() actual = ds.assign(x=[0, 1, 2], y=2) expected = Dataset({'x': [0, 1, 2], 'y': 2}) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) assert list(actual.variables) == ['x', 'y'] - self.assertDatasetIdentical(ds, Dataset()) + assert_identical(ds, Dataset()) actual = actual.assign(y=lambda ds: ds.x ** 2) expected = Dataset({'y': ('x', [0, 1, 4]), 'x': [0, 1, 2]}) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) actual = actual.assign_coords(z=2) expected = Dataset({'y': ('x', [0, 1, 4])}, {'z': 2, 'x': [0, 1, 2]}) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) ds = Dataset({'a': ('x', range(3))}, {'b': ('x', ['A'] * 2 + ['B'])}) actual = ds.groupby('b').assign(c=lambda ds: 2 * ds.a) expected = ds.merge({'c': ('x', [0, 2, 4])}) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) actual = ds.groupby('b').assign(c=lambda ds: ds.a.sum()) expected = ds.merge({'c': ('x', [1, 1, 2])}) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) actual = ds.groupby('b').assign_coords(c=lambda ds: ds.a.sum()) expected = expected.set_coords('c') - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) def test_assign_attrs(self): expected = Dataset(attrs=dict(a=1, b=2)) new = Dataset() actual = new.assign_attrs(a=1, b=2) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) assert new.attrs == {} expected.attrs['c'] = 3 new_actual = actual.assign_attrs({'c': 3}) - self.assertDatasetIdentical(new_actual, expected) + assert_identical(new_actual, expected) assert actual.attrs == dict(a=1, b=2) def test_assign_multiindex_level(self): @@ -2409,15 +2386,15 @@ def test_setitem_original_non_unique_index(self): actual = original.copy() actual['x'] = list(range(5)) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) actual = original.copy() actual['x'] = ('x', list(range(5))) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) actual = original.copy() actual.coords['x'] = list(range(5)) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) def test_setitem_both_non_unique_index(self): # regression test for GH956 @@ -2428,7 +2405,7 @@ def test_setitem_both_non_unique_index(self): expected = Dataset({'first': array, 'second': array}) actual = array.rename('first').to_dataset() actual['second'] = array - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_setitem_multiindex_level(self): data = create_test_multiindex() @@ -2454,7 +2431,7 @@ def get_args(v): expected = Dataset(dict((k, v.squeeze(*get_args(v))) for k, v in iteritems(data.variables))) expected.set_coords(data.coords, inplace=True) - self.assertDatasetIdentical(expected, data.squeeze(*args)) + assert_identical(expected, data.squeeze(*args)) # invalid squeeze with raises_regex(ValueError, 'cannot select a dimension'): data.squeeze('y') @@ -2463,24 +2440,24 @@ def test_squeeze_drop(self): data = Dataset({'foo': ('x', [1])}, {'x': [0]}) expected = Dataset({'foo': 1}) selected = data.squeeze(drop=True) - self.assertDatasetIdentical(expected, selected) + assert_identical(expected, selected) expected = Dataset({'foo': 1}, {'x': 0}) selected = data.squeeze(drop=False) - self.assertDatasetIdentical(expected, selected) + assert_identical(expected, selected) data = Dataset({'foo': (('x', 'y'), [[1]])}, {'x': [0], 'y': [0]}) expected = Dataset({'foo': 1}) selected = data.squeeze(drop=True) - self.assertDatasetIdentical(expected, selected) + assert_identical(expected, selected) expected = Dataset({'foo': ('x', [1])}, {'x': [0]}) selected = data.squeeze(dim='y', drop=True) - self.assertDatasetIdentical(expected, selected) + assert_identical(expected, selected) data = Dataset({'foo': (('x',), [])}, {'x': []}) selected = data.squeeze(drop=True) - self.assertDatasetIdentical(data, selected) + assert_identical(data, selected) def test_groupby(self): data = Dataset({'z': (['x', 'y'], np.random.randn(3, 5))}, @@ -2510,11 +2487,11 @@ def test_groupby_returns_new_type(self): actual = data.groupby('x').apply(lambda ds: ds['z']) expected = data['z'] - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = data['z'].groupby('x').apply(lambda x: x.to_dataset()) expected = data - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_groupby_iter(self): data = create_test_data() @@ -2566,18 +2543,18 @@ def reorder_dims(x): expected = reorder_dims(ds + ds.coords['dim1']) actual = grouped + ds.coords['dim1'] - self.assertDatasetIdentical(expected, reorder_dims(actual)) + assert_identical(expected, reorder_dims(actual)) actual = ds.coords['dim1'] + grouped - self.assertDatasetIdentical(expected, reorder_dims(actual)) + assert_identical(expected, reorder_dims(actual)) ds2 = 2 * ds expected = reorder_dims(ds + ds2) actual = grouped + ds2 - self.assertDatasetIdentical(expected, reorder_dims(actual)) + assert_identical(expected, reorder_dims(actual)) actual = ds2 + grouped - self.assertDatasetIdentical(expected, reorder_dims(actual)) + assert_identical(expected, reorder_dims(actual)) grouped = ds.groupby('numbers') zeros = DataArray([0, 0, 0, 0], [('numbers', range(4))]) @@ -2612,7 +2589,7 @@ def test_groupby_math_virtual(self): actual = grouped - grouped.mean() expected = Dataset({'x': ('t', [0, 0, 0])}, ds[['t', 't.day']]) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) def test_groupby_nan(self): # nan should be excluded from groupby @@ -2620,7 +2597,7 @@ def test_groupby_nan(self): {'bar': ('x', [1, 1, 2, np.nan])}) actual = ds.groupby('bar').mean() expected = Dataset({'foo': ('bar', [1.5, 3]), 'bar': [1, 2]}) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) def test_groupby_order(self): # groupby should preserve variables order @@ -2631,12 +2608,7 @@ def test_groupby_order(self): data_vars_ref = list(ds.data_vars.keys()) ds = ds.groupby('t').mean() data_vars = list(ds.data_vars.keys()) -<<<<<<< HEAD - self.assertEqual(data_vars, data_vars_ref) - -======= assert data_vars == data_vars_ref ->>>>>>> self_assert from unittest2pytest # coords are now at the end of the list, so the test below fails # all_vars = list(ds.variables.keys()) # all_vars_ref = list(ds.variables.keys()) @@ -2650,7 +2622,7 @@ def test_resample_and_first(self): actual = ds.resample(time='1D').first(keep_attrs=True) expected = ds.isel(time=[0, 4, 8]) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # upsampling expected_time = pd.date_range('2000-01-01', freq='3H', periods=19) @@ -2749,7 +2721,7 @@ def test_resample_old_vs_new_api(self): new_api = getattr(resampler, method)(keep_attrs=False) with pytest.warns(DeprecationWarning): old_api = ds.resample('1D', dim='time', how=method) - self.assertDatasetIdentical(new_api, old_api) + assert_identical(new_api, old_api) def test_to_array(self): ds = Dataset(OrderedDict([('a', 1), ('b', ('x', [1, 2, 3]))]), @@ -2759,11 +2731,11 @@ def test_to_array(self): dims = ('variable', 'x') expected = DataArray(data, coords, dims, attrs=ds.attrs) actual = ds.to_array() - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) actual = ds.to_array('abc', name='foo') expected = expected.rename({'variable': 'abc'}).rename('foo') - self.assertDataArrayIdentical(expected, actual) + assert_identical(expected, actual) def test_to_and_from_dataframe(self): x = np.random.randn(10) @@ -2783,7 +2755,7 @@ def test_to_and_from_dataframe(self): assert expected.equals(actual), (expected, actual) # check roundtrip - self.assertDatasetIdentical(ds, Dataset.from_dataframe(actual)) + assert_identical(ds, Dataset.from_dataframe(actual)) # test a case with a MultiIndex w = np.random.randn(2, 3) @@ -2797,26 +2769,26 @@ def test_to_and_from_dataframe(self): assert expected.equals(actual) # check roundtrip - self.assertDatasetIdentical(ds.assign_coords(x=[0, 1]), + assert_identical(ds.assign_coords(x=[0, 1]), Dataset.from_dataframe(actual)) # check pathological cases df = pd.DataFrame([1]) actual = Dataset.from_dataframe(df) expected = Dataset({0: ('index', [1])}, {'index': [0]}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) df = pd.DataFrame() actual = Dataset.from_dataframe(df) expected = Dataset(coords={'index': []}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # GH697 df = pd.DataFrame({'A': []}) actual = Dataset.from_dataframe(df) expected = Dataset({'A': DataArray([], dims=('index',))}, {'index': []}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # regression test for GH278 # use int64 to ensure consistent results for the pandas .equals method @@ -2895,13 +2867,13 @@ def test_to_and_from_dict(self): assert expected == actual # check roundtrip - self.assertDatasetIdentical(ds, Dataset.from_dict(actual)) + assert_identical(ds, Dataset.from_dict(actual)) # verify coords are included roundtrip expected = ds.set_coords('b') actual = Dataset.from_dict(expected.to_dict()) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # test some incomplete dicts: # this one has no attrs field, the dims are strings, and x, y are @@ -2911,13 +2883,13 @@ def test_to_and_from_dict(self): 'dims': 't', 'data_vars': {'a': {'dims': 't', 'data': x}, 'b': {'dims': 't', 'data': y}}} - self.assertDatasetIdentical(ds, Dataset.from_dict(d)) + assert_identical(ds, Dataset.from_dict(d)) # this is kind of a flattened version with no coords, or data_vars d = {'a': {'dims': 't', 'data': x}, 't': {'data': t, 'dims': 't'}, 'b': {'dims': 't', 'data': y}} - self.assertDatasetIdentical(ds, Dataset.from_dict(d)) + assert_identical(ds, Dataset.from_dict(d)) # this one is missing some necessary information d = {'a': {'data': x}, @@ -2937,7 +2909,7 @@ def test_to_and_from_dict_with_time_dim(self): ('t', ('t', t)), ('lat', ('lat', lat))])) roundtripped = Dataset.from_dict(ds.to_dict()) - self.assertDatasetIdentical(ds, roundtripped) + assert_identical(ds, roundtripped) def test_to_and_from_dict_with_nan_nat(self): x = np.random.randn(10, 3) @@ -2952,7 +2924,7 @@ def test_to_and_from_dict_with_nan_nat(self): ('t', ('t', t)), ('lat', ('lat', lat))])) roundtripped = Dataset.from_dict(ds.to_dict()) - self.assertDatasetIdentical(ds, roundtripped) + assert_identical(ds, roundtripped) def test_to_dict_with_numpy_attrs(self): # this doesn't need to roundtrip @@ -2976,7 +2948,7 @@ def test_to_dict_with_numpy_attrs(self): def test_pickle(self): data = create_test_data() roundtripped = pickle.loads(pickle.dumps(data)) - self.assertDatasetIdentical(data, roundtripped) + assert_identical(data, roundtripped) # regression test for #167: assert data.dims == roundtripped.dims @@ -3004,49 +2976,49 @@ def test_dropna(self): expected = ds.isel(a=slice(1, None, 2)) actual = ds.dropna('a') - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) expected = ds.isel(b=slice(1, 3)) actual = ds.dropna('b') - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) actual = ds.dropna('b', subset=['foo', 'bar']) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) expected = ds.isel(b=slice(1, None)) actual = ds.dropna('b', subset=['foo']) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) expected = ds.isel(b=slice(3)) actual = ds.dropna('b', subset=['bar']) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) actual = ds.dropna('a', subset=[]) - self.assertDatasetIdentical(actual, ds) + assert_identical(actual, ds) actual = ds.dropna('a', subset=['bar']) - self.assertDatasetIdentical(actual, ds) + assert_identical(actual, ds) actual = ds.dropna('a', how='all') - self.assertDatasetIdentical(actual, ds) + assert_identical(actual, ds) actual = ds.dropna('b', how='all', subset=['bar']) expected = ds.isel(b=[0, 1, 2]) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) actual = ds.dropna('b', thresh=1, subset=['bar']) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) actual = ds.dropna('b', thresh=2) - self.assertDatasetIdentical(actual, ds) + assert_identical(actual, ds) actual = ds.dropna('b', thresh=4) expected = ds.isel(b=[1, 2, 3]) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) actual = ds.dropna('a', thresh=3) expected = ds.isel(a=[1, 3]) - self.assertDatasetIdentical(actual, ds) + assert_identical(actual, ds) with raises_regex(ValueError, 'a single dataset dimension'): ds.dropna('foo') @@ -3062,39 +3034,39 @@ def test_fillna(self): # fill with -1 actual = ds.fillna(-1) expected = Dataset({'a': ('x', [-1, 1, -1, 3])}, {'x': [0, 1, 2, 3]}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = ds.fillna({'a': -1}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) other = Dataset({'a': -1}) actual = ds.fillna(other) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = ds.fillna({'a': other.a}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # fill with range(4) b = DataArray(range(4), coords=[('x', range(4))]) actual = ds.fillna(b) expected = b.rename('a').to_dataset() - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = ds.fillna(expected) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = ds.fillna(range(4)) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = ds.fillna(b[:3]) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # okay to only include some data variables ds['b'] = np.nan actual = ds.fillna({'a': -1}) expected = Dataset({'a': ('x', [-1, 1, -1, 3]), 'b': np.nan}, {'x': [0, 1, 2, 3]}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # but new data variables is not okay with raises_regex(ValueError, 'must be contained'): @@ -3102,21 +3074,21 @@ def test_fillna(self): # empty argument should be OK result = ds.fillna({}) - self.assertDatasetIdentical(ds, result) + assert_identical(ds, result) result = ds.fillna(Dataset(coords={'c': 42})) expected = ds.assign_coords(c=42) - self.assertDatasetIdentical(expected, result) + assert_identical(expected, result) # groupby expected = Dataset({'a': ('x', range(4))}, {'x': [0, 1, 2, 3]}) for target in [ds, expected]: target.coords['b'] = ('x', [0, 0, 1, 1]) actual = ds.groupby('b').fillna(DataArray([0, 2], dims='b')) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = ds.groupby('b').fillna(Dataset({'a': ('b', [0, 2])})) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # attrs with groupby ds.attrs['attr'] = 'ds' @@ -3141,27 +3113,27 @@ def test_where(self): ds = Dataset({'a': ('x', range(5))}) expected = Dataset({'a': ('x', [np.nan, np.nan, 2, 3, 4])}) actual = ds.where(ds > 1) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = ds.where(ds.a > 1) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = ds.where(ds.a.values > 1) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = ds.where(True) - self.assertDatasetIdentical(ds, actual) + assert_identical(ds, actual) expected = ds.copy(deep=True) expected['a'].values = [np.nan] * 5 actual = ds.where(False) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # 2d ds = Dataset({'a': (('x', 'y'), [[0, 1], [2, 3]])}) expected = Dataset({'a': (('x', 'y'), [[np.nan, 1], [2, 3]])}) actual = ds.where(ds > 0) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # groupby ds = Dataset({'a': ('x', range(5))}, {'c': ('x', [0, 0, 1, 1, 1])}) @@ -3169,7 +3141,7 @@ def test_where(self): expected = ds.copy(deep=True) expected['a'].values = [0, 1] + [np.nan] * 3 actual = ds.groupby('c').where(cond) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # attrs with groupby ds.attrs['attr'] = 'ds' @@ -3215,17 +3187,17 @@ def test_where_drop(self): array = DataArray(range(5), coords=[range(5)], dims=['x']) expected = DataArray(range(5)[2:], coords=[range(5)[2:]], dims=['x']) actual = array.where(array > 1, drop=True) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # dataset case ds = Dataset({'a': array}) expected = Dataset({'a': expected}) actual = ds.where(ds > 1, drop=True) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = ds.where(ds.a > 1, drop=True) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) with raises_regex(TypeError, 'must be a'): ds.where(np.arange(5) > 1, drop=True) @@ -3236,20 +3208,20 @@ def test_where_drop(self): expected = DataArray(np.array([7, 8, 3]), coords=[np.array([1, 5, 9])], dims=['x']) actual = array.where(array > 2, drop=True) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # 1d multiple variables ds = Dataset({'a': (('x'), [0, 1, 2, 3]), 'b': (('x'), [4, 5, 6, 7])}) expected = Dataset({'a': (('x'), [np.nan, 1, 2, 3]), 'b': (('x'), [4, 5, 6, np.nan])}) actual = ds.where((ds > 0) & (ds < 7), drop=True) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # 2d ds = Dataset({'a': (('x', 'y'), [[0, 1], [2, 3]])}) expected = Dataset({'a': (('x', 'y'), [[np.nan, 1], [2, 3]])}) actual = ds.where(ds > 0, drop=True) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # 2d with odd coordinates ds = Dataset({'a': (('x', 'y'), [[0, 1], [2, 3]])}, coords={ @@ -3259,7 +3231,7 @@ def test_where_drop(self): coords={'x': [3], 'y': [2], 'z': (['x', 'y'], [[np.pi * 3]])}) actual = ds.where(ds > 2, drop=True) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # 2d multiple variables ds = Dataset({'a': (('x', 'y'), [[0, 1], [2, 3]]), @@ -3267,7 +3239,7 @@ def test_where_drop(self): expected = Dataset({'a': (('x', 'y'), [[np.nan, 1], [2, 3]]), 'b': (('x', 'y'), [[4, 5], [6, 7]])}) actual = ds.where(ds > 0, drop=True) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_where_drop_empty(self): # regression test for GH1341 @@ -3276,13 +3248,13 @@ def test_where_drop_empty(self): mask = DataArray(np.zeros((100,), dtype='bool'), dims='nCells') actual = array.where(mask, drop=True) expected = DataArray(np.zeros((0, 10)), dims=['nCells', 'nVertLevels']) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_where_drop_no_indexes(self): ds = Dataset({'foo': ('x', [0.0, 1.0])}) expected = Dataset({'foo': ('x', [1.0])}) actual = ds.where(ds == 1, drop=True) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_reduce(self): data = create_test_data() @@ -3314,7 +3286,7 @@ def test_reduce(self): actual = data.mean('x', skipna=True) expected = xr.Dataset({'a': data['a'].mean('x'), 'b': data['b'].mean('x', skipna=True)}) - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) def test_reduce_bad_dim(self): data = create_test_data() @@ -3362,44 +3334,44 @@ def test_reduce_strings(self): expected = Dataset({'x': 'a'}) ds = Dataset({'x': ('y', ['a', 'b'])}) actual = ds.min() - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) expected = Dataset({'x': 'b'}) actual = ds.max() - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) expected = Dataset({'x': 0}) actual = ds.argmin() - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) expected = Dataset({'x': 1}) actual = ds.argmax() - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) expected = Dataset({'x': b'a'}) ds = Dataset({'x': ('y', np.array(['a', 'b'], 'S1'))}) actual = ds.min() - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) expected = Dataset({'x': u'a'}) ds = Dataset({'x': ('y', np.array(['a', 'b'], 'U1'))}) actual = ds.min() - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_reduce_dtypes(self): # regression test for GH342 expected = Dataset({'x': 1}) actual = Dataset({'x': True}).sum() - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # regression test for GH505 expected = Dataset({'x': 3}) actual = Dataset({'x': ('y', np.array([1, 2], 'uint16'))}).sum() - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) expected = Dataset({'x': 1 + 1j}) actual = Dataset({'x': ('y', [1, 1j])}).sum() - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_reduce_keep_attrs(self): data = create_test_data() @@ -3425,16 +3397,16 @@ def test_reduce_argmin(self): ds = Dataset({'a': ('x', [0, 1])}) expected = Dataset({'a': ([], 0)}) actual = ds.argmin() - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = ds.argmin('x') - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_reduce_scalars(self): ds = Dataset({'x': ('a', [2, 2]), 'y': 2, 'z': ('b', [2])}) expected = Dataset({'x': 0, 'y': 0, 'z': 0}) actual = ds.var() - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_reduce_only_one_axis(self): @@ -3446,7 +3418,7 @@ def mean_only_one_axis(x, axis): ds = Dataset({'a': (['x', 'y'], [[0, 1, 2, 3, 4]])}) expected = Dataset({'a': ('x', [2])}) actual = ds.reduce(mean_only_one_axis, 'y') - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) with raises_regex(TypeError, 'non-integer axis'): ds.reduce(mean_only_one_axis) @@ -3464,7 +3436,7 @@ def test_quantile(self): assert 'quantile' in ds_quantile for var, dar in ds.data_vars.items(): assert var in ds_quantile - self.assertDataArrayIdentical( + assert_identical( ds_quantile[var], dar.quantile(q, dim=dim)) dim = ['dim1', 'dim2'] ds_quantile = ds.quantile(q, dim=dim) @@ -3492,19 +3464,19 @@ def test_count(self): ds = Dataset({'x': ('a', [np.nan, 1]), 'y': 0, 'z': np.nan}) expected = Dataset({'x': 1, 'y': 1, 'z': 0}) actual = ds.count() - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_apply(self): data = create_test_data() data.attrs['foo'] = 'bar' - self.assertDatasetIdentical(data.apply(np.mean), data.mean()) + assert_identical(data.apply(np.mean), data.mean()) expected = data.mean(keep_attrs=True) actual = data.apply(lambda x: x.mean(keep_attrs=True), keep_attrs=True) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) - self.assertDatasetIdentical(data.apply(lambda x: x, keep_attrs=True), + assert_identical(data.apply(lambda x: x, keep_attrs=True), data.drop('time')) def scale(x, multiple=1): @@ -3512,16 +3484,11 @@ def scale(x, multiple=1): actual = data.apply(scale, multiple=2) assert_equal(actual['var1'], 2 * data['var1']) - self.assertDataArrayIdentical(actual['numbers'], data['numbers']) + assert_identical(actual['numbers'], data['numbers']) actual = data.apply(np.asarray) -<<<<<<< HEAD expected = data.drop('time') # time is not used on a data var - self.assertDatasetEqual(expected, actual) -======= - expected = data.drop('time') # time is not used on a data var assert_equal(expected, actual) ->>>>>>> assert_equal def make_example_math_dataset(self): variables = OrderedDict( @@ -3536,28 +3503,28 @@ def make_example_math_dataset(self): def test_dataset_number_math(self): ds = self.make_example_math_dataset() - self.assertDatasetIdentical(ds, +ds) - self.assertDatasetIdentical(ds, ds + 0) - self.assertDatasetIdentical(ds, 0 + ds) - self.assertDatasetIdentical(ds, ds + np.array(0)) - self.assertDatasetIdentical(ds, np.array(0) + ds) + assert_identical(ds, +ds) + assert_identical(ds, ds + 0) + assert_identical(ds, 0 + ds) + assert_identical(ds, ds + np.array(0)) + assert_identical(ds, np.array(0) + ds) actual = ds.copy(deep=True) actual += 0 - self.assertDatasetIdentical(ds, actual) + assert_identical(ds, actual) def test_unary_ops(self): ds = self.make_example_math_dataset() - self.assertDatasetIdentical(ds.apply(abs), abs(ds)) - self.assertDatasetIdentical(ds.apply(lambda x: x + 4), ds + 4) + assert_identical(ds.apply(abs), abs(ds)) + assert_identical(ds.apply(lambda x: x + 4), ds + 4) for func in [lambda x: x.isnull(), lambda x: x.round(), lambda x: x.astype(int)]: - self.assertDatasetIdentical(ds.apply(func), func(ds)) + assert_identical(ds.apply(func), func(ds)) - self.assertDatasetIdentical(ds.isnull(), ~ds.notnull()) + assert_identical(ds.isnull(), ~ds.notnull()) # don't actually patch these methods in with pytest.raises(AttributeError): @@ -3569,55 +3536,55 @@ def test_dataset_array_math(self): ds = self.make_example_math_dataset() expected = ds.apply(lambda x: x - ds['foo']) - self.assertDatasetIdentical(expected, ds - ds['foo']) - self.assertDatasetIdentical(expected, -ds['foo'] + ds) - self.assertDatasetIdentical(expected, ds - ds['foo'].variable) - self.assertDatasetIdentical(expected, -ds['foo'].variable + ds) + assert_identical(expected, ds - ds['foo']) + assert_identical(expected, -ds['foo'] + ds) + assert_identical(expected, ds - ds['foo'].variable) + assert_identical(expected, -ds['foo'].variable + ds) actual = ds.copy(deep=True) actual -= ds['foo'] - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) expected = ds.apply(lambda x: x + ds['bar']) - self.assertDatasetIdentical(expected, ds + ds['bar']) + assert_identical(expected, ds + ds['bar']) actual = ds.copy(deep=True) actual += ds['bar'] - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) expected = Dataset({'bar': ds['bar'] + np.arange(3)}) - self.assertDatasetIdentical(expected, ds[['bar']] + np.arange(3)) - self.assertDatasetIdentical(expected, np.arange(3) + ds[['bar']]) + assert_identical(expected, ds[['bar']] + np.arange(3)) + assert_identical(expected, np.arange(3) + ds[['bar']]) def test_dataset_dataset_math(self): ds = self.make_example_math_dataset() - self.assertDatasetIdentical(ds, ds + 0 * ds) - self.assertDatasetIdentical(ds, ds + {'foo': 0, 'bar': 0}) + assert_identical(ds, ds + 0 * ds) + assert_identical(ds, ds + {'foo': 0, 'bar': 0}) expected = ds.apply(lambda x: 2 * x) - self.assertDatasetIdentical(expected, 2 * ds) - self.assertDatasetIdentical(expected, ds + ds) - self.assertDatasetIdentical(expected, ds + ds.data_vars) - self.assertDatasetIdentical(expected, ds + dict(ds.data_vars)) + assert_identical(expected, 2 * ds) + assert_identical(expected, ds + ds) + assert_identical(expected, ds + ds.data_vars) + assert_identical(expected, ds + dict(ds.data_vars)) actual = ds.copy(deep=True) expected_id = id(actual) actual += ds - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) assert expected_id == id(actual) - self.assertDatasetIdentical(ds == ds, ds.notnull()) + assert_identical(ds == ds, ds.notnull()) subsampled = ds.isel(y=slice(2)) expected = 2 * subsampled - self.assertDatasetIdentical(expected, subsampled + ds) - self.assertDatasetIdentical(expected, ds + subsampled) + assert_identical(expected, subsampled + ds) + assert_identical(expected, ds + subsampled) def test_dataset_math_auto_align(self): ds = self.make_example_math_dataset() subset = ds.isel(y=[1, 3]) expected = 2 * subset actual = ds + subset - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = ds.isel(y=slice(1)) + ds.isel(y=slice(1, None)) expected = 2 * ds.drop(ds.y, dim='y') @@ -3625,22 +3592,22 @@ def test_dataset_math_auto_align(self): actual = ds + ds[['bar']] expected = (2 * ds[['bar']]).merge(ds.coords) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) - self.assertDatasetIdentical(ds + Dataset(), ds.coords.to_dataset()) - self.assertDatasetIdentical(Dataset() + Dataset(), Dataset()) + assert_identical(ds + Dataset(), ds.coords.to_dataset()) + assert_identical(Dataset() + Dataset(), Dataset()) ds2 = Dataset(coords={'bar': 42}) - self.assertDatasetIdentical(ds + ds2, ds.coords.merge(ds2)) + assert_identical(ds + ds2, ds.coords.merge(ds2)) # maybe unary arithmetic with empty datasets should raise instead? - self.assertDatasetIdentical(Dataset() + 1, Dataset()) + assert_identical(Dataset() + 1, Dataset()) actual = ds.copy(deep=True) other = ds.isel(y=slice(2)) actual += other expected = ds + other.reindex_like(ds) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_dataset_math_errors(self): ds = self.make_example_math_dataset() @@ -3659,7 +3626,7 @@ def test_dataset_math_errors(self): actual = ds.copy(deep=True) with pytest.raises(TypeError): actual += other - self.assertDatasetIdentical(actual, ds) + assert_identical(actual, ds) def test_dataset_transpose(self): ds = Dataset({'a': (('x', 'y'), np.random.randn(3, 4)), @@ -3667,15 +3634,15 @@ def test_dataset_transpose(self): actual = ds.transpose() expected = ds.apply(lambda x: x.transpose()) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) with pytest.warns(FutureWarning): actual = ds.T - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) actual = ds.transpose('x', 'y') expected = ds.apply(lambda x: x.transpose('x', 'y')) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) ds = create_test_data() actual = ds.transpose() @@ -3766,7 +3733,7 @@ def test_shift(self): ds = Dataset({'foo': ('x', [1, 2, 3])}, coords, attrs) actual = ds.shift(x=1) expected = Dataset({'foo': ('x', [np.nan, 1, 2])}, coords, attrs) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) with raises_regex(ValueError, 'dimensions'): ds.shift(foo=123) @@ -3779,7 +3746,7 @@ def test_roll(self): ex_coords = {'bar': ('x', list('cab')), 'x': [2, -4, 3]} expected = Dataset({'foo': ('x', [3, 1, 2])}, ex_coords, attrs) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) with raises_regex(ValueError, 'dimensions'): ds.roll(foo=123) @@ -3789,10 +3756,10 @@ def test_real_and_imag(self): ds = Dataset({'x': ((), 1 + 2j, attrs)}, attrs=attrs) expected_re = Dataset({'x': ((), 1, attrs)}, attrs=attrs) - self.assertDatasetIdentical(ds.real, expected_re) + assert_identical(ds.real, expected_re) expected_im = Dataset({'x': ((), 2, attrs)}, attrs=attrs) - self.assertDatasetIdentical(ds.imag, expected_im) + assert_identical(ds.imag, expected_im) def test_setattr_raises(self): ds = Dataset({}, coords={'scalar': 1}, attrs={'foo': 'bar'}) @@ -3818,42 +3785,23 @@ def test_filter_by_attrs(self): assert not bool(new_ds.data_vars) # Test return one DataArray. -<<<<<<< HEAD - new_ds = ds.filter_by_attrs( - standard_name='convective_precipitation_flux') - self.assertEqual(new_ds['precipitation'].standard_name, - 'convective_precipitation_flux') -======= new_ds = ds.filter_by_attrs(standard_name='convective_precipitation_flux') - assert new_ds['precipitation'].standard_name == 'convective_precipitation_flux' -<<<<<<< HEAD ->>>>>>> self_assert from unittest2pytest - self.assertDatasetEqual(new_ds['precipitation'], ds['precipitation']) -======= + assert (new_ds['precipitation'].standard_name + == 'convective_precipitation_flux') + assert_equal(new_ds['precipitation'], ds['precipitation']) ->>>>>>> assert_equal # Test return more than one DataArray. new_ds = ds.filter_by_attrs(standard_name='air_potential_temperature') assert len(new_ds.data_vars) == 2 for var in new_ds.data_vars: -<<<<<<< HEAD - self.assertEqual(new_ds[var].standard_name, - 'air_potential_temperature') -======= assert new_ds[var].standard_name == 'air_potential_temperature' ->>>>>>> self_assert from unittest2pytest # Test callable. new_ds = ds.filter_by_attrs(height=lambda v: v is not None) assert len(new_ds.data_vars) == 2 for var in new_ds.data_vars: -<<<<<<< HEAD - self.assertEqual(new_ds[var].standard_name, - 'air_potential_temperature') -======= assert new_ds[var].standard_name == 'air_potential_temperature' ->>>>>>> self_assert from unittest2pytest new_ds = ds.filter_by_attrs(height='10 m') assert len(new_ds.data_vars) == 1 @@ -3905,7 +3853,7 @@ def test_full_like(self): expect['d2'].values = [2.0, 2.0, 2.0] assert expect['d1'].dtype == int assert expect['d2'].dtype == float - self.assertDatasetIdentical(expect, actual) + assert_identical(expect, actual) # override dtype actual = full_like(ds, fill_value=True, dtype=bool) @@ -3914,7 +3862,7 @@ def test_full_like(self): expect['d2'].values = [True, True, True] assert expect['d1'].dtype == bool assert expect['d2'].dtype == bool - self.assertDatasetIdentical(expect, actual) + assert_identical(expect, actual) def test_combine_first(self): dsx0 = DataArray([0, 0], [('x', ['a', 'b'])]).to_dataset(name='dsx0') diff --git a/xarray/tests/test_tutorial.py b/xarray/tests/test_tutorial.py index 05a72153025..a7807325ec5 100644 --- a/xarray/tests/test_tutorial.py +++ b/xarray/tests/test_tutorial.py @@ -25,4 +25,4 @@ def setUp(self): def test_download_from_github(self): ds = tutorial.load_dataset(self.testfile) tiny = DataArray(range(5), name='tiny').to_dataset() - self.assertDatasetIdentical(ds, tiny) + assert_identical(ds, tiny) diff --git a/xarray/tests/test_ufuncs.py b/xarray/tests/test_ufuncs.py index 0fc704a03f5..0c87443432f 100644 --- a/xarray/tests/test_ufuncs.py +++ b/xarray/tests/test_ufuncs.py @@ -26,7 +26,7 @@ def test_unary(self): xr.DataArray([0, 0], dims='x'), xr.Dataset({'y': ('x', [0, 0])})] for a in args: - self.assertIdentical(a + 1, xu.cos(a)) + assert_identical(a + 1, xu.cos(a)) def test_binary(self): args = [0, @@ -36,10 +36,10 @@ def test_binary(self): xr.Dataset({'y': ('x', [0, 0])})] for n, t1 in enumerate(args): for t2 in args[n:]: - self.assertIdentical(t2 + 1, xu.maximum(t1, t2 + 1)) - self.assertIdentical(t2 + 1, xu.maximum(t2, t1 + 1)) - self.assertIdentical(t2 + 1, xu.maximum(t1 + 1, t2)) - self.assertIdentical(t2 + 1, xu.maximum(t2 + 1, t1)) + assert_identical(t2 + 1, xu.maximum(t1, t2 + 1)) + assert_identical(t2 + 1, xu.maximum(t2, t1 + 1)) + assert_identical(t2 + 1, xu.maximum(t1 + 1, t2)) + assert_identical(t2 + 1, xu.maximum(t2 + 1, t1)) def test_groupby(self): ds = xr.Dataset({'a': ('x', [0, 0, 0])}, {'c': ('x', [0, 0, 1])}) @@ -47,17 +47,17 @@ def test_groupby(self): group_mean = ds_grouped.mean('x') arr_grouped = ds['a'].groupby('c') - self.assertIdentical(ds, xu.maximum(ds_grouped, group_mean)) - self.assertIdentical(ds, xu.maximum(group_mean, ds_grouped)) + assert_identical(ds, xu.maximum(ds_grouped, group_mean)) + assert_identical(ds, xu.maximum(group_mean, ds_grouped)) - self.assertIdentical(ds, xu.maximum(arr_grouped, group_mean)) - self.assertIdentical(ds, xu.maximum(group_mean, arr_grouped)) + assert_identical(ds, xu.maximum(arr_grouped, group_mean)) + assert_identical(ds, xu.maximum(group_mean, arr_grouped)) - self.assertIdentical(ds, xu.maximum(ds_grouped, group_mean['a'])) - self.assertIdentical(ds, xu.maximum(group_mean['a'], ds_grouped)) + assert_identical(ds, xu.maximum(ds_grouped, group_mean['a'])) + assert_identical(ds, xu.maximum(group_mean['a'], ds_grouped)) - self.assertIdentical(ds.a, xu.maximum(arr_grouped, group_mean.a)) - self.assertIdentical(ds.a, xu.maximum(group_mean.a, arr_grouped)) + assert_identical(ds.a, xu.maximum(arr_grouped, group_mean.a)) + assert_identical(ds.a, xu.maximum(group_mean.a, arr_grouped)) with raises_regex(TypeError, 'only support binary ops'): xu.maximum(ds.a.variable, ds_grouped) @@ -65,4 +65,4 @@ def test_groupby(self): def test_pickle(self): a = 1.0 cos_pickled = pickle.loads(pickle.dumps(xu.cos)) - self.assertIdentical(cos_pickled(a), xu.cos(a)) + assert_identical(cos_pickled(a), xu.cos(a)) diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index b31669acd1a..60ddd8b60b7 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -60,7 +60,7 @@ def test_getitem_dict(self): v = self.cls(['x'], np.random.randn(5)) actual = v[{'x': 0}] expected = v[0] - self.assertVariableIdentical(expected, actual) + assert_identical(expected, actual) def test_getitem_1d(self): data = np.array([0, 1, 2]) @@ -99,9 +99,9 @@ def test_getitem_1d_fancy(self): # boolean indexing ind = Variable(('x', ), [True, False, True]) v_new = v[ind] - self.assertVariableIdentical(v[[0, 2]], v_new) + assert_identical(v[[0, 2]], v_new) v_new = v[[True, False, True]] - self.assertVariableIdentical(v[[0, 2]], v_new) + assert_identical(v[[0, 2]], v_new) with raises_regex(IndexError, "Boolean indexer should"): ind = Variable(('a', ), [True, False, True]) @@ -279,7 +279,7 @@ def test_object_conversion(self): def test_pandas_data(self): v = self.cls(['x'], pd.Series([0, 1, 2], index=[3, 2, 1])) - self.assertVariableIdentical(v, v[[0, 1, 2]]) + assert_identical(v, v[[0, 1, 2]]) v = self.cls(['x'], pd.Index([0, 1, 2])) assert v[0].values == v.values[0] @@ -299,13 +299,13 @@ def test_1d_math(self): v = self.cls(['x'], x) base_v = v.to_base_variable() # unary ops - self.assertVariableIdentical(base_v, +v) - self.assertVariableIdentical(base_v, abs(v)) + assert_identical(base_v, +v) + assert_identical(base_v, abs(v)) assert_equal((-v).values, -x) # binary ops with numbers - self.assertVariableIdentical(base_v, v + 0) - self.assertVariableIdentical(base_v, 0 + v) - self.assertVariableIdentical(base_v, v * 1) + assert_identical(base_v, v + 0) + assert_identical(base_v, 0 + v) + assert_identical(base_v, v * 1) assert_equal((v > 2).values, x > 2) assert_equal((0 == v).values, 0 == x) assert_equal((v - 1).values, x - 1) @@ -317,18 +317,13 @@ def test_1d_math(self): assert_equal(y - v, 1 - v) # verify attributes are dropped v2 = self.cls(['x'], x, {'units': 'meters'}) - self.assertVariableIdentical(base_v, +v2) + assert_identical(base_v, +v2) # binary ops with all variables assert_equal(v + v, 2 * v) w = self.cls(['x'], y, {'foo': 'bar'}) -<<<<<<< HEAD - self.assertVariableIdentical( - v + w, self.cls(['x'], x + y).to_base_variable()) - self.assertArrayEqual((v * w).values, x * y) -======= - self.assertVariableIdentical(v + w, self.cls(['x'], x + y).to_base_variable()) + assert_identical(v + w, self.cls(['x'], x + y).to_base_variable()) assert_equal((v * w).values, x * y) ->>>>>>> assert_equal + # something complicated assert_equal((v ** 2 * w - 1 + x).values, x ** 2 * y - 1 + x) # make sure dtype is preserved (for Index objects) @@ -347,7 +342,7 @@ def test_1d_reduce(self): v = self.cls(['x'], x) actual = v.sum() expected = Variable((), 10) - self.assertVariableIdentical(expected, actual) + assert_identical(expected, actual) assert type(actual) is Variable def test_array_interface(self): @@ -357,20 +352,14 @@ def test_array_interface(self): # test patched in methods assert_equal(v.astype(float), x.astype(float)) # think this is a break, that argsort changes the type - self.assertVariableIdentical(v.argsort(), v.to_base_variable()) - self.assertVariableIdentical( - v.clip(2, 3), self.cls('x', x.clip(2, 3)).to_base_variable()) + assert_identical(v.argsort(), v.to_base_variable()) + assert_identical(v.clip(2, 3), + self.cls('x', x.clip(2, 3)).to_base_variable()) # test ufuncs -<<<<<<< HEAD - self.assertVariableIdentical( - np.sin(v), self.cls(['x'], np.sin(x)).to_base_variable()) - self.assertIsInstance(np.sin(v), Variable) - self.assertNotIsInstance(np.sin(v), IndexVariable) -======= - self.assertVariableIdentical(np.sin(v), self.cls(['x'], np.sin(x)).to_base_variable()) + assert_identical(np.sin(v), + self.cls(['x'], np.sin(x)).to_base_variable()) assert isinstance(np.sin(v), Variable) assert not isinstance(np.sin(v), IndexVariable) ->>>>>>> self_assert from unittest2pytest def example_1d_objects(self): for data in [range(3), @@ -406,9 +395,9 @@ def test_eq_all_dtypes(self): expected = Variable('x', 3 * [False]) for v, _ in self.example_1d_objects(): actual = 'z' == v - self.assertVariableIdentical(expected, actual) + assert_identical(expected, actual) actual = ~('z' != v) - self.assertVariableIdentical(expected, actual) + assert_identical(expected, actual) def test_encoding_preserved(self): expected = self.cls('x', range(3), {'foo': 1}, {'bar': 2}) @@ -420,7 +409,7 @@ def test_encoding_preserved(self): expected.copy(deep=True), expected.copy(deep=False)]: - self.assertVariableIdentical(expected.to_base_variable(), + assert_identical(expected.to_base_variable(), actual.to_base_variable()) assert expected.encoding == actual.encoding @@ -429,11 +418,11 @@ def test_concat(self): y = np.arange(5, 10) v = self.cls(['a'], x) w = self.cls(['a'], y) - self.assertVariableIdentical(Variable(['b', 'a'], np.array([x, y])), + assert_identical(Variable(['b', 'a'], np.array([x, y])), Variable.concat([v, w], 'b')) - self.assertVariableIdentical(Variable(['b', 'a'], np.array([x, y])), + assert_identical(Variable(['b', 'a'], np.array([x, y])), Variable.concat((v, w), 'b')) - self.assertVariableIdentical(Variable(['b', 'a'], np.array([x, y])), + assert_identical(Variable(['b', 'a'], np.array([x, y])), Variable.concat((v, w), 'b')) with raises_regex(ValueError, 'inconsistent dimensions'): Variable.concat([v, Variable(['c'], y)], 'b') @@ -443,18 +432,14 @@ def test_concat(self): positions=[np.arange(0, 10, 2), np.arange(1, 10, 2)], dim='a') expected = Variable('a', np.array([x, y]).ravel(order='F')) - self.assertVariableIdentical(expected, actual) + assert_identical(expected, actual) # test concatenating along a dimension v = Variable(['time', 'x'], np.random.random((10, 8))) - self.assertVariableIdentical( - v, Variable.concat([v[:5], v[5:]], 'time')) - self.assertVariableIdentical( - v, Variable.concat([v[:5], v[5:6], v[6:]], 'time')) - self.assertVariableIdentical( - v, Variable.concat([v[:1], v[1:]], 'time')) + assert_identical(v, Variable.concat([v[:5], v[5:]], 'time')) + assert_identical(v, Variable.concat([v[:5], v[5:6], v[6:]], 'time')) + assert_identical(v, Variable.concat([v[:1], v[1:]], 'time')) # test dimension order - self.assertVariableIdentical( - v, Variable.concat([v[:, :5], v[:, 5:]], 'x')) + assert_identical(v, Variable.concat([v[:, :5], v[:, 5:]], 'x')) with raises_regex(ValueError, 'all input arrays must have'): Variable.concat([v[:, 0], v[:, 1:]], 'x') @@ -464,12 +449,12 @@ def test_concat_attrs(self): w = self.cls('a', np.ones(5)) expected = self.cls( 'a', np.concatenate([np.arange(5), np.ones(5)])).to_base_variable() - self.assertVariableIdentical(expected, Variable.concat([v, w], 'a')) + assert_identical(expected, Variable.concat([v, w], 'a')) w.attrs['foo'] = 2 - self.assertVariableIdentical(expected, Variable.concat([v, w], 'a')) + assert_identical(expected, Variable.concat([v, w], 'a')) w.attrs['foo'] = 'bar' expected.attrs['foo'] = 'bar' - self.assertVariableIdentical(expected, Variable.concat([v, w], 'a')) + assert_identical(expected, Variable.concat([v, w], 'a')) def test_concat_fixed_len_str(self): # regression test for #217 @@ -487,7 +472,7 @@ def test_concat_number_strings(self): b = self.cls('x', ['3', '4']) actual = Variable.concat([a, b], dim='x') expected = Variable('x', np.arange(5).astype(str).astype(object)) - self.assertVariableIdentical(expected, actual) + assert_identical(expected, actual) assert expected.dtype == object assert type(expected.values[0]) == str @@ -496,7 +481,7 @@ def test_copy(self): for deep in [True, False]: w = v.copy(deep=deep) assert type(v) is type(w) - self.assertVariableIdentical(v, w) + assert_identical(v, w) assert v.dtype == w.dtype if self.cls is Variable: if deep: @@ -505,7 +490,7 @@ def test_copy(self): else: assert source_ndarray(v.values) is \ source_ndarray(w.values) - self.assertVariableIdentical(v, copy(v)) + assert_identical(v, copy(v)) def test_copy_index(self): midx = pd.MultiIndex.from_product([['a', 'b'], [1, 2], [-1, -2]], @@ -520,10 +505,10 @@ def test_copy_index(self): def test_real_and_imag(self): v = self.cls('x', np.arange(3) - 1j * np.arange(3), {'foo': 'bar'}) expected_re = self.cls('x', np.arange(3), {'foo': 'bar'}) - self.assertVariableIdentical(v.real, expected_re) + assert_identical(v.real, expected_re) expected_im = self.cls('x', -np.arange(3), {'foo': 'bar'}) - self.assertVariableIdentical(v.imag, expected_im) + assert_identical(v.imag, expected_im) expected_abs = self.cls( 'x', np.sqrt(2 * np.arange(3) ** 2)).to_base_variable() @@ -554,8 +539,8 @@ def test_pandas_datetime64_with_tz(self): def test_multiindex(self): idx = pd.MultiIndex.from_product([list('abc'), [0, 1]]) v = self.cls('x', idx) - self.assertVariableIdentical(Variable((), ('a', 0)), v[0]) - self.assertVariableIdentical(v, v[:]) + assert_identical(Variable((), ('a', 0)), v[0]) + assert_identical(v, v[:]) def test_load(self): array = self.cls('x', np.arange(5)) @@ -565,7 +550,7 @@ def test_load(self): array.load() assert type(array._data) is type(orig_data) assert type(copied._data) is type(orig_data) - self.assertVariableIdentical(array, copied) + assert_identical(array, copied) def test_getitem_advanced(self): v = self.cls(['x', 'y'], [[0, 1, 2], [3, 4, 5]]) @@ -704,13 +689,13 @@ def test_getitem_fancy(self): ind = Variable(['a', 'b'], [[0]]) v_new = v[ind, :, :] expected = Variable(['a', 'b', 'y', 'z'], v.data[np.newaxis, ...]) - self.assertVariableIdentical(v_new, expected) + assert_identical(v_new, expected) v = Variable(['w', 'x', 'y', 'z'], [[[[1, 2, 3], [4, 5, 6]]]]) ind = Variable(['y'], [0]) v_new = v[ind, :, 1:2, 2] expected = Variable(['y', 'x'], [[6]]) - self.assertVariableIdentical(v_new, expected) + assert_identical(v_new, expected) # slice and vector mixed indexing resulting in the same dimension v = Variable(['x', 'y', 'z'], np.arange(60).reshape(3, 4, 5)) @@ -720,7 +705,7 @@ def test_getitem_fancy(self): expected[0] = v.data[0, 0] expected[1] = v.data[1, 1] expected[2] = v.data[2, 2] - self.assertVariableIdentical(v_new, expected) + assert_identical(v_new, expected) v_new = v[:, ind.data] assert v_new.shape == (3, 3, 5) @@ -889,32 +874,32 @@ def test_as_variable(self): expected_extra = Variable('x', data, attrs={'myattr': 'val'}, encoding={'scale_factor': 1}) - self.assertVariableIdentical(expected, as_variable(expected)) + assert_identical(expected, as_variable(expected)) ds = Dataset({'x': expected}) var = as_variable(ds['x']).to_base_variable() - self.assertVariableIdentical(expected, var) + assert_identical(expected, var) assert not isinstance(ds['x'], Variable) assert isinstance(as_variable(ds['x']), Variable) FakeVariable = namedtuple('FakeVariable', 'values dims') fake_xarray = FakeVariable(expected.values, expected.dims) - self.assertVariableIdentical(expected, as_variable(fake_xarray)) + assert_identical(expected, as_variable(fake_xarray)) FakeVariable = namedtuple('FakeVariable', 'data dims') fake_xarray = FakeVariable(expected.data, expected.dims) - self.assertVariableIdentical(expected, as_variable(fake_xarray)) + assert_identical(expected, as_variable(fake_xarray)) FakeVariable = namedtuple('FakeVariable', 'data values dims attrs encoding') fake_xarray = FakeVariable(expected_extra.data, expected_extra.values, expected_extra.dims, expected_extra.attrs, expected_extra.encoding) - self.assertVariableIdentical(expected_extra, as_variable(fake_xarray)) + assert_identical(expected_extra, as_variable(fake_xarray)) xarray_tuple = (expected_extra.dims, expected_extra.values, expected_extra.attrs, expected_extra.encoding) - self.assertVariableIdentical(expected_extra, as_variable(xarray_tuple)) + assert_identical(expected_extra, as_variable(xarray_tuple)) with raises_regex(TypeError, 'tuples to convert'): as_variable(tuple(data)) @@ -923,11 +908,11 @@ def test_as_variable(self): as_variable(data) actual = as_variable(data, name='x') - self.assertVariableIdentical(expected.to_index_variable(), actual) + assert_identical(expected.to_index_variable(), actual) actual = as_variable(0) expected = Variable([], 0) - self.assertVariableIdentical(expected, actual) + assert_identical(expected, actual) data = np.arange(9).reshape((3, 3)) expected = Variable(('x', 'y'), data) @@ -1030,25 +1015,25 @@ def test_items(self): data = np.random.random((10, 11)) v = Variable(['x', 'y'], data) # test slicing - self.assertVariableIdentical(v, v[:]) - self.assertVariableIdentical(v, v[...]) - self.assertVariableIdentical(Variable(['y'], data[0]), v[0]) - self.assertVariableIdentical(Variable(['x'], data[:, 0]), v[:, 0]) - self.assertVariableIdentical(Variable(['x', 'y'], data[:3, :2]), + assert_identical(v, v[:]) + assert_identical(v, v[...]) + assert_identical(Variable(['y'], data[0]), v[0]) + assert_identical(Variable(['x'], data[:, 0]), v[:, 0]) + assert_identical(Variable(['x', 'y'], data[:3, :2]), v[:3, :2]) # test array indexing x = Variable(['x'], np.arange(10)) y = Variable(['y'], np.arange(11)) - self.assertVariableIdentical(v, v[x.values]) - self.assertVariableIdentical(v, v[x]) - self.assertVariableIdentical(v[:3], v[x < 3]) - self.assertVariableIdentical(v[:, 3:], v[:, y >= 3]) - self.assertVariableIdentical(v[:3, 3:], v[x < 3, y >= 3]) - self.assertVariableIdentical(v[:3, :2], v[x[:3], y[:2]]) - self.assertVariableIdentical(v[:3, :2], v[range(3), range(2)]) + assert_identical(v, v[x.values]) + assert_identical(v, v[x]) + assert_identical(v[:3], v[x < 3]) + assert_identical(v[:, 3:], v[:, y >= 3]) + assert_identical(v[:3, 3:], v[x < 3, y >= 3]) + assert_identical(v[:3, :2], v[x[:3], y[:2]]) + assert_identical(v[:3, :2], v[range(3), range(2)]) # test iteration for n, item in enumerate(v): - self.assertVariableIdentical(Variable(['y'], data[n]), item) + assert_identical(Variable(['y'], data[n]), item) with raises_regex(TypeError, 'iteration over a 0-d'): iter(Variable([], 0)) # test setting @@ -1090,74 +1075,74 @@ def test_getitem_with_mask_2d_input(self): def test_isel(self): v = Variable(['time', 'x'], self.d) - self.assertVariableIdentical(v.isel(time=slice(None)), v) - self.assertVariableIdentical(v.isel(time=0), v[0]) - self.assertVariableIdentical(v.isel(time=slice(0, 3)), v[:3]) - self.assertVariableIdentical(v.isel(x=0), v[:, 0]) + assert_identical(v.isel(time=slice(None)), v) + assert_identical(v.isel(time=0), v[0]) + assert_identical(v.isel(time=slice(0, 3)), v[:3]) + assert_identical(v.isel(x=0), v[:, 0]) with raises_regex(ValueError, 'do not exist'): v.isel(not_a_dim=0) def test_index_0d_numpy_string(self): # regression test to verify our work around for indexing 0d strings v = Variable([], np.string_('asdf')) - self.assertVariableIdentical(v[()], v) + assert_identical(v[()], v) v = Variable([], np.unicode_(u'asdf')) - self.assertVariableIdentical(v[()], v) + assert_identical(v[()], v) def test_indexing_0d_unicode(self): # regression test for GH568 actual = Variable(('x'), [u'tmax'])[0][()] expected = Variable((), u'tmax') - self.assertVariableIdentical(actual, expected) + assert_identical(actual, expected) def test_shift(self): v = Variable('x', [1, 2, 3, 4, 5]) - self.assertVariableIdentical(v, v.shift(x=0)) + assert_identical(v, v.shift(x=0)) assert v is not v.shift(x=0) expected = Variable('x', [np.nan, 1, 2, 3, 4]) - self.assertVariableIdentical(expected, v.shift(x=1)) + assert_identical(expected, v.shift(x=1)) expected = Variable('x', [np.nan, np.nan, 1, 2, 3]) - self.assertVariableIdentical(expected, v.shift(x=2)) + assert_identical(expected, v.shift(x=2)) expected = Variable('x', [2, 3, 4, 5, np.nan]) - self.assertVariableIdentical(expected, v.shift(x=-1)) + assert_identical(expected, v.shift(x=-1)) expected = Variable('x', [np.nan] * 5) - self.assertVariableIdentical(expected, v.shift(x=5)) - self.assertVariableIdentical(expected, v.shift(x=6)) + assert_identical(expected, v.shift(x=5)) + assert_identical(expected, v.shift(x=6)) with raises_regex(ValueError, 'dimension'): v.shift(z=0) v = Variable('x', [1, 2, 3, 4, 5], {'foo': 'bar'}) - self.assertVariableIdentical(v, v.shift(x=0)) + assert_identical(v, v.shift(x=0)) expected = Variable('x', [np.nan, 1, 2, 3, 4], {'foo': 'bar'}) - self.assertVariableIdentical(expected, v.shift(x=1)) + assert_identical(expected, v.shift(x=1)) def test_shift2d(self): v = Variable(('x', 'y'), [[1, 2], [3, 4]]) expected = Variable(('x', 'y'), [[np.nan, np.nan], [np.nan, 1]]) - self.assertVariableIdentical(expected, v.shift(x=1, y=1)) + assert_identical(expected, v.shift(x=1, y=1)) def test_roll(self): v = Variable('x', [1, 2, 3, 4, 5]) - self.assertVariableIdentical(v, v.roll(x=0)) + assert_identical(v, v.roll(x=0)) assert v is not v.roll(x=0) expected = Variable('x', [5, 1, 2, 3, 4]) - self.assertVariableIdentical(expected, v.roll(x=1)) - self.assertVariableIdentical(expected, v.roll(x=-4)) - self.assertVariableIdentical(expected, v.roll(x=6)) + assert_identical(expected, v.roll(x=1)) + assert_identical(expected, v.roll(x=-4)) + assert_identical(expected, v.roll(x=6)) expected = Variable('x', [4, 5, 1, 2, 3]) - self.assertVariableIdentical(expected, v.roll(x=2)) - self.assertVariableIdentical(expected, v.roll(x=-3)) + assert_identical(expected, v.roll(x=2)) + assert_identical(expected, v.roll(x=-3)) with raises_regex(ValueError, 'dimension'): v.roll(z=0) @@ -1174,16 +1159,16 @@ def test_roll_consistency(self): def test_transpose(self): v = Variable(['time', 'x'], self.d) v2 = Variable(['x', 'time'], self.d.T) - self.assertVariableIdentical(v, v2.transpose()) - self.assertVariableIdentical(v.transpose(), v.T) + assert_identical(v, v2.transpose()) + assert_identical(v.transpose(), v.T) x = np.random.randn(2, 3, 4, 5) w = Variable(['a', 'b', 'c', 'd'], x) w2 = Variable(['d', 'b', 'c', 'a'], np.einsum('abcd->dbca', x)) assert w2.shape == (5, 3, 4, 2) - self.assertVariableIdentical(w2, w.transpose('d', 'b', 'c', 'a')) - self.assertVariableIdentical(w, w2.transpose('a', 'b', 'c', 'd')) + assert_identical(w2, w.transpose('d', 'b', 'c', 'a')) + assert_identical(w, w2.transpose('a', 'b', 'c', 'd')) w3 = Variable(['b', 'c', 'd', 'a'], np.einsum('abcd->bcda', x)) - self.assertVariableIdentical(w, w3.transpose('a', 'b', 'c', 'd')) + assert_identical(w, w3.transpose('a', 'b', 'c', 'd')) def test_transpose_0d(self): for value in [ @@ -1200,15 +1185,15 @@ def test_transpose_0d(self): def test_squeeze(self): v = Variable(['x', 'y'], [[1]]) - self.assertVariableIdentical(Variable([], 1), v.squeeze()) - self.assertVariableIdentical(Variable(['y'], [1]), v.squeeze('x')) - self.assertVariableIdentical(Variable(['y'], [1]), v.squeeze(['x'])) - self.assertVariableIdentical(Variable(['x'], [1]), v.squeeze('y')) - self.assertVariableIdentical(Variable([], 1), v.squeeze(['x', 'y'])) + assert_identical(Variable([], 1), v.squeeze()) + assert_identical(Variable(['y'], [1]), v.squeeze('x')) + assert_identical(Variable(['y'], [1]), v.squeeze(['x'])) + assert_identical(Variable(['x'], [1]), v.squeeze('y')) + assert_identical(Variable([], 1), v.squeeze(['x', 'y'])) v = Variable(['x', 'y'], [[1, 2]]) - self.assertVariableIdentical(Variable(['y'], [1, 2]), v.squeeze()) - self.assertVariableIdentical(Variable(['y'], [1, 2]), v.squeeze('x')) + assert_identical(Variable(['y'], [1, 2]), v.squeeze()) + assert_identical(Variable(['y'], [1, 2]), v.squeeze('x')) with raises_regex(ValueError, 'cannot select a dimension'): v.squeeze('y') @@ -1225,19 +1210,19 @@ def test_set_dims(self): v = Variable(['x'], [0, 1]) actual = v.set_dims(['x', 'y']) expected = Variable(['x', 'y'], [[0], [1]]) - self.assertVariableIdentical(actual, expected) + assert_identical(actual, expected) actual = v.set_dims(['y', 'x']) - self.assertVariableIdentical(actual, expected.T) + assert_identical(actual, expected.T) actual = v.set_dims(OrderedDict([('x', 2), ('y', 2)])) expected = Variable(['x', 'y'], [[0, 0], [1, 1]]) - self.assertVariableIdentical(actual, expected) + assert_identical(actual, expected) v = Variable(['foo'], [0, 1]) actual = v.set_dims('foo') expected = v - self.assertVariableIdentical(actual, expected) + assert_identical(actual, expected) with raises_regex(ValueError, 'must be a superset'): v.set_dims(['z']) @@ -1255,18 +1240,18 @@ def test_stack(self): v = Variable(['x', 'y'], [[0, 1], [2, 3]], {'foo': 'bar'}) actual = v.stack(z=('x', 'y')) expected = Variable('z', [0, 1, 2, 3], v.attrs) - self.assertVariableIdentical(actual, expected) + assert_identical(actual, expected) actual = v.stack(z=('x',)) expected = Variable(('y', 'z'), v.data.T, v.attrs) - self.assertVariableIdentical(actual, expected) + assert_identical(actual, expected) actual = v.stack(z=(),) - self.assertVariableIdentical(actual, v) + assert_identical(actual, v) actual = v.stack(X=('x',), Y=('y',)).transpose('X', 'Y') expected = Variable(('X', 'Y'), v.data, v.attrs) - self.assertVariableIdentical(actual, expected) + assert_identical(actual, expected) def test_stack_errors(self): v = Variable(['x', 'y'], [[0, 1], [2, 3]], {'foo': 'bar'}) @@ -1280,15 +1265,15 @@ def test_unstack(self): v = Variable('z', [0, 1, 2, 3], {'foo': 'bar'}) actual = v.unstack(z=OrderedDict([('x', 2), ('y', 2)])) expected = Variable(('x', 'y'), [[0, 1], [2, 3]], v.attrs) - self.assertVariableIdentical(actual, expected) + assert_identical(actual, expected) actual = v.unstack(z=OrderedDict([('x', 4), ('y', 1)])) expected = Variable(('x', 'y'), [[0], [1], [2], [3]], v.attrs) - self.assertVariableIdentical(actual, expected) + assert_identical(actual, expected) actual = v.unstack(z=OrderedDict([('x', 4)])) expected = Variable('x', [0, 1, 2, 3], v.attrs) - self.assertVariableIdentical(actual, expected) + assert_identical(actual, expected) def test_unstack_errors(self): v = Variable('z', [0, 1, 2, 3]) @@ -1303,44 +1288,44 @@ def test_unstack_2d(self): v = Variable(['x', 'y'], [[0, 1], [2, 3]]) actual = v.unstack(y={'z': 2}) expected = Variable(['x', 'z'], v.data) - self.assertVariableIdentical(actual, expected) + assert_identical(actual, expected) actual = v.unstack(x={'z': 2}) expected = Variable(['y', 'z'], v.data.T) - self.assertVariableIdentical(actual, expected) + assert_identical(actual, expected) def test_stack_unstack_consistency(self): v = Variable(['x', 'y'], [[0, 1], [2, 3]]) actual = (v.stack(z=('x', 'y')) .unstack(z=OrderedDict([('x', 2), ('y', 2)]))) - self.assertVariableIdentical(actual, v) + assert_identical(actual, v) def test_broadcasting_math(self): x = np.random.randn(2, 3) v = Variable(['a', 'b'], x) # 1d to 2d broadcasting - self.assertVariableIdentical( + assert_identical( v * v, Variable(['a', 'b'], np.einsum('ab,ab->ab', x, x))) - self.assertVariableIdentical( + assert_identical( v * v[0], Variable(['a', 'b'], np.einsum('ab,b->ab', x, x[0]))) - self.assertVariableIdentical( + assert_identical( v[0] * v, Variable(['b', 'a'], np.einsum('b,ab->ba', x[0], x))) - self.assertVariableIdentical( + assert_identical( v[0] * v[:, 0], Variable(['b', 'a'], np.einsum('b,a->ba', x[0], x[:, 0]))) # higher dim broadcasting y = np.random.randn(3, 4, 5) w = Variable(['b', 'c', 'd'], y) - self.assertVariableIdentical( + assert_identical( v * w, Variable(['a', 'b', 'c', 'd'], np.einsum('ab,bcd->abcd', x, y))) - self.assertVariableIdentical( + assert_identical( w * v, Variable(['b', 'c', 'd', 'a'], np.einsum('bcd,ab->bcda', y, x))) - self.assertVariableIdentical( + assert_identical( v * w[0], Variable(['a', 'b', 'c', 'd'], np.einsum('ab,cd->abcd', x, y[0]))) @@ -1368,15 +1353,15 @@ def test_inplace_math(self): def test_reduce(self): v = Variable(['x', 'y'], self.d, {'ignored': 'attributes'}) - self.assertVariableIdentical(v.reduce(np.std, 'x'), + assert_identical(v.reduce(np.std, 'x'), Variable(['y'], self.d.std(axis=0))) - self.assertVariableIdentical(v.reduce(np.std, axis=0), + assert_identical(v.reduce(np.std, axis=0), v.reduce(np.std, dim='x')) - self.assertVariableIdentical(v.reduce(np.std, ['y', 'x']), + assert_identical(v.reduce(np.std, ['y', 'x']), Variable([], self.d.std(axis=(0, 1)))) - self.assertVariableIdentical(v.reduce(np.std), + assert_identical(v.reduce(np.std), Variable([], self.d.std())) - self.assertVariableIdentical( + assert_identical( v.reduce(np.mean, 'x').reduce(np.std, 'y'), Variable([], self.d.mean(axis=0).std())) self.assertVariableAllClose(v.mean('x'), v.reduce(np.mean, 'x')) @@ -1442,37 +1427,36 @@ def test_big_endian_reduce(self): data = np.ones(5, dtype='>f4') v = Variable(['x'], data) expected = Variable([], 5) - self.assertVariableIdentical(expected, v.sum()) + assert_identical(expected, v.sum()) def test_reduce_funcs(self): v = Variable('x', np.array([1, np.nan, 2, 3])) - self.assertVariableIdentical(v.mean(), Variable([], 2)) - self.assertVariableIdentical(v.mean(skipna=True), Variable([], 2)) - self.assertVariableIdentical(v.mean(skipna=False), - Variable([], np.nan)) - self.assertVariableIdentical(np.mean(v), Variable([], 2)) - - self.assertVariableIdentical(v.prod(), Variable([], 6)) - self.assertVariableIdentical(v.cumsum(axis=0), + assert_identical(v.mean(), Variable([], 2)) + assert_identical(v.mean(skipna=True), Variable([], 2)) + assert_identical(v.mean(skipna=False), Variable([], np.nan)) + assert_identical(np.mean(v), Variable([], 2)) + + assert_identical(v.prod(), Variable([], 6)) + assert_identical(v.cumsum(axis=0), Variable('x', np.array([1, 1, 3, 6]))) - self.assertVariableIdentical(v.cumprod(axis=0), + assert_identical(v.cumprod(axis=0), Variable('x', np.array([1, 1, 2, 6]))) - self.assertVariableIdentical(v.var(), Variable([], 2.0 / 3)) + assert_identical(v.var(), Variable([], 2.0 / 3)) if LooseVersion(np.__version__) < '1.9': with pytest.raises(NotImplementedError): v.median() else: - self.assertVariableIdentical(v.median(), Variable([], 2)) + assert_identical(v.median(), Variable([], 2)) v = Variable('x', [True, False, False]) - self.assertVariableIdentical(v.any(), Variable([], True)) - self.assertVariableIdentical(v.all(dim='x'), Variable([], False)) + assert_identical(v.any(), Variable([], True)) + assert_identical(v.all(dim='x'), Variable([], False)) v = Variable('t', pd.date_range('2000-01-01', periods=3)) with pytest.raises(NotImplementedError): v.max(skipna=True) - self.assertVariableIdentical( + assert_identical( v.max(), Variable([], pd.Timestamp('2000-01-03'))) def test_reduce_keep_attrs(self): @@ -1493,19 +1477,19 @@ def test_reduce_keep_attrs(self): def test_count(self): expected = Variable([], 3) actual = Variable(['x'], [1, 2, 3, np.nan]).count() - self.assertVariableIdentical(expected, actual) + assert_identical(expected, actual) v = Variable(['x'], np.array(['1', '2', '3', np.nan], dtype=object)) actual = v.count() - self.assertVariableIdentical(expected, actual) + assert_identical(expected, actual) actual = Variable(['x'], [True, False, True]).count() - self.assertVariableIdentical(expected, actual) + assert_identical(expected, actual) assert actual.dtype == int expected = Variable(['x'], [2, 3]) actual = Variable(['x', 'y'], [[1, 0, np.nan], [1, 1, 1]]).count('y') - self.assertVariableIdentical(expected, actual) + assert_identical(expected, actual) def test_setitem(self): v = Variable(['x', 'y'], [[0, 3, 2], [3, 4, 5]]) @@ -1573,14 +1557,14 @@ def assert_assigned_2d(array, key_x, key_y, values): expected = Variable(['x', 'y', 'z'], np.ones((4, 3, 2))) expected[0, :, 0] = 0 expected[1, :, 1] = 0 - self.assertVariableIdentical(expected, v) + assert_identical(expected, v) # dimension broadcast v = Variable(['x', 'y'], np.ones((3, 2))) ind = Variable(['a', 'b'], [[0, 1]]) v[ind, :] = 0 expected = Variable(['x', 'y'], [[0, 0], [0, 0], [1, 1]]) - self.assertVariableIdentical(expected, v) + assert_identical(expected, v) with raises_regex(ValueError, "shape mismatch"): v[ind, ind] = np.zeros((1, 2, 1)) @@ -1597,12 +1581,12 @@ def assert_assigned_2d(array, key_x, key_y, values): ind = Variable(['a'], [0, 1]) v[dict(x=ind)] += 1 expected = Variable(['x', 'y'], [[1, 2], [3, 4], [4, 5]]) - self.assertVariableIdentical(v, expected) + assert_identical(v, expected) ind = Variable(['a'], [0, 0]) v[dict(x=ind)] += 1 expected = Variable(['x', 'y'], [[2, 3], [3, 4], [4, 5]]) - self.assertVariableIdentical(v, expected) + assert_identical(v, expected) @requires_dask @@ -1693,7 +1677,7 @@ def test_get_level_variable(self): names=['level_1', 'level_2']) x = IndexVariable('x', midx) level_1 = IndexVariable('x', midx.get_level_values('level_1')) - self.assertVariableIdentical(x.get_level_variable('level_1'), level_1) + assert_identical(x.get_level_variable('level_1'), level_1) with raises_regex(ValueError, 'has no MultiIndex'): IndexVariable('y', [10.0]).get_level_variable('level') @@ -1809,12 +1793,12 @@ def test_full_like(self): expect = orig.copy(deep=True) expect.values = [[2.0, 2.0], [2.0, 2.0]] - self.assertVariableIdentical(expect, full_like(orig, 2)) + assert_identical(expect, full_like(orig, 2)) # override dtype expect.values = [[True, True], [True, True]] assert expect.dtype == bool - self.assertVariableIdentical(expect, full_like(orig, True, dtype=bool)) + assert_identical(expect, full_like(orig, True, dtype=bool)) @requires_dask def test_full_like_dask(self): @@ -1848,17 +1832,17 @@ def check(actual, expect_dtype, expect_values): def test_zeros_like(self): orig = Variable(dims=('x', 'y'), data=[[1.5, 2.0], [3.1, 4.3]], attrs={'foo': 'bar'}) - self.assertVariableIdentical(zeros_like(orig), + assert_identical(zeros_like(orig), full_like(orig, 0)) - self.assertVariableIdentical(zeros_like(orig, dtype=int), + assert_identical(zeros_like(orig, dtype=int), full_like(orig, 0, dtype=int)) def test_ones_like(self): orig = Variable(dims=('x', 'y'), data=[[1.5, 2.0], [3.1, 4.3]], attrs={'foo': 'bar'}) - self.assertVariableIdentical(ones_like(orig), + assert_identical(ones_like(orig), full_like(orig, 1)) - self.assertVariableIdentical(ones_like(orig, dtype=int), + assert_identical(ones_like(orig, dtype=int), full_like(orig, 1, dtype=int)) def test_unsupported_type(self): From 72d3119e453e0f2d9628764e31ed986b5ae07dcd Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 14:35:11 -0500 Subject: [PATCH 08/41] pytest warning fix --- xarray/tests/test_variable.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index 60ddd8b60b7..a64312f5422 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -1705,7 +1705,7 @@ def test_concat_multiindex(self): assert isinstance(actual.to_index(), pd.MultiIndex) def test_coordinate_alias(self): - with pytest.warns('deprecated'): + with pytest.warns(Warning, 'deprecated'): x = Coordinate('x', [1, 2, 3]) assert isinstance(x, IndexVariable) From 286c0ebb74da7313869c186c96a60c31869bc965 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 14:41:02 -0500 Subject: [PATCH 09/41] revert assert_equal, as assert_array_equal needs to be separate --- xarray/tests/test_accessors.py | 18 +-- xarray/tests/test_backends.py | 36 +++--- xarray/tests/test_coding_times.py | 30 ++--- xarray/tests/test_combine.py | 8 +- xarray/tests/test_conventions.py | 28 ++--- xarray/tests/test_dask.py | 4 +- xarray/tests/test_dataarray.py | 176 +++++++++++++------------- xarray/tests/test_dataset.py | 188 ++++++++++++++-------------- xarray/tests/test_duck_array_ops.py | 14 +-- xarray/tests/test_indexing.py | 30 ++--- xarray/tests/test_plot.py | 34 ++--- xarray/tests/test_ufuncs.py | 2 +- xarray/tests/test_utils.py | 4 +- xarray/tests/test_variable.py | 145 +++++++++++---------- 14 files changed, 355 insertions(+), 362 deletions(-) diff --git a/xarray/tests/test_accessors.py b/xarray/tests/test_accessors.py index 90b8753ce09..4ebece774a5 100644 --- a/xarray/tests/test_accessors.py +++ b/xarray/tests/test_accessors.py @@ -37,10 +37,10 @@ def test_field_access(self): hours = xr.DataArray(self.times.hour, name='hour', coords=[self.times, ], dims=['time', ]) - assert_equal(years, self.data.time.dt.year) - assert_equal(months, self.data.time.dt.month) - assert_equal(days, self.data.time.dt.day) - assert_equal(hours, self.data.time.dt.hour) + self.assertDataArrayEqual(years, self.data.time.dt.year) + self.assertDataArrayEqual(months, self.data.time.dt.month) + self.assertDataArrayEqual(days, self.data.time.dt.day) + self.assertDataArrayEqual(hours, self.data.time.dt.hour) def test_not_datetime_type(self): nontime_data = self.data.copy() @@ -82,10 +82,10 @@ def test_dask_field_access(self): assert dask_hour.data.chunks == dask_chunks # Check the actual output from the accessors - assert_equal(years, dask_year.compute()) - assert_equal(months, dask_month.compute()) - assert_equal(days, dask_day.compute()) - assert_equal(hours, dask_hour.compute()) + self.assertDataArrayEqual(years, dask_year.compute()) + self.assertDataArrayEqual(months, dask_month.compute()) + self.assertDataArrayEqual(days, dask_day.compute()) + self.assertDataArrayEqual(hours, dask_hour.compute()) def test_seasons(self): dates = pd.date_range(start="2000/01/01", freq="M", periods=12) @@ -94,4 +94,4 @@ def test_seasons(self): "SON", "SON", "SON", "DJF"] seasons = xr.DataArray(seasons) - assert_equal(seasons.values, dates.dt.season.values) + self.assertArrayEqual(seasons.values, dates.dt.season.values) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 77b51f0b229..b3a43fba88e 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -369,7 +369,7 @@ def test_roundtrip_example_1_netcdf(self): # will depend on the encoding used. For example, # without CF encoding 'actual' will end up with # a dtype attribute. - assert_equal(expected, actual) + self.assertDatasetEqual(expected, actual) def test_roundtrip_coordinates(self): original = Dataset({'foo': ('x', [0, 1])}, @@ -785,7 +785,7 @@ def test_open_group(self): # check equivalent ways to specify group for group in 'foo', '/foo', 'foo/', '/foo/': with open_dataset(tmp_file, group=group) as actual: - assert_equal(actual['x'], expected['x']) + self.assertVariableEqual(actual['x'], expected['x']) # check that missing group raises appropriate exception with pytest.raises(IOError): @@ -812,7 +812,7 @@ def test_open_subgroup(self): # check equivalent ways to specify group for group in 'foo/bar', '/foo/bar', 'foo/bar/', '/foo/bar/': with open_dataset(tmp_file, group=group) as actual: - assert_equal(actual['x'], expected['x']) + self.assertVariableEqual(actual['x'], expected['x']) def test_write_groups(self): data1 = create_test_data() @@ -886,7 +886,7 @@ def test_open_encodings(self): expected['time'] = ('time', time, {}, encoding) with open_dataset(tmp_file) as actual: - assert_equal(actual['time'], expected['time']) + 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) @@ -933,7 +933,7 @@ def test_compression_encoding(self): # regression test for #156 expected = data.isel(dim1=0) with self.roundtrip(expected) as actual: - assert_equal(expected, actual) + self.assertDatasetEqual(expected, actual) def test_encoding_chunksizes_unlimited(self): # regression test for GH1225 @@ -948,7 +948,7 @@ def test_encoding_chunksizes_unlimited(self): 'original_shape': (3,), } with self.roundtrip(ds) as actual: - assert_equal(ds, actual) + self.assertDatasetEqual(ds, actual) def test_mask_and_scale(self): with create_tmp_file() as tmp_file: @@ -967,7 +967,7 @@ def test_mask_and_scale(self): expected = np.ma.array([-1, -1, 10, 10.1, 10.2], mask=[True, True, False, False, False]) actual = nc.variables['x'][:] - assert_equal(expected, actual) + self.assertArrayEqual(expected, actual) # now check xarray with open_dataset(tmp_file) as ds: @@ -1515,12 +1515,12 @@ def test_encoding_unlimited_dims(self): with self.roundtrip(ds, save_kwargs=dict(unlimited_dims=['y'])) as actual: assert actual.encoding['unlimited_dims'] == set('y') - assert_equal(ds, actual) + self.assertDatasetEqual(ds, actual) ds.encoding = {'unlimited_dims': ['y']} with self.roundtrip(ds) as actual: assert actual.encoding['unlimited_dims'] == set('y') - assert_equal(ds, actual) + self.assertDatasetEqual(ds, actual) class GenericNetCDFDataTestAutocloseTrue(GenericNetCDFDataTest): @@ -1548,7 +1548,7 @@ def test_array_type_after_indexing(self): def test_complex(self): expected = Dataset({'x': ('y', np.ones(5) + 1j * np.ones(5))}) with self.roundtrip(expected) as actual: - assert_equal(expected, actual) + self.assertDatasetEqual(expected, actual) @pytest.mark.xfail(reason='https://github.com/pydata/xarray/issues/535') def test_cross_engine_read_write_netcdf4(self): @@ -1578,11 +1578,11 @@ def test_encoding_unlimited_dims(self): with self.roundtrip(ds, save_kwargs=dict(unlimited_dims=['y'])) as actual: assert actual.encoding['unlimited_dims'] == set('y') - assert_equal(ds, actual) + self.assertDatasetEqual(ds, actual) ds.encoding = {'unlimited_dims': ['y']} with self.roundtrip(ds) as actual: assert actual.encoding['unlimited_dims'] == set('y') - assert_equal(ds, actual) + self.assertDatasetEqual(ds, actual) # tests pending h5netcdf fix @@ -2032,7 +2032,7 @@ def create_datasets(self, **kwargs): def test_cmp_local_file(self): with self.create_datasets() as (actual, expected): - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) # global attributes should be global attributes on the dataset assert 'NC_GLOBAL' not in actual.attrs @@ -2044,14 +2044,14 @@ def test_cmp_local_file(self): assert actual.attrs.keys() == expected.attrs.keys() with self.create_datasets() as (actual, expected): - assert_equal(actual.isel(l=2), expected.isel(l=2)) + self.assertDatasetEqual(actual.isel(l=2), expected.isel(l=2)) with self.create_datasets() as (actual, expected): - assert_equal(actual.isel(i=0, j=-1), + self.assertDatasetEqual(actual.isel(i=0, j=-1), expected.isel(i=0, j=-1)) with self.create_datasets() as (actual, expected): - assert_equal(actual.isel(j=slice(1, 2)), + self.assertDatasetEqual(actual.isel(j=slice(1, 2)), expected.isel(j=slice(1, 2))) def test_compatible_to_netcdf(self): @@ -2061,12 +2061,12 @@ def test_compatible_to_netcdf(self): actual.to_netcdf(tmp_file) actual = open_dataset(tmp_file) actual['bears'] = actual['bears'].astype(str) - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) @requires_dask def test_dask(self): with self.create_datasets(chunks={'j': 2}) as (actual, expected): - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) @network diff --git a/xarray/tests/test_coding_times.py b/xarray/tests/test_coding_times.py index 36d7af1f7a1..9e15f865ad8 100644 --- a/xarray/tests/test_coding_times.py +++ b/xarray/tests/test_coding_times.py @@ -59,7 +59,7 @@ def test_cf_datetime(self): calendar) if (isinstance(actual, np.ndarray) and np.issubdtype(actual.dtype, np.datetime64)): - # assert actual.dtype.kind == 'M' + # self.assertEqual(actual.dtype.kind, 'M') # For some reason, numpy 1.8 does not compare ns precision # datetime64 arrays as equal to arrays of datetime objects, # but it works for us precision. Thus, convert to us @@ -67,7 +67,7 @@ def test_cf_datetime(self): actual_cmp = actual.astype('M8[us]') else: actual_cmp = actual - assert_equal(expected, actual_cmp) + self.assertArrayEqual(expected, actual_cmp) encoded, _, _ = coding.times.encode_cf_datetime(actual, units, calendar) if '1-1-1' not in units: @@ -75,7 +75,7 @@ def test_cf_datetime(self): # units/encoding cannot be preserved in this case: # (Pdb) pd.to_datetime('1-1-1 00:00:0.0') # Timestamp('2001-01-01 00:00:00') - assert_equal(num_dates, np.around(encoded, 1)) + self.assertArrayEqual(num_dates, np.around(encoded, 1)) if (hasattr(num_dates, 'ndim') and num_dates.ndim == 1 and '1000' not in units): # verify that wrapping with a pandas.Index works @@ -83,7 +83,7 @@ def test_cf_datetime(self): # non-datetime64 compatible dates into a pandas.Index encoded, _, _ = coding.times.encode_cf_datetime( pd.Index(actual), units, calendar) - assert_equal(num_dates, np.around(encoded, 1)) + self.assertArrayEqual(num_dates, np.around(encoded, 1)) @requires_netCDF4 def test_decode_cf_datetime_overflow(self): @@ -108,7 +108,7 @@ def test_decode_cf_datetime_non_standard_units(self): # they cannot be parsed by netcdftime, but pd.Timestamp works units = 'hours since 1-1-1970' actual = coding.times.decode_cf_datetime(np.arange(100), units) - assert_equal(actual, expected) + self.assertArrayEqual(actual, expected) @requires_netCDF4 def test_decode_cf_datetime_non_iso_strings(self): @@ -120,7 +120,7 @@ def test_decode_cf_datetime_non_iso_strings(self): (np.arange(100), 'hours since 2000-01-01 0:00')] for num_dates, units in cases: actual = coding.times.decode_cf_datetime(num_dates, units) - assert_equal(actual, expected) + self.assertArrayEqual(actual, expected) @requires_netCDF4 def test_decode_non_standard_calendar(self): @@ -197,8 +197,8 @@ def test_decode_non_standard_calendar_multidim_time(self): actual = coding.times.decode_cf_datetime(mdim_time, units, calendar=calendar) assert actual.dtype == np.dtype('M8[ns]') - assert_equal(actual[:, 0], expected1) - assert_equal(actual[:, 1], expected2) + self.assertArrayEqual(actual[:, 0], expected1) + self.assertArrayEqual(actual[:, 1], expected2) @requires_netCDF4 def test_decode_non_standard_calendar_fallback(self): @@ -220,7 +220,7 @@ def test_decode_non_standard_calendar_fallback(self): str(w[0].message) assert actual.dtype == np.dtype('O') - assert_equal(actual, expected) + self.assertArrayEqual(actual, expected) @requires_netCDF4 def test_cf_datetime_nan(self): @@ -235,7 +235,7 @@ def test_cf_datetime_nan(self): warnings.filterwarnings('ignore', 'All-NaN') actual = coding.times.decode_cf_datetime(num_dates, units) expected = np.array(expected_list, dtype='datetime64[ns]') - assert_equal(expected, actual) + self.assertArrayEqual(expected, actual) @requires_netCDF4 def test_decoded_cf_datetime_array_2d(self): @@ -245,7 +245,7 @@ def test_decoded_cf_datetime_array_2d(self): result = coding.times.CFDatetimeCoder().decode(variable) assert result.dtype == 'datetime64[ns]' expected = pd.date_range('2000-01-01', periods=4).values.reshape(2, 2) - assert_equal(np.asarray(result), expected) + self.assertArrayEqual(np.asarray(result), expected) def test_infer_datetime_units(self): for dates, expected in [(pd.date_range('1900-01-01', periods=5), @@ -288,18 +288,18 @@ def test_cf_timedelta(self): expected = numbers actual, _ = coding.times.encode_cf_timedelta(timedeltas, units) - assert_equal(expected, actual) + self.assertArrayEqual(expected, actual) assert expected.dtype == actual.dtype if units is not None: expected = timedeltas actual = coding.times.decode_cf_timedelta(numbers, units) - assert_equal(expected, actual) + self.assertArrayEqual(expected, actual) assert expected.dtype == actual.dtype expected = np.timedelta64('NaT', 'ns') actual = coding.times.decode_cf_timedelta(np.array(np.nan), 'days') - assert_equal(expected, actual) + self.assertArrayEqual(expected, actual) def test_cf_timedelta_2d(self): timedeltas = ['1D', '2D', '3D'] @@ -310,7 +310,7 @@ def test_cf_timedelta_2d(self): expected = timedeltas actual = coding.times.decode_cf_timedelta(numbers, units) - assert_equal(expected, actual) + self.assertArrayEqual(expected, actual) assert expected.dtype == actual.dtype def test_infer_timedelta_units(self): diff --git a/xarray/tests/test_combine.py b/xarray/tests/test_combine.py index b59a4260646..4ea55cd0fbd 100644 --- a/xarray/tests/test_combine.py +++ b/xarray/tests/test_combine.py @@ -63,12 +63,12 @@ def rectify_dim_order(dataset): actual = concat(datasets, data[dim], coords='all') expected = np.array([data['extra'].values for _ in range(data.dims[dim])]) - assert_equal(actual['extra'].values, expected) + self.assertArrayEqual(actual['extra'].values, expected) actual = concat(datasets, data[dim], coords='different') - assert_equal(data['extra'], actual['extra']) + self.assertDataArrayEqual(data['extra'], actual['extra']) actual = concat(datasets, data[dim], coords='minimal') - assert_equal(data['extra'], actual['extra']) + self.assertDataArrayEqual(data['extra'], actual['extra']) # verify that the dim argument takes precedence over # concatenating dataset variables of the same name @@ -248,7 +248,7 @@ def test_concat(self): expected = DataArray(np.array([foo.values, bar.values]), dims=['w', 'x', 'y'], coords={'x': [0, 1]}) actual = concat([foo, bar], 'w') - assert_equal(expected, actual) + self.assertDataArrayEqual(expected, actual) # from iteration: grouped = [g for _, g in foo.groupby('x')] stacked = concat(grouped, ds['x']) diff --git a/xarray/tests/test_conventions.py b/xarray/tests/test_conventions.py index 8e69f1a6690..7dda6d3aed3 100644 --- a/xarray/tests/test_conventions.py +++ b/xarray/tests/test_conventions.py @@ -34,8 +34,8 @@ def test_wrapper_class(self): assert actual.size == expected.size assert actual.ndim == expected.ndim assert len(actual) == len(expected) - assert_equal(expected, actual) - assert_equal(expected[:1], actual[B[:1]]) + self.assertArrayEqual(expected, actual) + self.assertArrayEqual(expected[:1], actual[B[:1]]) with pytest.raises(IndexError): actual[B[:, :2]] @@ -59,34 +59,34 @@ def test_char_to_bytes(self): array = np.array([['a', 'b', 'c'], ['d', 'e', 'f']]) expected = np.array(['abc', 'def']) actual = conventions.char_to_bytes(array) - assert_equal(actual, expected) + self.assertArrayEqual(actual, expected) expected = np.array(['ad', 'be', 'cf']) actual = conventions.char_to_bytes(array.T) # non-contiguous - assert_equal(actual, expected) + self.assertArrayEqual(actual, expected) def test_char_to_bytes_ndim_zero(self): expected = np.array('a') actual = conventions.char_to_bytes(expected) - assert_equal(actual, expected) + self.assertArrayEqual(actual, expected) def test_char_to_bytes_size_zero(self): array = np.zeros((3, 0), dtype='S1') expected = np.array([b'', b'', b'']) actual = conventions.char_to_bytes(array) - assert_equal(actual, expected) + self.assertArrayEqual(actual, expected) def test_bytes_to_char(self): array = np.array([['ab', 'cd'], ['ef', 'gh']]) expected = np.array([[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]) actual = conventions.bytes_to_char(array) - assert_equal(actual, expected) + self.assertArrayEqual(actual, expected) expected = np.array([[['a', 'b'], ['e', 'f']], [['c', 'd'], ['g', 'h']]]) actual = conventions.bytes_to_char(array.T) - assert_equal(actual, expected) + self.assertArrayEqual(actual, expected) def test_vectorized_indexing(self): array = np.array([[b'a', b'b', b'c'], [b'd', b'e', b'f']], dtype='S') @@ -94,7 +94,7 @@ def test_vectorized_indexing(self): expected = np.array([[b'abc', b'def'], [b'def', b'abc']]) indexer = V[np.array([[0, 1], [1, 0]])] actual = stacked[indexer] - assert_equal(actual, expected) + self.assertArrayEqual(actual, expected) class TestBytesToStringArray(TestCase): @@ -109,8 +109,8 @@ def test_encoding(self): assert actual.shape == expected.shape assert actual.size == expected.size assert actual.ndim == expected.ndim - assert_equal(expected, actual) - assert_equal(expected[0], actual[B[0]]) + self.assertArrayEqual(expected, actual) + self.assertArrayEqual(expected[0], actual[B[0]]) def test_scalar(self): expected = np.array(u'abc', dtype=object) @@ -140,7 +140,7 @@ def test_booltype_array(self): x = np.array([1, 0, 1, 1, 0], dtype='i1') bx = conventions.BoolTypeArray(x) assert bx.dtype == np.bool - assert_equal(bx, np.array([True, False, True, True, False], + self.assertArrayEqual(bx, np.array([True, False, True, True, False], dtype=np.bool)) @@ -151,7 +151,7 @@ def test(self): a = conventions.NativeEndiannessArray(x) assert a.dtype == expected.dtype assert a.dtype == expected[:].dtype - assert_equal(a, expected) + self.assertArrayEqual(a, expected) def test_decode_cf_with_conflicting_fill_missing_value(): @@ -326,7 +326,7 @@ def test_decode_cf_datetime_transition_to_invalid(self): expected = [datetime(2000, 1, 1, 0, 0), datetime(2265, 10, 28, 0, 0)] - assert_equal(ds_decoded.time.values, expected) + self.assertArrayEqual(ds_decoded.time.values, expected) class CFEncodedInMemoryStore(WritableCFDataStore, InMemoryDataStore): diff --git a/xarray/tests/test_dask.py b/xarray/tests/test_dask.py index 5650f5a218a..c7d23c8e3e9 100644 --- a/xarray/tests/test_dask.py +++ b/xarray/tests/test_dask.py @@ -64,7 +64,7 @@ def test_basics(self): v = self.lazy_var assert self.data is v.data assert self.data.chunks == v.chunks - assert_equal(self.values, v) + self.assertArrayEqual(self.values, v) def test_copy(self): self.assertLazyAndIdentical(self.eager_var, self.lazy_var.copy()) @@ -245,7 +245,7 @@ def assertLazyAndAllClose(self, expected, actual): self.assertLazyAnd(expected, actual, self.assertDataArrayAllClose) def assertLazyAndEqual(self, expected, actual): - self.assertLazyAnd(expected, actual, assert_equal) + self.assertLazyAnd(expected, actual, self.assertDataArrayEqual) def setUp(self): self.values = np.random.randn(4, 6) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 1244efc7590..ea3134903be 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -63,16 +63,16 @@ def test_repr_multiindex(self): assert expected == repr(self.mda) def test_properties(self): - assert_equal(self.dv.variable, self.v) - assert_equal(self.dv.values, self.v.values) + self.assertVariableEqual(self.dv.variable, self.v) + self.assertArrayEqual(self.dv.values, self.v.values) for attr in ['dims', 'dtype', 'shape', 'size', 'nbytes', 'ndim', 'attrs']: assert getattr(self.dv, attr) == getattr(self.v, attr) assert len(self.dv) == len(self.v) - assert_equal(self.dv.variable, self.v) + self.assertVariableEqual(self.dv.variable, self.v) assert set(self.dv.coords) == set(self.ds.coords) for k, v in iteritems(self.dv.coords): - assert_equal(v, self.ds.coords[k]) + self.assertArrayEqual(v, self.ds.coords[k]) with pytest.raises(AttributeError): self.dv.dataset assert isinstance(self.ds['x'].to_index(), pd.Index) @@ -85,10 +85,10 @@ def test_data_property(self): array = DataArray(np.zeros((3, 4))) actual = array.copy() actual.values = np.ones((3, 4)) - assert_equal(np.ones((3, 4)), actual.values) + self.assertArrayEqual(np.ones((3, 4)), actual.values) actual.data = 2 * np.ones((3, 4)) - assert_equal(2 * np.ones((3, 4)), actual.data) - assert_equal(actual.data, actual.values) + self.assertArrayEqual(2 * np.ones((3, 4)), actual.data) + self.assertArrayEqual(actual.data, actual.values) def test_indexes(self): array = DataArray(np.zeros((2, 3)), @@ -179,7 +179,7 @@ def test_name(self): copied = arr.copy() arr.name = 'bar' assert arr.name == 'bar' - assert_equal(copied, arr) + self.assertDataArrayEqual(copied, arr) actual = DataArray(IndexVariable('x', [3])) actual.name = 'y' @@ -311,16 +311,16 @@ def test_constructor_from_self_described(self): assert_identical(expected, actual) actual = DataArray(expected.values, actual.coords) - assert_equal(expected, actual) + self.assertDataArrayEqual(expected, actual) frame = pd.DataFrame(data, index=pd.Index(['a', 'b'], name='x'), columns=pd.Index([-1, -2], name='y')) actual = DataArray(frame) - assert_equal(expected, actual) + self.assertDataArrayEqual(expected, actual) series = pd.Series(data[0], index=pd.Index([-1, -2], name='y')) actual = DataArray(series) - assert_equal(expected[0].reset_coords('x', drop=True), + self.assertDataArrayEqual(expected[0].reset_coords('x', drop=True), actual) panel = pd.Panel({0: frame}) @@ -441,7 +441,7 @@ def test_getitem(self): for i in [I[:], I[...], I[x.values], I[x.variable], I[x], I[x, y], I[x.values > -1], I[x.variable > -1], I[x > -1], I[x > -1, y > -1]]: - assert_equal(self.dv, self.dv[i]) + self.assertVariableEqual(self.dv, self.dv[i]) for i in [I[0], I[:, 0], I[:3, :2], I[x.values[:3]], I[x.variable[:3]], I[x[:3]], I[x[:3], y[:4]], @@ -492,21 +492,21 @@ def test_getitem_dataarray(self): da = DataArray(np.arange(12).reshape((3, 4)), dims=['x', 'y']) ind = DataArray([[0, 1], [0, 1]], dims=['x', 'z']) actual = da[ind] - assert_equal(actual.values, da.values[[[0, 1], [0, 1]], :]) + self.assertArrayEqual(actual, da.values[[[0, 1], [0, 1]], :]) da = DataArray(np.arange(12).reshape((3, 4)), dims=['x', 'y'], coords={'x': [0, 1, 2], 'y': ['a', 'b', 'c', 'd']}) ind = xr.DataArray([[0, 1], [0, 1]], dims=['X', 'Y']) actual = da[ind] expected = da.values[[[0, 1], [0, 1]], :] - assert_equal(actual, expected) + self.assertArrayEqual(actual, expected) assert actual.dims == ('X', 'Y', 'y') # boolean indexing ind = xr.DataArray([True, True, False], dims=['x']) - assert_equal(da[ind], da[[0, 1], :]) - assert_equal(da[ind], da[[0, 1]]) - assert_equal(da[ind], da[ind.values]) + self.assertDataArrayEqual(da[ind], da[[0, 1], :]) + self.assertDataArrayEqual(da[ind], da[[0, 1]]) + self.assertDataArrayEqual(da[ind], da[ind.values]) def test_setitem(self): # basic indexing should work as numpy's indexing @@ -524,7 +524,7 @@ def test_setitem(self): dims=['x', 'y']) orig[t] = 1 expected[t] = 1 - assert_equal(orig.values, expected) + self.assertArrayEqual(orig.values, expected) def test_setitem_fancy(self): # vectorized indexing @@ -743,7 +743,7 @@ def test_isel_fancy(self): assert_identical(actual['b'], stations['b']) expected = da.variable[:, stations['dim2s'].variable, stations['dim1s'].variable] - assert_equal(actual.values, expected) + self.assertArrayEqual(actual, expected) def test_sel(self): self.ds['x'] = ('x', np.array(list('abcdefghij'))) @@ -770,17 +770,17 @@ def test_sel_dataarray(self): # along new dimension ind = DataArray(['a', 'b', 'c'], dims=['new_dim']) actual = da.sel(x=ind) - assert_equal(actual, da.isel(x=[0, 1, 2])) + self.assertArrayEqual(actual, da.isel(x=[0, 1, 2])) assert 'new_dim' in actual.dims # with coordinate ind = DataArray(['a', 'b', 'c'], dims=['new_dim'], coords={'new_dim': [0, 1, 2]}) actual = da.sel(x=ind) - assert_equal(actual, da.isel(x=[0, 1, 2])) + self.assertArrayEqual(actual, da.isel(x=[0, 1, 2])) assert 'new_dim' in actual.dims assert 'new_dim' in actual.coords - assert_equal(actual['new_dim'].drop('x'), + self.assertDataArrayEqual(actual['new_dim'].drop('x'), ind['new_dim']) def test_sel_no_index(self): @@ -1019,8 +1019,8 @@ def test_virtual_time_components(self): dates = pd.date_range('2000-01-01', periods=10) da = DataArray(np.arange(1, 11), [('time', dates)]) - assert_equal(da['time.dayofyear'], da.values) - assert_equal(da.coords['time.dayofyear'], da.values) + self.assertArrayEqual(da['time.dayofyear'], da.values) + self.assertArrayEqual(da.coords['time.dayofyear'], da.values) def test_coords(self): # use int64 to ensure repr() consistency on windows @@ -1435,7 +1435,7 @@ def test_dataset_getitem(self): assert_identical(dv, self.dv) def test_array_interface(self): - assert_equal(np.asarray(self.dv), self.x) + self.assertArrayEqual(np.asarray(self.dv), self.x) # test patched in methods assert_array_equal(self.dv.astype(float), self.v.astype(float)) assert_array_equal(self.dv.argsort(), self.v.argsort()) @@ -1443,10 +1443,10 @@ def test_array_interface(self): # test ufuncs expected = deepcopy(self.ds) expected['foo'][:] = np.sin(self.x) - assert_equal(expected['foo'], np.sin(self.dv)) + self.assertDataArrayEqual(expected['foo'], np.sin(self.dv)) assert_array_equal(self.dv, np.maximum(self.v, self.dv)) bar = Variable(['x', 'y'], np.zeros((10, 20))) - assert_equal(self.dv, np.maximum(self.dv, bar)) + self.assertDataArrayEqual(self.dv, np.maximum(self.dv, bar)) def test_is_null(self): x = np.random.RandomState(42).randn(5, 6) @@ -1463,15 +1463,15 @@ def test_math(self): a = self.dv # variable math was already tested extensively, so let's just make sure # that all types are properly converted here - assert_equal(a, +a) - assert_equal(a, a + 0) - assert_equal(a, 0 + a) - assert_equal(a, a + 0 * v) - assert_equal(a, 0 * v + a) - assert_equal(a, a + 0 * x) - assert_equal(a, 0 * x + a) - assert_equal(a, a + 0 * a) - assert_equal(a, 0 * a + a) + self.assertDataArrayEqual(a, +a) + self.assertDataArrayEqual(a, a + 0) + self.assertDataArrayEqual(a, 0 + a) + self.assertDataArrayEqual(a, a + 0 * v) + self.assertDataArrayEqual(a, 0 * v + a) + self.assertDataArrayEqual(a, a + 0 * x) + self.assertDataArrayEqual(a, 0 * x + a) + self.assertDataArrayEqual(a, a + 0 * a) + self.assertDataArrayEqual(a, 0 * a + a) def test_math_automatic_alignment(self): a = DataArray(range(5), [('x', range(5))]) @@ -1499,7 +1499,7 @@ def test_inplace_math_basics(self): b += 1 assert b is a assert b.variable is v - assert_equal(b.values, x) + self.assertArrayEqual(b.values, x) assert source_ndarray(b.values) is x def test_inplace_math_automatic_alignment(self): @@ -1657,7 +1657,7 @@ def test_unstack_pandas_consistency(self): assert_identical(expected, actual) def test_transpose(self): - assert_equal(self.dv.variable.transpose(), + self.assertVariableEqual(self.dv.variable.transpose(), self.dv.transpose().variable) def test_squeeze(self): @@ -1772,7 +1772,7 @@ def test_reduce(self): expected = DataArray([0, 0], {'x': coords['x'], 'c': -999}, 'x') assert_identical(expected, actual) - assert_equal(self.dv.reduce(np.mean, 'x').variable, + self.assertVariableEqual(self.dv.reduce(np.mean, 'x').variable, self.v.reduce(np.mean, 'x')) orig = DataArray([[1, 0, np.nan], [3, 0, 3]], coords, dims=['x', 'y']) @@ -1787,7 +1787,7 @@ def test_reduce(self): actual = orig.mean(dim='x', skipna=True) expected = DataArray(orig.values.astype(int), dims=['x', 'y']).mean('x') - assert_equal(actual, expected) + self.assertDataArrayEqual(actual, expected) # skip due to bug in older versions of numpy.nanpercentile def test_quantile(self): @@ -1881,7 +1881,7 @@ def test_groupby_properties(self): expected_groups = {'a': range(0, 9), 'c': [9], 'b': range(10, 20)} assert expected_groups.keys() == grouped.groups.keys() for key in expected_groups: - assert_equal(expected_groups[key], grouped.groups[key]) + self.assertArrayEqual(expected_groups[key], grouped.groups[key]) assert 3 == len(grouped) def test_groupby_apply_identity(self): @@ -1971,7 +1971,7 @@ def test_groupby_apply_ndarray(self): array = self.make_groupby_example_array() grouped = array.groupby('abc') actual = grouped.apply(np.asarray) - assert_equal(array, actual) + self.assertDataArrayEqual(array, actual) def test_groupby_apply_changes_metadata(self): def change_metadata(x): @@ -1984,7 +1984,7 @@ def change_metadata(x): actual = grouped.apply(change_metadata) expected = array.copy() expected = change_metadata(expected) - assert_equal(expected, actual) + self.assertDataArrayEqual(expected, actual) def test_groupby_math(self): array = self.make_groupby_example_array() @@ -2608,25 +2608,25 @@ def test_to_pandas(self): # 0d actual = DataArray(42).to_pandas() expected = np.array(42) - assert_equal(actual, expected) + self.assertArrayEqual(actual, expected) # 1d values = np.random.randn(3) index = pd.Index(['a', 'b', 'c'], name='x') da = DataArray(values, coords=[index]) actual = da.to_pandas() - assert_equal(actual.values, values) - assert_equal(actual.index, index) - assert_equal(actual.index.name, 'x') + self.assertArrayEqual(actual.values, values) + self.assertArrayEqual(actual.index, index) + self.assertArrayEqual(actual.index.name, 'x') # 2d values = np.random.randn(3, 2) da = DataArray(values, coords=[('x', ['a', 'b', 'c']), ('y', [0, 1])], name='foo') actual = da.to_pandas() - assert_equal(actual.values, values) - assert_equal(actual.index, ['a', 'b', 'c']) - assert_equal(actual.columns, [0, 1]) + self.assertArrayEqual(actual.values, values) + self.assertArrayEqual(actual.index, ['a', 'b', 'c']) + self.assertArrayEqual(actual.columns, [0, 1]) # roundtrips for shape in [(3,), (3, 4), (3, 4, 5)]: @@ -2644,9 +2644,9 @@ def test_to_dataframe(self): [('B', [1, 2, 3]), ('A', list('cdef'))], name='foo') expected = arr.to_series() actual = arr.to_dataframe()['foo'] - assert_equal(expected.values, actual.values) - assert_equal(expected.name, actual.name) - assert_equal(expected.index.values, actual.index.values) + self.assertArrayEqual(expected.values, actual.values) + self.assertArrayEqual(expected.name, actual.name) + self.assertArrayEqual(expected.index.values, actual.index.values) # regression test for coords with different dimensions arr.coords['C'] = ('B', [-1, -2, -3]) @@ -2654,9 +2654,9 @@ def test_to_dataframe(self): expected['C'] = [-1] * 4 + [-2] * 4 + [-3] * 4 expected = expected[['C', 'foo']] actual = arr.to_dataframe() - assert_equal(expected.values, actual.values) - assert_equal(expected.columns.values, actual.columns.values) - assert_equal(expected.index.values, actual.index.values) + self.assertArrayEqual(expected.values, actual.values) + self.assertArrayEqual(expected.columns.values, actual.columns.values) + self.assertArrayEqual(expected.index.values, actual.index.values) arr.name = None # unnamed with raises_regex(ValueError, 'unnamed'): @@ -2666,8 +2666,8 @@ def test_to_pandas_name_matches_coordinate(self): # coordinate with same name as array arr = DataArray([1, 2, 3], dims='x', name='x') series = arr.to_series() - assert_equal([1, 2, 3], series.values) - assert_equal([0, 1, 2], series.index.values) + self.assertArrayEqual([1, 2, 3], series.values) + self.assertArrayEqual([0, 1, 2], series.index.values) assert 'x' == series.name assert 'x' == series.index.name @@ -2678,8 +2678,8 @@ def test_to_pandas_name_matches_coordinate(self): def test_to_and_from_series(self): expected = self.dv.to_dataframe()['foo'] actual = self.dv.to_series() - assert_equal(expected.values, actual.values) - assert_equal(expected.index.values, actual.index.values) + self.assertArrayEqual(expected.values, actual.values) + self.assertArrayEqual(expected.index.values, actual.index.values) assert 'foo' == actual.name # test roundtrip assert_identical( @@ -2790,17 +2790,17 @@ def test_to_masked_array(self): # Test round trip x_masked_2 = da.to_masked_array() da_2 = DataArray(x_masked_2) - assert_equal(x_masked, x_masked_2) - assert_equal(da, da_2) + self.assertArrayEqual(x_masked, x_masked_2) + self.assertDataArrayEqual(da, da_2) da_masked_array = da.to_masked_array(copy=True) assert isinstance(da_masked_array, np.ma.MaskedArray) # Test masks - assert_equal(da_masked_array.mask, x_masked.mask) + self.assertArrayEqual(da_masked_array.mask, x_masked.mask) # Test that mask is unpacked correctly - assert_equal(da.values, x_masked.filled(np.nan)) + self.assertArrayEqual(da.values, x_masked.filled(np.nan)) # Test that the underlying data (including nans) hasn't changed - assert_equal(da_masked_array, x_masked.filled(np.nan)) + self.assertArrayEqual(da_masked_array, x_masked.filled(np.nan)) # Test that copy=False gives access to values masked_array = da.to_masked_array(copy=False) @@ -2834,12 +2834,12 @@ def test_to_and_from_cdms2(self): expected_coords = [IndexVariable('distance', [-2, 2]), IndexVariable('time', [0, 1, 2])] actual = original.to_cdms2() - assert_equal(actual, original) + self.assertArrayEqual(actual, original) assert actual.id == original.name self.assertItemsEqual(actual.getAxisIds(), original.dims) for axis, coord in zip(actual.getAxisList(), expected_coords): assert axis.id == coord.name - assert_equal(axis, coord.values) + self.assertArrayEqual(axis, coord.values) assert actual.baz == original.attrs['baz'] component_times = actual.getAxis(1).asComponentTime() @@ -2876,7 +2876,7 @@ def test_to_and_from_iris(self): original.attrs['cell_methods'] = \ 'height: mean (comment: A cell method)' actual = original.to_iris() - assert_equal(actual.data, original.data) + self.assertArrayEqual(actual.data, original.data) assert actual.var_name == original.name self.assertItemsEqual([d.var_name for d in actual.dim_coords], original.dims) @@ -2948,7 +2948,7 @@ def test_to_and_from_iris_dask(self): # Be careful not to trigger the loading of the iris data actual_data = actual.core_data() if \ hasattr(actual, 'core_data') else actual.data - assert_equal(actual_data, original.data) + self.assertArrayEqual(actual_data, original.data) assert actual.var_name == original.name self.assertItemsEqual([d.var_name for d in actual.dim_coords], original.dims) @@ -3042,7 +3042,7 @@ def test_to_dataset_retains_keys(self): # convert to dateset and back again result = array.to_dataset('x').to_array(dim='x') - assert_equal(array, result) + self.assertDatasetEqual(array, result) def test__title_for_slice(self): array = DataArray(np.ones((4, 3, 2)), dims=['a', 'b', 'c'], @@ -3070,7 +3070,7 @@ def test_dataarray_diff_n1(self): da = DataArray(np.random.randn(3, 4), dims=['x', 'y']) actual = da.diff('y') expected = DataArray(np.diff(da.values, axis=1), dims=['x', 'y']) - assert_equal(expected, actual) + self.assertDataArrayEqual(expected, actual) def test_coordinate_diff(self): # regression test for GH634 @@ -3079,7 +3079,7 @@ def test_coordinate_diff(self): expected = DataArray([1] * 9, dims=['lon'], coords=[range(1, 10)], name='lon') actual = lon.diff('lon') - assert_equal(expected, actual) + self.assertDataArrayEqual(expected, actual) def test_shift(self): arr = DataArray([1, 2, 3], dims='x') @@ -3146,14 +3146,14 @@ def test_dot(self): actual = da.dot(dm) expected_vals = np.tensordot(da_vals, dm_vals, [2, 0]) expected = DataArray(expected_vals, coords=[x, y], dims=['x', 'y']) - assert_equal(expected, actual) + self.assertDataArrayEqual(expected, actual) # all shared dims actual = da.dot(da) expected_vals = np.tensordot(da_vals, da_vals, axes=([0, 1, 2], [0, 1, 2])) expected = DataArray(expected_vals) - assert_equal(expected, actual) + self.assertDataArrayEqual(expected, actual) # multiple shared dims dm_vals = np.arange(20 * 5 * 4).reshape((20, 5, 4)) @@ -3162,7 +3162,7 @@ def test_dot(self): actual = da.dot(dm) expected_vals = np.tensordot(da_vals, dm_vals, axes=([1, 2], [1, 2])) expected = DataArray(expected_vals, coords=[x, j], dims=['x', 'j']) - assert_equal(expected, actual) + self.assertDataArrayEqual(expected, actual) with pytest.raises(NotImplementedError): da.dot(dm.to_dataset(name='dm')) @@ -3183,7 +3183,7 @@ def test_binary_op_join_setting(self): missing_3, join=align_type) expected = xr.DataArray([np.nan, 2, 4, np.nan], [(dim, [0, 1, 2, 3])]) - assert_equal(actual, expected) + self.assertDataArrayEqual(actual, expected) def test_combine_first(self): ar0 = DataArray([[0, 0], [0, 0]], [('x', ['a', 'b']), ('y', [-1, 0])]) @@ -3193,17 +3193,17 @@ def test_combine_first(self): actual = ar0.combine_first(ar1) expected = DataArray([[0, 0, np.nan], [0, 0, 1], [np.nan, 1, 1]], [('x', ['a', 'b', 'c']), ('y', [-1, 0, 1])]) - assert_equal(actual, expected) + self.assertDataArrayEqual(actual, expected) actual = ar1.combine_first(ar0) expected = DataArray([[0, 0, np.nan], [0, 1, 1], [np.nan, 1, 1]], [('x', ['a', 'b', 'c']), ('y', [-1, 0, 1])]) - assert_equal(actual, expected) + self.assertDataArrayEqual(actual, expected) actual = ar0.combine_first(ar2) expected = DataArray([[0, 0], [0, 0], [2, 2]], [('x', ['a', 'b', 'd']), ('y', [-1, 0])]) - assert_equal(actual, expected) + self.assertDataArrayEqual(actual, expected) def test_sortby(self): da = DataArray([[1, 2], [3, 4], [5, 6]], @@ -3218,34 +3218,34 @@ def test_sortby(self): expected = sorted1d dax = DataArray([100, 99, 98], [('x', ['c', 'b', 'a'])]) actual = da.sortby(dax) - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) # test descending order sort actual = da.sortby(dax, ascending=False) - assert_equal(actual, da) + self.assertDatasetEqual(actual, da) # test alignment (fills in nan for 'c') dax_short = DataArray([98, 97], [('x', ['b', 'a'])]) actual = da.sortby(dax_short) - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) # test multi-dim sort by 1D dataarray values expected = sorted2d dax = DataArray([100, 99, 98], [('x', ['c', 'b', 'a'])]) day = DataArray([90, 80], [('y', [1, 0])]) actual = da.sortby([day, dax]) - assert_equal(actual, expected) + self.assertDataArrayEqual(actual, expected) if LooseVersion(np.__version__) < LooseVersion('1.11.0'): pytest.skip('numpy 1.11.0 or later to support object data-type.') expected = sorted1d actual = da.sortby('x') - assert_equal(actual, expected) + self.assertDataArrayEqual(actual, expected) expected = sorted2d actual = da.sortby(['x', 'y']) - assert_equal(actual, expected) + self.assertDataArrayEqual(actual, expected) @requires_bottleneck def test_rank(self): @@ -3253,8 +3253,8 @@ def test_rank(self): ar = DataArray([[3, 4, np.nan, 1]]) expect_0 = DataArray([[1, 1, np.nan, 1]]) expect_1 = DataArray([[2, 3, np.nan, 1]]) - assert_equal(ar.rank('dim_0'), expect_0) - assert_equal(ar.rank('dim_1'), expect_1) + self.assertDataArrayEqual(ar.rank('dim_0'), expect_0) + self.assertDataArrayEqual(ar.rank('dim_1'), expect_1) # int x = DataArray([3, 2, 1]) self.assertDataArrayEqual(x.rank('dim_0'), x) @@ -3264,7 +3264,7 @@ def test_rank(self): x = DataArray([3.0, 1.0, np.nan, 2.0, 4.0], dims=('z',)) y = DataArray([0.75, 0.25, np.nan, 0.5, 1.0], dims=('z',)) - assert_equal(y.rank('z', pct=True), y) + self.assertDataArrayEqual(y.rank('z', pct=True), y) @pytest.fixture(params=[1]) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 33ffce571ac..53dd67ca698 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -324,14 +324,14 @@ def test_constructor_pandas_sequence(self): ) ds_based_on_pandas = Dataset(pandas_objs, ds.coords, attrs=ds.attrs) del ds_based_on_pandas['x'] - assert_equal(ds, ds_based_on_pandas) + self.assertDatasetEqual(ds, ds_based_on_pandas) # reindex pandas obj, check align works rearranged_index = reversed(pandas_objs['foo'].index) pandas_objs['foo'] = pandas_objs['foo'].reindex(rearranged_index) ds_based_on_pandas = Dataset(pandas_objs, ds.coords, attrs=ds.attrs) del ds_based_on_pandas['x'] - assert_equal(ds, ds_based_on_pandas) + self.assertDatasetEqual(ds, ds_based_on_pandas) def test_constructor_pandas_single(self): @@ -345,7 +345,7 @@ def test_constructor_pandas_single(self): pandas_obj = a.to_pandas() ds_based_on_pandas = Dataset(pandas_obj) for dim in ds_based_on_pandas.data_vars: - assert_equal(ds_based_on_pandas[dim], pandas_obj[dim]) + self.assertArrayEqual(ds_based_on_pandas[dim], pandas_obj[dim]) def test_constructor_compat(self): data = OrderedDict([('x', DataArray(0, coords={'y': 1})), @@ -477,7 +477,7 @@ def test_variable(self): a['bar'] = (('time', 'x',), d) # order of creation is preserved assert list(a.variables) == ['foo', 'bar'] - assert_equal(a['foo'].values, d) + self.assertArrayEqual(a['foo'].values, d) # try to add variable with dim (10,3) with data that's (3,10) with pytest.raises(ValueError): a['qux'] = (('time', 'x'), d.T) @@ -555,11 +555,11 @@ def test_coords_modify(self): actual = data.copy(deep=True) actual.coords['x'] = ('x', ['a', 'b']) - assert_equal(actual['x'], ['a', 'b']) + self.assertArrayEqual(actual['x'], ['a', 'b']) actual = data.copy(deep=True) actual.coords['z'] = ('z', ['a', 'b']) - assert_equal(actual['z'], ['a', 'b']) + self.assertArrayEqual(actual['z'], ['a', 'b']) actual = data.copy(deep=True) with raises_regex(ValueError, 'conflicting sizes'): @@ -984,10 +984,10 @@ def test_isel_fancy(self): stations['dim2s'].variable] expected_var3 = data['var3'].variable[slice(None), stations['dim1s'].variable] - assert_equal(actual['a'].drop('dim2'), stations['a']) - assert_equal(actual['var1'], expected_var1) - assert_equal(actual['var2'], expected_var2) - assert_equal(actual['var3'], expected_var3) + self.assertDataArrayEqual(actual['a'].drop('dim2'), stations['a']) + self.assertArrayEqual(actual['var1'], expected_var1) + self.assertArrayEqual(actual['var2'], expected_var2) + self.assertArrayEqual(actual['var3'], expected_var3) def test_isel_dataarray(self): """ Test for indexing by DataArray """ @@ -1074,30 +1074,30 @@ def test_sel(self): loc_slicers = {'dim1': slice(None, None, 2), 'dim2': slice(0, 0.5), 'dim3': slice('a', 'c')} - assert_equal(data.isel(**int_slicers), + self.assertDatasetEqual(data.isel(**int_slicers), data.sel(**loc_slicers)) data['time'] = ('time', pd.date_range('2000-01-01', periods=20)) - assert_equal(data.isel(time=0), + self.assertDatasetEqual(data.isel(time=0), data.sel(time='2000-01-01')) - assert_equal(data.isel(time=slice(10)), + self.assertDatasetEqual(data.isel(time=slice(10)), data.sel(time=slice('2000-01-01', '2000-01-10'))) - assert_equal(data, data.sel(time=slice('1999', '2005'))) + self.assertDatasetEqual(data, data.sel(time=slice('1999', '2005'))) times = pd.date_range('2000-01-01', periods=3) - assert_equal(data.isel(time=slice(3)), + self.assertDatasetEqual(data.isel(time=slice(3)), data.sel(time=times)) - assert_equal(data.isel(time=slice(3)), + self.assertDatasetEqual(data.isel(time=slice(3)), data.sel(time=(data['time.dayofyear'] <= 3))) td = pd.to_timedelta(np.arange(3), unit='days') data = Dataset({'x': ('td', np.arange(3)), 'td': td}) - assert_equal(data, data.sel(td=td)) - assert_equal(data, data.sel(td=slice('3 days'))) - assert_equal(data.isel(td=0), + self.assertDatasetEqual(data, data.sel(td=td)) + self.assertDatasetEqual(data, data.sel(td=slice('3 days'))) + self.assertDatasetEqual(data.isel(td=0), data.sel(td=pd.Timedelta('0 days'))) - assert_equal(data.isel(td=0), + self.assertDatasetEqual(data.isel(td=0), data.sel(td=pd.Timedelta('0h'))) - assert_equal(data.isel(td=slice(1, 3)), + self.assertDatasetEqual(data.isel(td=slice(1, 3)), data.sel(td=slice('1 days', '2 days'))) def test_sel_dataarray(self): @@ -1105,14 +1105,14 @@ def test_sel_dataarray(self): ind = DataArray([0.0, 0.5, 1.0], dims=['dim2']) actual = data.sel(dim2=ind) - assert_equal(actual, data.isel(dim2=[0, 1, 2])) + self.assertDatasetEqual(actual, data.isel(dim2=[0, 1, 2])) # with different dimension ind = DataArray([0.0, 0.5, 1.0], dims=['new_dim']) actual = data.sel(dim2=ind) expected = data.isel(dim2=Variable('new_dim', [0, 1, 2])) assert 'new_dim' in actual.dims - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) # Multi-dimensional ind = DataArray([[0.0], [0.5], [1.0]], dims=['new_dim', 'new_dim2']) @@ -1121,7 +1121,7 @@ def test_sel_dataarray(self): [[0], [1], [2]])) assert 'new_dim' in actual.dims assert 'new_dim2' in actual.dims - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) # with coordinate ind = DataArray([0.0, 0.5, 1.0], dims=['new_dim'], @@ -1130,9 +1130,9 @@ def test_sel_dataarray(self): expected = data.isel(dim2=[0, 1, 2]).rename({'dim2': 'new_dim'}) assert 'new_dim' in actual.dims assert 'new_dim' in actual.coords - assert_equal(actual.drop('new_dim').drop('dim2'), + self.assertDatasetEqual(actual.drop('new_dim').drop('dim2'), expected.drop('new_dim')) - assert_equal(actual['new_dim'].drop('dim2'), + self.assertDataArrayEqual(actual['new_dim'].drop('dim2'), ind['new_dim']) # with conflicted coordinate (silently ignored) @@ -1140,18 +1140,18 @@ def test_sel_dataarray(self): coords={'dim2': ['a', 'b', 'c']}) actual = data.sel(dim2=ind) expected = data.isel(dim2=[0, 1, 2]) - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) # with conflicted coordinate (silently ignored) ind = DataArray([0.0, 0.5, 1.0], dims=['new_dim'], coords={'new_dim': ['a', 'b', 'c'], 'dim2': 3}) actual = data.sel(dim2=ind) - assert_equal(actual['new_dim'].drop('dim2'), + self.assertDataArrayEqual(actual['new_dim'].drop('dim2'), ind['new_dim'].drop('dim2')) expected = data.isel(dim2=[0, 1, 2]) expected['dim2'] = (('new_dim'), expected['dim2'].values) - assert_equal(actual['dim2'].drop('new_dim'), + self.assertDataArrayEqual(actual['dim2'].drop('new_dim'), expected['dim2']) assert actual['var1'].dims == ('dim1', 'new_dim') @@ -1162,7 +1162,7 @@ def test_sel_dataarray(self): 'new_dim': ('dim2', [1.1, 1.2, 1.3])}) actual = data.sel(dim2=ind) expected = data.isel(dim2=[0, 1, 2]) - assert_equal(actual.drop('new_dim'), expected) + self.assertDatasetEqual(actual.drop('new_dim'), expected) assert np.allclose(actual['new_dim'].values, ind['new_dim'].values) def test_sel_dataarray_mindex(self): @@ -1401,7 +1401,7 @@ def test_sel_fancy(self): idx_y = DataArray([0, 2, 1], dims=['b'], coords={'b': [0, 3, 6]}) expected_ary = data['foo'][[0, 1, 2], [0, 2, 1]] actual = data.sel(x=idx_x, y=idx_y) - assert_equal(expected_ary, actual['foo']) + self.assertArrayEqual(expected_ary, actual['foo']) assert_identical(actual['a'].drop('x'), idx_x['a']) assert_identical(actual['b'].drop('y'), idx_y['b']) @@ -1622,7 +1622,7 @@ def test_align(self): union = list('abcdefghijkl') left2, right2 = align(left, right, join='inner') - assert_equal(left2['dim3'], intersection) + self.assertArrayEqual(left2['dim3'], intersection) assert_identical(left2, right2) left2, right2 = align(left, right, join='outer') @@ -1639,8 +1639,6 @@ def test_align(self): assert_equal(left2['dim3'].variable, right2['dim3'].variable) assert_equal(left2['dim3'].variable, left['dim3'].variable) - self.assertDatasetIdentical(left2.sel(dim3=intersection), - assert_identical(left2.sel(dim3=intersection), right2.sel(dim3=intersection)) assert np.isnan(right2['var3'][:2]).all() @@ -1810,13 +1808,13 @@ def test_variable_indexing(self): v = data['var1'] d1 = data['dim1'] d2 = data['dim2'] - assert_equal(v, v[d1.values]) - assert_equal(v, v[d1]) - assert_equal(v[:3], v[d1 < 3]) - assert_equal(v[:, 3:], v[:, d2 >= 1.5]) - assert_equal(v[:3, 3:], v[d1 < 3, d2 >= 1.5]) - assert_equal(v[:3, :2], v[range(3), range(2)]) - assert_equal(v[:3, :2], v.loc[d1[:3], d2[:2]]) + self.assertVariableEqual(v, v[d1.values]) + self.assertVariableEqual(v, v[d1]) + self.assertVariableEqual(v[:3], v[d1 < 3]) + self.assertVariableEqual(v[:, 3:], v[:, d2 >= 1.5]) + self.assertVariableEqual(v[:3, 3:], v[d1 < 3, d2 >= 1.5]) + self.assertVariableEqual(v[:3, :2], v[range(3), range(2)]) + self.assertVariableEqual(v[:3, :2], v.loc[d1[:3], d2[:2]]) def test_drop_variables(self): data = create_test_data() @@ -1890,7 +1888,7 @@ def test_rename(self): if name in dims: dims[dims.index(name)] = newname - assert_equal(Variable(dims, v.values, v.attrs), + self.assertVariableEqual(Variable(dims, v.values, v.attrs), renamed[k].variable.to_base_variable()) assert v.encoding == renamed[k].encoding assert type(v) == type(renamed.variables[k]) @@ -1937,7 +1935,7 @@ def test_rename_inplace(self): assert not data.equals(copied) assert data.dims == {'y': 3, 't': 3} # check virtual variables - assert_equal(data['t.dayofyear'], [1, 2, 3]) + self.assertArrayEqual(data['t.dayofyear'], [1, 2, 3]) def test_swap_dims(self): original = Dataset({'x': [1, 2, 3], 'y': ('x', list('abc')), 'z': 42}) @@ -2153,7 +2151,7 @@ def test_update_auto_align(self): def test_getitem(self): data = create_test_data() assert isinstance(data['var1'], DataArray) - assert_equal(data['var1'].variable, data.variables['var1']) + self.assertVariableEqual(data['var1'].variable, data.variables['var1']) with pytest.raises(KeyError): data['notfound'] with pytest.raises(KeyError): @@ -2161,7 +2159,7 @@ def test_getitem(self): actual = data[['var1', 'var2']] expected = Dataset({'var1': data['var1'], 'var2': data['var2']}) - assert_equal(expected, actual) + self.assertDatasetEqual(expected, actual) actual = data['numbers'] expected = DataArray(data['numbers'].variable, @@ -2199,18 +2197,18 @@ def test_virtual_variables_time(self): data = create_test_data() expected = DataArray(1 + np.arange(20), coords=[data['time']], dims='time', name='dayofyear') - assert_identical(expected, data['time.dayofyear']) - assert_equal(data['time.month'].values, + + self.assertArrayEqual(data['time.month'].values, data.variables['time'].to_index().month) - assert_equal(data['time.season'].values, 'DJF') + self.assertArrayEqual(data['time.season'].values, 'DJF') # test virtual variable math - assert_equal(data['time.dayofyear'] + 1, 2 + np.arange(20)) - assert_equal(np.sin(data['time.dayofyear']), + self.assertArrayEqual(data['time.dayofyear'] + 1, 2 + np.arange(20)) + self.assertArrayEqual(np.sin(data['time.dayofyear']), np.sin(1 + np.arange(20))) # ensure they become coordinates expected = Dataset({}, {'dayofyear': data['time.dayofyear']}) actual = data[['time.dayofyear']] - assert_equal(expected, actual) + self.assertDatasetEqual(expected, actual) # non-coordinate variables ds = Dataset({'t': ('x', pd.date_range('2000-01-01', periods=3))}) assert (ds['t.year'] == 2000).all() @@ -2249,9 +2247,9 @@ def test_time_season(self): def test_slice_virtual_variable(self): data = create_test_data() - assert_equal(data['time.dayofyear'][:10].variable, + self.assertVariableEqual(data['time.dayofyear'][:10].variable, Variable(['time'], 1 + np.arange(10))) - assert_equal(data['time.dayofyear'][0].variable, Variable([], 1)) + self.assertVariableEqual(data['time.dayofyear'][0].variable, Variable([], 1)) def test_setitem(self): # assign a variable @@ -2286,7 +2284,7 @@ def test_setitem(self): data1['dim1'] = data1['dim1'][:5] # override an existing value data1['A'] = 3 * data2['A'] - assert_equal(data1['A'], 3 * data2['A']) + self.assertVariableEqual(data1['A'], 3 * data2['A']) with pytest.raises(NotImplementedError): data1[{'x': 0}] = 0 @@ -2298,7 +2296,7 @@ def test_setitem_pandas(self): ds_copy = ds.copy() ds_copy['bar'] = ds['bar'].to_pandas() - assert_equal(ds, ds_copy) + self.assertDatasetEqual(ds, ds_copy) def test_setitem_auto_align(self): ds = Dataset() @@ -2473,14 +2471,14 @@ def test_groupby(self): ('c', data.isel(x=2))] for actual, expected in zip(groupby, expected_items): assert actual[0] == expected[0] - assert_equal(actual[1], expected[1]) + self.assertDatasetEqual(actual[1], expected[1]) def identity(x): return x for k in ['x', 'c', 'y']: actual = data.groupby(k, squeeze=False).apply(identity) - assert_equal(data, actual) + self.assertDatasetEqual(data, actual) def test_groupby_returns_new_type(self): data = Dataset({'z': (['x', 'y'], np.random.randn(3, 5))}) @@ -2497,9 +2495,9 @@ def test_groupby_iter(self): data = create_test_data() for n, (t, sub) in enumerate(list(data.groupby('dim1'))[:3]): assert data['dim1'][n] == t - assert_equal(data['var1'][n], sub['var1']) - assert_equal(data['var2'][n], sub['var2']) - assert_equal(data['var3'][:, n], sub['var3']) + self.assertVariableEqual(data['var1'][n], sub['var1']) + self.assertVariableEqual(data['var2'][n], sub['var2']) + self.assertVariableEqual(data['var3'][:, n], sub['var3']) def test_groupby_errors(self): data = create_test_data() @@ -2561,10 +2559,10 @@ def reorder_dims(x): expected = ((ds + Variable('dim3', np.zeros(10))) .transpose('dim3', 'dim1', 'dim2', 'time')) actual = grouped + zeros - assert_equal(expected, actual) + self.assertDatasetEqual(expected, actual) actual = zeros + grouped - assert_equal(expected, actual) + self.assertDatasetEqual(expected, actual) with raises_regex(ValueError, 'incompat.* grouped binary'): grouped + ds @@ -2631,10 +2629,10 @@ def test_resample_and_first(self): for how in ['mean', 'sum', 'first', 'last', ]: method = getattr(actual, how) result = method() - assert_equal(expected, result) + self.assertDatasetEqual(expected, result) for method in [np.mean, ]: result = actual.reduce(method) - assert_equal(expected, result) + self.assertDatasetEqual(expected, result) def test_resample_by_mean_with_keep_attrs(self): times = pd.date_range('2000-01-01', freq='6H', periods=10) @@ -3264,9 +3262,9 @@ def test_reduce(self): actual = data.max() expected = Dataset(dict((k, v.max()) for k, v in iteritems(data.data_vars))) - assert_equal(expected, actual) + self.assertDatasetEqual(expected, actual) - assert_equal(data.min(dim=['dim1']), + self.assertDatasetEqual(data.min(dim=['dim1']), data.min(dim='dim1')) for reduct, expected in [('dim2', ['dim1', 'dim3', 'time']), @@ -3277,7 +3275,7 @@ def test_reduce(self): print(reduct, actual, expected) self.assertItemsEqual(actual, expected) - assert_equal(data.mean(dim=[]), data) + self.assertDatasetEqual(data.mean(dim=[]), data) # uint support data = xr.Dataset({'a': (('x', 'y'), @@ -3326,8 +3324,8 @@ def test_reduce_non_numeric(self): data1[v] = (dims, data, {'foo': 'variable'}) assert 'var4' not in data1.mean() - assert_equal(data1.mean(), data2.mean()) - assert_equal(data1.mean(dim='dim1'), + self.assertDatasetEqual(data1.mean(), data2.mean()) + self.assertDatasetEqual(data1.mean(dim='dim1'), data2.mean(dim='dim1')) def test_reduce_strings(self): @@ -3452,7 +3450,7 @@ def test_rank(self): # same as dataarray version x = z.var3 y = ds.var3.rank('dim3') - assert_equal(x, y) + self.assertDataArrayEqual(x, y) # coordinates stick self.assertItemsEqual(list(z.coords), list(ds.coords)) self.assertItemsEqual(list(x.coords), list(y.coords)) @@ -3588,7 +3586,7 @@ def test_dataset_math_auto_align(self): actual = ds.isel(y=slice(1)) + ds.isel(y=slice(1, None)) expected = 2 * ds.drop(ds.y, dim='y') - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) actual = ds + ds[['bar']] expected = (2 * ds[['bar']]).merge(ds.coords) @@ -3675,17 +3673,17 @@ def test_dataset_diff_n1_simple(self): ds = Dataset({'foo': ('x', [5, 5, 6, 6])}) actual = ds.diff('x') expected = Dataset({'foo': ('x', [0, 1, 0])}) - assert_equal(expected, actual) + self.assertDatasetEqual(expected, actual) def test_dataset_diff_n1_label(self): ds = Dataset({'foo': ('x', [5, 5, 6, 6])}, {'x': [0, 1, 2, 3]}) actual = ds.diff('x', label='lower') expected = Dataset({'foo': ('x', [0, 1, 0])}, {'x': [0, 1, 2]}) - assert_equal(expected, actual) + self.assertDatasetEqual(expected, actual) actual = ds.diff('x', label='upper') expected = Dataset({'foo': ('x', [0, 1, 0])}, {'x': [1, 2, 3]}) - assert_equal(expected, actual) + self.assertDatasetEqual(expected, actual) def test_dataset_diff_n1(self): ds = create_test_data(seed=1) @@ -3700,7 +3698,7 @@ def test_dataset_diff_n1(self): expected['var3'] = ds['var3'] expected = Dataset(expected, coords={'time': ds['time'].values}) expected.coords['numbers'] = ('dim3', ds['numbers'].values) - assert_equal(expected, actual) + self.assertDatasetEqual(expected, actual) def test_dataset_diff_n2(self): ds = create_test_data(seed=1) @@ -3715,7 +3713,7 @@ def test_dataset_diff_n2(self): expected['var3'] = ds['var3'] expected = Dataset(expected, coords={'time': ds['time'].values}) expected.coords['numbers'] = ('dim3', ds['numbers'].values) - assert_equal(expected, actual) + self.assertDatasetEqual(expected, actual) def test_dataset_diff_exception_n_neg(self): ds = create_test_data(seed=1) @@ -3815,29 +3813,29 @@ def test_binary_op_join_setting(self): with xr.set_options(arithmetic_join='outer'): actual = missing_2 + missing_0 expected = xr.Dataset({'x': [0, 1, 2]}) - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) # arithmetic join also applies to data_vars ds1 = xr.Dataset({'foo': 1, 'bar': 2}) ds2 = xr.Dataset({'bar': 2, 'baz': 3}) expected = xr.Dataset({'bar': 4}) # default is inner joining actual = ds1 + ds2 - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) with xr.set_options(arithmetic_join='outer'): expected = xr.Dataset({'foo': np.nan, 'bar': 4, 'baz': np.nan}) actual = ds1 + ds2 - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) with xr.set_options(arithmetic_join='left'): expected = xr.Dataset({'foo': np.nan, 'bar': 4}) actual = ds1 + ds2 - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) with xr.set_options(arithmetic_join='right'): expected = xr.Dataset({'bar': 4, 'baz': np.nan}) actual = ds1 + ds2 - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) def test_full_like(self): # For more thorough tests, see test_variable.py @@ -3872,15 +3870,15 @@ def test_combine_first(self): expected = Dataset({'dsx0': ('x', [0, 0, np.nan]), 'dsx1': ('x', [np.nan, 1, 1])}, coords={'x': ['a', 'b', 'c']}) - assert_equal(actual, expected) - assert_equal(actual, xr.merge([dsx0, dsx1])) + self.assertDatasetEqual(actual, expected) + self.assertDatasetEqual(actual, xr.merge([dsx0, dsx1])) # works just like xr.merge([self, other]) dsy2 = DataArray([2, 2, 2], [('x', ['b', 'c', 'd'])]).to_dataset(name='dsy2') actual = dsx0.combine_first(dsy2) expected = xr.merge([dsy2, dsx0]) - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) def test_sortby(self): ds = Dataset({'A': DataArray([[1, 2], [3, 4], [5, 6]], @@ -3904,16 +3902,16 @@ def test_sortby(self): expected = sorted1d dax = DataArray([100, 99, 98], [('x', ['c', 'b', 'a'])]) actual = ds.sortby(dax) - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) # test descending order sort actual = ds.sortby(dax, ascending=False) - assert_equal(actual, ds) + self.assertDatasetEqual(actual, ds) # test alignment (fills in nan for 'c') dax_short = DataArray([98, 97], [('x', ['b', 'a'])]) actual = ds.sortby(dax_short) - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) # test 1-D lexsort # dax0 is sorted first to give indices of [1, 2, 0] @@ -3921,13 +3919,13 @@ def test_sortby(self): dax0 = DataArray([100, 95, 95], [('x', ['c', 'b', 'a'])]) dax1 = DataArray([0, 1, 0], [('x', ['c', 'b', 'a'])]) actual = ds.sortby([dax0, dax1]) # lexsort underneath gives [2, 1, 0] - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) expected = sorted2d # test multi-dim sort by 1D dataarray values day = DataArray([90, 80], [('y', [1, 0])]) actual = ds.sortby([day, dax]) - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) # test exception-raising with pytest.raises(KeyError) as excinfo: @@ -3942,7 +3940,7 @@ def test_sortby(self): expected = sorted1d actual = ds.sortby('x') - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) # test pandas.MultiIndex indices = (('b', 1), ('b', 0), ('a', 1), ('a', 0)) @@ -3959,26 +3957,26 @@ def test_sortby(self): ('y', [1, 0])]), 'B': DataArray([[11, 12], [9, 10], [7, 8], [5, 6]], dims=['x', 'y'])}) - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) # multi-dim sort by coordinate objects expected = sorted2d actual = ds.sortby(['x', 'y']) - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) # test descending order sort actual = ds.sortby(['x', 'y'], ascending=False) - assert_equal(actual, ds) + self.assertDatasetEqual(actual, ds) def test_attribute_access(self): ds = create_test_data(seed=1) for key in ['var1', 'var2', 'var3', 'time', 'dim1', 'dim2', 'dim3', 'numbers']: - assert_equal(ds[key], getattr(ds, key)) + self.assertDataArrayEqual(ds[key], getattr(ds, key)) assert key in dir(ds) for key in ['dim3', 'dim1', 'numbers']: - assert_equal(ds['var3'][key], getattr(ds.var3, key)) + self.assertDataArrayEqual(ds['var3'][key], getattr(ds.var3, key)) assert key in dir(ds['var3']) # attrs assert ds['var3'].attrs['foo'] == ds.var3.foo diff --git a/xarray/tests/test_duck_array_ops.py b/xarray/tests/test_duck_array_ops.py index 82e5cdd3020..6f5fa2f306b 100644 --- a/xarray/tests/test_duck_array_ops.py +++ b/xarray/tests/test_duck_array_ops.py @@ -32,15 +32,15 @@ def test_first(self): for axis, expected in zip([0, 1, 2, -3, -2, -1], 2 * expected_results): actual = first(self.x, axis) - assert_equal(expected, actual) + self.assertArrayEqual(expected, actual) expected = self.x[0] actual = first(self.x, axis=0, skipna=False) - assert_equal(expected, actual) + self.assertArrayEqual(expected, actual) expected = self.x[..., 0] actual = first(self.x, axis=-1, skipna=False) - assert_equal(expected, actual) + self.assertArrayEqual(expected, actual) with raises_regex(IndexError, 'out of bounds'): first(self.x, 3) @@ -56,15 +56,15 @@ def test_last(self): for axis, expected in zip([0, 1, 2, -3, -2, -1], 2 * expected_results): actual = last(self.x, axis) - assert_equal(expected, actual) + self.assertArrayEqual(expected, actual) expected = self.x[-1] actual = last(self.x, axis=0, skipna=False) - assert_equal(expected, actual) + self.assertArrayEqual(expected, actual) expected = self.x[..., -1] actual = last(self.x, axis=-1, skipna=False) - assert_equal(expected, actual) + self.assertArrayEqual(expected, actual) with raises_regex(IndexError, 'out of bounds'): last(self.x, 3) @@ -73,7 +73,7 @@ def test_count(self): assert 12 == count(self.x) expected = array([[1, 2, 3], [3, 2, 1]]) - assert_equal(expected, count(self.x, axis=-1)) + self.assertArrayEqual(expected, count(self.x, axis=-1)) def test_all_nan_arrays(self): assert np.isnan(mean([np.nan, np.nan])) diff --git a/xarray/tests/test_indexing.py b/xarray/tests/test_indexing.py index cb2428e9282..01bc1fb6816 100644 --- a/xarray/tests/test_indexing.py +++ b/xarray/tests/test_indexing.py @@ -32,8 +32,8 @@ def test_expanded_indexer(self): I[..., 0, :], I[y], I[y, y], I[..., y, y], I[..., 0, 1, 2, 3, 4]]: j = indexing.expanded_indexer(i, x.ndim) - assert_equal(x[i], x[j]) - assert_equal(self.set_to_zero(x, i), + self.assertArrayEqual(x[i], x[j]) + self.assertArrayEqual(self.set_to_zero(x, i), self.set_to_zero(x, j)) with raises_regex(IndexError, 'too many indices'): indexing.expanded_indexer(I[1, 2, 3], 2) @@ -100,8 +100,8 @@ def test_get_dim_indexers(self): def test_remap_label_indexers(self): def test_indexer(data, x, expected_pos, expected_idx=None): pos, idx = indexing.remap_label_indexers(data, {'x': x}) - assert_equal(pos.get('x'), expected_pos) - assert_equal(idx.get('x'), expected_idx) + self.assertArrayEqual(pos.get('x'), expected_pos) + self.assertArrayEqual(idx.get('x'), expected_idx) data = Dataset({'x': ('x', [1, 2, 3])}) mindex = pd.MultiIndex.from_product([['a', 'b'], [1, 2], [-1, -2]], @@ -147,7 +147,7 @@ def test_slice_slice(self): expected = x[i][j] new_slice = indexing.slice_slice(i, j, size=100) actual = x[new_slice] - assert_equal(expected, actual) + self.assertArrayEqual(expected, actual) def test_lazily_indexed_array(self): original = np.random.rand(10, 20, 30) @@ -170,7 +170,7 @@ def test_lazily_indexed_array(self): v_lazy[:, j, k][i], v_lazy[:, :, k][:, j][i]]: assert expected.shape == actual.shape - assert_equal(expected, actual) + self.assertArrayEqual(expected, actual) assert isinstance(actual._data, indexing.LazilyIndexedArray) @@ -190,7 +190,7 @@ def test_lazily_indexed_array(self): expected = np.asarray(v[i][j]) actual = v_lazy[i][j] assert expected.shape == actual.shape - assert_equal(expected, actual) + self.assertArrayEqual(expected, actual) assert isinstance(actual._data, indexing.LazilyIndexedArray) assert isinstance(actual._data.array, indexing.NumpyIndexingAdapter) @@ -201,8 +201,8 @@ def test_setitem(self): original = np.arange(10) wrapped = indexing.CopyOnWriteArray(original) wrapped[B[:]] = 0 - assert_equal(original, np.arange(10)) - assert_equal(wrapped, np.zeros(10)) + self.assertArrayEqual(original, np.arange(10)) + self.assertArrayEqual(wrapped, np.zeros(10)) def test_sub_array(self): original = np.arange(10) @@ -210,9 +210,9 @@ def test_sub_array(self): child = wrapped[B[:5]] assert isinstance(child, indexing.CopyOnWriteArray) child[B[:]] = 0 - assert_equal(original, np.arange(10)) - assert_equal(wrapped, np.arange(10)) - assert_equal(child, np.zeros(5)) + self.assertArrayEqual(original, np.arange(10)) + self.assertArrayEqual(wrapped, np.arange(10)) + self.assertArrayEqual(child, np.zeros(5)) def test_index_scalar(self): # regression test for GH1374 @@ -224,7 +224,7 @@ class TestMemoryCachedArray(TestCase): def test_wrapper(self): original = indexing.LazilyIndexedArray(np.arange(10)) wrapped = indexing.MemoryCachedArray(original) - assert_equal(wrapped, np.arange(10)) + self.assertArrayEqual(wrapped, np.arange(10)) assert isinstance(wrapped.array, indexing.NumpyIndexingAdapter) def test_sub_array(self): @@ -232,7 +232,7 @@ def test_sub_array(self): wrapped = indexing.MemoryCachedArray(original) child = wrapped[B[:5]] assert isinstance(child, indexing.MemoryCachedArray) - assert_equal(child, np.arange(5)) + self.assertArrayEqual(child, np.arange(5)) assert isinstance(child.array, indexing.NumpyIndexingAdapter) assert isinstance(wrapped.array, indexing.LazilyIndexedArray) @@ -240,7 +240,7 @@ def test_setitem(self): original = np.arange(10) wrapped = indexing.MemoryCachedArray(original) wrapped[B[:]] = 0 - assert_equal(original, np.zeros(10)) + self.assertArrayEqual(original, np.zeros(10)) def test_index_scalar(self): # regression test for GH1374 diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 7884e5a3202..10c53221a6a 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -158,10 +158,10 @@ def test_can_pass_in_axis(self): self.pass_in_axis(self.darray.plot) def test__infer_interval_breaks(self): - assert_equal([-0.5, 0.5, 1.5], _infer_interval_breaks([0, 1])) - assert_equal([-0.5, 0.5, 5.0, 9.5, 10.5], + self.assertArrayEqual([-0.5, 0.5, 1.5], _infer_interval_breaks([0, 1])) + self.assertArrayEqual([-0.5, 0.5, 5.0, 9.5, 10.5], _infer_interval_breaks([0, 1, 9, 10])) - assert_equal( + self.assertArrayEqual( pd.date_range('20000101', periods=4) - np.timedelta64(12, 'h'), _infer_interval_breaks(pd.date_range('20000101', periods=3))) @@ -193,7 +193,7 @@ def test_convenient_facetgrid(self): d.coords['z'] = list('abcd') g = d.plot(x='x', y='y', col='z', col_wrap=2, cmap='cool') - assert_equal(g.axes.shape, [2, 2]) + self.assertArrayEqual(g.axes.shape, [2, 2]) for ax in g.axes.flat: assert ax.has_data() @@ -250,7 +250,7 @@ def test_convenient_facetgrid_4d(self): d = DataArray(a, dims=['y', 'x', 'columns', 'rows']) g = d.plot(x='x', y='y', col='columns', row='rows') - assert_equal(g.axes.shape, [3, 2]) + self.assertArrayEqual(g.axes.shape, [3, 2]) for ax in g.axes.flat: assert ax.has_data() @@ -428,7 +428,7 @@ def test_list_levels(self): for wrap_levels in [list, np.array, pd.Index, DataArray]: cmap_params = _determine_cmap_params( data, levels=wrap_levels(orig_levels)) - assert_equal(cmap_params['levels'], orig_levels) + self.assertArrayEqual(cmap_params['levels'], orig_levels) def test_divergentcontrol(self): neg = self.data - 0.1 @@ -529,7 +529,7 @@ def test_build_discrete_cmap(self): assert ncmap.N == len(levels) - 1 assert len(ncmap.colors) == len(levels) - 1 assert cnorm.N == len(levels) - assert_equal(cnorm.boundaries, levels) + self.assertArrayEqual(cnorm.boundaries, levels) assert max(levels) == cnorm.vmax assert min(levels) == cnorm.vmin if filled: @@ -545,7 +545,7 @@ def test_discrete_colormap_list_of_levels(self): ('min', [2, 5, 10, 15])]: for kind in ['imshow', 'pcolormesh', 'contourf', 'contour']: primitive = getattr(self.darray.plot, kind)(levels=levels) - assert_equal(levels, primitive.norm.boundaries) + self.assertArrayEqual(levels, primitive.norm.boundaries) assert max(levels) == primitive.norm.vmax assert min(levels) == primitive.norm.vmin if kind != 'contour': @@ -840,7 +840,7 @@ def test_convenient_facetgrid(self): d = DataArray(a, dims=['y', 'x', 'z']) g = self.plotfunc(d, x='x', y='y', col='z', col_wrap=2) - assert_equal(g.axes.shape, [2, 2]) + self.assertArrayEqual(g.axes.shape, [2, 2]) for (y, x), ax in np.ndenumerate(g.axes): assert ax.has_data() if x == 0: @@ -854,7 +854,7 @@ def test_convenient_facetgrid(self): # Infering labels g = self.plotfunc(d, col='z', col_wrap=2) - assert_equal(g.axes.shape, [2, 2]) + self.assertArrayEqual(g.axes.shape, [2, 2]) for (y, x), ax in np.ndenumerate(g.axes): assert ax.has_data() if x == 0: @@ -871,7 +871,7 @@ def test_convenient_facetgrid_4d(self): d = DataArray(a, dims=['y', 'x', 'columns', 'rows']) g = self.plotfunc(d, x='x', y='y', col='columns', row='rows') - assert_equal(g.axes.shape, [3, 2]) + self.assertArrayEqual(g.axes.shape, [3, 2]) for ax in g.axes.flat: assert ax.has_data() @@ -1189,7 +1189,7 @@ def test_groups(self): upperleft_array = self.darray.loc[upperleft_dict] z0 = self.darray.isel(z=0) - assert_equal(upperleft_array, z0) + self.assertDataArrayEqual(upperleft_array, z0) @pytest.mark.slow def test_float_index(self): @@ -1245,19 +1245,19 @@ def test_can_set_norm(self): @pytest.mark.slow def test_figure_size(self): - assert_equal(self.g.fig.get_size_inches(), (10, 3)) + self.assertArrayEqual(self.g.fig.get_size_inches(), (10, 3)) g = xplt.FacetGrid(self.darray, col='z', size=6) - assert_equal(g.fig.get_size_inches(), (19, 6)) + self.assertArrayEqual(g.fig.get_size_inches(), (19, 6)) g = self.darray.plot.imshow(col='z', size=6) - assert_equal(g.fig.get_size_inches(), (19, 6)) + self.assertArrayEqual(g.fig.get_size_inches(), (19, 6)) g = xplt.FacetGrid(self.darray, col='z', size=4, aspect=0.5) - assert_equal(g.fig.get_size_inches(), (7, 4)) + self.assertArrayEqual(g.fig.get_size_inches(), (7, 4)) g = xplt.FacetGrid(self.darray, col='z', figsize=(9, 4)) - assert_equal(g.fig.get_size_inches(), (9, 4)) + self.assertArrayEqual(g.fig.get_size_inches(), (9, 4)) with raises_regex(ValueError, "cannot provide both"): g = xplt.plot(self.darray, row=2, col='z', figsize=(6, 4), size=6) diff --git a/xarray/tests/test_ufuncs.py b/xarray/tests/test_ufuncs.py index 0c87443432f..f3208bf8a39 100644 --- a/xarray/tests/test_ufuncs.py +++ b/xarray/tests/test_ufuncs.py @@ -17,7 +17,7 @@ def assertIdentical(self, a, b): try: assert a.identical(b), (a, b) except AttributeError: - assert_equal(a, b) + self.assertArrayEqual(a, b) def test_unary(self): args = [0, diff --git a/xarray/tests/test_utils.py b/xarray/tests/test_utils.py index 159cd708291..d3c2c9a50f5 100644 --- a/xarray/tests/test_utils.py +++ b/xarray/tests/test_utils.py @@ -8,7 +8,7 @@ from xarray.core import duck_array_ops, utils from xarray.core.pycompat import OrderedDict -from . import TestCase, requires_dask, assert_equal +from . import TestCase, requires_dask class TestAlias(TestCase): @@ -33,7 +33,7 @@ def test(self): (pd.Index(td, dtype=object), td.astype(object)), ]: actual = utils.safe_cast_to_index(array) - assert_equal(expected, actual) + self.assertArrayEqual(expected, actual) assert expected.dtype == actual.dtype diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index a64312f5422..7c9bb9de8a9 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -25,8 +25,7 @@ from xarray.core.utils import NDArrayMixin from . import ( - TestCase, source_ndarray, requires_dask, raises_regex, assert_identical, - assert_equal) + TestCase, source_ndarray, requires_dask, raises_regex, assert_identical) from xarray.tests import requires_bottleneck @@ -36,7 +35,7 @@ def test_properties(self): data = 0.5 * np.arange(10) v = self.cls(['time'], data, {'foo': 'bar'}) assert v.dims == ('time',) - assert_equal(v.values, data) + self.assertArrayEqual(v.values, data) assert v.dtype == float assert v.shape == (10,) assert v.size == 10 @@ -68,24 +67,24 @@ def test_getitem_1d(self): v_new = v[dict(x=[0, 1])] assert v_new.dims == ('x', ) - assert_equal(v_new, data[[0, 1]]) + self.assertArrayEqual(v_new, data[[0, 1]]) v_new = v[dict(x=slice(None))] assert v_new.dims == ('x', ) - assert_equal(v_new, data) + self.assertArrayEqual(v_new, data) v_new = v[dict(x=Variable('a', [0, 1]))] assert v_new.dims == ('a', ) - assert_equal(v_new, data[[0, 1]]) + self.assertArrayEqual(v_new, data[[0, 1]]) v_new = v[dict(x=1)] assert v_new.dims == () - assert_equal(v_new, data[1]) + self.assertArrayEqual(v_new, data[1]) # tuple argument v_new = v[slice(None)] assert v_new.dims == ('x', ) - assert_equal(v_new, data) + self.assertArrayEqual(v_new, data) def test_getitem_1d_fancy(self): v = self.cls(['x'], [0, 1, 2]) @@ -94,7 +93,7 @@ def test_getitem_1d_fancy(self): v_new = v[ind] assert v_new.dims == ('a', 'b') expected = np.array(v._data)[([0, 1], [0, 1]), ] - assert_equal(v_new, expected) + self.assertArrayEqual(v_new, expected) # boolean indexing ind = Variable(('x', ), [True, False, True]) @@ -219,9 +218,9 @@ def test_0d_object_array_with_list(self): listarray = np.empty((1,), dtype=object) listarray[0] = [1, 2, 3] x = self.cls('x', listarray) - assert_equal(x.data, listarray) - assert_equal(x[0].data, listarray.squeeze()) - assert_equal(x.squeeze().data, listarray.squeeze()) + self.assertArrayEqual(x.data, listarray) + self.assertArrayEqual(x[0].data, listarray.squeeze()) + self.assertArrayEqual(x.squeeze().data, listarray.squeeze()) def test_index_and_concat_datetime(self): # regression test for #125 @@ -234,7 +233,7 @@ def test_index_and_concat_datetime(self): [expected[[i]] for i in range(10)]]: actual = Variable.concat(times, 't') assert expected.dtype == actual.dtype - assert_equal(expected, actual) + self.assertArrayEqual(expected, actual) def test_0d_time_data(self): # regression test for #105 @@ -252,7 +251,7 @@ def test_datetime64_conversion(self): ]: v = self.cls(['t'], values) assert v.dtype == np.dtype('datetime64[ns]') - assert_equal(v.values, times.values) + self.assertArrayEqual(v.values, times.values) assert v.values.dtype == np.dtype('datetime64[ns]') same_source = source_ndarray(v.values) is source_ndarray(values) assert preserve_source == same_source @@ -267,7 +266,7 @@ def test_timedelta64_conversion(self): ]: v = self.cls(['t'], values) assert v.dtype == np.dtype('timedelta64[ns]') - assert_equal(v.values, times.values) + self.assertArrayEqual(v.values, times.values) assert v.values.dtype == np.dtype('timedelta64[ns]') same_source = source_ndarray(v.values) is source_ndarray(values) assert preserve_source == same_source @@ -301,31 +300,27 @@ def test_1d_math(self): # unary ops assert_identical(base_v, +v) assert_identical(base_v, abs(v)) - assert_equal((-v).values, -x) + self.assertArrayEqual((-v).values, -x) # binary ops with numbers assert_identical(base_v, v + 0) assert_identical(base_v, 0 + v) assert_identical(base_v, v * 1) - assert_equal((v > 2).values, x > 2) - assert_equal((0 == v).values, 0 == x) - assert_equal((v - 1).values, x - 1) - assert_equal((1 - v).values, 1 - x) # binary ops with numpy arrays - assert_equal((v * x).values, x ** 2) - assert_equal((x * v).values, x ** 2) - assert_equal(v - y, v - 1) - assert_equal(y - v, 1 - v) + self.assertArrayEqual((v * x).values, x ** 2) + self.assertArrayEqual((x * v).values, x ** 2) + self.assertArrayEqual(v - y, v - 1) + self.assertArrayEqual(y - v, 1 - v) # verify attributes are dropped v2 = self.cls(['x'], x, {'units': 'meters'}) assert_identical(base_v, +v2) # binary ops with all variables - assert_equal(v + v, 2 * v) + self.assertArrayEqual(v + v, 2 * v) w = self.cls(['x'], y, {'foo': 'bar'}) assert_identical(v + w, self.cls(['x'], x + y).to_base_variable()) assert_equal((v * w).values, x * y) # something complicated - assert_equal((v ** 2 * w - 1 + x).values, x ** 2 * y - 1 + x) + self.assertArrayEqual((v ** 2 * w - 1 + x).values, x ** 2 * y - 1 + x) # make sure dtype is preserved (for Index objects) assert float == (+v).dtype assert float == (+v).values.dtype @@ -348,9 +343,9 @@ def test_1d_reduce(self): def test_array_interface(self): x = np.arange(5) v = self.cls(['x'], x) - assert_equal(np.asarray(v), x) + self.assertArrayEqual(np.asarray(v), x) # test patched in methods - assert_equal(v.astype(float), x.astype(float)) + self.assertArrayEqual(v.astype(float), x.astype(float)) # think this is a break, that argsort changes the type assert_identical(v.argsort(), v.to_base_variable()) assert_identical(v.clip(2, 3), @@ -371,8 +366,8 @@ def example_1d_objects(self): def test___array__(self): for v, data in self.example_1d_objects(): - assert_equal(v.values, np.asarray(data)) - assert_equal(np.asarray(v), np.asarray(data)) + self.assertArrayEqual(v.values, np.asarray(data)) + self.assertArrayEqual(np.asarray(v), np.asarray(data)) assert v[0].values == np.asarray(data)[0] assert np.asarray(v[0]) == np.asarray(data)[0] @@ -464,7 +459,7 @@ def test_concat_fixed_len_str(self): actual = Variable.concat([x, y], 'animal') expected = Variable( 'animal', np.array(['horse', 'aardvark'], dtype=kind)) - assert_equal(expected, actual) + self.assertVariableEqual(expected, actual) def test_concat_number_strings(self): # regression test for #305 @@ -500,7 +495,7 @@ def test_copy_index(self): w = v.copy(deep=deep) assert isinstance(w._data, PandasIndexAdapter) assert isinstance(w.to_index(), pd.MultiIndex) - assert_equal(v._data.array, w._data.array) + self.assertArrayEqual(v._data.array, w._data.array) def test_real_and_imag(self): v = self.cls('x', np.arange(3) - 1j * np.arange(3), {'foo': 'bar'}) @@ -559,28 +554,28 @@ def test_getitem_advanced(self): # orthogonal indexing v_new = v[([0, 1], [1, 0])] assert v_new.dims == ('x', 'y') - assert_equal(v_new, v_data[[0, 1]][:, [1, 0]]) + self.assertArrayEqual(v_new, v_data[[0, 1]][:, [1, 0]]) v_new = v[[0, 1]] assert v_new.dims == ('x', 'y') - assert_equal(v_new, v_data[[0, 1]]) + self.assertArrayEqual(v_new, v_data[[0, 1]]) # with mixed arguments ind = Variable(['a'], [0, 1]) v_new = v[dict(x=[0, 1], y=ind)] assert v_new.dims == ('x', 'a') - assert_equal(v_new, v_data[[0, 1]][:, [0, 1]]) + self.assertArrayEqual(v_new, v_data[[0, 1]][:, [0, 1]]) # boolean indexing v_new = v[dict(x=[True, False], y=[False, True, False])] assert v_new.dims == ('x', 'y') - assert_equal(v_new, v_data[0][1]) + self.assertArrayEqual(v_new, v_data[0][1]) # with scalar variable ind = Variable((), 2) v_new = v[dict(y=ind)] expected = v[dict(y=2)] - assert_equal(v_new, expected) + self.assertArrayEqual(v_new, expected) # with boolean variable with wrong shape ind = np.array([True, False]) @@ -598,9 +593,9 @@ def test_getitem_uint_1d(self): v_data = v.compute().data v_new = v[np.array([0])] - assert_equal(v_new, v_data[0]) + self.assertArrayEqual(v_new, v_data[0]) v_new = v[np.array([0], dtype="uint64")] - assert_equal(v_new, v_data[0]) + self.assertArrayEqual(v_new, v_data[0]) def test_getitem_uint(self): # regression test for #1405 @@ -608,12 +603,12 @@ def test_getitem_uint(self): v_data = v.compute().data v_new = v[np.array([0])] - assert_equal(v_new, v_data[[0], :]) + self.assertArrayEqual(v_new, v_data[[0], :]) v_new = v[np.array([0], dtype="uint64")] - assert_equal(v_new, v_data[[0], :]) + self.assertArrayEqual(v_new, v_data[[0], :]) v_new = v[np.uint64(0)] - assert_equal(v_new, v_data[0, :]) + self.assertArrayEqual(v_new, v_data[0, :]) def test_getitem_0d_array(self): # make sure 0d-np.array can be used as an indexer @@ -621,7 +616,7 @@ def test_getitem_0d_array(self): v_data = v.compute().data v_new = v[np.array([0])[0]] - assert_equal(v_new, v_data[0]) + self.assertArrayEqual(v_new, v_data[0]) def test_getitem_fancy(self): v = self.cls(['x', 'y'], [[0, 1, 2], [3, 4, 5]]) @@ -630,59 +625,59 @@ def test_getitem_fancy(self): ind = Variable(['a', 'b'], [[0, 1, 1], [1, 1, 0]]) v_new = v[ind] assert v_new.dims == ('a', 'b', 'y') - assert_equal(v_new, v_data[[[0, 1, 1], [1, 1, 0]], :]) + self.assertArrayEqual(v_new, v_data[[[0, 1, 1], [1, 1, 0]], :]) # It would be ok if indexed with the multi-dimensional array including # the same name ind = Variable(['x', 'b'], [[0, 1, 1], [1, 1, 0]]) v_new = v[ind] assert v_new.dims == ('x', 'b', 'y') - assert_equal(v_new, v_data[[[0, 1, 1], [1, 1, 0]], :]) + self.assertArrayEqual(v_new, v_data[[[0, 1, 1], [1, 1, 0]], :]) ind = Variable(['a', 'b'], [[0, 1, 2], [2, 1, 0]]) v_new = v[dict(y=ind)] assert v_new.dims == ('x', 'a', 'b') - assert_equal(v_new, v_data[:, ([0, 1, 2], [2, 1, 0])]) + self.assertArrayEqual(v_new, v_data[:, ([0, 1, 2], [2, 1, 0])]) ind = Variable(['a', 'b'], [[0, 0], [1, 1]]) v_new = v[dict(x=[1, 0], y=ind)] assert v_new.dims == ('x', 'a', 'b') - assert_equal(v_new, v_data[[1, 0]][:, ind]) + self.assertArrayEqual(v_new, v_data[[1, 0]][:, ind]) # along diagonal ind = Variable(['a'], [0, 1]) v_new = v[ind, ind] assert v_new.dims == ('a',) - assert_equal(v_new, v_data[[0, 1], [0, 1]]) + self.assertArrayEqual(v_new, v_data[[0, 1], [0, 1]]) # with integer ind = Variable(['a', 'b'], [[0, 0], [1, 1]]) v_new = v[dict(x=0, y=ind)] assert v_new.dims == ('a', 'b') - assert_equal(v_new[0], v_data[0][[0, 0]]) - assert_equal(v_new[1], v_data[0][[1, 1]]) + self.assertArrayEqual(v_new[0], v_data[0][[0, 0]]) + self.assertArrayEqual(v_new[1], v_data[0][[1, 1]]) # with slice ind = Variable(['a', 'b'], [[0, 0], [1, 1]]) v_new = v[dict(x=slice(None), y=ind)] assert v_new.dims == ('x', 'a', 'b') - assert_equal(v_new, v_data[:, [[0, 0], [1, 1]]]) + self.assertArrayEqual(v_new, v_data[:, [[0, 0], [1, 1]]]) ind = Variable(['a', 'b'], [[0, 0], [1, 1]]) v_new = v[dict(x=ind, y=slice(None))] assert v_new.dims == ('a', 'b', 'y') - assert_equal(v_new, v_data[[[0, 0], [1, 1]], :]) + self.assertArrayEqual(v_new, v_data[[[0, 0], [1, 1]], :]) ind = Variable(['a', 'b'], [[0, 0], [1, 1]]) v_new = v[dict(x=ind, y=slice(None, 1))] assert v_new.dims == ('a', 'b', 'y') - assert_equal(v_new, v_data[[[0, 0], [1, 1]], slice(None, 1)]) + self.assertArrayEqual(v_new, v_data[[[0, 0], [1, 1]], slice(None, 1)]) # slice matches explicit dimension ind = Variable(['y'], [0, 1]) v_new = v[ind, :2] assert v_new.dims == ('y',) - assert_equal(v_new, v_data[[0, 1], [0, 1]]) + self.assertArrayEqual(v_new, v_data[[0, 1], [0, 1]]) # with multiple slices v = self.cls(['x', 'y', 'z'], [[[1, 2, 3], [4, 5, 6]]]) @@ -739,8 +734,8 @@ def setUp(self): def test_data_and_values(self): v = Variable(['time', 'x'], self.d) - assert_equal(v.data, self.d) - assert_equal(v.values, self.d) + self.assertArrayEqual(v.data, self.d) + self.assertArrayEqual(v.values, self.d) assert source_ndarray(v.values) is self.d with pytest.raises(ValueError): # wrong size @@ -1041,31 +1036,31 @@ def test_items(self): assert np.all(v.values == 0) # test orthogonal setting v[range(10), range(11)] = 1 - assert_equal(v.values, np.ones((10, 11))) + self.assertArrayEqual(v.values, np.ones((10, 11))) def test_getitem_basic(self): v = self.cls(['x', 'y'], [[0, 1, 2], [3, 4, 5]]) v_new = v[dict(x=0)] assert v_new.dims == ('y', ) - assert_equal(v_new, v._data[0]) + self.assertArrayEqual(v_new, v._data[0]) v_new = v[dict(x=0, y=slice(None))] assert v_new.dims == ('y', ) - assert_equal(v_new, v._data[0]) + self.assertArrayEqual(v_new, v._data[0]) v_new = v[dict(x=0, y=1)] assert v_new.dims == () - assert_equal(v_new, v._data[0, 1]) + self.assertArrayEqual(v_new, v._data[0, 1]) v_new = v[dict(y=1)] assert v_new.dims == ('x', ) - assert_equal(v_new, v._data[:, 1]) + self.assertArrayEqual(v_new, v._data[:, 1]) # tuple argument v_new = v[(slice(None), 1)] assert v_new.dims == ('x', ) - assert_equal(v_new, v._data[:, 1]) + self.assertArrayEqual(v_new, v._data[:, 1]) def test_getitem_with_mask_2d_input(self): v = Variable(('x', 'y'), [[0, 1, 2], [3, 4, 5]]) @@ -1154,7 +1149,7 @@ def test_roll_consistency(self): for shift in [-3, 0, 1, 7, 11]: expected = np.roll(v.values, shift, axis=axis) actual = v.roll(**{dim: shift}).values - assert_equal(expected, actual) + self.assertArrayEqual(expected, actual) def test_transpose(self): v = Variable(['time', 'x'], self.d) @@ -1346,7 +1341,7 @@ def test_inplace_math(self): assert v is v2 # since we provided an ndarray for data, it is also modified in-place assert source_ndarray(v.values) is x - assert_equal(v.values, np.arange(5) + 1) + self.assertArrayEqual(v.values, np.arange(5) + 1) with raises_regex(ValueError, 'dimensions cannot change'): v += Variable('y', np.arange(5)) @@ -1417,7 +1412,7 @@ def test_rank(self): # pct v = Variable(['x'], [3.0, 1.0, np.nan, 2.0, 4.0]) v_expect = Variable(['x'], [0.75, 0.25, np.nan, 0.5, 1.0]) - assert_equal(v.rank('x', pct=True), v_expect) + self.assertVariableEqual(v.rank('x', pct=True), v_expect) # invalid dim with raises_regex(ValueError, 'not found'): v.rank('y') @@ -1498,13 +1493,13 @@ def test_setitem(self): v = Variable(['x', 'y'], [[0, 3, 2], [3, 4, 5]]) v[dict(x=[0, 1])] = 1 - assert_equal(v[[0, 1]], np.ones_like(v[[0, 1]])) + self.assertArrayEqual(v[[0, 1]], np.ones_like(v[[0, 1]])) # boolean indexing v = Variable(['x', 'y'], [[0, 3, 2], [3, 4, 5]]) v[dict(x=[True, False])] = 1 - assert_equal(v[0], np.ones_like(v[0])) + self.assertArrayEqual(v[0], np.ones_like(v[0])) v = Variable(['x', 'y'], [[0, 3, 2], [3, 4, 5]]) v[dict(x=[True, False], y=[False, True, False])] = 1 assert v[0, 1] == 1 @@ -1516,7 +1511,7 @@ def assert_assigned_2d(array, key_x, key_y, values): expected[key_x, key_y] = values v = Variable(['x', 'y'], array) v[dict(x=key_x, y=key_y)] = values - assert_equal(expected, v) + self.assertArrayEqual(expected, v) # 1d vectorized indexing assert_assigned_2d(np.random.randn(4, 3), @@ -1572,8 +1567,8 @@ def assert_assigned_2d(array, key_x, key_y, values): v = Variable(['x', 'y'], [[0, 3, 2], [3, 4, 5]]) ind = Variable(['a'], [0, 1]) v[dict(x=ind)] = Variable(['a', 'y'], np.ones((2, 3), dtype=int) * 10) - assert_equal(v[0], np.ones_like(v[0]) * 10) - assert_equal(v[1], np.ones_like(v[1]) * 10) + self.assertArrayEqual(v[0], np.ones_like(v[0]) * 10) + self.assertArrayEqual(v[1], np.ones_like(v[1]) * 10) assert v.dims == ('x', 'y') # dimension should not change # increment @@ -1652,7 +1647,7 @@ def test_data(self): assert isinstance(x._data, PandasIndexAdapter) assert isinstance(x.data, np.ndarray) assert float == x.dtype - assert_equal(np.arange(3), x) + self.assertArrayEqual(np.arange(3), x) assert float == x.values.dtype with raises_regex(TypeError, 'cannot be modified'): x[:] = 0 @@ -1742,7 +1737,7 @@ def test_unchanged_types(self): def test_converted_types(self): for input_array in [[[0, 1, 2]], pd.DataFrame([[0, 1, 2]])]: actual = as_compatible_data(input_array) - assert_equal(np.asarray(input_array), actual) + self.assertArrayEqual(np.asarray(input_array), actual) assert np.ndarray == type(actual) assert np.asarray(input_array).dtype == actual.dtype @@ -1750,14 +1745,14 @@ def test_masked_array(self): original = np.ma.MaskedArray(np.arange(5)) expected = np.arange(5) actual = as_compatible_data(original) - assert_equal(expected, actual) + self.assertArrayEqual(expected, actual) assert np.dtype(int) == actual.dtype original = np.ma.MaskedArray(np.arange(5), mask=4 * [False] + [True]) expected = np.arange(5.0) expected[-1] = np.nan actual = as_compatible_data(original) - assert_equal(expected, actual) + self.assertArrayEqual(expected, actual) assert np.dtype(float) == actual.dtype def test_datetime(self): @@ -1811,7 +1806,7 @@ def check(actual, expect_dtype, expect_values): assert actual.dims == orig.dims assert actual.attrs == orig.attrs assert actual.chunks == orig.chunks - assert_equal(actual.values, expect_values) + self.assertArrayEqual(actual.values, expect_values) check(full_like(orig, 2), orig.dtype, np.full_like(orig.values, 2)) From 075ad3664ffe7c2281670e91149970b69fedcdb5 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 14:42:08 -0500 Subject: [PATCH 10/41] assert dataset & dataarray equal --- xarray/tests/__init__.py | 4 +- xarray/tests/test_accessors.py | 16 ++--- xarray/tests/test_backends.py | 28 ++++---- xarray/tests/test_combine.py | 6 +- xarray/tests/test_dask.py | 2 +- xarray/tests/test_dataarray.py | 84 +++++++++++----------- xarray/tests/test_dataset.py | 126 ++++++++++++++++----------------- xarray/tests/test_plot.py | 2 +- 8 files changed, 134 insertions(+), 134 deletions(-) diff --git a/xarray/tests/__init__.py b/xarray/tests/__init__.py index 7a87dbc859f..dc4530901d5 100644 --- a/xarray/tests/__init__.py +++ b/xarray/tests/__init__.py @@ -162,7 +162,7 @@ def assertAllClose(self, a1, a2, rtol=1e-05, atol=1e-8): __tracebackhide__ = True # noqa: F841 assert allclose_or_equiv(a1, a2, rtol=rtol, atol=atol) - def assertDatasetEqual(self, d1, d2): + def assert_equal(self, d1, d2): __tracebackhide__ = True # noqa: F841 assert_equal(d1, d2) @@ -180,7 +180,7 @@ def assertCoordinatesEqual(self, d1, d2): __tracebackhide__ = True # noqa: F841 assert_equal(d1, d2) - def assertDataArrayEqual(self, ar1, ar2): + def assert_equal(self, ar1, ar2): __tracebackhide__ = True # noqa: F841 assert_equal(ar1, ar2) diff --git a/xarray/tests/test_accessors.py b/xarray/tests/test_accessors.py index 4ebece774a5..cd3e2a3d7ea 100644 --- a/xarray/tests/test_accessors.py +++ b/xarray/tests/test_accessors.py @@ -37,10 +37,10 @@ def test_field_access(self): hours = xr.DataArray(self.times.hour, name='hour', coords=[self.times, ], dims=['time', ]) - self.assertDataArrayEqual(years, self.data.time.dt.year) - self.assertDataArrayEqual(months, self.data.time.dt.month) - self.assertDataArrayEqual(days, self.data.time.dt.day) - self.assertDataArrayEqual(hours, self.data.time.dt.hour) + self.assert_equal(years, self.data.time.dt.year) + self.assert_equal(months, self.data.time.dt.month) + self.assert_equal(days, self.data.time.dt.day) + self.assert_equal(hours, self.data.time.dt.hour) def test_not_datetime_type(self): nontime_data = self.data.copy() @@ -82,10 +82,10 @@ def test_dask_field_access(self): assert dask_hour.data.chunks == dask_chunks # Check the actual output from the accessors - self.assertDataArrayEqual(years, dask_year.compute()) - self.assertDataArrayEqual(months, dask_month.compute()) - self.assertDataArrayEqual(days, dask_day.compute()) - self.assertDataArrayEqual(hours, dask_hour.compute()) + self.assert_equal(years, dask_year.compute()) + self.assert_equal(months, dask_month.compute()) + self.assert_equal(days, dask_day.compute()) + self.assert_equal(hours, dask_hour.compute()) def test_seasons(self): dates = pd.date_range(start="2000/01/01", freq="M", periods=12) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index b3a43fba88e..7d22472db09 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -369,7 +369,7 @@ def test_roundtrip_example_1_netcdf(self): # will depend on the encoding used. For example, # without CF encoding 'actual' will end up with # a dtype attribute. - self.assertDatasetEqual(expected, actual) + self.assert_equal(expected, actual) def test_roundtrip_coordinates(self): original = Dataset({'foo': ('x', [0, 1])}, @@ -933,7 +933,7 @@ def test_compression_encoding(self): # regression test for #156 expected = data.isel(dim1=0) with self.roundtrip(expected) as actual: - self.assertDatasetEqual(expected, actual) + self.assert_equal(expected, actual) def test_encoding_chunksizes_unlimited(self): # regression test for GH1225 @@ -948,7 +948,7 @@ def test_encoding_chunksizes_unlimited(self): 'original_shape': (3,), } with self.roundtrip(ds) as actual: - self.assertDatasetEqual(ds, actual) + self.assert_equal(ds, actual) def test_mask_and_scale(self): with create_tmp_file() as tmp_file: @@ -1515,12 +1515,12 @@ def test_encoding_unlimited_dims(self): with self.roundtrip(ds, save_kwargs=dict(unlimited_dims=['y'])) as actual: assert actual.encoding['unlimited_dims'] == set('y') - self.assertDatasetEqual(ds, actual) + self.assert_equal(ds, actual) ds.encoding = {'unlimited_dims': ['y']} with self.roundtrip(ds) as actual: assert actual.encoding['unlimited_dims'] == set('y') - self.assertDatasetEqual(ds, actual) + self.assert_equal(ds, actual) class GenericNetCDFDataTestAutocloseTrue(GenericNetCDFDataTest): @@ -1548,7 +1548,7 @@ def test_array_type_after_indexing(self): def test_complex(self): expected = Dataset({'x': ('y', np.ones(5) + 1j * np.ones(5))}) with self.roundtrip(expected) as actual: - self.assertDatasetEqual(expected, actual) + self.assert_equal(expected, actual) @pytest.mark.xfail(reason='https://github.com/pydata/xarray/issues/535') def test_cross_engine_read_write_netcdf4(self): @@ -1578,11 +1578,11 @@ def test_encoding_unlimited_dims(self): with self.roundtrip(ds, save_kwargs=dict(unlimited_dims=['y'])) as actual: assert actual.encoding['unlimited_dims'] == set('y') - self.assertDatasetEqual(ds, actual) + self.assert_equal(ds, actual) ds.encoding = {'unlimited_dims': ['y']} with self.roundtrip(ds) as actual: assert actual.encoding['unlimited_dims'] == set('y') - self.assertDatasetEqual(ds, actual) + self.assert_equal(ds, actual) # tests pending h5netcdf fix @@ -2032,7 +2032,7 @@ def create_datasets(self, **kwargs): def test_cmp_local_file(self): with self.create_datasets() as (actual, expected): - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) # global attributes should be global attributes on the dataset assert 'NC_GLOBAL' not in actual.attrs @@ -2044,14 +2044,14 @@ def test_cmp_local_file(self): assert actual.attrs.keys() == expected.attrs.keys() with self.create_datasets() as (actual, expected): - self.assertDatasetEqual(actual.isel(l=2), expected.isel(l=2)) + self.assert_equal(actual.isel(l=2), expected.isel(l=2)) with self.create_datasets() as (actual, expected): - self.assertDatasetEqual(actual.isel(i=0, j=-1), + self.assert_equal(actual.isel(i=0, j=-1), expected.isel(i=0, j=-1)) with self.create_datasets() as (actual, expected): - self.assertDatasetEqual(actual.isel(j=slice(1, 2)), + self.assert_equal(actual.isel(j=slice(1, 2)), expected.isel(j=slice(1, 2))) def test_compatible_to_netcdf(self): @@ -2061,12 +2061,12 @@ def test_compatible_to_netcdf(self): actual.to_netcdf(tmp_file) actual = open_dataset(tmp_file) actual['bears'] = actual['bears'].astype(str) - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) @requires_dask def test_dask(self): with self.create_datasets(chunks={'j': 2}) as (actual, expected): - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) @network diff --git a/xarray/tests/test_combine.py b/xarray/tests/test_combine.py index 4ea55cd0fbd..ef3512588dc 100644 --- a/xarray/tests/test_combine.py +++ b/xarray/tests/test_combine.py @@ -66,9 +66,9 @@ def rectify_dim_order(dataset): self.assertArrayEqual(actual['extra'].values, expected) actual = concat(datasets, data[dim], coords='different') - self.assertDataArrayEqual(data['extra'], actual['extra']) + self.assert_equal(data['extra'], actual['extra']) actual = concat(datasets, data[dim], coords='minimal') - self.assertDataArrayEqual(data['extra'], actual['extra']) + self.assert_equal(data['extra'], actual['extra']) # verify that the dim argument takes precedence over # concatenating dataset variables of the same name @@ -248,7 +248,7 @@ def test_concat(self): expected = DataArray(np.array([foo.values, bar.values]), dims=['w', 'x', 'y'], coords={'x': [0, 1]}) actual = concat([foo, bar], 'w') - self.assertDataArrayEqual(expected, actual) + self.assert_equal(expected, actual) # from iteration: grouped = [g for _, g in foo.groupby('x')] stacked = concat(grouped, ds['x']) diff --git a/xarray/tests/test_dask.py b/xarray/tests/test_dask.py index c7d23c8e3e9..6b4e4255cf1 100644 --- a/xarray/tests/test_dask.py +++ b/xarray/tests/test_dask.py @@ -245,7 +245,7 @@ def assertLazyAndAllClose(self, expected, actual): self.assertLazyAnd(expected, actual, self.assertDataArrayAllClose) def assertLazyAndEqual(self, expected, actual): - self.assertLazyAnd(expected, actual, self.assertDataArrayEqual) + self.assertLazyAnd(expected, actual, self.assert_equal) def setUp(self): self.values = np.random.randn(4, 6) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index ea3134903be..b370a1a51c3 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -179,7 +179,7 @@ def test_name(self): copied = arr.copy() arr.name = 'bar' assert arr.name == 'bar' - self.assertDataArrayEqual(copied, arr) + self.assert_equal(copied, arr) actual = DataArray(IndexVariable('x', [3])) actual.name = 'y' @@ -311,16 +311,16 @@ def test_constructor_from_self_described(self): assert_identical(expected, actual) actual = DataArray(expected.values, actual.coords) - self.assertDataArrayEqual(expected, actual) + self.assert_equal(expected, actual) frame = pd.DataFrame(data, index=pd.Index(['a', 'b'], name='x'), columns=pd.Index([-1, -2], name='y')) actual = DataArray(frame) - self.assertDataArrayEqual(expected, actual) + self.assert_equal(expected, actual) series = pd.Series(data[0], index=pd.Index([-1, -2], name='y')) actual = DataArray(series) - self.assertDataArrayEqual(expected[0].reset_coords('x', drop=True), + self.assert_equal(expected[0].reset_coords('x', drop=True), actual) panel = pd.Panel({0: frame}) @@ -504,9 +504,9 @@ def test_getitem_dataarray(self): # boolean indexing ind = xr.DataArray([True, True, False], dims=['x']) - self.assertDataArrayEqual(da[ind], da[[0, 1], :]) - self.assertDataArrayEqual(da[ind], da[[0, 1]]) - self.assertDataArrayEqual(da[ind], da[ind.values]) + self.assert_equal(da[ind], da[[0, 1], :]) + self.assert_equal(da[ind], da[[0, 1]]) + self.assert_equal(da[ind], da[ind.values]) def test_setitem(self): # basic indexing should work as numpy's indexing @@ -780,7 +780,7 @@ def test_sel_dataarray(self): self.assertArrayEqual(actual, da.isel(x=[0, 1, 2])) assert 'new_dim' in actual.dims assert 'new_dim' in actual.coords - self.assertDataArrayEqual(actual['new_dim'].drop('x'), + self.assert_equal(actual['new_dim'].drop('x'), ind['new_dim']) def test_sel_no_index(self): @@ -1443,10 +1443,10 @@ def test_array_interface(self): # test ufuncs expected = deepcopy(self.ds) expected['foo'][:] = np.sin(self.x) - self.assertDataArrayEqual(expected['foo'], np.sin(self.dv)) + self.assert_equal(expected['foo'], np.sin(self.dv)) assert_array_equal(self.dv, np.maximum(self.v, self.dv)) bar = Variable(['x', 'y'], np.zeros((10, 20))) - self.assertDataArrayEqual(self.dv, np.maximum(self.dv, bar)) + self.assert_equal(self.dv, np.maximum(self.dv, bar)) def test_is_null(self): x = np.random.RandomState(42).randn(5, 6) @@ -1463,15 +1463,15 @@ def test_math(self): a = self.dv # variable math was already tested extensively, so let's just make sure # that all types are properly converted here - self.assertDataArrayEqual(a, +a) - self.assertDataArrayEqual(a, a + 0) - self.assertDataArrayEqual(a, 0 + a) - self.assertDataArrayEqual(a, a + 0 * v) - self.assertDataArrayEqual(a, 0 * v + a) - self.assertDataArrayEqual(a, a + 0 * x) - self.assertDataArrayEqual(a, 0 * x + a) - self.assertDataArrayEqual(a, a + 0 * a) - self.assertDataArrayEqual(a, 0 * a + a) + self.assert_equal(a, +a) + self.assert_equal(a, a + 0) + self.assert_equal(a, 0 + a) + self.assert_equal(a, a + 0 * v) + self.assert_equal(a, 0 * v + a) + self.assert_equal(a, a + 0 * x) + self.assert_equal(a, 0 * x + a) + self.assert_equal(a, a + 0 * a) + self.assert_equal(a, 0 * a + a) def test_math_automatic_alignment(self): a = DataArray(range(5), [('x', range(5))]) @@ -1787,7 +1787,7 @@ def test_reduce(self): actual = orig.mean(dim='x', skipna=True) expected = DataArray(orig.values.astype(int), dims=['x', 'y']).mean('x') - self.assertDataArrayEqual(actual, expected) + self.assert_equal(actual, expected) # skip due to bug in older versions of numpy.nanpercentile def test_quantile(self): @@ -1971,7 +1971,7 @@ def test_groupby_apply_ndarray(self): array = self.make_groupby_example_array() grouped = array.groupby('abc') actual = grouped.apply(np.asarray) - self.assertDataArrayEqual(array, actual) + self.assert_equal(array, actual) def test_groupby_apply_changes_metadata(self): def change_metadata(x): @@ -1984,7 +1984,7 @@ def change_metadata(x): actual = grouped.apply(change_metadata) expected = array.copy() expected = change_metadata(expected) - self.assertDataArrayEqual(expected, actual) + self.assert_equal(expected, actual) def test_groupby_math(self): array = self.make_groupby_example_array() @@ -2791,7 +2791,7 @@ def test_to_masked_array(self): x_masked_2 = da.to_masked_array() da_2 = DataArray(x_masked_2) self.assertArrayEqual(x_masked, x_masked_2) - self.assertDataArrayEqual(da, da_2) + self.assert_equal(da, da_2) da_masked_array = da.to_masked_array(copy=True) assert isinstance(da_masked_array, np.ma.MaskedArray) @@ -3042,7 +3042,7 @@ def test_to_dataset_retains_keys(self): # convert to dateset and back again result = array.to_dataset('x').to_array(dim='x') - self.assertDatasetEqual(array, result) + self.assert_equal(array, result) def test__title_for_slice(self): array = DataArray(np.ones((4, 3, 2)), dims=['a', 'b', 'c'], @@ -3070,7 +3070,7 @@ def test_dataarray_diff_n1(self): da = DataArray(np.random.randn(3, 4), dims=['x', 'y']) actual = da.diff('y') expected = DataArray(np.diff(da.values, axis=1), dims=['x', 'y']) - self.assertDataArrayEqual(expected, actual) + self.assert_equal(expected, actual) def test_coordinate_diff(self): # regression test for GH634 @@ -3079,7 +3079,7 @@ def test_coordinate_diff(self): expected = DataArray([1] * 9, dims=['lon'], coords=[range(1, 10)], name='lon') actual = lon.diff('lon') - self.assertDataArrayEqual(expected, actual) + self.assert_equal(expected, actual) def test_shift(self): arr = DataArray([1, 2, 3], dims='x') @@ -3146,14 +3146,14 @@ def test_dot(self): actual = da.dot(dm) expected_vals = np.tensordot(da_vals, dm_vals, [2, 0]) expected = DataArray(expected_vals, coords=[x, y], dims=['x', 'y']) - self.assertDataArrayEqual(expected, actual) + self.assert_equal(expected, actual) # all shared dims actual = da.dot(da) expected_vals = np.tensordot(da_vals, da_vals, axes=([0, 1, 2], [0, 1, 2])) expected = DataArray(expected_vals) - self.assertDataArrayEqual(expected, actual) + self.assert_equal(expected, actual) # multiple shared dims dm_vals = np.arange(20 * 5 * 4).reshape((20, 5, 4)) @@ -3162,7 +3162,7 @@ def test_dot(self): actual = da.dot(dm) expected_vals = np.tensordot(da_vals, dm_vals, axes=([1, 2], [1, 2])) expected = DataArray(expected_vals, coords=[x, j], dims=['x', 'j']) - self.assertDataArrayEqual(expected, actual) + self.assert_equal(expected, actual) with pytest.raises(NotImplementedError): da.dot(dm.to_dataset(name='dm')) @@ -3183,7 +3183,7 @@ def test_binary_op_join_setting(self): missing_3, join=align_type) expected = xr.DataArray([np.nan, 2, 4, np.nan], [(dim, [0, 1, 2, 3])]) - self.assertDataArrayEqual(actual, expected) + self.assert_equal(actual, expected) def test_combine_first(self): ar0 = DataArray([[0, 0], [0, 0]], [('x', ['a', 'b']), ('y', [-1, 0])]) @@ -3193,17 +3193,17 @@ def test_combine_first(self): actual = ar0.combine_first(ar1) expected = DataArray([[0, 0, np.nan], [0, 0, 1], [np.nan, 1, 1]], [('x', ['a', 'b', 'c']), ('y', [-1, 0, 1])]) - self.assertDataArrayEqual(actual, expected) + self.assert_equal(actual, expected) actual = ar1.combine_first(ar0) expected = DataArray([[0, 0, np.nan], [0, 1, 1], [np.nan, 1, 1]], [('x', ['a', 'b', 'c']), ('y', [-1, 0, 1])]) - self.assertDataArrayEqual(actual, expected) + self.assert_equal(actual, expected) actual = ar0.combine_first(ar2) expected = DataArray([[0, 0], [0, 0], [2, 2]], [('x', ['a', 'b', 'd']), ('y', [-1, 0])]) - self.assertDataArrayEqual(actual, expected) + self.assert_equal(actual, expected) def test_sortby(self): da = DataArray([[1, 2], [3, 4], [5, 6]], @@ -3218,34 +3218,34 @@ def test_sortby(self): expected = sorted1d dax = DataArray([100, 99, 98], [('x', ['c', 'b', 'a'])]) actual = da.sortby(dax) - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) # test descending order sort actual = da.sortby(dax, ascending=False) - self.assertDatasetEqual(actual, da) + self.assert_equal(actual, da) # test alignment (fills in nan for 'c') dax_short = DataArray([98, 97], [('x', ['b', 'a'])]) actual = da.sortby(dax_short) - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) # test multi-dim sort by 1D dataarray values expected = sorted2d dax = DataArray([100, 99, 98], [('x', ['c', 'b', 'a'])]) day = DataArray([90, 80], [('y', [1, 0])]) actual = da.sortby([day, dax]) - self.assertDataArrayEqual(actual, expected) + self.assert_equal(actual, expected) if LooseVersion(np.__version__) < LooseVersion('1.11.0'): pytest.skip('numpy 1.11.0 or later to support object data-type.') expected = sorted1d actual = da.sortby('x') - self.assertDataArrayEqual(actual, expected) + self.assert_equal(actual, expected) expected = sorted2d actual = da.sortby(['x', 'y']) - self.assertDataArrayEqual(actual, expected) + self.assert_equal(actual, expected) @requires_bottleneck def test_rank(self): @@ -3253,8 +3253,8 @@ def test_rank(self): ar = DataArray([[3, 4, np.nan, 1]]) expect_0 = DataArray([[1, 1, np.nan, 1]]) expect_1 = DataArray([[2, 3, np.nan, 1]]) - self.assertDataArrayEqual(ar.rank('dim_0'), expect_0) - self.assertDataArrayEqual(ar.rank('dim_1'), expect_1) + self.assert_equal(ar.rank('dim_0'), expect_0) + self.assert_equal(ar.rank('dim_1'), expect_1) # int x = DataArray([3, 2, 1]) self.assertDataArrayEqual(x.rank('dim_0'), x) @@ -3264,7 +3264,7 @@ def test_rank(self): x = DataArray([3.0, 1.0, np.nan, 2.0, 4.0], dims=('z',)) y = DataArray([0.75, 0.25, np.nan, 0.5, 1.0], dims=('z',)) - self.assertDataArrayEqual(y.rank('z', pct=True), y) + self.assert_equal(y.rank('z', pct=True), y) @pytest.fixture(params=[1]) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 53dd67ca698..96754483d63 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -324,14 +324,14 @@ def test_constructor_pandas_sequence(self): ) ds_based_on_pandas = Dataset(pandas_objs, ds.coords, attrs=ds.attrs) del ds_based_on_pandas['x'] - self.assertDatasetEqual(ds, ds_based_on_pandas) + self.assert_equal(ds, ds_based_on_pandas) # reindex pandas obj, check align works rearranged_index = reversed(pandas_objs['foo'].index) pandas_objs['foo'] = pandas_objs['foo'].reindex(rearranged_index) ds_based_on_pandas = Dataset(pandas_objs, ds.coords, attrs=ds.attrs) del ds_based_on_pandas['x'] - self.assertDatasetEqual(ds, ds_based_on_pandas) + self.assert_equal(ds, ds_based_on_pandas) def test_constructor_pandas_single(self): @@ -984,7 +984,7 @@ def test_isel_fancy(self): stations['dim2s'].variable] expected_var3 = data['var3'].variable[slice(None), stations['dim1s'].variable] - self.assertDataArrayEqual(actual['a'].drop('dim2'), stations['a']) + self.assert_equal(actual['a'].drop('dim2'), stations['a']) self.assertArrayEqual(actual['var1'], expected_var1) self.assertArrayEqual(actual['var2'], expected_var2) self.assertArrayEqual(actual['var3'], expected_var3) @@ -1074,30 +1074,30 @@ def test_sel(self): loc_slicers = {'dim1': slice(None, None, 2), 'dim2': slice(0, 0.5), 'dim3': slice('a', 'c')} - self.assertDatasetEqual(data.isel(**int_slicers), + self.assert_equal(data.isel(**int_slicers), data.sel(**loc_slicers)) data['time'] = ('time', pd.date_range('2000-01-01', periods=20)) - self.assertDatasetEqual(data.isel(time=0), + self.assert_equal(data.isel(time=0), data.sel(time='2000-01-01')) - self.assertDatasetEqual(data.isel(time=slice(10)), + self.assert_equal(data.isel(time=slice(10)), data.sel(time=slice('2000-01-01', '2000-01-10'))) - self.assertDatasetEqual(data, data.sel(time=slice('1999', '2005'))) + self.assert_equal(data, data.sel(time=slice('1999', '2005'))) times = pd.date_range('2000-01-01', periods=3) - self.assertDatasetEqual(data.isel(time=slice(3)), + self.assert_equal(data.isel(time=slice(3)), data.sel(time=times)) - self.assertDatasetEqual(data.isel(time=slice(3)), + self.assert_equal(data.isel(time=slice(3)), data.sel(time=(data['time.dayofyear'] <= 3))) td = pd.to_timedelta(np.arange(3), unit='days') data = Dataset({'x': ('td', np.arange(3)), 'td': td}) - self.assertDatasetEqual(data, data.sel(td=td)) - self.assertDatasetEqual(data, data.sel(td=slice('3 days'))) - self.assertDatasetEqual(data.isel(td=0), + self.assert_equal(data, data.sel(td=td)) + self.assert_equal(data, data.sel(td=slice('3 days'))) + self.assert_equal(data.isel(td=0), data.sel(td=pd.Timedelta('0 days'))) - self.assertDatasetEqual(data.isel(td=0), + self.assert_equal(data.isel(td=0), data.sel(td=pd.Timedelta('0h'))) - self.assertDatasetEqual(data.isel(td=slice(1, 3)), + self.assert_equal(data.isel(td=slice(1, 3)), data.sel(td=slice('1 days', '2 days'))) def test_sel_dataarray(self): @@ -1105,14 +1105,14 @@ def test_sel_dataarray(self): ind = DataArray([0.0, 0.5, 1.0], dims=['dim2']) actual = data.sel(dim2=ind) - self.assertDatasetEqual(actual, data.isel(dim2=[0, 1, 2])) + self.assert_equal(actual, data.isel(dim2=[0, 1, 2])) # with different dimension ind = DataArray([0.0, 0.5, 1.0], dims=['new_dim']) actual = data.sel(dim2=ind) expected = data.isel(dim2=Variable('new_dim', [0, 1, 2])) assert 'new_dim' in actual.dims - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) # Multi-dimensional ind = DataArray([[0.0], [0.5], [1.0]], dims=['new_dim', 'new_dim2']) @@ -1121,7 +1121,7 @@ def test_sel_dataarray(self): [[0], [1], [2]])) assert 'new_dim' in actual.dims assert 'new_dim2' in actual.dims - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) # with coordinate ind = DataArray([0.0, 0.5, 1.0], dims=['new_dim'], @@ -1130,9 +1130,9 @@ def test_sel_dataarray(self): expected = data.isel(dim2=[0, 1, 2]).rename({'dim2': 'new_dim'}) assert 'new_dim' in actual.dims assert 'new_dim' in actual.coords - self.assertDatasetEqual(actual.drop('new_dim').drop('dim2'), + self.assert_equal(actual.drop('new_dim').drop('dim2'), expected.drop('new_dim')) - self.assertDataArrayEqual(actual['new_dim'].drop('dim2'), + self.assert_equal(actual['new_dim'].drop('dim2'), ind['new_dim']) # with conflicted coordinate (silently ignored) @@ -1140,18 +1140,18 @@ def test_sel_dataarray(self): coords={'dim2': ['a', 'b', 'c']}) actual = data.sel(dim2=ind) expected = data.isel(dim2=[0, 1, 2]) - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) # with conflicted coordinate (silently ignored) ind = DataArray([0.0, 0.5, 1.0], dims=['new_dim'], coords={'new_dim': ['a', 'b', 'c'], 'dim2': 3}) actual = data.sel(dim2=ind) - self.assertDataArrayEqual(actual['new_dim'].drop('dim2'), + self.assert_equal(actual['new_dim'].drop('dim2'), ind['new_dim'].drop('dim2')) expected = data.isel(dim2=[0, 1, 2]) expected['dim2'] = (('new_dim'), expected['dim2'].values) - self.assertDataArrayEqual(actual['dim2'].drop('new_dim'), + self.assert_equal(actual['dim2'].drop('new_dim'), expected['dim2']) assert actual['var1'].dims == ('dim1', 'new_dim') @@ -1162,7 +1162,7 @@ def test_sel_dataarray(self): 'new_dim': ('dim2', [1.1, 1.2, 1.3])}) actual = data.sel(dim2=ind) expected = data.isel(dim2=[0, 1, 2]) - self.assertDatasetEqual(actual.drop('new_dim'), expected) + self.assert_equal(actual.drop('new_dim'), expected) assert np.allclose(actual['new_dim'].values, ind['new_dim'].values) def test_sel_dataarray_mindex(self): @@ -2159,7 +2159,7 @@ def test_getitem(self): actual = data[['var1', 'var2']] expected = Dataset({'var1': data['var1'], 'var2': data['var2']}) - self.assertDatasetEqual(expected, actual) + self.assert_equal(expected, actual) actual = data['numbers'] expected = DataArray(data['numbers'].variable, @@ -2208,7 +2208,7 @@ def test_virtual_variables_time(self): # ensure they become coordinates expected = Dataset({}, {'dayofyear': data['time.dayofyear']}) actual = data[['time.dayofyear']] - self.assertDatasetEqual(expected, actual) + self.assert_equal(expected, actual) # non-coordinate variables ds = Dataset({'t': ('x', pd.date_range('2000-01-01', periods=3))}) assert (ds['t.year'] == 2000).all() @@ -2296,7 +2296,7 @@ def test_setitem_pandas(self): ds_copy = ds.copy() ds_copy['bar'] = ds['bar'].to_pandas() - self.assertDatasetEqual(ds, ds_copy) + self.assert_equal(ds, ds_copy) def test_setitem_auto_align(self): ds = Dataset() @@ -2471,14 +2471,14 @@ def test_groupby(self): ('c', data.isel(x=2))] for actual, expected in zip(groupby, expected_items): assert actual[0] == expected[0] - self.assertDatasetEqual(actual[1], expected[1]) + self.assert_equal(actual[1], expected[1]) def identity(x): return x for k in ['x', 'c', 'y']: actual = data.groupby(k, squeeze=False).apply(identity) - self.assertDatasetEqual(data, actual) + self.assert_equal(data, actual) def test_groupby_returns_new_type(self): data = Dataset({'z': (['x', 'y'], np.random.randn(3, 5))}) @@ -2559,10 +2559,10 @@ def reorder_dims(x): expected = ((ds + Variable('dim3', np.zeros(10))) .transpose('dim3', 'dim1', 'dim2', 'time')) actual = grouped + zeros - self.assertDatasetEqual(expected, actual) + self.assert_equal(expected, actual) actual = zeros + grouped - self.assertDatasetEqual(expected, actual) + self.assert_equal(expected, actual) with raises_regex(ValueError, 'incompat.* grouped binary'): grouped + ds @@ -2629,10 +2629,10 @@ def test_resample_and_first(self): for how in ['mean', 'sum', 'first', 'last', ]: method = getattr(actual, how) result = method() - self.assertDatasetEqual(expected, result) + self.assert_equal(expected, result) for method in [np.mean, ]: result = actual.reduce(method) - self.assertDatasetEqual(expected, result) + self.assert_equal(expected, result) def test_resample_by_mean_with_keep_attrs(self): times = pd.date_range('2000-01-01', freq='6H', periods=10) @@ -3262,9 +3262,9 @@ def test_reduce(self): actual = data.max() expected = Dataset(dict((k, v.max()) for k, v in iteritems(data.data_vars))) - self.assertDatasetEqual(expected, actual) + self.assert_equal(expected, actual) - self.assertDatasetEqual(data.min(dim=['dim1']), + self.assert_equal(data.min(dim=['dim1']), data.min(dim='dim1')) for reduct, expected in [('dim2', ['dim1', 'dim3', 'time']), @@ -3275,7 +3275,7 @@ def test_reduce(self): print(reduct, actual, expected) self.assertItemsEqual(actual, expected) - self.assertDatasetEqual(data.mean(dim=[]), data) + self.assert_equal(data.mean(dim=[]), data) # uint support data = xr.Dataset({'a': (('x', 'y'), @@ -3324,8 +3324,8 @@ def test_reduce_non_numeric(self): data1[v] = (dims, data, {'foo': 'variable'}) assert 'var4' not in data1.mean() - self.assertDatasetEqual(data1.mean(), data2.mean()) - self.assertDatasetEqual(data1.mean(dim='dim1'), + self.assert_equal(data1.mean(), data2.mean()) + self.assert_equal(data1.mean(dim='dim1'), data2.mean(dim='dim1')) def test_reduce_strings(self): @@ -3450,7 +3450,7 @@ def test_rank(self): # same as dataarray version x = z.var3 y = ds.var3.rank('dim3') - self.assertDataArrayEqual(x, y) + self.assert_equal(x, y) # coordinates stick self.assertItemsEqual(list(z.coords), list(ds.coords)) self.assertItemsEqual(list(x.coords), list(y.coords)) @@ -3586,7 +3586,7 @@ def test_dataset_math_auto_align(self): actual = ds.isel(y=slice(1)) + ds.isel(y=slice(1, None)) expected = 2 * ds.drop(ds.y, dim='y') - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) actual = ds + ds[['bar']] expected = (2 * ds[['bar']]).merge(ds.coords) @@ -3673,17 +3673,17 @@ def test_dataset_diff_n1_simple(self): ds = Dataset({'foo': ('x', [5, 5, 6, 6])}) actual = ds.diff('x') expected = Dataset({'foo': ('x', [0, 1, 0])}) - self.assertDatasetEqual(expected, actual) + self.assert_equal(expected, actual) def test_dataset_diff_n1_label(self): ds = Dataset({'foo': ('x', [5, 5, 6, 6])}, {'x': [0, 1, 2, 3]}) actual = ds.diff('x', label='lower') expected = Dataset({'foo': ('x', [0, 1, 0])}, {'x': [0, 1, 2]}) - self.assertDatasetEqual(expected, actual) + self.assert_equal(expected, actual) actual = ds.diff('x', label='upper') expected = Dataset({'foo': ('x', [0, 1, 0])}, {'x': [1, 2, 3]}) - self.assertDatasetEqual(expected, actual) + self.assert_equal(expected, actual) def test_dataset_diff_n1(self): ds = create_test_data(seed=1) @@ -3698,7 +3698,7 @@ def test_dataset_diff_n1(self): expected['var3'] = ds['var3'] expected = Dataset(expected, coords={'time': ds['time'].values}) expected.coords['numbers'] = ('dim3', ds['numbers'].values) - self.assertDatasetEqual(expected, actual) + self.assert_equal(expected, actual) def test_dataset_diff_n2(self): ds = create_test_data(seed=1) @@ -3713,7 +3713,7 @@ def test_dataset_diff_n2(self): expected['var3'] = ds['var3'] expected = Dataset(expected, coords={'time': ds['time'].values}) expected.coords['numbers'] = ('dim3', ds['numbers'].values) - self.assertDatasetEqual(expected, actual) + self.assert_equal(expected, actual) def test_dataset_diff_exception_n_neg(self): ds = create_test_data(seed=1) @@ -3813,29 +3813,29 @@ def test_binary_op_join_setting(self): with xr.set_options(arithmetic_join='outer'): actual = missing_2 + missing_0 expected = xr.Dataset({'x': [0, 1, 2]}) - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) # arithmetic join also applies to data_vars ds1 = xr.Dataset({'foo': 1, 'bar': 2}) ds2 = xr.Dataset({'bar': 2, 'baz': 3}) expected = xr.Dataset({'bar': 4}) # default is inner joining actual = ds1 + ds2 - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) with xr.set_options(arithmetic_join='outer'): expected = xr.Dataset({'foo': np.nan, 'bar': 4, 'baz': np.nan}) actual = ds1 + ds2 - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) with xr.set_options(arithmetic_join='left'): expected = xr.Dataset({'foo': np.nan, 'bar': 4}) actual = ds1 + ds2 - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) with xr.set_options(arithmetic_join='right'): expected = xr.Dataset({'bar': 4, 'baz': np.nan}) actual = ds1 + ds2 - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) def test_full_like(self): # For more thorough tests, see test_variable.py @@ -3870,15 +3870,15 @@ def test_combine_first(self): expected = Dataset({'dsx0': ('x', [0, 0, np.nan]), 'dsx1': ('x', [np.nan, 1, 1])}, coords={'x': ['a', 'b', 'c']}) - self.assertDatasetEqual(actual, expected) - self.assertDatasetEqual(actual, xr.merge([dsx0, dsx1])) + self.assert_equal(actual, expected) + self.assert_equal(actual, xr.merge([dsx0, dsx1])) # works just like xr.merge([self, other]) dsy2 = DataArray([2, 2, 2], [('x', ['b', 'c', 'd'])]).to_dataset(name='dsy2') actual = dsx0.combine_first(dsy2) expected = xr.merge([dsy2, dsx0]) - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) def test_sortby(self): ds = Dataset({'A': DataArray([[1, 2], [3, 4], [5, 6]], @@ -3902,16 +3902,16 @@ def test_sortby(self): expected = sorted1d dax = DataArray([100, 99, 98], [('x', ['c', 'b', 'a'])]) actual = ds.sortby(dax) - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) # test descending order sort actual = ds.sortby(dax, ascending=False) - self.assertDatasetEqual(actual, ds) + self.assert_equal(actual, ds) # test alignment (fills in nan for 'c') dax_short = DataArray([98, 97], [('x', ['b', 'a'])]) actual = ds.sortby(dax_short) - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) # test 1-D lexsort # dax0 is sorted first to give indices of [1, 2, 0] @@ -3919,13 +3919,13 @@ def test_sortby(self): dax0 = DataArray([100, 95, 95], [('x', ['c', 'b', 'a'])]) dax1 = DataArray([0, 1, 0], [('x', ['c', 'b', 'a'])]) actual = ds.sortby([dax0, dax1]) # lexsort underneath gives [2, 1, 0] - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) expected = sorted2d # test multi-dim sort by 1D dataarray values day = DataArray([90, 80], [('y', [1, 0])]) actual = ds.sortby([day, dax]) - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) # test exception-raising with pytest.raises(KeyError) as excinfo: @@ -3940,7 +3940,7 @@ def test_sortby(self): expected = sorted1d actual = ds.sortby('x') - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) # test pandas.MultiIndex indices = (('b', 1), ('b', 0), ('a', 1), ('a', 0)) @@ -3957,26 +3957,26 @@ def test_sortby(self): ('y', [1, 0])]), 'B': DataArray([[11, 12], [9, 10], [7, 8], [5, 6]], dims=['x', 'y'])}) - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) # multi-dim sort by coordinate objects expected = sorted2d actual = ds.sortby(['x', 'y']) - self.assertDatasetEqual(actual, expected) + self.assert_equal(actual, expected) # test descending order sort actual = ds.sortby(['x', 'y'], ascending=False) - self.assertDatasetEqual(actual, ds) + self.assert_equal(actual, ds) def test_attribute_access(self): ds = create_test_data(seed=1) for key in ['var1', 'var2', 'var3', 'time', 'dim1', 'dim2', 'dim3', 'numbers']: - self.assertDataArrayEqual(ds[key], getattr(ds, key)) + self.assert_equal(ds[key], getattr(ds, key)) assert key in dir(ds) for key in ['dim3', 'dim1', 'numbers']: - self.assertDataArrayEqual(ds['var3'][key], getattr(ds.var3, key)) + self.assert_equal(ds['var3'][key], getattr(ds.var3, key)) assert key in dir(ds['var3']) # attrs assert ds['var3'].attrs['foo'] == ds.var3.foo diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 10c53221a6a..02585e4c416 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -1189,7 +1189,7 @@ def test_groups(self): upperleft_array = self.darray.loc[upperleft_dict] z0 = self.darray.isel(z=0) - self.assertDataArrayEqual(upperleft_array, z0) + self.assert_equal(upperleft_array, z0) @pytest.mark.slow def test_float_index(self): From b7a086f8c5be5db43908afca5e46ba9334c594b5 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 14:46:23 -0500 Subject: [PATCH 11/41] imports --- xarray/tests/test_dask.py | 3 ++- xarray/tests/test_dataset.py | 2 +- xarray/tests/test_ufuncs.py | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/xarray/tests/test_dask.py b/xarray/tests/test_dask.py index 6b4e4255cf1..936bd5736a1 100644 --- a/xarray/tests/test_dask.py +++ b/xarray/tests/test_dask.py @@ -14,7 +14,8 @@ from xarray import Variable, DataArray, Dataset import xarray.ufuncs as xu from xarray.core.pycompat import suppress, OrderedDict -from . import TestCase, assert_frame_equal, raises_regex +from . import ( + TestCase, assert_frame_equal, raises_regex, assert_equal, assert_identical) from xarray.tests import mock diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 96754483d63..055f9a0282c 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -30,7 +30,7 @@ from . import (TestCase, raises_regex, InaccessibleArray, UnexpectedDataAccess, requires_dask, source_ndarray) -from xarray.tests import (assert_equal, assert_allclose, +from xarray.tests import (assert_equal, assert_allclose, assert_identical, assert_array_equal, requires_bottleneck, requires_scipy) diff --git a/xarray/tests/test_ufuncs.py b/xarray/tests/test_ufuncs.py index f3208bf8a39..717b50602db 100644 --- a/xarray/tests/test_ufuncs.py +++ b/xarray/tests/test_ufuncs.py @@ -8,7 +8,7 @@ import xarray.ufuncs as xu import xarray as xr -from . import TestCase, raises_regex +from . import TestCase, raises_regex, assert_equal, assert_identical class TestOps(TestCase): From c06dd484582a7e8c314c849ab30b2d81e87c351d Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 14:48:52 -0500 Subject: [PATCH 12/41] assertItemsEqual (but may need coercion to sets) --- xarray/tests/test_dataarray.py | 10 +++--- xarray/tests/test_dataset.py | 56 ++++++++++++++++----------------- xarray/tests/test_formatting.py | 2 +- xarray/tests/test_utils.py | 4 +-- 4 files changed, 36 insertions(+), 36 deletions(-) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index b370a1a51c3..94d581d2e5e 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -2836,7 +2836,7 @@ def test_to_and_from_cdms2(self): actual = original.to_cdms2() self.assertArrayEqual(actual, original) assert actual.id == original.name - self.assertItemsEqual(actual.getAxisIds(), original.dims) + assert actual.getAxisIds() == original.dims for axis, coord in zip(actual.getAxisList(), expected_coords): assert axis.id == coord.name self.assertArrayEqual(axis, coord.values) @@ -2878,8 +2878,8 @@ def test_to_and_from_iris(self): actual = original.to_iris() self.assertArrayEqual(actual.data, original.data) assert actual.var_name == original.name - self.assertItemsEqual([d.var_name for d in actual.dim_coords], - original.dims) + assert [d.var_name for d in actual.dim_coords] == \ + original.dims assert actual.cell_methods == \ (iris.coords.CellMethod(method='mean', coords=('height',), @@ -2950,8 +2950,8 @@ def test_to_and_from_iris_dask(self): hasattr(actual, 'core_data') else actual.data self.assertArrayEqual(actual_data, original.data) assert actual.var_name == original.name - self.assertItemsEqual([d.var_name for d in actual.dim_coords], - original.dims) + assert [d.var_name for d in actual.dim_coords] == \ + original.dims assert actual.cell_methods == \ (iris.coords.CellMethod(method='mean', coords=('height',), diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 055f9a0282c..038ef3ccb5a 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -390,7 +390,7 @@ def test_constructor_with_coords(self): ds = Dataset({}, {'a': ('x', [1])}) assert not ds.data_vars - self.assertItemsEqual(ds.coords.keys(), ['a']) + assert ds.coords.keys() == ['a'] mindex = pd.MultiIndex.from_product([['a', 'b'], [1, 2]], names=('level_1', 'level_2')) @@ -412,9 +412,9 @@ def test_properties(self): assert type(ds.dims.mapping.mapping) is dict with pytest.warns(FutureWarning): - self.assertItemsEqual(ds, list(ds.variables)) + assert ds == list(ds.variables) with pytest.warns(FutureWarning): - self.assertItemsEqual(ds.keys(), list(ds.variables)) + assert ds.keys() == list(ds.variables) assert 'aasldfjalskdfj' not in ds.variables assert 'dim1' in repr(ds.variables) with pytest.warns(FutureWarning): @@ -422,18 +422,18 @@ def test_properties(self): with pytest.warns(FutureWarning): assert bool(ds) == True - self.assertItemsEqual(ds.data_vars, ['var1', 'var2', 'var3']) - self.assertItemsEqual(ds.data_vars.keys(), ['var1', 'var2', 'var3']) + assert ds.data_vars == ['var1', 'var2', 'var3'] + assert ds.data_vars.keys() == ['var1', 'var2', 'var3'] assert 'var1' in ds.data_vars assert 'dim1' not in ds.data_vars assert 'numbers' not in ds.data_vars assert len(ds.data_vars) == 3 - self.assertItemsEqual(ds.indexes, ['dim2', 'dim3', 'time']) + assert ds.indexes == ['dim2', 'dim3', 'time'] assert len(ds.indexes) == 3 assert 'dim2' in repr(ds.indexes) - self.assertItemsEqual(ds.coords, ['time', 'dim2', 'dim3', 'numbers']) + assert ds.coords == ['time', 'dim2', 'dim3', 'numbers'] assert 'dim2' in ds.coords assert 'numbers' in ds.coords assert 'var1' not in ds.coords @@ -521,7 +521,7 @@ def test_coords_properties(self): assert 4 == len(data.coords) - self.assertItemsEqual(['x', 'y', 'a', 'b'], list(data.coords)) + assert ['x', 'y', 'a', 'b'] == list(data.coords) assert_identical(data.coords['x'].variable, data['x'].variable) assert_identical(data.coords['y'].variable, data['y'].variable) @@ -817,7 +817,7 @@ def test_isel(self): ret = data.isel(**slicers) # Verify that only the specified dimension was altered - self.assertItemsEqual(data.dims, ret.dims) + assert data.dims == ret.dims for d in data.dims: if d in slicers: assert ret.dims[d] == \ @@ -843,21 +843,21 @@ def test_isel(self): ret = data.isel(dim1=0) assert {'time': 20, 'dim2': 9, 'dim3': 10} == ret.dims - self.assertItemsEqual(data.data_vars, ret.data_vars) - self.assertItemsEqual(data.coords, ret.coords) - self.assertItemsEqual(data.indexes, ret.indexes) + assert data.data_vars == ret.data_vars + assert data.coords == ret.coords + assert data.indexes == ret.indexes ret = data.isel(time=slice(2), dim1=0, dim2=slice(5)) assert {'time': 2, 'dim2': 5, 'dim3': 10} == ret.dims - self.assertItemsEqual(data.data_vars, ret.data_vars) - self.assertItemsEqual(data.coords, ret.coords) - self.assertItemsEqual(data.indexes, ret.indexes) + assert data.data_vars == ret.data_vars + assert data.coords == ret.coords + assert data.indexes == ret.indexes ret = data.isel(time=0, dim1=0, dim2=slice(5)) - self.assertItemsEqual({'dim2': 5, 'dim3': 10}, ret.dims) - self.assertItemsEqual(data.data_vars, ret.data_vars) - self.assertItemsEqual(data.coords, ret.coords) - self.assertItemsEqual(data.indexes, list(ret.indexes) + ['time']) + assert {'dim2': 5, 'dim3': 10} == ret.dims + assert data.data_vars == ret.data_vars + assert data.coords == ret.coords + assert data.indexes == list(ret.indexes) + ['time'] def test_isel_fancy(self): # isel with fancy indexing. @@ -2413,12 +2413,12 @@ def test_setitem_multiindex_level(self): def test_delitem(self): data = create_test_data() all_items = set(data.variables) - self.assertItemsEqual(data.variables, all_items) + assert data.variables == all_items del data['var1'] - self.assertItemsEqual(data.variables, all_items - set(['var1'])) + assert data.variables == all_items - set(['var1']) del data['numbers'] - self.assertItemsEqual(data.variables, - all_items - set(['var1', 'numbers'])) + assert data.variables == \ + all_items - set(['var1', 'numbers']) assert 'numbers' not in data.coords def test_squeeze(self): @@ -3273,7 +3273,7 @@ def test_reduce(self): ((), ['dim1', 'dim2', 'dim3', 'time'])]: actual = data.min(dim=reduct).dims print(reduct, actual, expected) - self.assertItemsEqual(actual, expected) + assert actual == expected self.assert_equal(data.mean(dim=[]), data) @@ -3312,7 +3312,7 @@ def test_reduce_cumsum_test_dims(self): ]: actual = getattr(data, cumfunc)(dim=reduct).dims print(reduct, actual, expected) - self.assertItemsEqual(actual, expected) + assert actual == expected def test_reduce_non_numeric(self): data1 = create_test_data(seed=44) @@ -3446,14 +3446,14 @@ def test_rank(self): ds = create_test_data(seed=1234) # only ds.var3 depends on dim3 z = ds.rank('dim3') - self.assertItemsEqual(['var3'], list(z.data_vars)) + assert ['var3'] == list(z.data_vars) # same as dataarray version x = z.var3 y = ds.var3.rank('dim3') self.assert_equal(x, y) # coordinates stick - self.assertItemsEqual(list(z.coords), list(ds.coords)) - self.assertItemsEqual(list(x.coords), list(y.coords)) + assert list(z.coords) == list(ds.coords) + assert list(x.coords) == list(y.coords) # invalid dim with raises_regex(ValueError, 'does not contain'): x.rank('invalid_dim') diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index 53342825dcd..185faf8cab5 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -35,7 +35,7 @@ def test_first_n_items(self): for n in [3, 10, 13, 100, 200]: actual = formatting.first_n_items(array, n) expected = array.flat[:n] - self.assertItemsEqual(expected, actual) + assert expected == actual with raises_regex(ValueError, 'at least one item'): formatting.first_n_items(array, 0) diff --git a/xarray/tests/test_utils.py b/xarray/tests/test_utils.py index d3c2c9a50f5..b8c4f356364 100644 --- a/xarray/tests/test_utils.py +++ b/xarray/tests/test_utils.py @@ -125,7 +125,7 @@ def test_frozen(self): def test_sorted_keys_dict(self): x = {'a': 1, 'b': 2, 'c': 3} y = utils.SortedKeysDict(x) - self.assertItemsEqual(y, ['a', 'b', 'c']) + assert y == ['a', 'b', 'c'] assert repr(utils.SortedKeysDict()) == \ "SortedKeysDict({})" @@ -140,7 +140,7 @@ def test_chain_map(self): m['x'] = 100 assert m['x'] == 100 assert m.maps[0]['x'] == 100 - self.assertItemsEqual(['x', 'y', 'z'], m) + assert ['x', 'y', 'z'] == m def test_repr_object(): From 6b67572743dc06bde2b3edd9ab8f6f375601d536 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 14:54:51 -0500 Subject: [PATCH 13/41] Revert "assertItemsEqual (but may need coercion to sets)" This reverts commit fa8f89a1200ce957f9d53c524585529f6364d7d1. --- xarray/tests/test_dataarray.py | 10 +++--- xarray/tests/test_dataset.py | 56 ++++++++++++++++----------------- xarray/tests/test_formatting.py | 2 +- xarray/tests/test_utils.py | 4 +-- 4 files changed, 36 insertions(+), 36 deletions(-) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 94d581d2e5e..b370a1a51c3 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -2836,7 +2836,7 @@ def test_to_and_from_cdms2(self): actual = original.to_cdms2() self.assertArrayEqual(actual, original) assert actual.id == original.name - assert actual.getAxisIds() == original.dims + self.assertItemsEqual(actual.getAxisIds(), original.dims) for axis, coord in zip(actual.getAxisList(), expected_coords): assert axis.id == coord.name self.assertArrayEqual(axis, coord.values) @@ -2878,8 +2878,8 @@ def test_to_and_from_iris(self): actual = original.to_iris() self.assertArrayEqual(actual.data, original.data) assert actual.var_name == original.name - assert [d.var_name for d in actual.dim_coords] == \ - original.dims + self.assertItemsEqual([d.var_name for d in actual.dim_coords], + original.dims) assert actual.cell_methods == \ (iris.coords.CellMethod(method='mean', coords=('height',), @@ -2950,8 +2950,8 @@ def test_to_and_from_iris_dask(self): hasattr(actual, 'core_data') else actual.data self.assertArrayEqual(actual_data, original.data) assert actual.var_name == original.name - assert [d.var_name for d in actual.dim_coords] == \ - original.dims + self.assertItemsEqual([d.var_name for d in actual.dim_coords], + original.dims) assert actual.cell_methods == \ (iris.coords.CellMethod(method='mean', coords=('height',), diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 038ef3ccb5a..055f9a0282c 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -390,7 +390,7 @@ def test_constructor_with_coords(self): ds = Dataset({}, {'a': ('x', [1])}) assert not ds.data_vars - assert ds.coords.keys() == ['a'] + self.assertItemsEqual(ds.coords.keys(), ['a']) mindex = pd.MultiIndex.from_product([['a', 'b'], [1, 2]], names=('level_1', 'level_2')) @@ -412,9 +412,9 @@ def test_properties(self): assert type(ds.dims.mapping.mapping) is dict with pytest.warns(FutureWarning): - assert ds == list(ds.variables) + self.assertItemsEqual(ds, list(ds.variables)) with pytest.warns(FutureWarning): - assert ds.keys() == list(ds.variables) + self.assertItemsEqual(ds.keys(), list(ds.variables)) assert 'aasldfjalskdfj' not in ds.variables assert 'dim1' in repr(ds.variables) with pytest.warns(FutureWarning): @@ -422,18 +422,18 @@ def test_properties(self): with pytest.warns(FutureWarning): assert bool(ds) == True - assert ds.data_vars == ['var1', 'var2', 'var3'] - assert ds.data_vars.keys() == ['var1', 'var2', 'var3'] + self.assertItemsEqual(ds.data_vars, ['var1', 'var2', 'var3']) + self.assertItemsEqual(ds.data_vars.keys(), ['var1', 'var2', 'var3']) assert 'var1' in ds.data_vars assert 'dim1' not in ds.data_vars assert 'numbers' not in ds.data_vars assert len(ds.data_vars) == 3 - assert ds.indexes == ['dim2', 'dim3', 'time'] + self.assertItemsEqual(ds.indexes, ['dim2', 'dim3', 'time']) assert len(ds.indexes) == 3 assert 'dim2' in repr(ds.indexes) - assert ds.coords == ['time', 'dim2', 'dim3', 'numbers'] + self.assertItemsEqual(ds.coords, ['time', 'dim2', 'dim3', 'numbers']) assert 'dim2' in ds.coords assert 'numbers' in ds.coords assert 'var1' not in ds.coords @@ -521,7 +521,7 @@ def test_coords_properties(self): assert 4 == len(data.coords) - assert ['x', 'y', 'a', 'b'] == list(data.coords) + self.assertItemsEqual(['x', 'y', 'a', 'b'], list(data.coords)) assert_identical(data.coords['x'].variable, data['x'].variable) assert_identical(data.coords['y'].variable, data['y'].variable) @@ -817,7 +817,7 @@ def test_isel(self): ret = data.isel(**slicers) # Verify that only the specified dimension was altered - assert data.dims == ret.dims + self.assertItemsEqual(data.dims, ret.dims) for d in data.dims: if d in slicers: assert ret.dims[d] == \ @@ -843,21 +843,21 @@ def test_isel(self): ret = data.isel(dim1=0) assert {'time': 20, 'dim2': 9, 'dim3': 10} == ret.dims - assert data.data_vars == ret.data_vars - assert data.coords == ret.coords - assert data.indexes == ret.indexes + self.assertItemsEqual(data.data_vars, ret.data_vars) + self.assertItemsEqual(data.coords, ret.coords) + self.assertItemsEqual(data.indexes, ret.indexes) ret = data.isel(time=slice(2), dim1=0, dim2=slice(5)) assert {'time': 2, 'dim2': 5, 'dim3': 10} == ret.dims - assert data.data_vars == ret.data_vars - assert data.coords == ret.coords - assert data.indexes == ret.indexes + self.assertItemsEqual(data.data_vars, ret.data_vars) + self.assertItemsEqual(data.coords, ret.coords) + self.assertItemsEqual(data.indexes, ret.indexes) ret = data.isel(time=0, dim1=0, dim2=slice(5)) - assert {'dim2': 5, 'dim3': 10} == ret.dims - assert data.data_vars == ret.data_vars - assert data.coords == ret.coords - assert data.indexes == list(ret.indexes) + ['time'] + self.assertItemsEqual({'dim2': 5, 'dim3': 10}, ret.dims) + self.assertItemsEqual(data.data_vars, ret.data_vars) + self.assertItemsEqual(data.coords, ret.coords) + self.assertItemsEqual(data.indexes, list(ret.indexes) + ['time']) def test_isel_fancy(self): # isel with fancy indexing. @@ -2413,12 +2413,12 @@ def test_setitem_multiindex_level(self): def test_delitem(self): data = create_test_data() all_items = set(data.variables) - assert data.variables == all_items + self.assertItemsEqual(data.variables, all_items) del data['var1'] - assert data.variables == all_items - set(['var1']) + self.assertItemsEqual(data.variables, all_items - set(['var1'])) del data['numbers'] - assert data.variables == \ - all_items - set(['var1', 'numbers']) + self.assertItemsEqual(data.variables, + all_items - set(['var1', 'numbers'])) assert 'numbers' not in data.coords def test_squeeze(self): @@ -3273,7 +3273,7 @@ def test_reduce(self): ((), ['dim1', 'dim2', 'dim3', 'time'])]: actual = data.min(dim=reduct).dims print(reduct, actual, expected) - assert actual == expected + self.assertItemsEqual(actual, expected) self.assert_equal(data.mean(dim=[]), data) @@ -3312,7 +3312,7 @@ def test_reduce_cumsum_test_dims(self): ]: actual = getattr(data, cumfunc)(dim=reduct).dims print(reduct, actual, expected) - assert actual == expected + self.assertItemsEqual(actual, expected) def test_reduce_non_numeric(self): data1 = create_test_data(seed=44) @@ -3446,14 +3446,14 @@ def test_rank(self): ds = create_test_data(seed=1234) # only ds.var3 depends on dim3 z = ds.rank('dim3') - assert ['var3'] == list(z.data_vars) + self.assertItemsEqual(['var3'], list(z.data_vars)) # same as dataarray version x = z.var3 y = ds.var3.rank('dim3') self.assert_equal(x, y) # coordinates stick - assert list(z.coords) == list(ds.coords) - assert list(x.coords) == list(y.coords) + self.assertItemsEqual(list(z.coords), list(ds.coords)) + self.assertItemsEqual(list(x.coords), list(y.coords)) # invalid dim with raises_regex(ValueError, 'does not contain'): x.rank('invalid_dim') diff --git a/xarray/tests/test_formatting.py b/xarray/tests/test_formatting.py index 185faf8cab5..53342825dcd 100644 --- a/xarray/tests/test_formatting.py +++ b/xarray/tests/test_formatting.py @@ -35,7 +35,7 @@ def test_first_n_items(self): for n in [3, 10, 13, 100, 200]: actual = formatting.first_n_items(array, n) expected = array.flat[:n] - assert expected == actual + self.assertItemsEqual(expected, actual) with raises_regex(ValueError, 'at least one item'): formatting.first_n_items(array, 0) diff --git a/xarray/tests/test_utils.py b/xarray/tests/test_utils.py index b8c4f356364..d3c2c9a50f5 100644 --- a/xarray/tests/test_utils.py +++ b/xarray/tests/test_utils.py @@ -125,7 +125,7 @@ def test_frozen(self): def test_sorted_keys_dict(self): x = {'a': 1, 'b': 2, 'c': 3} y = utils.SortedKeysDict(x) - assert y == ['a', 'b', 'c'] + self.assertItemsEqual(y, ['a', 'b', 'c']) assert repr(utils.SortedKeysDict()) == \ "SortedKeysDict({})" @@ -140,7 +140,7 @@ def test_chain_map(self): m['x'] = 100 assert m['x'] == 100 assert m.maps[0]['x'] == 100 - assert ['x', 'y', 'z'] == m + self.assertItemsEqual(['x', 'y', 'z'], m) def test_repr_object(): From c336bbc49f2765851980e2f97b6283ffe72bb60d Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 14:55:32 -0500 Subject: [PATCH 14/41] add back in assertitemsequal --- xarray/tests/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/tests/__init__.py b/xarray/tests/__init__.py index dc4530901d5..7a87dbc859f 100644 --- a/xarray/tests/__init__.py +++ b/xarray/tests/__init__.py @@ -162,7 +162,7 @@ def assertAllClose(self, a1, a2, rtol=1e-05, atol=1e-8): __tracebackhide__ = True # noqa: F841 assert allclose_or_equiv(a1, a2, rtol=rtol, atol=atol) - def assert_equal(self, d1, d2): + def assertDatasetEqual(self, d1, d2): __tracebackhide__ = True # noqa: F841 assert_equal(d1, d2) @@ -180,7 +180,7 @@ def assertCoordinatesEqual(self, d1, d2): __tracebackhide__ = True # noqa: F841 assert_equal(d1, d2) - def assert_equal(self, ar1, ar2): + def assertDataArrayEqual(self, ar1, ar2): __tracebackhide__ = True # noqa: F841 assert_equal(ar1, ar2) From b9dfe052f6eff679a5d8bceebc2082765761aae4 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 14:59:31 -0500 Subject: [PATCH 15/41] self.assert_equal -> assert_equal --- xarray/tests/test_accessors.py | 16 ++--- xarray/tests/test_backends.py | 28 ++++---- xarray/tests/test_combine.py | 6 +- xarray/tests/test_dask.py | 2 +- xarray/tests/test_dataarray.py | 84 +++++++++++----------- xarray/tests/test_dataset.py | 126 ++++++++++++++++----------------- xarray/tests/test_plot.py | 2 +- 7 files changed, 132 insertions(+), 132 deletions(-) diff --git a/xarray/tests/test_accessors.py b/xarray/tests/test_accessors.py index cd3e2a3d7ea..0411ea7fc92 100644 --- a/xarray/tests/test_accessors.py +++ b/xarray/tests/test_accessors.py @@ -37,10 +37,10 @@ def test_field_access(self): hours = xr.DataArray(self.times.hour, name='hour', coords=[self.times, ], dims=['time', ]) - self.assert_equal(years, self.data.time.dt.year) - self.assert_equal(months, self.data.time.dt.month) - self.assert_equal(days, self.data.time.dt.day) - self.assert_equal(hours, self.data.time.dt.hour) + assert_equal(years, self.data.time.dt.year) + assert_equal(months, self.data.time.dt.month) + assert_equal(days, self.data.time.dt.day) + assert_equal(hours, self.data.time.dt.hour) def test_not_datetime_type(self): nontime_data = self.data.copy() @@ -82,10 +82,10 @@ def test_dask_field_access(self): assert dask_hour.data.chunks == dask_chunks # Check the actual output from the accessors - self.assert_equal(years, dask_year.compute()) - self.assert_equal(months, dask_month.compute()) - self.assert_equal(days, dask_day.compute()) - self.assert_equal(hours, dask_hour.compute()) + assert_equal(years, dask_year.compute()) + assert_equal(months, dask_month.compute()) + assert_equal(days, dask_day.compute()) + assert_equal(hours, dask_hour.compute()) def test_seasons(self): dates = pd.date_range(start="2000/01/01", freq="M", periods=12) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 7d22472db09..fe31673302c 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -369,7 +369,7 @@ def test_roundtrip_example_1_netcdf(self): # will depend on the encoding used. For example, # without CF encoding 'actual' will end up with # a dtype attribute. - self.assert_equal(expected, actual) + assert_equal(expected, actual) def test_roundtrip_coordinates(self): original = Dataset({'foo': ('x', [0, 1])}, @@ -933,7 +933,7 @@ def test_compression_encoding(self): # regression test for #156 expected = data.isel(dim1=0) with self.roundtrip(expected) as actual: - self.assert_equal(expected, actual) + assert_equal(expected, actual) def test_encoding_chunksizes_unlimited(self): # regression test for GH1225 @@ -948,7 +948,7 @@ def test_encoding_chunksizes_unlimited(self): 'original_shape': (3,), } with self.roundtrip(ds) as actual: - self.assert_equal(ds, actual) + assert_equal(ds, actual) def test_mask_and_scale(self): with create_tmp_file() as tmp_file: @@ -1515,12 +1515,12 @@ def test_encoding_unlimited_dims(self): with self.roundtrip(ds, save_kwargs=dict(unlimited_dims=['y'])) as actual: assert actual.encoding['unlimited_dims'] == set('y') - self.assert_equal(ds, actual) + assert_equal(ds, actual) ds.encoding = {'unlimited_dims': ['y']} with self.roundtrip(ds) as actual: assert actual.encoding['unlimited_dims'] == set('y') - self.assert_equal(ds, actual) + assert_equal(ds, actual) class GenericNetCDFDataTestAutocloseTrue(GenericNetCDFDataTest): @@ -1548,7 +1548,7 @@ def test_array_type_after_indexing(self): def test_complex(self): expected = Dataset({'x': ('y', np.ones(5) + 1j * np.ones(5))}) with self.roundtrip(expected) as actual: - self.assert_equal(expected, actual) + assert_equal(expected, actual) @pytest.mark.xfail(reason='https://github.com/pydata/xarray/issues/535') def test_cross_engine_read_write_netcdf4(self): @@ -1578,11 +1578,11 @@ def test_encoding_unlimited_dims(self): with self.roundtrip(ds, save_kwargs=dict(unlimited_dims=['y'])) as actual: assert actual.encoding['unlimited_dims'] == set('y') - self.assert_equal(ds, actual) + assert_equal(ds, actual) ds.encoding = {'unlimited_dims': ['y']} with self.roundtrip(ds) as actual: assert actual.encoding['unlimited_dims'] == set('y') - self.assert_equal(ds, actual) + assert_equal(ds, actual) # tests pending h5netcdf fix @@ -2032,7 +2032,7 @@ def create_datasets(self, **kwargs): def test_cmp_local_file(self): with self.create_datasets() as (actual, expected): - self.assert_equal(actual, expected) + assert_equal(actual, expected) # global attributes should be global attributes on the dataset assert 'NC_GLOBAL' not in actual.attrs @@ -2044,14 +2044,14 @@ def test_cmp_local_file(self): assert actual.attrs.keys() == expected.attrs.keys() with self.create_datasets() as (actual, expected): - self.assert_equal(actual.isel(l=2), expected.isel(l=2)) + assert_equal(actual.isel(l=2), expected.isel(l=2)) with self.create_datasets() as (actual, expected): - self.assert_equal(actual.isel(i=0, j=-1), + assert_equal(actual.isel(i=0, j=-1), expected.isel(i=0, j=-1)) with self.create_datasets() as (actual, expected): - self.assert_equal(actual.isel(j=slice(1, 2)), + assert_equal(actual.isel(j=slice(1, 2)), expected.isel(j=slice(1, 2))) def test_compatible_to_netcdf(self): @@ -2061,12 +2061,12 @@ def test_compatible_to_netcdf(self): actual.to_netcdf(tmp_file) actual = open_dataset(tmp_file) actual['bears'] = actual['bears'].astype(str) - self.assert_equal(actual, expected) + assert_equal(actual, expected) @requires_dask def test_dask(self): with self.create_datasets(chunks={'j': 2}) as (actual, expected): - self.assert_equal(actual, expected) + assert_equal(actual, expected) @network diff --git a/xarray/tests/test_combine.py b/xarray/tests/test_combine.py index ef3512588dc..5546e5d3da7 100644 --- a/xarray/tests/test_combine.py +++ b/xarray/tests/test_combine.py @@ -66,9 +66,9 @@ def rectify_dim_order(dataset): self.assertArrayEqual(actual['extra'].values, expected) actual = concat(datasets, data[dim], coords='different') - self.assert_equal(data['extra'], actual['extra']) + assert_equal(data['extra'], actual['extra']) actual = concat(datasets, data[dim], coords='minimal') - self.assert_equal(data['extra'], actual['extra']) + assert_equal(data['extra'], actual['extra']) # verify that the dim argument takes precedence over # concatenating dataset variables of the same name @@ -248,7 +248,7 @@ def test_concat(self): expected = DataArray(np.array([foo.values, bar.values]), dims=['w', 'x', 'y'], coords={'x': [0, 1]}) actual = concat([foo, bar], 'w') - self.assert_equal(expected, actual) + assert_equal(expected, actual) # from iteration: grouped = [g for _, g in foo.groupby('x')] stacked = concat(grouped, ds['x']) diff --git a/xarray/tests/test_dask.py b/xarray/tests/test_dask.py index 936bd5736a1..a144b0dd67f 100644 --- a/xarray/tests/test_dask.py +++ b/xarray/tests/test_dask.py @@ -246,7 +246,7 @@ def assertLazyAndAllClose(self, expected, actual): self.assertLazyAnd(expected, actual, self.assertDataArrayAllClose) def assertLazyAndEqual(self, expected, actual): - self.assertLazyAnd(expected, actual, self.assert_equal) + self.assertLazyAnd(expected, actual, assert_equal) def setUp(self): self.values = np.random.randn(4, 6) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index b370a1a51c3..c0c71169510 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -179,7 +179,7 @@ def test_name(self): copied = arr.copy() arr.name = 'bar' assert arr.name == 'bar' - self.assert_equal(copied, arr) + assert_equal(copied, arr) actual = DataArray(IndexVariable('x', [3])) actual.name = 'y' @@ -311,16 +311,16 @@ def test_constructor_from_self_described(self): assert_identical(expected, actual) actual = DataArray(expected.values, actual.coords) - self.assert_equal(expected, actual) + assert_equal(expected, actual) frame = pd.DataFrame(data, index=pd.Index(['a', 'b'], name='x'), columns=pd.Index([-1, -2], name='y')) actual = DataArray(frame) - self.assert_equal(expected, actual) + assert_equal(expected, actual) series = pd.Series(data[0], index=pd.Index([-1, -2], name='y')) actual = DataArray(series) - self.assert_equal(expected[0].reset_coords('x', drop=True), + assert_equal(expected[0].reset_coords('x', drop=True), actual) panel = pd.Panel({0: frame}) @@ -504,9 +504,9 @@ def test_getitem_dataarray(self): # boolean indexing ind = xr.DataArray([True, True, False], dims=['x']) - self.assert_equal(da[ind], da[[0, 1], :]) - self.assert_equal(da[ind], da[[0, 1]]) - self.assert_equal(da[ind], da[ind.values]) + assert_equal(da[ind], da[[0, 1], :]) + assert_equal(da[ind], da[[0, 1]]) + assert_equal(da[ind], da[ind.values]) def test_setitem(self): # basic indexing should work as numpy's indexing @@ -780,7 +780,7 @@ def test_sel_dataarray(self): self.assertArrayEqual(actual, da.isel(x=[0, 1, 2])) assert 'new_dim' in actual.dims assert 'new_dim' in actual.coords - self.assert_equal(actual['new_dim'].drop('x'), + assert_equal(actual['new_dim'].drop('x'), ind['new_dim']) def test_sel_no_index(self): @@ -1443,10 +1443,10 @@ def test_array_interface(self): # test ufuncs expected = deepcopy(self.ds) expected['foo'][:] = np.sin(self.x) - self.assert_equal(expected['foo'], np.sin(self.dv)) + assert_equal(expected['foo'], np.sin(self.dv)) assert_array_equal(self.dv, np.maximum(self.v, self.dv)) bar = Variable(['x', 'y'], np.zeros((10, 20))) - self.assert_equal(self.dv, np.maximum(self.dv, bar)) + assert_equal(self.dv, np.maximum(self.dv, bar)) def test_is_null(self): x = np.random.RandomState(42).randn(5, 6) @@ -1463,15 +1463,15 @@ def test_math(self): a = self.dv # variable math was already tested extensively, so let's just make sure # that all types are properly converted here - self.assert_equal(a, +a) - self.assert_equal(a, a + 0) - self.assert_equal(a, 0 + a) - self.assert_equal(a, a + 0 * v) - self.assert_equal(a, 0 * v + a) - self.assert_equal(a, a + 0 * x) - self.assert_equal(a, 0 * x + a) - self.assert_equal(a, a + 0 * a) - self.assert_equal(a, 0 * a + a) + assert_equal(a, +a) + assert_equal(a, a + 0) + assert_equal(a, 0 + a) + assert_equal(a, a + 0 * v) + assert_equal(a, 0 * v + a) + assert_equal(a, a + 0 * x) + assert_equal(a, 0 * x + a) + assert_equal(a, a + 0 * a) + assert_equal(a, 0 * a + a) def test_math_automatic_alignment(self): a = DataArray(range(5), [('x', range(5))]) @@ -1787,7 +1787,7 @@ def test_reduce(self): actual = orig.mean(dim='x', skipna=True) expected = DataArray(orig.values.astype(int), dims=['x', 'y']).mean('x') - self.assert_equal(actual, expected) + assert_equal(actual, expected) # skip due to bug in older versions of numpy.nanpercentile def test_quantile(self): @@ -1971,7 +1971,7 @@ def test_groupby_apply_ndarray(self): array = self.make_groupby_example_array() grouped = array.groupby('abc') actual = grouped.apply(np.asarray) - self.assert_equal(array, actual) + assert_equal(array, actual) def test_groupby_apply_changes_metadata(self): def change_metadata(x): @@ -1984,7 +1984,7 @@ def change_metadata(x): actual = grouped.apply(change_metadata) expected = array.copy() expected = change_metadata(expected) - self.assert_equal(expected, actual) + assert_equal(expected, actual) def test_groupby_math(self): array = self.make_groupby_example_array() @@ -2791,7 +2791,7 @@ def test_to_masked_array(self): x_masked_2 = da.to_masked_array() da_2 = DataArray(x_masked_2) self.assertArrayEqual(x_masked, x_masked_2) - self.assert_equal(da, da_2) + assert_equal(da, da_2) da_masked_array = da.to_masked_array(copy=True) assert isinstance(da_masked_array, np.ma.MaskedArray) @@ -3042,7 +3042,7 @@ def test_to_dataset_retains_keys(self): # convert to dateset and back again result = array.to_dataset('x').to_array(dim='x') - self.assert_equal(array, result) + assert_equal(array, result) def test__title_for_slice(self): array = DataArray(np.ones((4, 3, 2)), dims=['a', 'b', 'c'], @@ -3070,7 +3070,7 @@ def test_dataarray_diff_n1(self): da = DataArray(np.random.randn(3, 4), dims=['x', 'y']) actual = da.diff('y') expected = DataArray(np.diff(da.values, axis=1), dims=['x', 'y']) - self.assert_equal(expected, actual) + assert_equal(expected, actual) def test_coordinate_diff(self): # regression test for GH634 @@ -3079,7 +3079,7 @@ def test_coordinate_diff(self): expected = DataArray([1] * 9, dims=['lon'], coords=[range(1, 10)], name='lon') actual = lon.diff('lon') - self.assert_equal(expected, actual) + assert_equal(expected, actual) def test_shift(self): arr = DataArray([1, 2, 3], dims='x') @@ -3146,14 +3146,14 @@ def test_dot(self): actual = da.dot(dm) expected_vals = np.tensordot(da_vals, dm_vals, [2, 0]) expected = DataArray(expected_vals, coords=[x, y], dims=['x', 'y']) - self.assert_equal(expected, actual) + assert_equal(expected, actual) # all shared dims actual = da.dot(da) expected_vals = np.tensordot(da_vals, da_vals, axes=([0, 1, 2], [0, 1, 2])) expected = DataArray(expected_vals) - self.assert_equal(expected, actual) + assert_equal(expected, actual) # multiple shared dims dm_vals = np.arange(20 * 5 * 4).reshape((20, 5, 4)) @@ -3162,7 +3162,7 @@ def test_dot(self): actual = da.dot(dm) expected_vals = np.tensordot(da_vals, dm_vals, axes=([1, 2], [1, 2])) expected = DataArray(expected_vals, coords=[x, j], dims=['x', 'j']) - self.assert_equal(expected, actual) + assert_equal(expected, actual) with pytest.raises(NotImplementedError): da.dot(dm.to_dataset(name='dm')) @@ -3183,7 +3183,7 @@ def test_binary_op_join_setting(self): missing_3, join=align_type) expected = xr.DataArray([np.nan, 2, 4, np.nan], [(dim, [0, 1, 2, 3])]) - self.assert_equal(actual, expected) + assert_equal(actual, expected) def test_combine_first(self): ar0 = DataArray([[0, 0], [0, 0]], [('x', ['a', 'b']), ('y', [-1, 0])]) @@ -3193,17 +3193,17 @@ def test_combine_first(self): actual = ar0.combine_first(ar1) expected = DataArray([[0, 0, np.nan], [0, 0, 1], [np.nan, 1, 1]], [('x', ['a', 'b', 'c']), ('y', [-1, 0, 1])]) - self.assert_equal(actual, expected) + assert_equal(actual, expected) actual = ar1.combine_first(ar0) expected = DataArray([[0, 0, np.nan], [0, 1, 1], [np.nan, 1, 1]], [('x', ['a', 'b', 'c']), ('y', [-1, 0, 1])]) - self.assert_equal(actual, expected) + assert_equal(actual, expected) actual = ar0.combine_first(ar2) expected = DataArray([[0, 0], [0, 0], [2, 2]], [('x', ['a', 'b', 'd']), ('y', [-1, 0])]) - self.assert_equal(actual, expected) + assert_equal(actual, expected) def test_sortby(self): da = DataArray([[1, 2], [3, 4], [5, 6]], @@ -3218,34 +3218,34 @@ def test_sortby(self): expected = sorted1d dax = DataArray([100, 99, 98], [('x', ['c', 'b', 'a'])]) actual = da.sortby(dax) - self.assert_equal(actual, expected) + assert_equal(actual, expected) # test descending order sort actual = da.sortby(dax, ascending=False) - self.assert_equal(actual, da) + assert_equal(actual, da) # test alignment (fills in nan for 'c') dax_short = DataArray([98, 97], [('x', ['b', 'a'])]) actual = da.sortby(dax_short) - self.assert_equal(actual, expected) + assert_equal(actual, expected) # test multi-dim sort by 1D dataarray values expected = sorted2d dax = DataArray([100, 99, 98], [('x', ['c', 'b', 'a'])]) day = DataArray([90, 80], [('y', [1, 0])]) actual = da.sortby([day, dax]) - self.assert_equal(actual, expected) + assert_equal(actual, expected) if LooseVersion(np.__version__) < LooseVersion('1.11.0'): pytest.skip('numpy 1.11.0 or later to support object data-type.') expected = sorted1d actual = da.sortby('x') - self.assert_equal(actual, expected) + assert_equal(actual, expected) expected = sorted2d actual = da.sortby(['x', 'y']) - self.assert_equal(actual, expected) + assert_equal(actual, expected) @requires_bottleneck def test_rank(self): @@ -3253,8 +3253,8 @@ def test_rank(self): ar = DataArray([[3, 4, np.nan, 1]]) expect_0 = DataArray([[1, 1, np.nan, 1]]) expect_1 = DataArray([[2, 3, np.nan, 1]]) - self.assert_equal(ar.rank('dim_0'), expect_0) - self.assert_equal(ar.rank('dim_1'), expect_1) + assert_equal(ar.rank('dim_0'), expect_0) + assert_equal(ar.rank('dim_1'), expect_1) # int x = DataArray([3, 2, 1]) self.assertDataArrayEqual(x.rank('dim_0'), x) @@ -3264,7 +3264,7 @@ def test_rank(self): x = DataArray([3.0, 1.0, np.nan, 2.0, 4.0], dims=('z',)) y = DataArray([0.75, 0.25, np.nan, 0.5, 1.0], dims=('z',)) - self.assert_equal(y.rank('z', pct=True), y) + assert_equal(y.rank('z', pct=True), y) @pytest.fixture(params=[1]) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 055f9a0282c..d6e77f2639c 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -324,14 +324,14 @@ def test_constructor_pandas_sequence(self): ) ds_based_on_pandas = Dataset(pandas_objs, ds.coords, attrs=ds.attrs) del ds_based_on_pandas['x'] - self.assert_equal(ds, ds_based_on_pandas) + assert_equal(ds, ds_based_on_pandas) # reindex pandas obj, check align works rearranged_index = reversed(pandas_objs['foo'].index) pandas_objs['foo'] = pandas_objs['foo'].reindex(rearranged_index) ds_based_on_pandas = Dataset(pandas_objs, ds.coords, attrs=ds.attrs) del ds_based_on_pandas['x'] - self.assert_equal(ds, ds_based_on_pandas) + assert_equal(ds, ds_based_on_pandas) def test_constructor_pandas_single(self): @@ -984,7 +984,7 @@ def test_isel_fancy(self): stations['dim2s'].variable] expected_var3 = data['var3'].variable[slice(None), stations['dim1s'].variable] - self.assert_equal(actual['a'].drop('dim2'), stations['a']) + assert_equal(actual['a'].drop('dim2'), stations['a']) self.assertArrayEqual(actual['var1'], expected_var1) self.assertArrayEqual(actual['var2'], expected_var2) self.assertArrayEqual(actual['var3'], expected_var3) @@ -1074,30 +1074,30 @@ def test_sel(self): loc_slicers = {'dim1': slice(None, None, 2), 'dim2': slice(0, 0.5), 'dim3': slice('a', 'c')} - self.assert_equal(data.isel(**int_slicers), + assert_equal(data.isel(**int_slicers), data.sel(**loc_slicers)) data['time'] = ('time', pd.date_range('2000-01-01', periods=20)) - self.assert_equal(data.isel(time=0), + assert_equal(data.isel(time=0), data.sel(time='2000-01-01')) - self.assert_equal(data.isel(time=slice(10)), + assert_equal(data.isel(time=slice(10)), data.sel(time=slice('2000-01-01', '2000-01-10'))) - self.assert_equal(data, data.sel(time=slice('1999', '2005'))) + assert_equal(data, data.sel(time=slice('1999', '2005'))) times = pd.date_range('2000-01-01', periods=3) - self.assert_equal(data.isel(time=slice(3)), + assert_equal(data.isel(time=slice(3)), data.sel(time=times)) - self.assert_equal(data.isel(time=slice(3)), + assert_equal(data.isel(time=slice(3)), data.sel(time=(data['time.dayofyear'] <= 3))) td = pd.to_timedelta(np.arange(3), unit='days') data = Dataset({'x': ('td', np.arange(3)), 'td': td}) - self.assert_equal(data, data.sel(td=td)) - self.assert_equal(data, data.sel(td=slice('3 days'))) - self.assert_equal(data.isel(td=0), + assert_equal(data, data.sel(td=td)) + assert_equal(data, data.sel(td=slice('3 days'))) + assert_equal(data.isel(td=0), data.sel(td=pd.Timedelta('0 days'))) - self.assert_equal(data.isel(td=0), + assert_equal(data.isel(td=0), data.sel(td=pd.Timedelta('0h'))) - self.assert_equal(data.isel(td=slice(1, 3)), + assert_equal(data.isel(td=slice(1, 3)), data.sel(td=slice('1 days', '2 days'))) def test_sel_dataarray(self): @@ -1105,14 +1105,14 @@ def test_sel_dataarray(self): ind = DataArray([0.0, 0.5, 1.0], dims=['dim2']) actual = data.sel(dim2=ind) - self.assert_equal(actual, data.isel(dim2=[0, 1, 2])) + assert_equal(actual, data.isel(dim2=[0, 1, 2])) # with different dimension ind = DataArray([0.0, 0.5, 1.0], dims=['new_dim']) actual = data.sel(dim2=ind) expected = data.isel(dim2=Variable('new_dim', [0, 1, 2])) assert 'new_dim' in actual.dims - self.assert_equal(actual, expected) + assert_equal(actual, expected) # Multi-dimensional ind = DataArray([[0.0], [0.5], [1.0]], dims=['new_dim', 'new_dim2']) @@ -1121,7 +1121,7 @@ def test_sel_dataarray(self): [[0], [1], [2]])) assert 'new_dim' in actual.dims assert 'new_dim2' in actual.dims - self.assert_equal(actual, expected) + assert_equal(actual, expected) # with coordinate ind = DataArray([0.0, 0.5, 1.0], dims=['new_dim'], @@ -1130,9 +1130,9 @@ def test_sel_dataarray(self): expected = data.isel(dim2=[0, 1, 2]).rename({'dim2': 'new_dim'}) assert 'new_dim' in actual.dims assert 'new_dim' in actual.coords - self.assert_equal(actual.drop('new_dim').drop('dim2'), + assert_equal(actual.drop('new_dim').drop('dim2'), expected.drop('new_dim')) - self.assert_equal(actual['new_dim'].drop('dim2'), + assert_equal(actual['new_dim'].drop('dim2'), ind['new_dim']) # with conflicted coordinate (silently ignored) @@ -1140,18 +1140,18 @@ def test_sel_dataarray(self): coords={'dim2': ['a', 'b', 'c']}) actual = data.sel(dim2=ind) expected = data.isel(dim2=[0, 1, 2]) - self.assert_equal(actual, expected) + assert_equal(actual, expected) # with conflicted coordinate (silently ignored) ind = DataArray([0.0, 0.5, 1.0], dims=['new_dim'], coords={'new_dim': ['a', 'b', 'c'], 'dim2': 3}) actual = data.sel(dim2=ind) - self.assert_equal(actual['new_dim'].drop('dim2'), + assert_equal(actual['new_dim'].drop('dim2'), ind['new_dim'].drop('dim2')) expected = data.isel(dim2=[0, 1, 2]) expected['dim2'] = (('new_dim'), expected['dim2'].values) - self.assert_equal(actual['dim2'].drop('new_dim'), + assert_equal(actual['dim2'].drop('new_dim'), expected['dim2']) assert actual['var1'].dims == ('dim1', 'new_dim') @@ -1162,7 +1162,7 @@ def test_sel_dataarray(self): 'new_dim': ('dim2', [1.1, 1.2, 1.3])}) actual = data.sel(dim2=ind) expected = data.isel(dim2=[0, 1, 2]) - self.assert_equal(actual.drop('new_dim'), expected) + assert_equal(actual.drop('new_dim'), expected) assert np.allclose(actual['new_dim'].values, ind['new_dim'].values) def test_sel_dataarray_mindex(self): @@ -2159,7 +2159,7 @@ def test_getitem(self): actual = data[['var1', 'var2']] expected = Dataset({'var1': data['var1'], 'var2': data['var2']}) - self.assert_equal(expected, actual) + assert_equal(expected, actual) actual = data['numbers'] expected = DataArray(data['numbers'].variable, @@ -2208,7 +2208,7 @@ def test_virtual_variables_time(self): # ensure they become coordinates expected = Dataset({}, {'dayofyear': data['time.dayofyear']}) actual = data[['time.dayofyear']] - self.assert_equal(expected, actual) + assert_equal(expected, actual) # non-coordinate variables ds = Dataset({'t': ('x', pd.date_range('2000-01-01', periods=3))}) assert (ds['t.year'] == 2000).all() @@ -2296,7 +2296,7 @@ def test_setitem_pandas(self): ds_copy = ds.copy() ds_copy['bar'] = ds['bar'].to_pandas() - self.assert_equal(ds, ds_copy) + assert_equal(ds, ds_copy) def test_setitem_auto_align(self): ds = Dataset() @@ -2471,14 +2471,14 @@ def test_groupby(self): ('c', data.isel(x=2))] for actual, expected in zip(groupby, expected_items): assert actual[0] == expected[0] - self.assert_equal(actual[1], expected[1]) + assert_equal(actual[1], expected[1]) def identity(x): return x for k in ['x', 'c', 'y']: actual = data.groupby(k, squeeze=False).apply(identity) - self.assert_equal(data, actual) + assert_equal(data, actual) def test_groupby_returns_new_type(self): data = Dataset({'z': (['x', 'y'], np.random.randn(3, 5))}) @@ -2559,10 +2559,10 @@ def reorder_dims(x): expected = ((ds + Variable('dim3', np.zeros(10))) .transpose('dim3', 'dim1', 'dim2', 'time')) actual = grouped + zeros - self.assert_equal(expected, actual) + assert_equal(expected, actual) actual = zeros + grouped - self.assert_equal(expected, actual) + assert_equal(expected, actual) with raises_regex(ValueError, 'incompat.* grouped binary'): grouped + ds @@ -2629,10 +2629,10 @@ def test_resample_and_first(self): for how in ['mean', 'sum', 'first', 'last', ]: method = getattr(actual, how) result = method() - self.assert_equal(expected, result) + assert_equal(expected, result) for method in [np.mean, ]: result = actual.reduce(method) - self.assert_equal(expected, result) + assert_equal(expected, result) def test_resample_by_mean_with_keep_attrs(self): times = pd.date_range('2000-01-01', freq='6H', periods=10) @@ -3262,9 +3262,9 @@ def test_reduce(self): actual = data.max() expected = Dataset(dict((k, v.max()) for k, v in iteritems(data.data_vars))) - self.assert_equal(expected, actual) + assert_equal(expected, actual) - self.assert_equal(data.min(dim=['dim1']), + assert_equal(data.min(dim=['dim1']), data.min(dim='dim1')) for reduct, expected in [('dim2', ['dim1', 'dim3', 'time']), @@ -3275,7 +3275,7 @@ def test_reduce(self): print(reduct, actual, expected) self.assertItemsEqual(actual, expected) - self.assert_equal(data.mean(dim=[]), data) + assert_equal(data.mean(dim=[]), data) # uint support data = xr.Dataset({'a': (('x', 'y'), @@ -3324,8 +3324,8 @@ def test_reduce_non_numeric(self): data1[v] = (dims, data, {'foo': 'variable'}) assert 'var4' not in data1.mean() - self.assert_equal(data1.mean(), data2.mean()) - self.assert_equal(data1.mean(dim='dim1'), + assert_equal(data1.mean(), data2.mean()) + assert_equal(data1.mean(dim='dim1'), data2.mean(dim='dim1')) def test_reduce_strings(self): @@ -3450,7 +3450,7 @@ def test_rank(self): # same as dataarray version x = z.var3 y = ds.var3.rank('dim3') - self.assert_equal(x, y) + assert_equal(x, y) # coordinates stick self.assertItemsEqual(list(z.coords), list(ds.coords)) self.assertItemsEqual(list(x.coords), list(y.coords)) @@ -3586,7 +3586,7 @@ def test_dataset_math_auto_align(self): actual = ds.isel(y=slice(1)) + ds.isel(y=slice(1, None)) expected = 2 * ds.drop(ds.y, dim='y') - self.assert_equal(actual, expected) + assert_equal(actual, expected) actual = ds + ds[['bar']] expected = (2 * ds[['bar']]).merge(ds.coords) @@ -3673,17 +3673,17 @@ def test_dataset_diff_n1_simple(self): ds = Dataset({'foo': ('x', [5, 5, 6, 6])}) actual = ds.diff('x') expected = Dataset({'foo': ('x', [0, 1, 0])}) - self.assert_equal(expected, actual) + assert_equal(expected, actual) def test_dataset_diff_n1_label(self): ds = Dataset({'foo': ('x', [5, 5, 6, 6])}, {'x': [0, 1, 2, 3]}) actual = ds.diff('x', label='lower') expected = Dataset({'foo': ('x', [0, 1, 0])}, {'x': [0, 1, 2]}) - self.assert_equal(expected, actual) + assert_equal(expected, actual) actual = ds.diff('x', label='upper') expected = Dataset({'foo': ('x', [0, 1, 0])}, {'x': [1, 2, 3]}) - self.assert_equal(expected, actual) + assert_equal(expected, actual) def test_dataset_diff_n1(self): ds = create_test_data(seed=1) @@ -3698,7 +3698,7 @@ def test_dataset_diff_n1(self): expected['var3'] = ds['var3'] expected = Dataset(expected, coords={'time': ds['time'].values}) expected.coords['numbers'] = ('dim3', ds['numbers'].values) - self.assert_equal(expected, actual) + assert_equal(expected, actual) def test_dataset_diff_n2(self): ds = create_test_data(seed=1) @@ -3713,7 +3713,7 @@ def test_dataset_diff_n2(self): expected['var3'] = ds['var3'] expected = Dataset(expected, coords={'time': ds['time'].values}) expected.coords['numbers'] = ('dim3', ds['numbers'].values) - self.assert_equal(expected, actual) + assert_equal(expected, actual) def test_dataset_diff_exception_n_neg(self): ds = create_test_data(seed=1) @@ -3813,29 +3813,29 @@ def test_binary_op_join_setting(self): with xr.set_options(arithmetic_join='outer'): actual = missing_2 + missing_0 expected = xr.Dataset({'x': [0, 1, 2]}) - self.assert_equal(actual, expected) + assert_equal(actual, expected) # arithmetic join also applies to data_vars ds1 = xr.Dataset({'foo': 1, 'bar': 2}) ds2 = xr.Dataset({'bar': 2, 'baz': 3}) expected = xr.Dataset({'bar': 4}) # default is inner joining actual = ds1 + ds2 - self.assert_equal(actual, expected) + assert_equal(actual, expected) with xr.set_options(arithmetic_join='outer'): expected = xr.Dataset({'foo': np.nan, 'bar': 4, 'baz': np.nan}) actual = ds1 + ds2 - self.assert_equal(actual, expected) + assert_equal(actual, expected) with xr.set_options(arithmetic_join='left'): expected = xr.Dataset({'foo': np.nan, 'bar': 4}) actual = ds1 + ds2 - self.assert_equal(actual, expected) + assert_equal(actual, expected) with xr.set_options(arithmetic_join='right'): expected = xr.Dataset({'bar': 4, 'baz': np.nan}) actual = ds1 + ds2 - self.assert_equal(actual, expected) + assert_equal(actual, expected) def test_full_like(self): # For more thorough tests, see test_variable.py @@ -3870,15 +3870,15 @@ def test_combine_first(self): expected = Dataset({'dsx0': ('x', [0, 0, np.nan]), 'dsx1': ('x', [np.nan, 1, 1])}, coords={'x': ['a', 'b', 'c']}) - self.assert_equal(actual, expected) - self.assert_equal(actual, xr.merge([dsx0, dsx1])) + assert_equal(actual, expected) + assert_equal(actual, xr.merge([dsx0, dsx1])) # works just like xr.merge([self, other]) dsy2 = DataArray([2, 2, 2], [('x', ['b', 'c', 'd'])]).to_dataset(name='dsy2') actual = dsx0.combine_first(dsy2) expected = xr.merge([dsy2, dsx0]) - self.assert_equal(actual, expected) + assert_equal(actual, expected) def test_sortby(self): ds = Dataset({'A': DataArray([[1, 2], [3, 4], [5, 6]], @@ -3902,16 +3902,16 @@ def test_sortby(self): expected = sorted1d dax = DataArray([100, 99, 98], [('x', ['c', 'b', 'a'])]) actual = ds.sortby(dax) - self.assert_equal(actual, expected) + assert_equal(actual, expected) # test descending order sort actual = ds.sortby(dax, ascending=False) - self.assert_equal(actual, ds) + assert_equal(actual, ds) # test alignment (fills in nan for 'c') dax_short = DataArray([98, 97], [('x', ['b', 'a'])]) actual = ds.sortby(dax_short) - self.assert_equal(actual, expected) + assert_equal(actual, expected) # test 1-D lexsort # dax0 is sorted first to give indices of [1, 2, 0] @@ -3919,13 +3919,13 @@ def test_sortby(self): dax0 = DataArray([100, 95, 95], [('x', ['c', 'b', 'a'])]) dax1 = DataArray([0, 1, 0], [('x', ['c', 'b', 'a'])]) actual = ds.sortby([dax0, dax1]) # lexsort underneath gives [2, 1, 0] - self.assert_equal(actual, expected) + assert_equal(actual, expected) expected = sorted2d # test multi-dim sort by 1D dataarray values day = DataArray([90, 80], [('y', [1, 0])]) actual = ds.sortby([day, dax]) - self.assert_equal(actual, expected) + assert_equal(actual, expected) # test exception-raising with pytest.raises(KeyError) as excinfo: @@ -3940,7 +3940,7 @@ def test_sortby(self): expected = sorted1d actual = ds.sortby('x') - self.assert_equal(actual, expected) + assert_equal(actual, expected) # test pandas.MultiIndex indices = (('b', 1), ('b', 0), ('a', 1), ('a', 0)) @@ -3957,26 +3957,26 @@ def test_sortby(self): ('y', [1, 0])]), 'B': DataArray([[11, 12], [9, 10], [7, 8], [5, 6]], dims=['x', 'y'])}) - self.assert_equal(actual, expected) + assert_equal(actual, expected) # multi-dim sort by coordinate objects expected = sorted2d actual = ds.sortby(['x', 'y']) - self.assert_equal(actual, expected) + assert_equal(actual, expected) # test descending order sort actual = ds.sortby(['x', 'y'], ascending=False) - self.assert_equal(actual, ds) + assert_equal(actual, ds) def test_attribute_access(self): ds = create_test_data(seed=1) for key in ['var1', 'var2', 'var3', 'time', 'dim1', 'dim2', 'dim3', 'numbers']: - self.assert_equal(ds[key], getattr(ds, key)) + assert_equal(ds[key], getattr(ds, key)) assert key in dir(ds) for key in ['dim3', 'dim1', 'numbers']: - self.assert_equal(ds['var3'][key], getattr(ds.var3, key)) + assert_equal(ds['var3'][key], getattr(ds.var3, key)) assert key in dir(ds['var3']) # attrs assert ds['var3'].attrs['foo'] == ds.var3.foo diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 02585e4c416..5e3470b4ef0 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -1189,7 +1189,7 @@ def test_groups(self): upperleft_array = self.darray.loc[upperleft_dict] z0 = self.darray.isel(z=0) - self.assert_equal(upperleft_array, z0) + assert_equal(upperleft_array, z0) @pytest.mark.slow def test_float_index(self): From 04cd8a514e681e34ddfb1ce259697de3af79e970 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 15:02:05 -0500 Subject: [PATCH 16/41] pytest.warns message matching --- xarray/tests/test_coding_times.py | 2 +- xarray/tests/test_conventions.py | 2 +- xarray/tests/test_extensions.py | 2 +- xarray/tests/test_utils.py | 2 +- xarray/tests/test_variable.py | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/xarray/tests/test_coding_times.py b/xarray/tests/test_coding_times.py index 9e15f865ad8..0cf36c57d6f 100644 --- a/xarray/tests/test_coding_times.py +++ b/xarray/tests/test_coding_times.py @@ -166,7 +166,7 @@ def test_decode_non_standard_calendar_single_element_fallback(self): dt = nc4.netcdftime.datetime(2001, 2, 29) for calendar in ['360_day', 'all_leap', '366_day']: num_time = nc4.date2num(dt, units, calendar) - with pytest.warns(Warning, 'Unable to decode time axis'): + with pytest.warns(Warning, match='Unable to decode time axis'): actual = coding.times.decode_cf_datetime(num_time, units, calendar=calendar) expected = np.asarray(nc4.num2date(num_time, units, calendar)) diff --git a/xarray/tests/test_conventions.py b/xarray/tests/test_conventions.py index 7dda6d3aed3..bc28ad25b37 100644 --- a/xarray/tests/test_conventions.py +++ b/xarray/tests/test_conventions.py @@ -196,7 +196,7 @@ def test_incompatible_attributes(self): def test_missing_fillvalue(self): v = Variable(['x'], np.array([np.nan, 1, 2, 3])) v.encoding = {'dtype': 'int16'} - with pytest.warns(Warning, 'floating point data as an integer'): + with pytest.warns(Warning, match='floating point data as an integer'): conventions.encode_cf_variable(v) def test_multidimensional_coordinates(self): diff --git a/xarray/tests/test_extensions.py b/xarray/tests/test_extensions.py index 818cc345731..9456f335572 100644 --- a/xarray/tests/test_extensions.py +++ b/xarray/tests/test_extensions.py @@ -55,7 +55,7 @@ def foo(self): del xr.Dataset.demo assert not hasattr(xr.Dataset, 'demo') - with pytest.warns(Warning, 'overriding a preexisting attribute'): + with pytest.warns(Warning, match='overriding a preexisting attribute'): @xr.register_dataarray_accessor('demo') class Foo(object): pass diff --git a/xarray/tests/test_utils.py b/xarray/tests/test_utils.py index d3c2c9a50f5..5741333e18f 100644 --- a/xarray/tests/test_utils.py +++ b/xarray/tests/test_utils.py @@ -17,7 +17,7 @@ def new_method(): pass old_method = utils.alias(new_method, 'old_method') assert 'deprecated' in old_method.__doc__ - with pytest.warns(Warning, 'deprecated'): + with pytest.warns(Warning, match='deprecated'): old_method() diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index 7c9bb9de8a9..31a1d85c8a2 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -1700,7 +1700,7 @@ def test_concat_multiindex(self): assert isinstance(actual.to_index(), pd.MultiIndex) def test_coordinate_alias(self): - with pytest.warns(Warning, 'deprecated'): + with pytest.warns(Warning, match='deprecated'): x = Coordinate('x', [1, 2, 3]) assert isinstance(x, IndexVariable) From c69b6aaa8382ba4253392fc8025ff145d680f8b6 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 15:20:20 -0500 Subject: [PATCH 17/41] undo backend changes - too many errors --- xarray/tests/test_backends.py | 343 +++++++++++++++++----------------- 1 file changed, 171 insertions(+), 172 deletions(-) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index fe31673302c..4263ae2bf1e 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -29,8 +29,7 @@ requires_scipy_or_netCDF4, requires_dask, requires_h5netcdf, requires_pynio, requires_pathlib, requires_zarr, requires_rasterio, has_netCDF4, has_scipy, assert_allclose, - flaky, network, assert_identical, raises_regex, assert_equal, - assert_identical) + flaky, network, assert_identical, raises_regex) from .test_dataset import create_test_data @@ -118,11 +117,11 @@ def __getitem__(self, key): array = UnreliableArray([0]) with pytest.raises(UnreliableArrayFailure): array[0] - assert array[0] == 0 + self.assertEqual(array[0], 0) actual = robust_getitem(array, 0, catch=UnreliableArrayFailure, initial_delay=0) - assert actual == 0 + self.assertEqual(actual, 0) class NetCDF3Only(object): @@ -174,7 +173,7 @@ def test_zero_dimensional_variable(self): expected['bytes_var'] = ([], b'foobar') expected['string_var'] = ([], u'foobar') with self.roundtrip(expected) as actual: - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) def test_write_store(self): expected = create_test_data() @@ -202,7 +201,7 @@ def test_roundtrip_test_data(self): expected = create_test_data() with self.roundtrip(expected) as actual: self.check_dtypes_roundtripped(expected, actual) - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) def test_load(self): expected = create_test_data() @@ -214,12 +213,12 @@ def assert_loads(vars=None): with self.roundtrip(expected) as actual: for k, v in actual.variables.items(): # IndexVariables are eagerly loaded into memory - assert v._in_memory == (k in actual.dims) + self.assertEqual(v._in_memory, k in actual.dims) yield actual for k, v in actual.variables.items(): if k in vars: - assert v._in_memory - assert_identical(expected, actual) + self.assertTrue(v._in_memory) + self.assertDatasetIdentical(expected, actual) with pytest.raises(AssertionError): # make sure the contextmanager works! @@ -235,7 +234,7 @@ def assert_loads(vars=None): # verify we can read data even after closing the file with self.roundtrip(expected) as ds: actual = ds.load() - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) def test_dataset_compute(self): expected = create_test_data() @@ -244,17 +243,17 @@ def test_dataset_compute(self): # Test Dataset.compute() for k, v in actual.variables.items(): # IndexVariables are eagerly cached - assert v._in_memory == (k in actual.dims) + self.assertEqual(v._in_memory, k in actual.dims) computed = actual.compute() for k, v in actual.variables.items(): - assert v._in_memory == (k in actual.dims) + self.assertEqual(v._in_memory, k in actual.dims) for v in computed.variables.values(): - assert v._in_memory + self.assertTrue(v._in_memory) - assert_identical(expected, actual) - assert_identical(expected, computed) + self.assertDatasetIdentical(expected, actual) + self.assertDatasetIdentical(expected, computed) def test_pickle(self): expected = Dataset({'foo': ('x', [42])}) @@ -264,14 +263,14 @@ def test_pickle(self): # windows doesn't like opening the same file twice roundtripped.close() unpickled_ds = pickle.loads(raw_pickle) - assert_identical(expected, unpickled_ds) + self.assertDatasetIdentical(expected, unpickled_ds) def test_pickle_dataarray(self): expected = Dataset({'foo': ('x', [42])}) with self.roundtrip( expected, allow_cleanup_failure=ON_WINDOWS) as roundtripped: unpickled_array = pickle.loads(pickle.dumps(roundtripped['foo'])) - assert_identical(expected['foo'], unpickled_array) + self.assertDatasetIdentical(expected['foo'], unpickled_array) def test_dataset_caching(self): expected = Dataset({'foo': ('x', [5, 6, 7])}) @@ -292,7 +291,7 @@ def test_dataset_caching(self): def test_roundtrip_None_variable(self): expected = Dataset({None: (('x', 'y'), [[0, 1], [2, 3]])}) with self.roundtrip(expected) as actual: - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) def test_roundtrip_object_dtype(self): floats = np.array([0.0, 0.0, 1.0, 2.0, 3.0], dtype=object) @@ -313,7 +312,7 @@ def test_roundtrip_object_dtype(self): expected = original.copy(deep=True) with self.roundtrip(original) as actual: try: - assert_identical(expected, actual) + 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) @@ -323,44 +322,44 @@ def test_roundtrip_object_dtype(self): # https://github.com/pydata/xarray/issues/1647 expected['bytes_nans'][-1] = b'' expected['strings_nans'][-1] = u'' - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) def test_roundtrip_string_data(self): expected = Dataset({'x': ('t', ['ab', 'cdef'])}) with self.roundtrip(expected) as actual: - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) def test_roundtrip_string_encoded_characters(self): expected = Dataset({'x': ('t', [u'ab', u'cdef'])}) expected['x'].encoding['dtype'] = 'S1' with self.roundtrip(expected) as actual: - assert_identical(expected, actual) - assert actual['x'].encoding['_Encoding'] == 'utf-8' + self.assertDatasetIdentical(expected, actual) + self.assertEqual(actual['x'].encoding['_Encoding'], 'utf-8') expected['x'].encoding['_Encoding'] = 'ascii' with self.roundtrip(expected) as actual: - assert_identical(expected, actual) - assert actual['x'].encoding['_Encoding'] == 'ascii' + self.assertDatasetIdentical(expected, actual) + self.assertEqual(actual['x'].encoding['_Encoding'], 'ascii') def test_roundtrip_datetime_data(self): times = pd.to_datetime(['2000-01-01', '2000-01-02', 'NaT']) expected = Dataset({'t': ('t', times), 't0': times[0]}) kwds = {'encoding': {'t0': {'units': 'days since 1950-01-01'}}} with self.roundtrip(expected, save_kwargs=kwds) as actual: - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) assert actual.t0.encoding['units'] == 'days since 1950-01-01' def test_roundtrip_timedelta_data(self): time_deltas = pd.to_timedelta(['1h', '2h', 'NaT']) expected = Dataset({'td': ('td', time_deltas), 'td0': time_deltas[0]}) with self.roundtrip(expected) as actual: - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) def test_roundtrip_float64_data(self): expected = Dataset({'x': ('y', np.array([1.0, 2.0, np.pi], dtype='float64'))}) with self.roundtrip(expected) as actual: - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) def test_roundtrip_example_1_netcdf(self): expected = open_example_dataset('example_1.nc') @@ -369,33 +368,33 @@ def test_roundtrip_example_1_netcdf(self): # will depend on the encoding used. For example, # without CF encoding 'actual' will end up with # a dtype attribute. - assert_equal(expected, actual) + self.assertDatasetEqual(expected, actual) 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: - assert_identical(original, actual) + self.assertDatasetIdentical(original, actual) def test_roundtrip_global_coordinates(self): original = Dataset({'x': [2, 3], 'y': ('a', [42]), 'z': ('x', [4, 5])}) with self.roundtrip(original) as actual: - assert_identical(original, actual) + self.assertDatasetIdentical(original, actual) def test_roundtrip_coordinates_with_space(self): original = Dataset(coords={'x': 0, 'y z': 1}) expected = Dataset({'y z': 1}, {'x': 0}) with pytest.warns(xr.SerializationWarning): with self.roundtrip(original) as actual: - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) def test_roundtrip_boolean_dtype(self): original = create_boolean_data() - assert original['x'].dtype == 'bool' + self.assertEqual(original['x'].dtype, 'bool') with self.roundtrip(original) as actual: - assert_identical(original, actual) - assert actual['x'].dtype == 'bool' + self.assertDatasetIdentical(original, actual) + self.assertEqual(actual['x'].dtype, 'bool') def test_orthogonal_indexing(self): in_memory = create_test_data() @@ -439,7 +438,7 @@ def test_isel_dataarray(self): with self.roundtrip(in_memory) as on_disk: expected = in_memory.isel(dim2=in_memory['dim2'] < 3) actual = on_disk.isel(dim2=on_disk['dim2'] < 3) - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) def validate_array_type(self, ds): # Make sure that only NumpyIndexingAdapter stores a bare np.ndarray. @@ -511,11 +510,11 @@ def test_roundtrip_bytes_with_fill_value(self): expected = original.copy(deep=True) print(original) with self.roundtrip(original) as actual: - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) original = Dataset({'x': ('t', values, {}, {'_FillValue': b''})}) with self.roundtrip(original) as actual: - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) def test_roundtrip_string_with_fill_value_nchar(self): values = np.array([u'ab', u'cdef', np.nan], dtype=object) @@ -526,27 +525,27 @@ def test_roundtrip_string_with_fill_value_nchar(self): # Not supported yet. with pytest.raises(NotImplementedError): with self.roundtrip(original) as actual: - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) def test_unsigned_roundtrip_mask_and_scale(self): decoded = create_unsigned_masked_scaled_data() encoded = create_encoded_unsigned_masked_scaled_data() with self.roundtrip(decoded) as actual: for k in decoded.variables: - assert decoded.variables[k].dtype == \ - actual.variables[k].dtype + self.assertEqual(decoded.variables[k].dtype, + actual.variables[k].dtype) self.assertDatasetAllClose(decoded, actual, decode_bytes=False) with self.roundtrip(decoded, open_kwargs=dict(decode_cf=False)) as actual: for k in encoded.variables: - assert encoded.variables[k].dtype == \ - actual.variables[k].dtype + self.assertEqual(encoded.variables[k].dtype, + actual.variables[k].dtype) self.assertDatasetAllClose(encoded, actual, decode_bytes=False) with self.roundtrip(encoded, open_kwargs=dict(decode_cf=False)) as actual: for k in encoded.variables: - assert encoded.variables[k].dtype == \ - actual.variables[k].dtype + self.assertEqual(encoded.variables[k].dtype, + actual.variables[k].dtype) self.assertDatasetAllClose(encoded, actual, decode_bytes=False) # make sure roundtrip encoding didn't change the # original dataset. @@ -554,14 +553,14 @@ def test_unsigned_roundtrip_mask_and_scale(self): encoded, create_encoded_unsigned_masked_scaled_data()) with self.roundtrip(encoded) as actual: for k in decoded.variables: - assert decoded.variables[k].dtype == \ - actual.variables[k].dtype + self.assertEqual(decoded.variables[k].dtype, + actual.variables[k].dtype) self.assertDatasetAllClose(decoded, actual, decode_bytes=False) with self.roundtrip(encoded, open_kwargs=dict(decode_cf=False)) as actual: for k in encoded.variables: - assert encoded.variables[k].dtype == \ - actual.variables[k].dtype + self.assertEqual(encoded.variables[k].dtype, + actual.variables[k].dtype) self.assertDatasetAllClose(encoded, actual, decode_bytes=False) def test_roundtrip_mask_and_scale(self): @@ -595,25 +594,25 @@ def equals_latlon(obj): original = Dataset({'temp': ('x', [0, 1]), 'precip': ('x', [0, -1])}, {'lat': ('x', [2, 3]), 'lon': ('x', [4, 5])}) with self.roundtrip(original) as actual: - assert_identical(actual, original) + 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: - assert equals_latlon(ds['temp'].attrs['coordinates']) - assert equals_latlon(ds['precip'].attrs['coordinates']) - assert 'coordinates' not in ds.attrs - assert 'coordinates' not in ds['lat'].attrs - assert 'coordinates' not in ds['lon'].attrs + 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: - assert_identical(actual, modified) + 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: - assert equals_latlon(ds.attrs['coordinates']) - assert 'coordinates' not in ds['lat'].attrs - assert 'coordinates' not in ds['lon'].attrs + self.assertTrue(equals_latlon(ds.attrs['coordinates'])) + self.assertNotIn('coordinates', ds['lat'].attrs) + self.assertNotIn('coordinates', ds['lon'].attrs) def test_roundtrip_endian(self): ds = Dataset({'x': np.arange(3, 10, dtype='>i2'), @@ -626,7 +625,7 @@ def test_roundtrip_endian(self): # one hold mixed endian data (ds) the other should be # all big endian (actual). assertDatasetIdentical # should still pass though. - assert_identical(ds, actual) + self.assertDatasetIdentical(ds, actual) if type(self) is NetCDF4DataTest: ds['z'].encoding['endian'] = 'big' @@ -649,8 +648,8 @@ 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: - assert actual.x.encoding['dtype'] == 'f4' - assert ds.x.encoding == {} + self.assertEqual(actual.x.encoding['dtype'], 'f4') + self.assertEqual(ds.x.encoding, {}) kwargs = dict(encoding={'x': {'foo': 'bar'}}) with raises_regex(ValueError, 'unexpected encoding'): @@ -671,30 +670,30 @@ def test_encoding_kwarg(self): units = 'days since 1900-01-01' kwargs = dict(encoding={'t': {'units': units}}) with self.roundtrip(ds, save_kwargs=kwargs) as actual: - assert actual.t.encoding['units'] == units - assert_identical(actual, ds) + self.assertEqual(actual.t.encoding['units'], units) + self.assertDatasetIdentical(actual, ds) def test_default_fill_value(self): # Test default encoding for float: ds = Dataset({'x': ('y', np.arange(10.0))}) kwargs = dict(encoding={'x': {'dtype': 'f4'}}) with self.roundtrip(ds, save_kwargs=kwargs) as actual: - assert actual.x.encoding['_FillValue'] == \ - np.nan - assert ds.x.encoding == {} + self.assertEqual(actual.x.encoding['_FillValue'], + np.nan) + self.assertEqual(ds.x.encoding, {}) # Test default encoding for int: ds = Dataset({'x': ('y', np.arange(10.0))}) kwargs = dict(encoding={'x': {'dtype': 'int16'}}) with self.roundtrip(ds, save_kwargs=kwargs) as actual: - assert '_FillValue' not in actual.x.encoding - assert ds.x.encoding == {} + self.assertTrue('_FillValue' not in actual.x.encoding) + self.assertEqual(ds.x.encoding, {}) # Test default encoding for implicit int: ds = Dataset({'x': ('y', np.arange(10, dtype='int16'))}) with self.roundtrip(ds) as actual: - assert '_FillValue' not in actual.x.encoding - assert ds.x.encoding == {} + self.assertTrue('_FillValue' not in actual.x.encoding) + self.assertEqual(ds.x.encoding, {}) def test_explicitly_omit_fill_value(self): ds = Dataset({'x': ('y', [np.pi, -np.pi])}) @@ -706,14 +705,14 @@ def test_encoding_same_dtype(self): ds = Dataset({'x': ('y', np.arange(10.0, dtype='f4'))}) kwargs = dict(encoding={'x': {'dtype': 'f4'}}) with self.roundtrip(ds, save_kwargs=kwargs) as actual: - assert actual.x.encoding['dtype'] == 'f4' - assert ds.x.encoding == {} + self.assertEqual(actual.x.encoding['dtype'], 'f4') + self.assertEqual(ds.x.encoding, {}) def test_append_write(self): # regression for GH1215 data = create_test_data() with self.roundtrip_append(data) as actual: - assert_identical(data, actual) + self.assertDatasetIdentical(data, actual) def test_append_overwrite_values(self): # regression for GH1215 @@ -724,7 +723,7 @@ def test_append_overwrite_values(self): data['var9'] = data['var2'] * 3 self.save(data[['var2', 'var9']], tmp_file, mode='a') with self.open(tmp_file) as actual: - assert_identical(data, actual) + self.assertDatasetIdentical(data, actual) def test_vectorized_indexing(self): self._test_vectorized_indexing(vindex_support=False) @@ -821,9 +820,9 @@ def test_write_groups(self): data1.to_netcdf(tmp_file, group='data/1') data2.to_netcdf(tmp_file, group='data/2', mode='a') with open_dataset(tmp_file, group='data/1') as actual1: - assert_identical(data1, actual1) + self.assertDatasetIdentical(data1, actual1) with open_dataset(tmp_file, group='data/2') as actual2: - assert_identical(data2, actual2) + self.assertDatasetIdentical(data2, actual2) def test_roundtrip_string_with_fill_value_vlen(self): values = np.array([u'ab', u'cdef', np.nan], dtype=object) @@ -836,12 +835,12 @@ def test_roundtrip_string_with_fill_value_vlen(self): original = Dataset({'x': ('t', values, {}, {'_FillValue': u'XXX'})}) with pytest.raises(NotImplementedError): with self.roundtrip(original) as actual: - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) original = Dataset({'x': ('t', values, {}, {'_FillValue': u''})}) with pytest.raises(NotImplementedError): with self.roundtrip(original) as actual: - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) def test_roundtrip_character_array(self): with create_tmp_file() as tmp_file: @@ -856,16 +855,16 @@ def test_roundtrip_character_array(self): values = np.array(['abc', 'def'], dtype='S') expected = Dataset({'x': ('x', values)}) with open_dataset(tmp_file) as actual: - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) # regression test for #157 with self.roundtrip(actual) as roundtripped: - assert_identical(expected, roundtripped) + self.assertDatasetIdentical(expected, roundtripped) def test_default_to_char_arrays(self): data = Dataset({'x': np.array(['foo', 'zzzz'], dtype='S')}) with self.roundtrip(data) as actual: - assert_identical(data, actual) - assert actual['x'].dtype == np.dtype('S4') + self.assertDatasetIdentical(data, actual) + self.assertEqual(actual['x'].dtype, np.dtype('S4')) def test_open_encodings(self): # Create a netCDF file with explicit time units @@ -890,14 +889,14 @@ def test_open_encodings(self): actual_encoding = dict((k, v) for k, v in iteritems(actual['time'].encoding) if k in expected['time'].encoding) - assert actual_encoding == expected['time'].encoding + self.assertDictEqual(actual_encoding, expected['time'].encoding) def test_dump_encodings(self): # regression test for #709 ds = Dataset({'x': ('y', np.arange(10.0))}) kwargs = dict(encoding={'x': {'zlib': True}}) with self.roundtrip(ds, save_kwargs=kwargs) as actual: - assert actual.x.encoding['zlib'] + self.assertTrue(actual.x.encoding['zlib']) def test_dump_and_open_encodings(self): # Create a netCDF file with explicit time units @@ -928,12 +927,12 @@ def test_compression_encoding(self): 'original_shape': data.var2.shape}) with self.roundtrip(data) as actual: for k, v in iteritems(data['var2'].encoding): - assert v == actual['var2'].encoding[k] + self.assertEqual(v, actual['var2'].encoding[k]) # regression test for #156 expected = data.isel(dim1=0) with self.roundtrip(expected) as actual: - assert_equal(expected, actual) + self.assertDatasetEqual(expected, actual) def test_encoding_chunksizes_unlimited(self): # regression test for GH1225 @@ -948,7 +947,7 @@ def test_encoding_chunksizes_unlimited(self): 'original_shape': (3,), } with self.roundtrip(ds) as actual: - assert_equal(ds, actual) + self.assertDatasetEqual(ds, actual) def test_mask_and_scale(self): with create_tmp_file() as tmp_file: @@ -972,7 +971,7 @@ def test_mask_and_scale(self): # now check xarray with open_dataset(tmp_file) as ds: expected = create_masked_and_scaled_data() - assert_identical(expected, ds) + self.assertDatasetIdentical(expected, ds) def test_0dimensional_variable(self): # This fix verifies our work-around to this netCDF4-python bug: @@ -984,7 +983,7 @@ def test_0dimensional_variable(self): with open_dataset(tmp_file) as ds: expected = Dataset({'x': ((), 123)}) - assert_identical(expected, ds) + self.assertDatasetIdentical(expected, ds) def test_already_open_dataset(self): with create_tmp_file() as tmp_file: @@ -996,7 +995,7 @@ def test_already_open_dataset(self): with backends.NetCDF4DataStore(nc, autoclose=False) as store: with open_dataset(store) as ds: expected = Dataset({'x': ((), 42)}) - assert_identical(expected, ds) + self.assertDatasetIdentical(expected, ds) def test_variable_len_strings(self): with create_tmp_file() as tmp_file: @@ -1010,7 +1009,7 @@ def test_variable_len_strings(self): expected = Dataset({'x': ('x', values)}) for kwargs in [{}, {'decode_cf': True}]: with open_dataset(tmp_file, **kwargs) as actual: - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) @requires_netCDF4 @@ -1032,7 +1031,7 @@ def test_variable_order(self): ds.coords['c'] = 4 with self.roundtrip(ds) as actual: - assert list(ds.variables) == list(actual.variables) + self.assertEqual(list(ds.variables), list(actual.variables)) def test_unsorted_index_raises(self): # should be fixed in netcdf4 v1.2.1 @@ -1051,7 +1050,7 @@ def test_unsorted_index_raises(self): try: ds2.randovar.values except IndexError as err: - assert 'first by calling .load' in str(err) + self.assertIn('first by calling .load', str(err)) class NetCDF4DataStoreAutocloseTrue(NetCDF4DataTest): @@ -1110,17 +1109,17 @@ def test_auto_chunk(self): original, open_kwargs={'auto_chunk': False}) as actual: for k, v in actual.variables.items(): # only index variables should be in memory - assert v._in_memory == (k in actual.dims) + self.assertEqual(v._in_memory, k in actual.dims) # there should be no chunks - assert v.chunks == None + self.assertEqual(v.chunks, None) with self.roundtrip( original, open_kwargs={'auto_chunk': True}) as actual: for k, v in actual.variables.items(): # only index variables should be in memory - assert v._in_memory == (k in actual.dims) + self.assertEqual(v._in_memory, k in actual.dims) # chunk size should be the same as original - assert v.chunks == original[k].chunks + self.assertEqual(v.chunks, original[k].chunks) def test_chunk_encoding(self): # These datasets have no dask chunks. All chunking specified in @@ -1130,7 +1129,7 @@ def test_chunk_encoding(self): data['var2'].encoding.update({'chunks': chunks}) with self.roundtrip(data) as actual: - assert chunks == actual['var2'].encoding['chunks'] + self.assertEqual(chunks, actual['var2'].encoding['chunks']) # expect an error with non-integer chunks data['var2'].encoding.update({'chunks': (5, 4.5)}) @@ -1147,7 +1146,7 @@ def test_chunk_encoding_with_dask(self): # zarr automatically gets chunk information from dask chunks ds_chunk4 = ds.chunk({'x': 4}) with self.roundtrip(ds_chunk4) as actual: - assert (4,) == actual['var1'].encoding['chunks'] + self.assertEqual((4,), actual['var1'].encoding['chunks']) # should fail if dask_chunks are irregular... ds_chunk_irreg = ds.chunk({'x': (5, 4, 3)}) @@ -1160,14 +1159,14 @@ def test_chunk_encoding_with_dask(self): # ... except if the last chunk is smaller than the first ds_chunk_irreg = ds.chunk({'x': (5, 5, 2)}) with self.roundtrip(ds_chunk_irreg) as actual: - assert (5,) == actual['var1'].encoding['chunks'] + self.assertEqual((5,), actual['var1'].encoding['chunks']) # - encoding specified - # specify compatible encodings for chunk_enc in 4, (4, ): ds_chunk4['var1'].encoding.update({'chunks': chunk_enc}) with self.roundtrip(ds_chunk4) as actual: - assert (4,) == actual['var1'].encoding['chunks'] + self.assertEqual((4,), actual['var1'].encoding['chunks']) # specify incompatible encoding ds_chunk4['var1'].encoding.update({'chunks': (5, 5)}) @@ -1226,11 +1225,11 @@ def test_write_persistence_modes(self): # overwrite mode with self.roundtrip(original, save_kwargs={'mode': 'w'}) as actual: - assert_identical(original, actual) + self.assertDatasetIdentical(original, actual) # don't overwrite mode with self.roundtrip(original, save_kwargs={'mode': 'w-'}) as actual: - assert_identical(original, actual) + self.assertDatasetIdentical(original, actual) # make sure overwriting works as expected with self.create_zarr_target() as store: @@ -1238,7 +1237,7 @@ def test_write_persistence_modes(self): # should overwrite with no error original.to_zarr(store, mode='w') actual = xr.open_zarr(store) - assert_identical(original, actual) + self.assertDatasetIdentical(original, actual) with pytest.raises(ValueError): original.to_zarr(store, mode='w-') @@ -1262,11 +1261,11 @@ def test_group(self): group = 'some/random/path' with self.roundtrip(original, save_kwargs={'group': group}, open_kwargs={'group': group}) as actual: - assert_identical(original, actual) + self.assertDatasetIdentical(original, actual) with pytest.raises(KeyError): with self.roundtrip(original, save_kwargs={'group': group}) as actual: - assert_identical(original, actual) + self.assertDatasetIdentical(original, actual) # TODO: implement zarr object encoding and make these tests pass @pytest.mark.xfail(reason="Zarr object encoding not implemented") @@ -1348,7 +1347,7 @@ def test_bytesio_pickle(self): fobj = BytesIO(data.to_netcdf()) with open_dataset(fobj, autoclose=self.autoclose) as ds: unpickled = pickle.loads(pickle.dumps(ds)) - assert_identical(unpickled, data) + self.assertDatasetIdentical(unpickled, data) class ScipyInMemoryDataTestAutocloseTrue(ScipyInMemoryDataTest): @@ -1402,13 +1401,13 @@ def test_array_attrs(self): def test_roundtrip_example_1_netcdf_gz(self): with open_example_dataset('example_1.nc.gz') as expected: with open_example_dataset('example_1.nc') as actual: - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) def test_netcdf3_endianness(self): # regression test for GH416 expected = open_example_dataset('bears.nc', engine='scipy') for var in expected.variables.values(): - assert var.dtype.isnative + self.assertTrue(var.dtype.isnative) @requires_netCDF4 def test_nc4_scipy(self): @@ -1514,13 +1513,13 @@ def test_encoding_unlimited_dims(self): ds = Dataset({'x': ('y', np.arange(10.0))}) with self.roundtrip(ds, save_kwargs=dict(unlimited_dims=['y'])) as actual: - assert actual.encoding['unlimited_dims'] == set('y') - assert_equal(ds, actual) + self.assertEqual(actual.encoding['unlimited_dims'], set('y')) + self.assertDatasetEqual(ds, actual) ds.encoding = {'unlimited_dims': ['y']} with self.roundtrip(ds) as actual: - assert actual.encoding['unlimited_dims'] == set('y') - assert_equal(ds, actual) + self.assertEqual(actual.encoding['unlimited_dims'], set('y')) + self.assertDatasetEqual(ds, actual) class GenericNetCDFDataTestAutocloseTrue(GenericNetCDFDataTest): @@ -1548,7 +1547,7 @@ def test_array_type_after_indexing(self): def test_complex(self): expected = Dataset({'x': ('y', np.ones(5) + 1j * np.ones(5))}) with self.roundtrip(expected) as actual: - assert_equal(expected, actual) + self.assertDatasetEqual(expected, actual) @pytest.mark.xfail(reason='https://github.com/pydata/xarray/issues/535') def test_cross_engine_read_write_netcdf4(self): @@ -1563,7 +1562,7 @@ def test_cross_engine_read_write_netcdf4(self): data.to_netcdf(tmp_file, engine=write_engine) for read_engine in valid_engines: with open_dataset(tmp_file, engine=read_engine) as actual: - assert_identical(data, actual) + self.assertDatasetIdentical(data, actual) def test_read_byte_attrs_as_unicode(self): with create_tmp_file() as tmp_file: @@ -1571,18 +1570,18 @@ def test_read_byte_attrs_as_unicode(self): nc.foo = b'bar' with open_dataset(tmp_file) as actual: expected = Dataset(attrs={'foo': 'bar'}) - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) def test_encoding_unlimited_dims(self): ds = Dataset({'x': ('y', np.arange(10.0))}) with self.roundtrip(ds, save_kwargs=dict(unlimited_dims=['y'])) as actual: - assert actual.encoding['unlimited_dims'] == set('y') - assert_equal(ds, actual) + self.assertEqual(actual.encoding['unlimited_dims'], set('y')) + self.assertDatasetEqual(ds, actual) ds.encoding = {'unlimited_dims': ['y']} with self.roundtrip(ds) as actual: - assert actual.encoding['unlimited_dims'] == set('y') - assert_equal(ds, actual) + self.assertEqual(actual.encoding['unlimited_dims'], set('y')) + self.assertDatasetEqual(ds, actual) # tests pending h5netcdf fix @@ -1727,12 +1726,12 @@ def test_open_mfdataset_does_same_as_concat(self): with open_mfdataset(files, data_vars=opt) as ds: kwargs = dict(data_vars=opt, dim='t') ds_expect = xr.concat([ds1, ds2], **kwargs) - assert_identical(ds, ds_expect) + self.assertDatasetIdentical(ds, ds_expect) with open_mfdataset(files, coords=opt) as ds: kwargs = dict(coords=opt, dim='t') ds_expect = xr.concat([ds1, ds2], **kwargs) - assert_identical(ds, ds_expect) + self.assertDatasetIdentical(ds, ds_expect) def test_common_coord_when_datavars_all(self): opt = 'all' @@ -1747,9 +1746,9 @@ def test_common_coord_when_datavars_all(self): var_shape = ds[self.var_name].shape - assert var_shape == coord_shape - assert coord_shape1 != coord_shape - assert coord_shape2 != coord_shape + self.assertEqual(var_shape, coord_shape) + self.assertNotEqual(coord_shape1, coord_shape) + self.assertNotEqual(coord_shape2, coord_shape) def test_common_coord_when_datavars_minimal(self): opt = 'minimal' @@ -1764,9 +1763,9 @@ def test_common_coord_when_datavars_minimal(self): var_shape = ds[self.var_name].shape - assert var_shape != coord_shape - assert coord_shape1 == coord_shape - assert coord_shape2 == coord_shape + self.assertNotEqual(var_shape, coord_shape) + self.assertEqual(coord_shape1, coord_shape) + self.assertEqual(coord_shape2, coord_shape) def test_invalid_data_vars_value_should_fail(self): @@ -1807,7 +1806,7 @@ def test_roundtrip_datetime_data(self): times = pd.to_datetime(['2000-01-01', '2000-01-02', 'NaT']) expected = Dataset({'t': ('t', times), 't0': times[0]}) with self.roundtrip(expected) as actual: - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) def test_write_store(self): # Override method in DatasetIOTestCases - not applicable to dask @@ -1828,14 +1827,14 @@ def test_open_mfdataset(self): original.isel(x=slice(5, 10)).to_netcdf(tmp2) with open_mfdataset([tmp1, tmp2], autoclose=self.autoclose) as actual: - assert isinstance(actual.foo.variable.data, da.Array) - assert actual.foo.variable.data.chunks == \ - ((5, 5),) - assert_identical(original, actual) + self.assertIsInstance(actual.foo.variable.data, da.Array) + self.assertEqual(actual.foo.variable.data.chunks, + ((5, 5),)) + self.assertDatasetIdentical(original, actual) with open_mfdataset([tmp1, tmp2], chunks={'x': 3}, autoclose=self.autoclose) as actual: - assert actual.foo.variable.data.chunks == \ - ((3, 2, 3, 2),) + self.assertEqual(actual.foo.variable.data.chunks, + ((3, 2, 3, 2),)) with raises_regex(IOError, 'no files to open'): open_mfdataset('foo-bar-baz-*.nc', autoclose=self.autoclose) @@ -1851,7 +1850,7 @@ def test_open_mfdataset_pathlib(self): original.isel(x=slice(5, 10)).to_netcdf(tmp2) with open_mfdataset([tmp1, tmp2], autoclose=self.autoclose) as actual: - assert_identical(original, actual) + self.assertDatasetIdentical(original, actual) def test_attrs_mfdataset(self): original = Dataset({'foo': ('x', np.random.randn(10))}) @@ -1866,7 +1865,7 @@ def test_attrs_mfdataset(self): with open_mfdataset([tmp1, tmp2]) as actual: # presumes that attributes inherited from # first dataset loaded - assert actual.test1 == ds1.test1 + self.assertEqual(actual.test1, ds1.test1) # attributes from ds2 are not retained, e.g., with raises_regex(AttributeError, 'no attribute'): @@ -1883,7 +1882,7 @@ def preprocess(ds): expected = preprocess(original) with open_mfdataset(tmp, preprocess=preprocess, autoclose=self.autoclose) as actual: - assert_identical(expected, actual) + self.assertDatasetIdentical(expected, actual) def test_save_mfdataset_roundtrip(self): original = Dataset({'foo': ('x', np.random.randn(10))}) @@ -1894,7 +1893,7 @@ def test_save_mfdataset_roundtrip(self): save_mfdataset(datasets, [tmp1, tmp2]) with open_mfdataset([tmp1, tmp2], autoclose=self.autoclose) as actual: - assert_identical(actual, original) + self.assertDatasetIdentical(actual, original) def test_save_mfdataset_invalid(self): ds = Dataset() @@ -1921,7 +1920,7 @@ def test_save_mfdataset_pathlib_roundtrip(self): save_mfdataset(datasets, [tmp1, tmp2]) with open_mfdataset([tmp1, tmp2], autoclose=self.autoclose) as actual: - assert_identical(actual, original) + self.assertDatasetIdentical(actual, original) def test_open_and_do_math(self): original = Dataset({'foo': ('x', np.random.randn(10))}) @@ -1940,21 +1939,21 @@ def test_open_mfdataset_concat_dim_none(self): Dataset({'x': np.nan}).to_netcdf(tmp2) with open_mfdataset([tmp1, tmp2], concat_dim=None, autoclose=self.autoclose) as actual: - assert_identical(data, actual) + self.assertDatasetIdentical(data, actual) def test_open_dataset(self): original = Dataset({'foo': ('x', np.random.randn(10))}) with create_tmp_file() as tmp: original.to_netcdf(tmp) with open_dataset(tmp, chunks={'x': 5}) as actual: - assert isinstance(actual.foo.variable.data, da.Array) - assert actual.foo.variable.data.chunks == ((5, 5),) - assert_identical(original, actual) + self.assertIsInstance(actual.foo.variable.data, da.Array) + self.assertEqual(actual.foo.variable.data.chunks, ((5, 5),)) + self.assertDatasetIdentical(original, actual) with open_dataset(tmp, chunks=5) as actual: - assert_identical(original, actual) + self.assertDatasetIdentical(original, actual) with open_dataset(tmp) as actual: - assert isinstance(actual.foo.variable.data, np.ndarray) - assert_identical(original, actual) + self.assertIsInstance(actual.foo.variable.data, np.ndarray) + self.assertDatasetIdentical(original, actual) def test_dask_roundtrip(self): with create_tmp_file() as tmp: @@ -1962,11 +1961,11 @@ def test_dask_roundtrip(self): data.to_netcdf(tmp) chunks = {'dim1': 4, 'dim2': 4, 'dim3': 4, 'time': 10} with open_dataset(tmp, chunks=chunks) as dask_ds: - assert_identical(data, dask_ds) + self.assertDatasetIdentical(data, dask_ds) with create_tmp_file() as tmp2: dask_ds.to_netcdf(tmp2) with open_dataset(tmp2) as on_disk: - assert_identical(data, on_disk) + self.assertDatasetIdentical(data, on_disk) def test_deterministic_names(self): with create_tmp_file() as tmp: @@ -1979,9 +1978,9 @@ def test_deterministic_names(self): repeat_names = dict((k, v.data.name) for k, v in ds.data_vars.items()) for var_name, dask_name in original_names.items(): - assert var_name in dask_name - assert dask_name[:13] == 'open_dataset-' - assert original_names == repeat_names + self.assertIn(var_name, dask_name) + self.assertEqual(dask_name[:13], 'open_dataset-') + self.assertEqual(original_names, repeat_names) def test_dataarray_compute(self): # Test DataArray.compute() on dask backend. @@ -1989,8 +1988,8 @@ def test_dataarray_compute(self): # however dask is the only tested backend which supports DataArrays actual = DataArray([1, 2]).chunk() computed = actual.compute() - assert not actual._in_memory - assert computed._in_memory + self.assertFalse(actual._in_memory) + self.assertTrue(computed._in_memory) self.assertDataArrayAllClose(actual, computed, decode_bytes=False) def test_vectorized_indexing(self): @@ -2032,11 +2031,11 @@ def create_datasets(self, **kwargs): def test_cmp_local_file(self): with self.create_datasets() as (actual, expected): - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) # global attributes should be global attributes on the dataset - assert 'NC_GLOBAL' not in actual.attrs - assert 'history' in actual.attrs + self.assertNotIn('NC_GLOBAL', actual.attrs) + self.assertIn('history', actual.attrs) # we don't check attributes exactly with assertDatasetIdentical() # because the test DAP server seems to insert some extra @@ -2044,14 +2043,14 @@ def test_cmp_local_file(self): assert actual.attrs.keys() == expected.attrs.keys() with self.create_datasets() as (actual, expected): - assert_equal(actual.isel(l=2), expected.isel(l=2)) + self.assertDatasetEqual(actual.isel(l=2), expected.isel(l=2)) with self.create_datasets() as (actual, expected): - assert_equal(actual.isel(i=0, j=-1), + self.assertDatasetEqual(actual.isel(i=0, j=-1), expected.isel(i=0, j=-1)) with self.create_datasets() as (actual, expected): - assert_equal(actual.isel(j=slice(1, 2)), + self.assertDatasetEqual(actual.isel(j=slice(1, 2)), expected.isel(j=slice(1, 2))) def test_compatible_to_netcdf(self): @@ -2061,12 +2060,12 @@ def test_compatible_to_netcdf(self): actual.to_netcdf(tmp_file) actual = open_dataset(tmp_file) actual['bears'] = actual['bears'].astype(str) - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) @requires_dask def test_dask(self): with self.create_datasets(chunks={'j': 2}) as (actual, expected): - assert_equal(actual, expected) + self.assertDatasetEqual(actual, expected) @network @@ -2129,7 +2128,7 @@ def test_weakrefs(self): on_disk = open_dataset(tmp_file, engine='pynio') actual = on_disk.rename({'foo': 'bar', 'x': 'y'}) del on_disk # trigger garbage collection - assert_identical(actual, expected) + self.assertDatasetIdentical(actual, expected) class TestPyNioAutocloseTrue(TestPyNio): @@ -2405,7 +2404,7 @@ def test_chunks(self): with xr.open_rasterio(tmp_file, chunks=(1, 2, 2)) as actual: import dask.array as da - assert isinstance(actual.data, da.Array) + self.assertIsInstance(actual.data, da.Array) assert 'open_rasterio' in actual.data.name # ref @@ -2480,12 +2479,12 @@ def test_extract_nc4_variable_encoding(self): var = xr.Variable(('x',), [1, 2, 3], {}, {'chunking': (2, 1)}) encoding = _extract_nc4_variable_encoding(var) - assert {} == encoding + self.assertEqual({}, encoding) # regression test var = xr.Variable(('x',), [1, 2, 3], {}, {'shuffle': True}) encoding = _extract_nc4_variable_encoding(var, raise_on_invalid=True) - assert {'shuffle': True} == encoding + self.assertEqual({'shuffle': True}, encoding) def test_extract_h5nc_encoding(self): # not supported with h5netcdf (yet) @@ -2609,7 +2608,7 @@ def test_dataarray_to_netcdf_no_name(self): original_da.to_netcdf(tmp) with open_dataarray(tmp) as loaded_da: - assert_identical(original_da, loaded_da) + self.assertDataArrayIdentical(original_da, loaded_da) def test_dataarray_to_netcdf_with_name(self): original_da = DataArray(np.arange(12).reshape((3, 4)), @@ -2619,7 +2618,7 @@ def test_dataarray_to_netcdf_with_name(self): original_da.to_netcdf(tmp) with open_dataarray(tmp) as loaded_da: - assert_identical(original_da, loaded_da) + self.assertDataArrayIdentical(original_da, loaded_da) def test_dataarray_to_netcdf_coord_name_clash(self): original_da = DataArray(np.arange(12).reshape((3, 4)), @@ -2630,7 +2629,7 @@ def test_dataarray_to_netcdf_coord_name_clash(self): original_da.to_netcdf(tmp) with open_dataarray(tmp) as loaded_da: - assert_identical(original_da, loaded_da) + self.assertDataArrayIdentical(original_da, loaded_da) def test_open_dataarray_options(self): data = DataArray( @@ -2641,7 +2640,7 @@ def test_open_dataarray_options(self): expected = data.drop('y') with open_dataarray(tmp, drop_variables=['y']) as loaded: - assert_identical(expected, loaded) + self.assertDataArrayIdentical(expected, loaded) def test_dataarray_to_netcdf_return_bytes(self): # regression test for GH1410 @@ -2658,4 +2657,4 @@ def test_dataarray_to_netcdf_no_name_pathlib(self): original_da.to_netcdf(tmp) with open_dataarray(tmp) as loaded_da: - assert_identical(original_da, loaded_da) + self.assertDataArrayIdentical(original_da, loaded_da) From 145c0136d841c6b151ebcf49a44506c3990fbe03 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 15:20:28 -0500 Subject: [PATCH 18/41] a few triages --- xarray/tests/test_plot.py | 3 ++- xarray/tests/test_ufuncs.py | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 5e3470b4ef0..05135a7d5d5 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -24,7 +24,8 @@ _build_discrete_cmap, _color_palette, import_seaborn) -from . import TestCase, requires_matplotlib, requires_seaborn, raises_regex +from . import ( + TestCase, requires_matplotlib, requires_seaborn, raises_regex, assert_equal) @pytest.mark.flaky diff --git a/xarray/tests/test_ufuncs.py b/xarray/tests/test_ufuncs.py index 717b50602db..7449beabaf8 100644 --- a/xarray/tests/test_ufuncs.py +++ b/xarray/tests/test_ufuncs.py @@ -26,7 +26,7 @@ def test_unary(self): xr.DataArray([0, 0], dims='x'), xr.Dataset({'y': ('x', [0, 0])})] for a in args: - assert_identical(a + 1, xu.cos(a)) + self.assertIdentical(a + 1, xu.cos(a)) def test_binary(self): args = [0, @@ -36,10 +36,10 @@ def test_binary(self): xr.Dataset({'y': ('x', [0, 0])})] for n, t1 in enumerate(args): for t2 in args[n:]: - assert_identical(t2 + 1, xu.maximum(t1, t2 + 1)) - assert_identical(t2 + 1, xu.maximum(t2, t1 + 1)) - assert_identical(t2 + 1, xu.maximum(t1 + 1, t2)) - assert_identical(t2 + 1, xu.maximum(t2 + 1, t1)) + self.assertIdentical(t2 + 1, xu.maximum(t1, t2 + 1)) + self.assertIdentical(t2 + 1, xu.maximum(t2, t1 + 1)) + self.assertIdentical(t2 + 1, xu.maximum(t1 + 1, t2)) + self.assertIdentical(t2 + 1, xu.maximum(t2 + 1, t1)) def test_groupby(self): ds = xr.Dataset({'a': ('x', [0, 0, 0])}, {'c': ('x', [0, 0, 1])}) @@ -65,4 +65,4 @@ def test_groupby(self): def test_pickle(self): a = 1.0 cos_pickled = pickle.loads(pickle.dumps(xu.cos)) - assert_identical(cos_pickled(a), xu.cos(a)) + self.assertIdentical(cos_pickled(a), xu.cos(a)) From 3f286bac6081b8596972563540d96cdb13413598 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 15:50:21 -0500 Subject: [PATCH 19/41] another import --- xarray/tests/test_tutorial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_tutorial.py b/xarray/tests/test_tutorial.py index a7807325ec5..9ad797a9ac9 100644 --- a/xarray/tests/test_tutorial.py +++ b/xarray/tests/test_tutorial.py @@ -7,7 +7,7 @@ from xarray import tutorial, DataArray from xarray.core.pycompat import suppress -from . import TestCase, network +from . import TestCase, network, assert_identical @network From 62c22eb434afb27b4f1d34ffda63fb3188d1eeab Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 15:55:01 -0500 Subject: [PATCH 20/41] can't deprecate importorskip yet --- xarray/tests/__init__.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/xarray/tests/__init__.py b/xarray/tests/__init__.py index 7a87dbc859f..27043f14f9d 100644 --- a/xarray/tests/__init__.py +++ b/xarray/tests/__init__.py @@ -45,12 +45,6 @@ def _importorskip(modname, minversion=None): - """ - This is deprecated - instead use pytest.importorskip. For example: - - matplotlib = pytest.importorskip('matplotlib', minversion='1.9.0') - - """ try: mod = importlib.import_module(modname) has = True From ccfbff3454546dc82cbebb101a079ef447f8959a Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 16:03:18 -0500 Subject: [PATCH 21/41] assert_array_equal --- xarray/tests/test_accessors.py | 4 +- xarray/tests/test_backends.py | 2 +- xarray/tests/test_coding_times.py | 32 ++++--- xarray/tests/test_combine.py | 4 +- xarray/tests/test_conventions.py | 32 ++++--- xarray/tests/test_dask.py | 5 +- xarray/tests/test_dataarray.py | 82 ++++++++-------- xarray/tests/test_dataset.py | 28 +++--- xarray/tests/test_duck_array_ops.py | 15 +-- xarray/tests/test_indexing.py | 33 +++---- xarray/tests/test_plot.py | 35 +++---- xarray/tests/test_ufuncs.py | 7 +- xarray/tests/test_utils.py | 4 +- xarray/tests/test_variable.py | 139 ++++++++++++++-------------- 14 files changed, 217 insertions(+), 205 deletions(-) diff --git a/xarray/tests/test_accessors.py b/xarray/tests/test_accessors.py index 0411ea7fc92..454c037fde1 100644 --- a/xarray/tests/test_accessors.py +++ b/xarray/tests/test_accessors.py @@ -7,7 +7,7 @@ import pandas as pd from . import (TestCase, requires_dask, raises_regex, assert_equal, - assert_identical, assert_allclose) + assert_identical, assert_allclose, assert_array_equal) class TestDatetimeAccessor(TestCase): @@ -94,4 +94,4 @@ def test_seasons(self): "SON", "SON", "SON", "DJF"] seasons = xr.DataArray(seasons) - self.assertArrayEqual(seasons.values, dates.dt.season.values) + assert_array_equal(seasons.values, dates.dt.season.values) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 4263ae2bf1e..3be4b6b93f6 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -966,7 +966,7 @@ def test_mask_and_scale(self): expected = np.ma.array([-1, -1, 10, 10.1, 10.2], mask=[True, True, False, False, False]) actual = nc.variables['x'][:] - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) # now check xarray with open_dataset(tmp_file) as ds: diff --git a/xarray/tests/test_coding_times.py b/xarray/tests/test_coding_times.py index 0cf36c57d6f..b35959cab1a 100644 --- a/xarray/tests/test_coding_times.py +++ b/xarray/tests/test_coding_times.py @@ -8,7 +8,9 @@ import pandas as pd from xarray import Variable, coding -from . import TestCase, requires_netCDF4, assert_equal, assert_identical +from . import ( + TestCase, requires_netCDF4, assert_equal, assert_identical, + assert_array_equal) import pytest @@ -67,7 +69,7 @@ def test_cf_datetime(self): actual_cmp = actual.astype('M8[us]') else: actual_cmp = actual - self.assertArrayEqual(expected, actual_cmp) + assert_array_equal(expected, actual_cmp) encoded, _, _ = coding.times.encode_cf_datetime(actual, units, calendar) if '1-1-1' not in units: @@ -75,7 +77,7 @@ def test_cf_datetime(self): # units/encoding cannot be preserved in this case: # (Pdb) pd.to_datetime('1-1-1 00:00:0.0') # Timestamp('2001-01-01 00:00:00') - self.assertArrayEqual(num_dates, np.around(encoded, 1)) + assert_array_equal(num_dates, np.around(encoded, 1)) if (hasattr(num_dates, 'ndim') and num_dates.ndim == 1 and '1000' not in units): # verify that wrapping with a pandas.Index works @@ -83,7 +85,7 @@ def test_cf_datetime(self): # non-datetime64 compatible dates into a pandas.Index encoded, _, _ = coding.times.encode_cf_datetime( pd.Index(actual), units, calendar) - self.assertArrayEqual(num_dates, np.around(encoded, 1)) + assert_array_equal(num_dates, np.around(encoded, 1)) @requires_netCDF4 def test_decode_cf_datetime_overflow(self): @@ -108,7 +110,7 @@ def test_decode_cf_datetime_non_standard_units(self): # they cannot be parsed by netcdftime, but pd.Timestamp works units = 'hours since 1-1-1970' actual = coding.times.decode_cf_datetime(np.arange(100), units) - self.assertArrayEqual(actual, expected) + assert_array_equal(actual, expected) @requires_netCDF4 def test_decode_cf_datetime_non_iso_strings(self): @@ -120,7 +122,7 @@ def test_decode_cf_datetime_non_iso_strings(self): (np.arange(100), 'hours since 2000-01-01 0:00')] for num_dates, units in cases: actual = coding.times.decode_cf_datetime(num_dates, units) - self.assertArrayEqual(actual, expected) + assert_array_equal(actual, expected) @requires_netCDF4 def test_decode_non_standard_calendar(self): @@ -197,8 +199,8 @@ def test_decode_non_standard_calendar_multidim_time(self): actual = coding.times.decode_cf_datetime(mdim_time, units, calendar=calendar) assert actual.dtype == np.dtype('M8[ns]') - self.assertArrayEqual(actual[:, 0], expected1) - self.assertArrayEqual(actual[:, 1], expected2) + assert_array_equal(actual[:, 0], expected1) + assert_array_equal(actual[:, 1], expected2) @requires_netCDF4 def test_decode_non_standard_calendar_fallback(self): @@ -220,7 +222,7 @@ def test_decode_non_standard_calendar_fallback(self): str(w[0].message) assert actual.dtype == np.dtype('O') - self.assertArrayEqual(actual, expected) + assert_array_equal(actual, expected) @requires_netCDF4 def test_cf_datetime_nan(self): @@ -235,7 +237,7 @@ def test_cf_datetime_nan(self): warnings.filterwarnings('ignore', 'All-NaN') actual = coding.times.decode_cf_datetime(num_dates, units) expected = np.array(expected_list, dtype='datetime64[ns]') - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) @requires_netCDF4 def test_decoded_cf_datetime_array_2d(self): @@ -245,7 +247,7 @@ def test_decoded_cf_datetime_array_2d(self): result = coding.times.CFDatetimeCoder().decode(variable) assert result.dtype == 'datetime64[ns]' expected = pd.date_range('2000-01-01', periods=4).values.reshape(2, 2) - self.assertArrayEqual(np.asarray(result), expected) + assert_array_equal(np.asarray(result), expected) def test_infer_datetime_units(self): for dates, expected in [(pd.date_range('1900-01-01', periods=5), @@ -288,18 +290,18 @@ def test_cf_timedelta(self): expected = numbers actual, _ = coding.times.encode_cf_timedelta(timedeltas, units) - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) assert expected.dtype == actual.dtype if units is not None: expected = timedeltas actual = coding.times.decode_cf_timedelta(numbers, units) - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) assert expected.dtype == actual.dtype expected = np.timedelta64('NaT', 'ns') actual = coding.times.decode_cf_timedelta(np.array(np.nan), 'days') - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) def test_cf_timedelta_2d(self): timedeltas = ['1D', '2D', '3D'] @@ -310,7 +312,7 @@ def test_cf_timedelta_2d(self): expected = timedeltas actual = coding.times.decode_cf_timedelta(numbers, units) - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) assert expected.dtype == actual.dtype def test_infer_timedelta_units(self): diff --git a/xarray/tests/test_combine.py b/xarray/tests/test_combine.py index 5546e5d3da7..365e274a191 100644 --- a/xarray/tests/test_combine.py +++ b/xarray/tests/test_combine.py @@ -12,7 +12,7 @@ from xarray.core.pycompat import iteritems, OrderedDict from . import (TestCase, InaccessibleArray, requires_dask, raises_regex, - assert_equal, assert_identical) + assert_equal, assert_identical, assert_array_equal) from .test_dataset import create_test_data @@ -63,7 +63,7 @@ def rectify_dim_order(dataset): actual = concat(datasets, data[dim], coords='all') expected = np.array([data['extra'].values for _ in range(data.dims[dim])]) - self.assertArrayEqual(actual['extra'].values, expected) + assert_array_equal(actual['extra'].values, expected) actual = concat(datasets, data[dim], coords='different') assert_equal(data['extra'], actual['extra']) diff --git a/xarray/tests/test_conventions.py b/xarray/tests/test_conventions.py index bc28ad25b37..7f1e16f9d6d 100644 --- a/xarray/tests/test_conventions.py +++ b/xarray/tests/test_conventions.py @@ -12,7 +12,9 @@ from xarray import conventions, Variable, Dataset, open_dataset from xarray.core import utils, indexing from xarray.testing import assert_identical -from . import TestCase, requires_netCDF4, unittest, raises_regex, IndexerMaker +from . import ( + TestCase, requires_netCDF4, unittest, raises_regex, IndexerMaker, + assert_array_equal) from .test_backends import CFEncodedDataTest from xarray.core.pycompat import iteritems from xarray.backends.memory import InMemoryDataStore @@ -34,8 +36,8 @@ def test_wrapper_class(self): assert actual.size == expected.size assert actual.ndim == expected.ndim assert len(actual) == len(expected) - self.assertArrayEqual(expected, actual) - self.assertArrayEqual(expected[:1], actual[B[:1]]) + assert_array_equal(expected, actual) + assert_array_equal(expected[:1], actual[B[:1]]) with pytest.raises(IndexError): actual[B[:, :2]] @@ -59,34 +61,34 @@ def test_char_to_bytes(self): array = np.array([['a', 'b', 'c'], ['d', 'e', 'f']]) expected = np.array(['abc', 'def']) actual = conventions.char_to_bytes(array) - self.assertArrayEqual(actual, expected) + assert_array_equal(actual, expected) expected = np.array(['ad', 'be', 'cf']) actual = conventions.char_to_bytes(array.T) # non-contiguous - self.assertArrayEqual(actual, expected) + assert_array_equal(actual, expected) def test_char_to_bytes_ndim_zero(self): expected = np.array('a') actual = conventions.char_to_bytes(expected) - self.assertArrayEqual(actual, expected) + assert_array_equal(actual, expected) def test_char_to_bytes_size_zero(self): array = np.zeros((3, 0), dtype='S1') expected = np.array([b'', b'', b'']) actual = conventions.char_to_bytes(array) - self.assertArrayEqual(actual, expected) + assert_array_equal(actual, expected) def test_bytes_to_char(self): array = np.array([['ab', 'cd'], ['ef', 'gh']]) expected = np.array([[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]) actual = conventions.bytes_to_char(array) - self.assertArrayEqual(actual, expected) + assert_array_equal(actual, expected) expected = np.array([[['a', 'b'], ['e', 'f']], [['c', 'd'], ['g', 'h']]]) actual = conventions.bytes_to_char(array.T) - self.assertArrayEqual(actual, expected) + assert_array_equal(actual, expected) def test_vectorized_indexing(self): array = np.array([[b'a', b'b', b'c'], [b'd', b'e', b'f']], dtype='S') @@ -94,7 +96,7 @@ def test_vectorized_indexing(self): expected = np.array([[b'abc', b'def'], [b'def', b'abc']]) indexer = V[np.array([[0, 1], [1, 0]])] actual = stacked[indexer] - self.assertArrayEqual(actual, expected) + assert_array_equal(actual, expected) class TestBytesToStringArray(TestCase): @@ -109,8 +111,8 @@ def test_encoding(self): assert actual.shape == expected.shape assert actual.size == expected.size assert actual.ndim == expected.ndim - self.assertArrayEqual(expected, actual) - self.assertArrayEqual(expected[0], actual[B[0]]) + assert_array_equal(expected, actual) + assert_array_equal(expected[0], actual[B[0]]) def test_scalar(self): expected = np.array(u'abc', dtype=object) @@ -140,7 +142,7 @@ def test_booltype_array(self): x = np.array([1, 0, 1, 1, 0], dtype='i1') bx = conventions.BoolTypeArray(x) assert bx.dtype == np.bool - self.assertArrayEqual(bx, np.array([True, False, True, True, False], + assert_array_equal(bx, np.array([True, False, True, True, False], dtype=np.bool)) @@ -151,7 +153,7 @@ def test(self): a = conventions.NativeEndiannessArray(x) assert a.dtype == expected.dtype assert a.dtype == expected[:].dtype - self.assertArrayEqual(a, expected) + assert_array_equal(a, expected) def test_decode_cf_with_conflicting_fill_missing_value(): @@ -326,7 +328,7 @@ def test_decode_cf_datetime_transition_to_invalid(self): expected = [datetime(2000, 1, 1, 0, 0), datetime(2265, 10, 28, 0, 0)] - self.assertArrayEqual(ds_decoded.time.values, expected) + assert_array_equal(ds_decoded.time.values, expected) class CFEncodedInMemoryStore(WritableCFDataStore, InMemoryDataStore): diff --git a/xarray/tests/test_dask.py b/xarray/tests/test_dask.py index a144b0dd67f..4d614e9be38 100644 --- a/xarray/tests/test_dask.py +++ b/xarray/tests/test_dask.py @@ -15,7 +15,8 @@ import xarray.ufuncs as xu from xarray.core.pycompat import suppress, OrderedDict from . import ( - TestCase, assert_frame_equal, raises_regex, assert_equal, assert_identical) + TestCase, assert_frame_equal, raises_regex, assert_equal, assert_identical, + assert_array_equal) from xarray.tests import mock @@ -65,7 +66,7 @@ def test_basics(self): v = self.lazy_var assert self.data is v.data assert self.data.chunks == v.chunks - self.assertArrayEqual(self.values, v) + assert_array_equal(self.values, v) def test_copy(self): self.assertLazyAndIdentical(self.eager_var, self.lazy_var.copy()) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index c0c71169510..2e31f891796 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -64,7 +64,7 @@ def test_repr_multiindex(self): def test_properties(self): self.assertVariableEqual(self.dv.variable, self.v) - self.assertArrayEqual(self.dv.values, self.v.values) + assert_array_equal(self.dv.values, self.v.values) for attr in ['dims', 'dtype', 'shape', 'size', 'nbytes', 'ndim', 'attrs']: assert getattr(self.dv, attr) == getattr(self.v, attr) @@ -72,7 +72,7 @@ def test_properties(self): self.assertVariableEqual(self.dv.variable, self.v) assert set(self.dv.coords) == set(self.ds.coords) for k, v in iteritems(self.dv.coords): - self.assertArrayEqual(v, self.ds.coords[k]) + assert_array_equal(v, self.ds.coords[k]) with pytest.raises(AttributeError): self.dv.dataset assert isinstance(self.ds['x'].to_index(), pd.Index) @@ -85,10 +85,10 @@ def test_data_property(self): array = DataArray(np.zeros((3, 4))) actual = array.copy() actual.values = np.ones((3, 4)) - self.assertArrayEqual(np.ones((3, 4)), actual.values) + assert_array_equal(np.ones((3, 4)), actual.values) actual.data = 2 * np.ones((3, 4)) - self.assertArrayEqual(2 * np.ones((3, 4)), actual.data) - self.assertArrayEqual(actual.data, actual.values) + assert_array_equal(2 * np.ones((3, 4)), actual.data) + assert_array_equal(actual.data, actual.values) def test_indexes(self): array = DataArray(np.zeros((2, 3)), @@ -492,14 +492,14 @@ def test_getitem_dataarray(self): da = DataArray(np.arange(12).reshape((3, 4)), dims=['x', 'y']) ind = DataArray([[0, 1], [0, 1]], dims=['x', 'z']) actual = da[ind] - self.assertArrayEqual(actual, da.values[[[0, 1], [0, 1]], :]) + assert_array_equal(actual, da.values[[[0, 1], [0, 1]], :]) da = DataArray(np.arange(12).reshape((3, 4)), dims=['x', 'y'], coords={'x': [0, 1, 2], 'y': ['a', 'b', 'c', 'd']}) ind = xr.DataArray([[0, 1], [0, 1]], dims=['X', 'Y']) actual = da[ind] expected = da.values[[[0, 1], [0, 1]], :] - self.assertArrayEqual(actual, expected) + assert_array_equal(actual, expected) assert actual.dims == ('X', 'Y', 'y') # boolean indexing @@ -524,7 +524,7 @@ def test_setitem(self): dims=['x', 'y']) orig[t] = 1 expected[t] = 1 - self.assertArrayEqual(orig.values, expected) + assert_array_equal(orig.values, expected) def test_setitem_fancy(self): # vectorized indexing @@ -743,7 +743,7 @@ def test_isel_fancy(self): assert_identical(actual['b'], stations['b']) expected = da.variable[:, stations['dim2s'].variable, stations['dim1s'].variable] - self.assertArrayEqual(actual, expected) + assert_array_equal(actual, expected) def test_sel(self): self.ds['x'] = ('x', np.array(list('abcdefghij'))) @@ -770,14 +770,14 @@ def test_sel_dataarray(self): # along new dimension ind = DataArray(['a', 'b', 'c'], dims=['new_dim']) actual = da.sel(x=ind) - self.assertArrayEqual(actual, da.isel(x=[0, 1, 2])) + assert_array_equal(actual, da.isel(x=[0, 1, 2])) assert 'new_dim' in actual.dims # with coordinate ind = DataArray(['a', 'b', 'c'], dims=['new_dim'], coords={'new_dim': [0, 1, 2]}) actual = da.sel(x=ind) - self.assertArrayEqual(actual, da.isel(x=[0, 1, 2])) + assert_array_equal(actual, da.isel(x=[0, 1, 2])) assert 'new_dim' in actual.dims assert 'new_dim' in actual.coords assert_equal(actual['new_dim'].drop('x'), @@ -1019,8 +1019,8 @@ def test_virtual_time_components(self): dates = pd.date_range('2000-01-01', periods=10) da = DataArray(np.arange(1, 11), [('time', dates)]) - self.assertArrayEqual(da['time.dayofyear'], da.values) - self.assertArrayEqual(da.coords['time.dayofyear'], da.values) + assert_array_equal(da['time.dayofyear'], da.values) + assert_array_equal(da.coords['time.dayofyear'], da.values) def test_coords(self): # use int64 to ensure repr() consistency on windows @@ -1435,7 +1435,7 @@ def test_dataset_getitem(self): assert_identical(dv, self.dv) def test_array_interface(self): - self.assertArrayEqual(np.asarray(self.dv), self.x) + assert_array_equal(np.asarray(self.dv), self.x) # test patched in methods assert_array_equal(self.dv.astype(float), self.v.astype(float)) assert_array_equal(self.dv.argsort(), self.v.argsort()) @@ -1499,7 +1499,7 @@ def test_inplace_math_basics(self): b += 1 assert b is a assert b.variable is v - self.assertArrayEqual(b.values, x) + assert_array_equal(b.values, x) assert source_ndarray(b.values) is x def test_inplace_math_automatic_alignment(self): @@ -1881,7 +1881,7 @@ def test_groupby_properties(self): expected_groups = {'a': range(0, 9), 'c': [9], 'b': range(10, 20)} assert expected_groups.keys() == grouped.groups.keys() for key in expected_groups: - self.assertArrayEqual(expected_groups[key], grouped.groups[key]) + assert_array_equal(expected_groups[key], grouped.groups[key]) assert 3 == len(grouped) def test_groupby_apply_identity(self): @@ -2608,25 +2608,25 @@ def test_to_pandas(self): # 0d actual = DataArray(42).to_pandas() expected = np.array(42) - self.assertArrayEqual(actual, expected) + assert_array_equal(actual, expected) # 1d values = np.random.randn(3) index = pd.Index(['a', 'b', 'c'], name='x') da = DataArray(values, coords=[index]) actual = da.to_pandas() - self.assertArrayEqual(actual.values, values) - self.assertArrayEqual(actual.index, index) - self.assertArrayEqual(actual.index.name, 'x') + assert_array_equal(actual.values, values) + assert_array_equal(actual.index, index) + assert_array_equal(actual.index.name, 'x') # 2d values = np.random.randn(3, 2) da = DataArray(values, coords=[('x', ['a', 'b', 'c']), ('y', [0, 1])], name='foo') actual = da.to_pandas() - self.assertArrayEqual(actual.values, values) - self.assertArrayEqual(actual.index, ['a', 'b', 'c']) - self.assertArrayEqual(actual.columns, [0, 1]) + assert_array_equal(actual.values, values) + assert_array_equal(actual.index, ['a', 'b', 'c']) + assert_array_equal(actual.columns, [0, 1]) # roundtrips for shape in [(3,), (3, 4), (3, 4, 5)]: @@ -2644,9 +2644,9 @@ def test_to_dataframe(self): [('B', [1, 2, 3]), ('A', list('cdef'))], name='foo') expected = arr.to_series() actual = arr.to_dataframe()['foo'] - self.assertArrayEqual(expected.values, actual.values) - self.assertArrayEqual(expected.name, actual.name) - self.assertArrayEqual(expected.index.values, actual.index.values) + assert_array_equal(expected.values, actual.values) + assert_array_equal(expected.name, actual.name) + assert_array_equal(expected.index.values, actual.index.values) # regression test for coords with different dimensions arr.coords['C'] = ('B', [-1, -2, -3]) @@ -2654,9 +2654,9 @@ def test_to_dataframe(self): expected['C'] = [-1] * 4 + [-2] * 4 + [-3] * 4 expected = expected[['C', 'foo']] actual = arr.to_dataframe() - self.assertArrayEqual(expected.values, actual.values) - self.assertArrayEqual(expected.columns.values, actual.columns.values) - self.assertArrayEqual(expected.index.values, actual.index.values) + assert_array_equal(expected.values, actual.values) + assert_array_equal(expected.columns.values, actual.columns.values) + assert_array_equal(expected.index.values, actual.index.values) arr.name = None # unnamed with raises_regex(ValueError, 'unnamed'): @@ -2666,8 +2666,8 @@ def test_to_pandas_name_matches_coordinate(self): # coordinate with same name as array arr = DataArray([1, 2, 3], dims='x', name='x') series = arr.to_series() - self.assertArrayEqual([1, 2, 3], series.values) - self.assertArrayEqual([0, 1, 2], series.index.values) + assert_array_equal([1, 2, 3], series.values) + assert_array_equal([0, 1, 2], series.index.values) assert 'x' == series.name assert 'x' == series.index.name @@ -2678,8 +2678,8 @@ def test_to_pandas_name_matches_coordinate(self): def test_to_and_from_series(self): expected = self.dv.to_dataframe()['foo'] actual = self.dv.to_series() - self.assertArrayEqual(expected.values, actual.values) - self.assertArrayEqual(expected.index.values, actual.index.values) + assert_array_equal(expected.values, actual.values) + assert_array_equal(expected.index.values, actual.index.values) assert 'foo' == actual.name # test roundtrip assert_identical( @@ -2790,17 +2790,17 @@ def test_to_masked_array(self): # Test round trip x_masked_2 = da.to_masked_array() da_2 = DataArray(x_masked_2) - self.assertArrayEqual(x_masked, x_masked_2) + assert_array_equal(x_masked, x_masked_2) assert_equal(da, da_2) da_masked_array = da.to_masked_array(copy=True) assert isinstance(da_masked_array, np.ma.MaskedArray) # Test masks - self.assertArrayEqual(da_masked_array.mask, x_masked.mask) + assert_array_equal(da_masked_array.mask, x_masked.mask) # Test that mask is unpacked correctly - self.assertArrayEqual(da.values, x_masked.filled(np.nan)) + assert_array_equal(da.values, x_masked.filled(np.nan)) # Test that the underlying data (including nans) hasn't changed - self.assertArrayEqual(da_masked_array, x_masked.filled(np.nan)) + assert_array_equal(da_masked_array, x_masked.filled(np.nan)) # Test that copy=False gives access to values masked_array = da.to_masked_array(copy=False) @@ -2834,12 +2834,12 @@ def test_to_and_from_cdms2(self): expected_coords = [IndexVariable('distance', [-2, 2]), IndexVariable('time', [0, 1, 2])] actual = original.to_cdms2() - self.assertArrayEqual(actual, original) + assert_array_equal(actual, original) assert actual.id == original.name self.assertItemsEqual(actual.getAxisIds(), original.dims) for axis, coord in zip(actual.getAxisList(), expected_coords): assert axis.id == coord.name - self.assertArrayEqual(axis, coord.values) + assert_array_equal(axis, coord.values) assert actual.baz == original.attrs['baz'] component_times = actual.getAxis(1).asComponentTime() @@ -2876,7 +2876,7 @@ def test_to_and_from_iris(self): original.attrs['cell_methods'] = \ 'height: mean (comment: A cell method)' actual = original.to_iris() - self.assertArrayEqual(actual.data, original.data) + assert_array_equal(actual.data, original.data) assert actual.var_name == original.name self.assertItemsEqual([d.var_name for d in actual.dim_coords], original.dims) @@ -2948,7 +2948,7 @@ def test_to_and_from_iris_dask(self): # Be careful not to trigger the loading of the iris data actual_data = actual.core_data() if \ hasattr(actual, 'core_data') else actual.data - self.assertArrayEqual(actual_data, original.data) + assert_array_equal(actual_data, original.data) assert actual.var_name == original.name self.assertItemsEqual([d.var_name for d in actual.dim_coords], original.dims) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index d6e77f2639c..61252f271ca 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -345,7 +345,7 @@ def test_constructor_pandas_single(self): pandas_obj = a.to_pandas() ds_based_on_pandas = Dataset(pandas_obj) for dim in ds_based_on_pandas.data_vars: - self.assertArrayEqual(ds_based_on_pandas[dim], pandas_obj[dim]) + assert_array_equal(ds_based_on_pandas[dim], pandas_obj[dim]) def test_constructor_compat(self): data = OrderedDict([('x', DataArray(0, coords={'y': 1})), @@ -477,7 +477,7 @@ def test_variable(self): a['bar'] = (('time', 'x',), d) # order of creation is preserved assert list(a.variables) == ['foo', 'bar'] - self.assertArrayEqual(a['foo'].values, d) + assert_array_equal(a['foo'].values, d) # try to add variable with dim (10,3) with data that's (3,10) with pytest.raises(ValueError): a['qux'] = (('time', 'x'), d.T) @@ -555,11 +555,11 @@ def test_coords_modify(self): actual = data.copy(deep=True) actual.coords['x'] = ('x', ['a', 'b']) - self.assertArrayEqual(actual['x'], ['a', 'b']) + assert_array_equal(actual['x'], ['a', 'b']) actual = data.copy(deep=True) actual.coords['z'] = ('z', ['a', 'b']) - self.assertArrayEqual(actual['z'], ['a', 'b']) + assert_array_equal(actual['z'], ['a', 'b']) actual = data.copy(deep=True) with raises_regex(ValueError, 'conflicting sizes'): @@ -985,9 +985,9 @@ def test_isel_fancy(self): expected_var3 = data['var3'].variable[slice(None), stations['dim1s'].variable] assert_equal(actual['a'].drop('dim2'), stations['a']) - self.assertArrayEqual(actual['var1'], expected_var1) - self.assertArrayEqual(actual['var2'], expected_var2) - self.assertArrayEqual(actual['var3'], expected_var3) + assert_array_equal(actual['var1'], expected_var1) + assert_array_equal(actual['var2'], expected_var2) + assert_array_equal(actual['var3'], expected_var3) def test_isel_dataarray(self): """ Test for indexing by DataArray """ @@ -1401,7 +1401,7 @@ def test_sel_fancy(self): idx_y = DataArray([0, 2, 1], dims=['b'], coords={'b': [0, 3, 6]}) expected_ary = data['foo'][[0, 1, 2], [0, 2, 1]] actual = data.sel(x=idx_x, y=idx_y) - self.assertArrayEqual(expected_ary, actual['foo']) + assert_array_equal(expected_ary, actual['foo']) assert_identical(actual['a'].drop('x'), idx_x['a']) assert_identical(actual['b'].drop('y'), idx_y['b']) @@ -1622,7 +1622,7 @@ def test_align(self): union = list('abcdefghijkl') left2, right2 = align(left, right, join='inner') - self.assertArrayEqual(left2['dim3'], intersection) + assert_array_equal(left2['dim3'], intersection) assert_identical(left2, right2) left2, right2 = align(left, right, join='outer') @@ -1935,7 +1935,7 @@ def test_rename_inplace(self): assert not data.equals(copied) assert data.dims == {'y': 3, 't': 3} # check virtual variables - self.assertArrayEqual(data['t.dayofyear'], [1, 2, 3]) + assert_array_equal(data['t.dayofyear'], [1, 2, 3]) def test_swap_dims(self): original = Dataset({'x': [1, 2, 3], 'y': ('x', list('abc')), 'z': 42}) @@ -2198,12 +2198,12 @@ def test_virtual_variables_time(self): expected = DataArray(1 + np.arange(20), coords=[data['time']], dims='time', name='dayofyear') - self.assertArrayEqual(data['time.month'].values, + assert_array_equal(data['time.month'].values, data.variables['time'].to_index().month) - self.assertArrayEqual(data['time.season'].values, 'DJF') + assert_array_equal(data['time.season'].values, 'DJF') # test virtual variable math - self.assertArrayEqual(data['time.dayofyear'] + 1, 2 + np.arange(20)) - self.assertArrayEqual(np.sin(data['time.dayofyear']), + assert_array_equal(data['time.dayofyear'] + 1, 2 + np.arange(20)) + assert_array_equal(np.sin(data['time.dayofyear']), np.sin(1 + np.arange(20))) # ensure they become coordinates expected = Dataset({}, {'dayofyear': data['time.dayofyear']}) diff --git a/xarray/tests/test_duck_array_ops.py b/xarray/tests/test_duck_array_ops.py index 6f5fa2f306b..9fb1b1aad40 100644 --- a/xarray/tests/test_duck_array_ops.py +++ b/xarray/tests/test_duck_array_ops.py @@ -4,6 +4,7 @@ from pytest import mark import numpy as np from numpy import array, nan +from . import assert_array_equal from xarray.core.duck_array_ops import ( first, last, count, mean, array_notnull_equiv, ) @@ -32,15 +33,15 @@ def test_first(self): for axis, expected in zip([0, 1, 2, -3, -2, -1], 2 * expected_results): actual = first(self.x, axis) - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) expected = self.x[0] actual = first(self.x, axis=0, skipna=False) - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) expected = self.x[..., 0] actual = first(self.x, axis=-1, skipna=False) - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) with raises_regex(IndexError, 'out of bounds'): first(self.x, 3) @@ -56,15 +57,15 @@ def test_last(self): for axis, expected in zip([0, 1, 2, -3, -2, -1], 2 * expected_results): actual = last(self.x, axis) - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) expected = self.x[-1] actual = last(self.x, axis=0, skipna=False) - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) expected = self.x[..., -1] actual = last(self.x, axis=-1, skipna=False) - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) with raises_regex(IndexError, 'out of bounds'): last(self.x, 3) @@ -73,7 +74,7 @@ def test_count(self): assert 12 == count(self.x) expected = array([[1, 2, 3], [3, 2, 1]]) - self.assertArrayEqual(expected, count(self.x, axis=-1)) + assert_array_equal(expected, count(self.x, axis=-1)) def test_all_nan_arrays(self): assert np.isnan(mean([np.nan, np.nan])) diff --git a/xarray/tests/test_indexing.py b/xarray/tests/test_indexing.py index 01bc1fb6816..96f9c4e5dd7 100644 --- a/xarray/tests/test_indexing.py +++ b/xarray/tests/test_indexing.py @@ -12,7 +12,8 @@ from xarray.core import indexing from xarray.core import nputils from xarray.core.pycompat import native_int_types -from . import TestCase, ReturnItem, raises_regex, IndexerMaker +from . import ( + TestCase, ReturnItem, raises_regex, IndexerMaker, assert_array_equal) B = IndexerMaker(indexing.BasicIndexer) @@ -32,8 +33,8 @@ def test_expanded_indexer(self): I[..., 0, :], I[y], I[y, y], I[..., y, y], I[..., 0, 1, 2, 3, 4]]: j = indexing.expanded_indexer(i, x.ndim) - self.assertArrayEqual(x[i], x[j]) - self.assertArrayEqual(self.set_to_zero(x, i), + assert_array_equal(x[i], x[j]) + assert_array_equal(self.set_to_zero(x, i), self.set_to_zero(x, j)) with raises_regex(IndexError, 'too many indices'): indexing.expanded_indexer(I[1, 2, 3], 2) @@ -100,8 +101,8 @@ def test_get_dim_indexers(self): def test_remap_label_indexers(self): def test_indexer(data, x, expected_pos, expected_idx=None): pos, idx = indexing.remap_label_indexers(data, {'x': x}) - self.assertArrayEqual(pos.get('x'), expected_pos) - self.assertArrayEqual(idx.get('x'), expected_idx) + assert_array_equal(pos.get('x'), expected_pos) + assert_array_equal(idx.get('x'), expected_idx) data = Dataset({'x': ('x', [1, 2, 3])}) mindex = pd.MultiIndex.from_product([['a', 'b'], [1, 2], [-1, -2]], @@ -147,7 +148,7 @@ def test_slice_slice(self): expected = x[i][j] new_slice = indexing.slice_slice(i, j, size=100) actual = x[new_slice] - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) def test_lazily_indexed_array(self): original = np.random.rand(10, 20, 30) @@ -170,7 +171,7 @@ def test_lazily_indexed_array(self): v_lazy[:, j, k][i], v_lazy[:, :, k][:, j][i]]: assert expected.shape == actual.shape - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) assert isinstance(actual._data, indexing.LazilyIndexedArray) @@ -190,7 +191,7 @@ def test_lazily_indexed_array(self): expected = np.asarray(v[i][j]) actual = v_lazy[i][j] assert expected.shape == actual.shape - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) assert isinstance(actual._data, indexing.LazilyIndexedArray) assert isinstance(actual._data.array, indexing.NumpyIndexingAdapter) @@ -201,8 +202,8 @@ def test_setitem(self): original = np.arange(10) wrapped = indexing.CopyOnWriteArray(original) wrapped[B[:]] = 0 - self.assertArrayEqual(original, np.arange(10)) - self.assertArrayEqual(wrapped, np.zeros(10)) + assert_array_equal(original, np.arange(10)) + assert_array_equal(wrapped, np.zeros(10)) def test_sub_array(self): original = np.arange(10) @@ -210,9 +211,9 @@ def test_sub_array(self): child = wrapped[B[:5]] assert isinstance(child, indexing.CopyOnWriteArray) child[B[:]] = 0 - self.assertArrayEqual(original, np.arange(10)) - self.assertArrayEqual(wrapped, np.arange(10)) - self.assertArrayEqual(child, np.zeros(5)) + assert_array_equal(original, np.arange(10)) + assert_array_equal(wrapped, np.arange(10)) + assert_array_equal(child, np.zeros(5)) def test_index_scalar(self): # regression test for GH1374 @@ -224,7 +225,7 @@ class TestMemoryCachedArray(TestCase): def test_wrapper(self): original = indexing.LazilyIndexedArray(np.arange(10)) wrapped = indexing.MemoryCachedArray(original) - self.assertArrayEqual(wrapped, np.arange(10)) + assert_array_equal(wrapped, np.arange(10)) assert isinstance(wrapped.array, indexing.NumpyIndexingAdapter) def test_sub_array(self): @@ -232,7 +233,7 @@ def test_sub_array(self): wrapped = indexing.MemoryCachedArray(original) child = wrapped[B[:5]] assert isinstance(child, indexing.MemoryCachedArray) - self.assertArrayEqual(child, np.arange(5)) + assert_array_equal(child, np.arange(5)) assert isinstance(child.array, indexing.NumpyIndexingAdapter) assert isinstance(wrapped.array, indexing.LazilyIndexedArray) @@ -240,7 +241,7 @@ def test_setitem(self): original = np.arange(10) wrapped = indexing.MemoryCachedArray(original) wrapped[B[:]] = 0 - self.assertArrayEqual(original, np.zeros(10)) + assert_array_equal(original, np.zeros(10)) def test_index_scalar(self): # regression test for GH1374 diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 05135a7d5d5..6d584e8d77d 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -25,7 +25,8 @@ _color_palette, import_seaborn) from . import ( - TestCase, requires_matplotlib, requires_seaborn, raises_regex, assert_equal) + TestCase, requires_matplotlib, requires_seaborn, raises_regex, assert_equal, + assert_array_equal) @pytest.mark.flaky @@ -159,10 +160,10 @@ def test_can_pass_in_axis(self): self.pass_in_axis(self.darray.plot) def test__infer_interval_breaks(self): - self.assertArrayEqual([-0.5, 0.5, 1.5], _infer_interval_breaks([0, 1])) - self.assertArrayEqual([-0.5, 0.5, 5.0, 9.5, 10.5], + assert_array_equal([-0.5, 0.5, 1.5], _infer_interval_breaks([0, 1])) + assert_array_equal([-0.5, 0.5, 5.0, 9.5, 10.5], _infer_interval_breaks([0, 1, 9, 10])) - self.assertArrayEqual( + assert_array_equal( pd.date_range('20000101', periods=4) - np.timedelta64(12, 'h'), _infer_interval_breaks(pd.date_range('20000101', periods=3))) @@ -194,7 +195,7 @@ def test_convenient_facetgrid(self): d.coords['z'] = list('abcd') g = d.plot(x='x', y='y', col='z', col_wrap=2, cmap='cool') - self.assertArrayEqual(g.axes.shape, [2, 2]) + assert_array_equal(g.axes.shape, [2, 2]) for ax in g.axes.flat: assert ax.has_data() @@ -251,7 +252,7 @@ def test_convenient_facetgrid_4d(self): d = DataArray(a, dims=['y', 'x', 'columns', 'rows']) g = d.plot(x='x', y='y', col='columns', row='rows') - self.assertArrayEqual(g.axes.shape, [3, 2]) + assert_array_equal(g.axes.shape, [3, 2]) for ax in g.axes.flat: assert ax.has_data() @@ -429,7 +430,7 @@ def test_list_levels(self): for wrap_levels in [list, np.array, pd.Index, DataArray]: cmap_params = _determine_cmap_params( data, levels=wrap_levels(orig_levels)) - self.assertArrayEqual(cmap_params['levels'], orig_levels) + assert_array_equal(cmap_params['levels'], orig_levels) def test_divergentcontrol(self): neg = self.data - 0.1 @@ -530,7 +531,7 @@ def test_build_discrete_cmap(self): assert ncmap.N == len(levels) - 1 assert len(ncmap.colors) == len(levels) - 1 assert cnorm.N == len(levels) - self.assertArrayEqual(cnorm.boundaries, levels) + assert_array_equal(cnorm.boundaries, levels) assert max(levels) == cnorm.vmax assert min(levels) == cnorm.vmin if filled: @@ -546,7 +547,7 @@ def test_discrete_colormap_list_of_levels(self): ('min', [2, 5, 10, 15])]: for kind in ['imshow', 'pcolormesh', 'contourf', 'contour']: primitive = getattr(self.darray.plot, kind)(levels=levels) - self.assertArrayEqual(levels, primitive.norm.boundaries) + assert_array_equal(levels, primitive.norm.boundaries) assert max(levels) == primitive.norm.vmax assert min(levels) == primitive.norm.vmin if kind != 'contour': @@ -841,7 +842,7 @@ def test_convenient_facetgrid(self): d = DataArray(a, dims=['y', 'x', 'z']) g = self.plotfunc(d, x='x', y='y', col='z', col_wrap=2) - self.assertArrayEqual(g.axes.shape, [2, 2]) + assert_array_equal(g.axes.shape, [2, 2]) for (y, x), ax in np.ndenumerate(g.axes): assert ax.has_data() if x == 0: @@ -855,7 +856,7 @@ def test_convenient_facetgrid(self): # Infering labels g = self.plotfunc(d, col='z', col_wrap=2) - self.assertArrayEqual(g.axes.shape, [2, 2]) + assert_array_equal(g.axes.shape, [2, 2]) for (y, x), ax in np.ndenumerate(g.axes): assert ax.has_data() if x == 0: @@ -872,7 +873,7 @@ def test_convenient_facetgrid_4d(self): d = DataArray(a, dims=['y', 'x', 'columns', 'rows']) g = self.plotfunc(d, x='x', y='y', col='columns', row='rows') - self.assertArrayEqual(g.axes.shape, [3, 2]) + assert_array_equal(g.axes.shape, [3, 2]) for ax in g.axes.flat: assert ax.has_data() @@ -1246,19 +1247,19 @@ def test_can_set_norm(self): @pytest.mark.slow def test_figure_size(self): - self.assertArrayEqual(self.g.fig.get_size_inches(), (10, 3)) + assert_array_equal(self.g.fig.get_size_inches(), (10, 3)) g = xplt.FacetGrid(self.darray, col='z', size=6) - self.assertArrayEqual(g.fig.get_size_inches(), (19, 6)) + assert_array_equal(g.fig.get_size_inches(), (19, 6)) g = self.darray.plot.imshow(col='z', size=6) - self.assertArrayEqual(g.fig.get_size_inches(), (19, 6)) + assert_array_equal(g.fig.get_size_inches(), (19, 6)) g = xplt.FacetGrid(self.darray, col='z', size=4, aspect=0.5) - self.assertArrayEqual(g.fig.get_size_inches(), (7, 4)) + assert_array_equal(g.fig.get_size_inches(), (7, 4)) g = xplt.FacetGrid(self.darray, col='z', figsize=(9, 4)) - self.assertArrayEqual(g.fig.get_size_inches(), (9, 4)) + assert_array_equal(g.fig.get_size_inches(), (9, 4)) with raises_regex(ValueError, "cannot provide both"): g = xplt.plot(self.darray, row=2, col='z', figsize=(6, 4), size=6) diff --git a/xarray/tests/test_ufuncs.py b/xarray/tests/test_ufuncs.py index 7449beabaf8..d813eab1256 100644 --- a/xarray/tests/test_ufuncs.py +++ b/xarray/tests/test_ufuncs.py @@ -8,7 +8,10 @@ import xarray.ufuncs as xu import xarray as xr -from . import TestCase, raises_regex, assert_equal, assert_identical +from . import ( + TestCase, raises_regex, assert_equal, assert_identical, assert_array_equal, + assert_equal) + class TestOps(TestCase): @@ -17,7 +20,7 @@ def assertIdentical(self, a, b): try: assert a.identical(b), (a, b) except AttributeError: - self.assertArrayEqual(a, b) + assert_array_equal(a, b) def test_unary(self): args = [0, diff --git a/xarray/tests/test_utils.py b/xarray/tests/test_utils.py index 5741333e18f..e5833fd01f4 100644 --- a/xarray/tests/test_utils.py +++ b/xarray/tests/test_utils.py @@ -8,7 +8,7 @@ from xarray.core import duck_array_ops, utils from xarray.core.pycompat import OrderedDict -from . import TestCase, requires_dask +from . import TestCase, requires_dask, assert_array_equal class TestAlias(TestCase): @@ -33,7 +33,7 @@ def test(self): (pd.Index(td, dtype=object), td.astype(object)), ]: actual = utils.safe_cast_to_index(array) - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) assert expected.dtype == actual.dtype diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index 31a1d85c8a2..41baf41eeae 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -25,7 +25,8 @@ from xarray.core.utils import NDArrayMixin from . import ( - TestCase, source_ndarray, requires_dask, raises_regex, assert_identical) + TestCase, source_ndarray, requires_dask, raises_regex, assert_identical, + assert_equal, assert_array_equal) from xarray.tests import requires_bottleneck @@ -35,7 +36,7 @@ def test_properties(self): data = 0.5 * np.arange(10) v = self.cls(['time'], data, {'foo': 'bar'}) assert v.dims == ('time',) - self.assertArrayEqual(v.values, data) + assert_array_equal(v.values, data) assert v.dtype == float assert v.shape == (10,) assert v.size == 10 @@ -67,24 +68,24 @@ def test_getitem_1d(self): v_new = v[dict(x=[0, 1])] assert v_new.dims == ('x', ) - self.assertArrayEqual(v_new, data[[0, 1]]) + assert_array_equal(v_new, data[[0, 1]]) v_new = v[dict(x=slice(None))] assert v_new.dims == ('x', ) - self.assertArrayEqual(v_new, data) + assert_array_equal(v_new, data) v_new = v[dict(x=Variable('a', [0, 1]))] assert v_new.dims == ('a', ) - self.assertArrayEqual(v_new, data[[0, 1]]) + assert_array_equal(v_new, data[[0, 1]]) v_new = v[dict(x=1)] assert v_new.dims == () - self.assertArrayEqual(v_new, data[1]) + assert_array_equal(v_new, data[1]) # tuple argument v_new = v[slice(None)] assert v_new.dims == ('x', ) - self.assertArrayEqual(v_new, data) + assert_array_equal(v_new, data) def test_getitem_1d_fancy(self): v = self.cls(['x'], [0, 1, 2]) @@ -93,7 +94,7 @@ def test_getitem_1d_fancy(self): v_new = v[ind] assert v_new.dims == ('a', 'b') expected = np.array(v._data)[([0, 1], [0, 1]), ] - self.assertArrayEqual(v_new, expected) + assert_array_equal(v_new, expected) # boolean indexing ind = Variable(('x', ), [True, False, True]) @@ -218,9 +219,9 @@ def test_0d_object_array_with_list(self): listarray = np.empty((1,), dtype=object) listarray[0] = [1, 2, 3] x = self.cls('x', listarray) - self.assertArrayEqual(x.data, listarray) - self.assertArrayEqual(x[0].data, listarray.squeeze()) - self.assertArrayEqual(x.squeeze().data, listarray.squeeze()) + assert_array_equal(x.data, listarray) + assert_array_equal(x[0].data, listarray.squeeze()) + assert_array_equal(x.squeeze().data, listarray.squeeze()) def test_index_and_concat_datetime(self): # regression test for #125 @@ -233,7 +234,7 @@ def test_index_and_concat_datetime(self): [expected[[i]] for i in range(10)]]: actual = Variable.concat(times, 't') assert expected.dtype == actual.dtype - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) def test_0d_time_data(self): # regression test for #105 @@ -251,7 +252,7 @@ def test_datetime64_conversion(self): ]: v = self.cls(['t'], values) assert v.dtype == np.dtype('datetime64[ns]') - self.assertArrayEqual(v.values, times.values) + assert_array_equal(v.values, times.values) assert v.values.dtype == np.dtype('datetime64[ns]') same_source = source_ndarray(v.values) is source_ndarray(values) assert preserve_source == same_source @@ -266,7 +267,7 @@ def test_timedelta64_conversion(self): ]: v = self.cls(['t'], values) assert v.dtype == np.dtype('timedelta64[ns]') - self.assertArrayEqual(v.values, times.values) + assert_array_equal(v.values, times.values) assert v.values.dtype == np.dtype('timedelta64[ns]') same_source = source_ndarray(v.values) is source_ndarray(values) assert preserve_source == same_source @@ -300,27 +301,27 @@ def test_1d_math(self): # unary ops assert_identical(base_v, +v) assert_identical(base_v, abs(v)) - self.assertArrayEqual((-v).values, -x) + assert_array_equal((-v).values, -x) # binary ops with numbers assert_identical(base_v, v + 0) assert_identical(base_v, 0 + v) assert_identical(base_v, v * 1) # binary ops with numpy arrays - self.assertArrayEqual((v * x).values, x ** 2) - self.assertArrayEqual((x * v).values, x ** 2) - self.assertArrayEqual(v - y, v - 1) - self.assertArrayEqual(y - v, 1 - v) + assert_array_equal((v * x).values, x ** 2) + assert_array_equal((x * v).values, x ** 2) + assert_array_equal(v - y, v - 1) + assert_array_equal(y - v, 1 - v) # verify attributes are dropped v2 = self.cls(['x'], x, {'units': 'meters'}) assert_identical(base_v, +v2) # binary ops with all variables - self.assertArrayEqual(v + v, 2 * v) + assert_array_equal(v + v, 2 * v) w = self.cls(['x'], y, {'foo': 'bar'}) assert_identical(v + w, self.cls(['x'], x + y).to_base_variable()) - assert_equal((v * w).values, x * y) + assert_array_equal((v * w).values, x * y) # something complicated - self.assertArrayEqual((v ** 2 * w - 1 + x).values, x ** 2 * y - 1 + x) + assert_array_equal((v ** 2 * w - 1 + x).values, x ** 2 * y - 1 + x) # make sure dtype is preserved (for Index objects) assert float == (+v).dtype assert float == (+v).values.dtype @@ -343,9 +344,9 @@ def test_1d_reduce(self): def test_array_interface(self): x = np.arange(5) v = self.cls(['x'], x) - self.assertArrayEqual(np.asarray(v), x) + assert_array_equal(np.asarray(v), x) # test patched in methods - self.assertArrayEqual(v.astype(float), x.astype(float)) + assert_array_equal(v.astype(float), x.astype(float)) # think this is a break, that argsort changes the type assert_identical(v.argsort(), v.to_base_variable()) assert_identical(v.clip(2, 3), @@ -366,8 +367,8 @@ def example_1d_objects(self): def test___array__(self): for v, data in self.example_1d_objects(): - self.assertArrayEqual(v.values, np.asarray(data)) - self.assertArrayEqual(np.asarray(v), np.asarray(data)) + assert_array_equal(v.values, np.asarray(data)) + assert_array_equal(np.asarray(v), np.asarray(data)) assert v[0].values == np.asarray(data)[0] assert np.asarray(v[0]) == np.asarray(data)[0] @@ -495,7 +496,7 @@ def test_copy_index(self): w = v.copy(deep=deep) assert isinstance(w._data, PandasIndexAdapter) assert isinstance(w.to_index(), pd.MultiIndex) - self.assertArrayEqual(v._data.array, w._data.array) + assert_array_equal(v._data.array, w._data.array) def test_real_and_imag(self): v = self.cls('x', np.arange(3) - 1j * np.arange(3), {'foo': 'bar'}) @@ -554,28 +555,28 @@ def test_getitem_advanced(self): # orthogonal indexing v_new = v[([0, 1], [1, 0])] assert v_new.dims == ('x', 'y') - self.assertArrayEqual(v_new, v_data[[0, 1]][:, [1, 0]]) + assert_array_equal(v_new, v_data[[0, 1]][:, [1, 0]]) v_new = v[[0, 1]] assert v_new.dims == ('x', 'y') - self.assertArrayEqual(v_new, v_data[[0, 1]]) + assert_array_equal(v_new, v_data[[0, 1]]) # with mixed arguments ind = Variable(['a'], [0, 1]) v_new = v[dict(x=[0, 1], y=ind)] assert v_new.dims == ('x', 'a') - self.assertArrayEqual(v_new, v_data[[0, 1]][:, [0, 1]]) + assert_array_equal(v_new, v_data[[0, 1]][:, [0, 1]]) # boolean indexing v_new = v[dict(x=[True, False], y=[False, True, False])] assert v_new.dims == ('x', 'y') - self.assertArrayEqual(v_new, v_data[0][1]) + assert_array_equal(v_new, v_data[0][1]) # with scalar variable ind = Variable((), 2) v_new = v[dict(y=ind)] expected = v[dict(y=2)] - self.assertArrayEqual(v_new, expected) + assert_array_equal(v_new, expected) # with boolean variable with wrong shape ind = np.array([True, False]) @@ -593,9 +594,9 @@ def test_getitem_uint_1d(self): v_data = v.compute().data v_new = v[np.array([0])] - self.assertArrayEqual(v_new, v_data[0]) + assert_array_equal(v_new, v_data[0]) v_new = v[np.array([0], dtype="uint64")] - self.assertArrayEqual(v_new, v_data[0]) + assert_array_equal(v_new, v_data[0]) def test_getitem_uint(self): # regression test for #1405 @@ -603,12 +604,12 @@ def test_getitem_uint(self): v_data = v.compute().data v_new = v[np.array([0])] - self.assertArrayEqual(v_new, v_data[[0], :]) + assert_array_equal(v_new, v_data[[0], :]) v_new = v[np.array([0], dtype="uint64")] - self.assertArrayEqual(v_new, v_data[[0], :]) + assert_array_equal(v_new, v_data[[0], :]) v_new = v[np.uint64(0)] - self.assertArrayEqual(v_new, v_data[0, :]) + assert_array_equal(v_new, v_data[0, :]) def test_getitem_0d_array(self): # make sure 0d-np.array can be used as an indexer @@ -616,7 +617,7 @@ def test_getitem_0d_array(self): v_data = v.compute().data v_new = v[np.array([0])[0]] - self.assertArrayEqual(v_new, v_data[0]) + assert_array_equal(v_new, v_data[0]) def test_getitem_fancy(self): v = self.cls(['x', 'y'], [[0, 1, 2], [3, 4, 5]]) @@ -625,59 +626,59 @@ def test_getitem_fancy(self): ind = Variable(['a', 'b'], [[0, 1, 1], [1, 1, 0]]) v_new = v[ind] assert v_new.dims == ('a', 'b', 'y') - self.assertArrayEqual(v_new, v_data[[[0, 1, 1], [1, 1, 0]], :]) + assert_array_equal(v_new, v_data[[[0, 1, 1], [1, 1, 0]], :]) # It would be ok if indexed with the multi-dimensional array including # the same name ind = Variable(['x', 'b'], [[0, 1, 1], [1, 1, 0]]) v_new = v[ind] assert v_new.dims == ('x', 'b', 'y') - self.assertArrayEqual(v_new, v_data[[[0, 1, 1], [1, 1, 0]], :]) + assert_array_equal(v_new, v_data[[[0, 1, 1], [1, 1, 0]], :]) ind = Variable(['a', 'b'], [[0, 1, 2], [2, 1, 0]]) v_new = v[dict(y=ind)] assert v_new.dims == ('x', 'a', 'b') - self.assertArrayEqual(v_new, v_data[:, ([0, 1, 2], [2, 1, 0])]) + assert_array_equal(v_new, v_data[:, ([0, 1, 2], [2, 1, 0])]) ind = Variable(['a', 'b'], [[0, 0], [1, 1]]) v_new = v[dict(x=[1, 0], y=ind)] assert v_new.dims == ('x', 'a', 'b') - self.assertArrayEqual(v_new, v_data[[1, 0]][:, ind]) + assert_array_equal(v_new, v_data[[1, 0]][:, ind]) # along diagonal ind = Variable(['a'], [0, 1]) v_new = v[ind, ind] assert v_new.dims == ('a',) - self.assertArrayEqual(v_new, v_data[[0, 1], [0, 1]]) + assert_array_equal(v_new, v_data[[0, 1], [0, 1]]) # with integer ind = Variable(['a', 'b'], [[0, 0], [1, 1]]) v_new = v[dict(x=0, y=ind)] assert v_new.dims == ('a', 'b') - self.assertArrayEqual(v_new[0], v_data[0][[0, 0]]) - self.assertArrayEqual(v_new[1], v_data[0][[1, 1]]) + assert_array_equal(v_new[0], v_data[0][[0, 0]]) + assert_array_equal(v_new[1], v_data[0][[1, 1]]) # with slice ind = Variable(['a', 'b'], [[0, 0], [1, 1]]) v_new = v[dict(x=slice(None), y=ind)] assert v_new.dims == ('x', 'a', 'b') - self.assertArrayEqual(v_new, v_data[:, [[0, 0], [1, 1]]]) + assert_array_equal(v_new, v_data[:, [[0, 0], [1, 1]]]) ind = Variable(['a', 'b'], [[0, 0], [1, 1]]) v_new = v[dict(x=ind, y=slice(None))] assert v_new.dims == ('a', 'b', 'y') - self.assertArrayEqual(v_new, v_data[[[0, 0], [1, 1]], :]) + assert_array_equal(v_new, v_data[[[0, 0], [1, 1]], :]) ind = Variable(['a', 'b'], [[0, 0], [1, 1]]) v_new = v[dict(x=ind, y=slice(None, 1))] assert v_new.dims == ('a', 'b', 'y') - self.assertArrayEqual(v_new, v_data[[[0, 0], [1, 1]], slice(None, 1)]) + assert_array_equal(v_new, v_data[[[0, 0], [1, 1]], slice(None, 1)]) # slice matches explicit dimension ind = Variable(['y'], [0, 1]) v_new = v[ind, :2] assert v_new.dims == ('y',) - self.assertArrayEqual(v_new, v_data[[0, 1], [0, 1]]) + assert_array_equal(v_new, v_data[[0, 1], [0, 1]]) # with multiple slices v = self.cls(['x', 'y', 'z'], [[[1, 2, 3], [4, 5, 6]]]) @@ -734,8 +735,8 @@ def setUp(self): def test_data_and_values(self): v = Variable(['time', 'x'], self.d) - self.assertArrayEqual(v.data, self.d) - self.assertArrayEqual(v.values, self.d) + assert_array_equal(v.data, self.d) + assert_array_equal(v.values, self.d) assert source_ndarray(v.values) is self.d with pytest.raises(ValueError): # wrong size @@ -1036,31 +1037,31 @@ def test_items(self): assert np.all(v.values == 0) # test orthogonal setting v[range(10), range(11)] = 1 - self.assertArrayEqual(v.values, np.ones((10, 11))) + assert_array_equal(v.values, np.ones((10, 11))) def test_getitem_basic(self): v = self.cls(['x', 'y'], [[0, 1, 2], [3, 4, 5]]) v_new = v[dict(x=0)] assert v_new.dims == ('y', ) - self.assertArrayEqual(v_new, v._data[0]) + assert_array_equal(v_new, v._data[0]) v_new = v[dict(x=0, y=slice(None))] assert v_new.dims == ('y', ) - self.assertArrayEqual(v_new, v._data[0]) + assert_array_equal(v_new, v._data[0]) v_new = v[dict(x=0, y=1)] assert v_new.dims == () - self.assertArrayEqual(v_new, v._data[0, 1]) + assert_array_equal(v_new, v._data[0, 1]) v_new = v[dict(y=1)] assert v_new.dims == ('x', ) - self.assertArrayEqual(v_new, v._data[:, 1]) + assert_array_equal(v_new, v._data[:, 1]) # tuple argument v_new = v[(slice(None), 1)] assert v_new.dims == ('x', ) - self.assertArrayEqual(v_new, v._data[:, 1]) + assert_array_equal(v_new, v._data[:, 1]) def test_getitem_with_mask_2d_input(self): v = Variable(('x', 'y'), [[0, 1, 2], [3, 4, 5]]) @@ -1149,7 +1150,7 @@ def test_roll_consistency(self): for shift in [-3, 0, 1, 7, 11]: expected = np.roll(v.values, shift, axis=axis) actual = v.roll(**{dim: shift}).values - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) def test_transpose(self): v = Variable(['time', 'x'], self.d) @@ -1341,7 +1342,7 @@ def test_inplace_math(self): assert v is v2 # since we provided an ndarray for data, it is also modified in-place assert source_ndarray(v.values) is x - self.assertArrayEqual(v.values, np.arange(5) + 1) + assert_array_equal(v.values, np.arange(5) + 1) with raises_regex(ValueError, 'dimensions cannot change'): v += Variable('y', np.arange(5)) @@ -1493,13 +1494,13 @@ def test_setitem(self): v = Variable(['x', 'y'], [[0, 3, 2], [3, 4, 5]]) v[dict(x=[0, 1])] = 1 - self.assertArrayEqual(v[[0, 1]], np.ones_like(v[[0, 1]])) + assert_array_equal(v[[0, 1]], np.ones_like(v[[0, 1]])) # boolean indexing v = Variable(['x', 'y'], [[0, 3, 2], [3, 4, 5]]) v[dict(x=[True, False])] = 1 - self.assertArrayEqual(v[0], np.ones_like(v[0])) + assert_array_equal(v[0], np.ones_like(v[0])) v = Variable(['x', 'y'], [[0, 3, 2], [3, 4, 5]]) v[dict(x=[True, False], y=[False, True, False])] = 1 assert v[0, 1] == 1 @@ -1511,7 +1512,7 @@ def assert_assigned_2d(array, key_x, key_y, values): expected[key_x, key_y] = values v = Variable(['x', 'y'], array) v[dict(x=key_x, y=key_y)] = values - self.assertArrayEqual(expected, v) + assert_array_equal(expected, v) # 1d vectorized indexing assert_assigned_2d(np.random.randn(4, 3), @@ -1567,8 +1568,8 @@ def assert_assigned_2d(array, key_x, key_y, values): v = Variable(['x', 'y'], [[0, 3, 2], [3, 4, 5]]) ind = Variable(['a'], [0, 1]) v[dict(x=ind)] = Variable(['a', 'y'], np.ones((2, 3), dtype=int) * 10) - self.assertArrayEqual(v[0], np.ones_like(v[0]) * 10) - self.assertArrayEqual(v[1], np.ones_like(v[1]) * 10) + assert_array_equal(v[0], np.ones_like(v[0]) * 10) + assert_array_equal(v[1], np.ones_like(v[1]) * 10) assert v.dims == ('x', 'y') # dimension should not change # increment @@ -1647,7 +1648,7 @@ def test_data(self): assert isinstance(x._data, PandasIndexAdapter) assert isinstance(x.data, np.ndarray) assert float == x.dtype - self.assertArrayEqual(np.arange(3), x) + assert_array_equal(np.arange(3), x) assert float == x.values.dtype with raises_regex(TypeError, 'cannot be modified'): x[:] = 0 @@ -1737,7 +1738,7 @@ def test_unchanged_types(self): def test_converted_types(self): for input_array in [[[0, 1, 2]], pd.DataFrame([[0, 1, 2]])]: actual = as_compatible_data(input_array) - self.assertArrayEqual(np.asarray(input_array), actual) + assert_array_equal(np.asarray(input_array), actual) assert np.ndarray == type(actual) assert np.asarray(input_array).dtype == actual.dtype @@ -1745,14 +1746,14 @@ def test_masked_array(self): original = np.ma.MaskedArray(np.arange(5)) expected = np.arange(5) actual = as_compatible_data(original) - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) assert np.dtype(int) == actual.dtype original = np.ma.MaskedArray(np.arange(5), mask=4 * [False] + [True]) expected = np.arange(5.0) expected[-1] = np.nan actual = as_compatible_data(original) - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) assert np.dtype(float) == actual.dtype def test_datetime(self): @@ -1806,7 +1807,7 @@ def check(actual, expect_dtype, expect_values): assert actual.dims == orig.dims assert actual.attrs == orig.attrs assert actual.chunks == orig.chunks - self.assertArrayEqual(actual.values, expect_values) + assert_array_equal(actual.values, expect_values) check(full_like(orig, 2), orig.dtype, np.full_like(orig.values, 2)) From 66c9cd656c13bb136b25bf38ba59efc628e56198 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 16:06:43 -0500 Subject: [PATCH 22/41] assert_identical half way in ufuncs --- xarray/tests/test_ufuncs.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/xarray/tests/test_ufuncs.py b/xarray/tests/test_ufuncs.py index d813eab1256..961312370b7 100644 --- a/xarray/tests/test_ufuncs.py +++ b/xarray/tests/test_ufuncs.py @@ -13,9 +13,8 @@ assert_equal) - class TestOps(TestCase): - def assertIdentical(self, a, b): + def assert_identical(self, a, b): assert type(a) is type(b) or (float(a) == float(b)) try: assert a.identical(b), (a, b) @@ -29,7 +28,7 @@ def test_unary(self): xr.DataArray([0, 0], dims='x'), xr.Dataset({'y': ('x', [0, 0])})] for a in args: - self.assertIdentical(a + 1, xu.cos(a)) + self.assert_identical(a + 1, xu.cos(a)) def test_binary(self): args = [0, @@ -39,10 +38,10 @@ def test_binary(self): xr.Dataset({'y': ('x', [0, 0])})] for n, t1 in enumerate(args): for t2 in args[n:]: - self.assertIdentical(t2 + 1, xu.maximum(t1, t2 + 1)) - self.assertIdentical(t2 + 1, xu.maximum(t2, t1 + 1)) - self.assertIdentical(t2 + 1, xu.maximum(t1 + 1, t2)) - self.assertIdentical(t2 + 1, xu.maximum(t2 + 1, t1)) + self.assert_identical(t2 + 1, xu.maximum(t1, t2 + 1)) + self.assert_identical(t2 + 1, xu.maximum(t2, t1 + 1)) + self.assert_identical(t2 + 1, xu.maximum(t1 + 1, t2)) + self.assert_identical(t2 + 1, xu.maximum(t2 + 1, t1)) def test_groupby(self): ds = xr.Dataset({'a': ('x', [0, 0, 0])}, {'c': ('x', [0, 0, 1])}) @@ -68,4 +67,4 @@ def test_groupby(self): def test_pickle(self): a = 1.0 cos_pickled = pickle.loads(pickle.dumps(xu.cos)) - self.assertIdentical(cos_pickled(a), xu.cos(a)) + self.assert_identical(cos_pickled(a), xu.cos(a)) From 4e32f9633cc9274192ce7cac67d99b66dfbd7c1c Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 16:37:44 -0500 Subject: [PATCH 23/41] backends merge --- xarray/tests/test_backends.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 3be4b6b93f6..375107a1943 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -599,7 +599,8 @@ def equals_latlon(obj): 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.assertTrue( + equals_latlon(ds['precip'].attrs['coordinates'])) self.assertNotIn('coordinates', ds.attrs) self.assertNotIn('coordinates', ds['lat'].attrs) self.assertNotIn('coordinates', ds['lon'].attrs) @@ -889,7 +890,8 @@ def test_open_encodings(self): 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) + self.assertDictEqual(actual_encoding, + expected['time'].encoding) def test_dump_encodings(self): # regression test for #709 @@ -914,9 +916,10 @@ def test_dump_and_open_encodings(self): with create_tmp_file() as tmp_file2: xarray_dataset.to_netcdf(tmp_file2) with nc4.Dataset(tmp_file2, 'r') as ds: - assert ds.variables['time'].getncattr('units') == units - self.assertArrayEqual(ds.variables['time'], - np.arange(10) + 4) + self.assertEqual( + ds.variables['time'].getncattr('units'), units) + self.assertArrayEqual( + ds.variables['time'], np.arange(10) + 4) def test_compression_encoding(self): data = create_test_data() @@ -966,7 +969,7 @@ def test_mask_and_scale(self): expected = np.ma.array([-1, -1, 10, 10.1, 10.2], mask=[True, True, False, False, False]) actual = nc.variables['x'][:] - assert_array_equal(expected, actual) + self.assertArrayEqual(expected, actual) # now check xarray with open_dataset(tmp_file) as ds: @@ -2043,7 +2046,8 @@ def test_cmp_local_file(self): assert actual.attrs.keys() == expected.attrs.keys() with self.create_datasets() as (actual, expected): - self.assertDatasetEqual(actual.isel(l=2), expected.isel(l=2)) + self.assertDatasetEqual( + actual.isel(l=2), expected.isel(l=2)) # noqa: E741 with self.create_datasets() as (actual, expected): self.assertDatasetEqual(actual.isel(i=0, j=-1), From 71b34f9faefd484e0929e087eb56e6bbee5ccacb Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 16:39:28 -0500 Subject: [PATCH 24/41] lint v1 --- xarray/tests/test_dataset.py | 7 ++++--- xarray/tests/test_plot.py | 8 ++++---- xarray/tests/test_utils.py | 3 ++- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 61252f271ca..008b9487208 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -1631,7 +1631,7 @@ def test_align(self): assert_equal(left2['dim3'].variable, right2['dim3'].variable) assert_identical(left2.sel(dim3=intersection), - right2.sel(dim3=intersection)) + right2.sel(dim3=intersection)) assert np.isnan(left2['var3'][-2:]).all() assert np.isnan(right2['var3'][:2]).all() @@ -1646,9 +1646,10 @@ def test_align(self): left2, right2 = align(left, right, join='right') assert_equal(left2['dim3'].variable, right2['dim3'].variable) assert_equal(left2['dim3'].variable, right['dim3'].variable) - self.assertDatasetIdentical(left2.sel(dim3=intersection), + assert_identical(left2.sel(dim3=intersection), - right2.sel(dim3=intersection)) + right2.sel(dim3=intersection)) + assert np.isnan(left2['var3'][-2:]).all() with raises_regex(ValueError, 'invalid value for join'): diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 6d584e8d77d..ec1d79d2198 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -25,8 +25,8 @@ _color_palette, import_seaborn) from . import ( - TestCase, requires_matplotlib, requires_seaborn, raises_regex, assert_equal, - assert_array_equal) + TestCase, requires_matplotlib, requires_seaborn, raises_regex, + assert_equal, assert_array_equal) @pytest.mark.flaky @@ -162,7 +162,7 @@ def test_can_pass_in_axis(self): def test__infer_interval_breaks(self): assert_array_equal([-0.5, 0.5, 1.5], _infer_interval_breaks([0, 1])) assert_array_equal([-0.5, 0.5, 5.0, 9.5, 10.5], - _infer_interval_breaks([0, 1, 9, 10])) + _infer_interval_breaks([0, 1, 9, 10])) assert_array_equal( pd.date_range('20000101', periods=4) - np.timedelta64(12, 'h'), _infer_interval_breaks(pd.date_range('20000101', periods=3))) @@ -809,7 +809,7 @@ def test_colorbar_kwargs(self): # note that there are two ways to achieve this fig, (ax, cax) = plt.subplots(1, 2) self.plotmethod(ax=ax, add_colorbar=True, - cbar_kwargs={'label':'MyBar', 'cax':cax}) + cbar_kwargs={'label': 'MyBar', 'cax': cax}) assert ax.has_data() assert cax.has_data() alltxt = text_in_fig() diff --git a/xarray/tests/test_utils.py b/xarray/tests/test_utils.py index e5833fd01f4..2353e1b5259 100644 --- a/xarray/tests/test_utils.py +++ b/xarray/tests/test_utils.py @@ -53,7 +53,8 @@ def test_0d(self): # verify our work around for pd.isnull not working for 0-dimensional # object arrays assert duck_array_ops.array_equiv(0, np.array(0, dtype=object)) - assert duck_array_ops.array_equiv(np.nan, np.array(np.nan, dtype=object)) + assert duck_array_ops.array_equiv(np.nan, + np.array(np.nan, dtype=object)) assert not duck_array_ops.array_equiv(0, np.array(1, dtype=object)) From 362ed63a28b6f5bd9331acd9fa6b018081975ed6 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 17:14:58 -0500 Subject: [PATCH 25/41] lint v2 --- xarray/tests/test_dataarray.py | 77 +- xarray/tests/test_dataset.py | 1925 ++++++++++++++++++++------------ 2 files changed, 1264 insertions(+), 738 deletions(-) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 2e31f891796..63be8018a82 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -320,8 +320,7 @@ def test_constructor_from_self_described(self): series = pd.Series(data[0], index=pd.Index([-1, -2], name='y')) actual = DataArray(series) - assert_equal(expected[0].reset_coords('x', drop=True), - actual) + assert_equal(expected[0].reset_coords('x', drop=True), actual) panel = pd.Panel({0: frame}) actual = DataArray(panel) @@ -654,21 +653,20 @@ def test_isel(self): assert_identical(self.dv[0], self.dv.isel(x=0)) assert_identical(self.dv, self.dv.isel(x=slice(None))) assert_identical(self.dv[:3], self.dv.isel(x=slice(3))) - assert_identical(self.dv[:3, :5], - self.dv.isel(x=slice(3), y=slice(5))) + assert_identical(self.dv[:3, :5], self.dv.isel(x=slice(3), y=slice(5))) def test_isel_types(self): # regression test for #1405 da = DataArray([1, 2, 3], dims='x') # uint64 assert_identical(da.isel(x=np.array([0], dtype="uint64")), - da.isel(x=np.array([0]))) + da.isel(x=np.array([0]))) # uint32 assert_identical(da.isel(x=np.array([0], dtype="uint32")), - da.isel(x=np.array([0]))) + da.isel(x=np.array([0]))) # int64 assert_identical(da.isel(x=np.array([0], dtype="int64")), - da.isel(x=np.array([0]))) + da.isel(x=np.array([0]))) def test_isel_fancy(self): shape = (10, 7, 6) @@ -780,8 +778,7 @@ def test_sel_dataarray(self): assert_array_equal(actual, da.isel(x=[0, 1, 2])) assert 'new_dim' in actual.dims assert 'new_dim' in actual.coords - assert_equal(actual['new_dim'].drop('x'), - ind['new_dim']) + assert_equal(actual['new_dim'].drop('x'), ind['new_dim']) def test_sel_no_index(self): array = DataArray(np.arange(10), dims='x') @@ -897,8 +894,7 @@ def test_loc(self): assert_identical(da[1], da.loc[{'x': 'b'}]) assert_identical(da[1], da.loc['b', ...]) assert_identical(da[:3], da.loc[['a', 'b', 'c']]) - assert_identical(da[:3, :4], - da.loc[['a', 'b', 'c'], np.arange(4)]) + assert_identical(da[:3, :4], da.loc[['a', 'b', 'c'], np.arange(4)]) assert_identical(da[:, :4], da.loc[:, self.ds['y'] < 4]) def test_loc_assign(self): @@ -999,15 +995,14 @@ def test_sel(lab_indexer, pos_indexer, replaced_idx=False, test_sel({'one': 'a'}, range(4), replaced_idx=True) assert_identical(mdata.loc['a'], mdata.sel(x='a')) - assert_identical(mdata.loc[('a', 1), ...], - mdata.sel(x=('a', 1))) + assert_identical(mdata.loc[('a', 1), ...], mdata.sel(x=('a', 1))) assert_identical(mdata.loc[{'one': 'a'}, ...], - mdata.sel(x={'one': 'a'})) + mdata.sel(x={'one': 'a'})) with pytest.raises(IndexError): mdata.loc[('a', 1)] assert_identical(mdata.sel(x={'one': 'a', 'two': 1}), - mdata.sel(one='a', two=1)) + mdata.sel(one='a', two=1)) def test_virtual_default_coords(self): array = DataArray(np.zeros((5,)), dims='x') @@ -2880,25 +2875,25 @@ def test_to_and_from_iris(self): assert actual.var_name == original.name self.assertItemsEqual([d.var_name for d in actual.dim_coords], original.dims) - assert actual.cell_methods == \ - (iris.coords.CellMethod(method='mean', - coords=('height',), - intervals=(), - comments=('A cell method',)),) + assert (actual.cell_methods == + iris.coords.CellMethod(method='mean', + coords=('height',), + intervals=(), + comments=('A cell method',)),) for coord, orginal_key in zip((actual.coords()), original.coords): original_coord = original.coords[orginal_key] assert coord.var_name == original_coord.name self.assertArrayEqual( coord.points, CFDatetimeCoder().encode(original_coord).values) - assert actual.coord_dims(coord) == \ - original.get_axis_num \ - (original.coords[coord.var_name].dims) - - assert actual.coord('distance2').attributes['foo'] == \ - original.coords['distance2'].attrs['foo'] - assert actual.coord('distance').units == \ - cf_units.Unit(original.coords['distance'].units) + assert (actual.coord_dims(coord) == + original.get_axis_num( + original.coords[coord.var_name].dims)) + + assert (actual.coord('distance2').attributes['foo'] == + original.coords['distance2'].attrs['foo']) + assert (actual.coord('distance').units == + cf_units.Unit(original.coords['distance'].units)) assert actual.attributes['baz'] == original.attrs['baz'] assert actual.standard_name == original.attrs['standard_name'] @@ -2952,25 +2947,25 @@ def test_to_and_from_iris_dask(self): assert actual.var_name == original.name self.assertItemsEqual([d.var_name for d in actual.dim_coords], original.dims) - assert actual.cell_methods == \ - (iris.coords.CellMethod(method='mean', - coords=('height',), - intervals=(), - comments=('A cell method',)),) + assert (actual.cell_methods == + iris.coords.CellMethod(method='mean', + coords=('height',), + intervals=(), + comments=('A cell method',)),) for coord, orginal_key in zip((actual.coords()), original.coords): original_coord = original.coords[orginal_key] assert coord.var_name == original_coord.name self.assertArrayEqual( coord.points, CFDatetimeCoder().encode(original_coord).values) - assert actual.coord_dims(coord) == \ - original.get_axis_num \ - (original.coords[coord.var_name].dims) - - assert actual.coord('distance2').attributes['foo'] == \ - original.coords['distance2'].attrs['foo'] - assert actual.coord('distance').units == \ - cf_units.Unit(original.coords['distance'].units) + assert (actual.coord_dims(coord) == + original.get_axis_num( + original.coords[coord.var_name].dims)) + + assert (actual.coord('distance2').attributes['foo'] == original.coords[ + 'distance2'].attrs['foo']) + assert (actual.coord('distance').units == + cf_units.Unit(original.coords['distance'].units)) assert actual.attributes['baz'] == original.attrs['baz'] assert actual.standard_name == original.attrs['standard_name'] diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 008b9487208..6c3f31b9f48 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -37,9 +37,11 @@ def create_test_data(seed=None): rs = np.random.RandomState(seed) - _vars = {'var1': ['dim1', 'dim2'], - 'var2': ['dim1', 'dim2'], - 'var3': ['dim3', 'dim1']} + _vars = { + 'var1': ['dim1', 'dim2'], + 'var2': ['dim1', 'dim2'], + 'var3': ['dim3', 'dim1'] + } _dims = {'dim1': 8, 'dim2': 9, 'dim3': 10} obj = Dataset() @@ -49,16 +51,18 @@ def create_test_data(seed=None): for v, dims in sorted(_vars.items()): data = rs.normal(size=tuple(_dims[d] for d in dims)) obj[v] = (dims, data, {'foo': 'variable'}) - obj.coords['numbers'] = ('dim3', np.array([0, 1, 2, 0, 0, 1, 1, 2, 2, 3], - dtype='int64')) + obj.coords['numbers'] = ('dim3', + np.array( + [0, 1, 2, 0, 0, 1, 1, 2, 2, 3], + dtype='int64')) obj.encoding = {'foo': 'bar'} assert all(obj.data.flags.writeable for obj in obj.variables.values()) return obj def create_test_multiindex(): - mindex = pd.MultiIndex.from_product([['a', 'b'], [1, 2]], - names=('level_1', 'level_2')) + mindex = pd.MultiIndex.from_product( + [['a', 'b'], [1, 2]], names=('level_1', 'level_2')) return Dataset({}, {'x': mindex}) @@ -80,8 +84,9 @@ def lazy_inaccessible(k, v): return v data = indexing.LazilyIndexedArray(InaccessibleArray(v.values)) return Variable(v.dims, data, v.attrs) - return dict((k, lazy_inaccessible(k, v)) for - k, v in iteritems(self._variables)) + + return dict((k, lazy_inaccessible(k, v)) + for k, v in iteritems(self._variables)) class TestDataset(TestCase): @@ -153,8 +158,7 @@ def test_repr_multiindex(self): # verify that long level names are not truncated mindex = pd.MultiIndex.from_product( - [['a', 'b'], [1, 2]], - names=('a_quite_long_level_name', 'level_2')) + [['a', 'b'], [1, 2]], names=('a_quite_long_level_name', 'level_2')) data = Dataset({}, {'x': mindex}) expected = dedent("""\ @@ -252,8 +256,9 @@ def test_constructor(self): def test_constructor_invalid_dims(self): # regression for GH1120 with pytest.raises(MergeError): - Dataset(data_vars=dict(v=('y', [1, 2, 3, 4])), - coords=dict(y=DataArray([.1, .2, .3, .4], dims='x'))) + Dataset( + data_vars=dict(v=('y', [1, 2, 3, 4])), + coords=dict(y=DataArray([.1, .2, .3, .4], dims='x'))) def test_constructor_1d(self): expected = Dataset({'x': (['x'], 5.0 + np.arange(5))}) @@ -273,9 +278,12 @@ class Arbitrary(object): pass d = pd.Timestamp('2000-01-01T12') - args = [True, None, 3.4, np.nan, 'hello', u'uni', b'raw', - np.datetime64('2000-01-01'), d, d.to_pydatetime(), - Arbitrary()] + args = [ + True, None, 3.4, np.nan, 'hello', u'uni', b'raw', + np.datetime64('2000-01-01'), d, + d.to_pydatetime(), + Arbitrary() + ] for arg in args: print(arg) expected = Dataset({'x': ([], arg)}) @@ -291,9 +299,12 @@ def test_constructor_auto_align(self): b = DataArray([3, 4], [('x', [1, 2])]) # verify align uses outer join - expected = Dataset({'a': ('x', [1, 2, np.nan]), - 'b': ('x', [np.nan, 3, 4])}, - {'x': [0, 1, 2]}) + expected = Dataset({ + 'a': ('x', [1, 2, np.nan]), + 'b': ('x', [np.nan, 3, 4]) + }, { + 'x': [0, 1, 2] + }) actual = Dataset({'a': a, 'b': b}) assert_identical(expected, actual) @@ -319,9 +330,8 @@ def test_constructor_auto_align(self): def test_constructor_pandas_sequence(self): ds = self.make_example_math_dataset() - pandas_objs = OrderedDict( - (var_name, ds[var_name].to_pandas()) for var_name in ['foo', 'bar'] - ) + pandas_objs = OrderedDict((var_name, ds[var_name].to_pandas()) + for var_name in ['foo', 'bar']) ds_based_on_pandas = Dataset(pandas_objs, ds.coords, attrs=ds.attrs) del ds_based_on_pandas['x'] assert_equal(ds, ds_based_on_pandas) @@ -348,8 +358,9 @@ def test_constructor_pandas_single(self): assert_array_equal(ds_based_on_pandas[dim], pandas_obj[dim]) def test_constructor_compat(self): - data = OrderedDict([('x', DataArray(0, coords={'y': 1})), - ('y', ('z', [1, 1, 1]))]) + data = OrderedDict([('x', DataArray(0, coords={ + 'y': 1 + })), ('y', ('z', [1, 1, 1]))]) with pytest.raises(MergeError): Dataset(data, compat='equals') expected = Dataset({'x': 0}, {'y': ('z', [1, 1, 1])}) @@ -358,21 +369,33 @@ def test_constructor_compat(self): actual = Dataset(data, compat='broadcast_equals') assert_identical(expected, actual) - data = OrderedDict([('y', ('z', [1, 1, 1])), - ('x', DataArray(0, coords={'y': 1}))]) + data = OrderedDict([('y', ('z', [1, 1, 1])), ('x', + DataArray( + 0, coords={ + 'y': 1 + }))]) actual = Dataset(data) assert_identical(expected, actual) - original = Dataset({'a': (('x', 'y'), np.ones((2, 3)))}, - {'c': (('x', 'y'), np.zeros((2, 3))), 'x': [0, 1]}) - expected = Dataset({'a': ('x', np.ones(2)), - 'b': ('y', np.ones(3))}, - {'c': (('x', 'y'), np.zeros((2, 3))), 'x': [0, 1]}) + original = Dataset({ + 'a': (('x', 'y'), np.ones((2, 3))) + }, { + 'c': (('x', 'y'), np.zeros((2, 3))), + 'x': [0, 1] + }) + expected = Dataset({ + 'a': ('x', np.ones(2)), + 'b': ('y', np.ones(3)) + }, { + 'c': (('x', 'y'), np.zeros((2, 3))), + 'x': [0, 1] + }) # use an OrderedDict to ensure test results are reproducible; otherwise # the order of appearance of x and y matters for the order of # dimensions in 'c' - actual = Dataset(OrderedDict([('a', original['a'][:, 0]), - ('b', original['a'][0].drop('x'))])) + actual = Dataset( + OrderedDict([('a', original['a'][:, 0]), + ('b', original['a'][0].drop('x'))])) assert_identical(expected, actual) data = {'x': DataArray(0, coords={'y': 3}), 'y': ('z', [1, 1, 1])} @@ -392,16 +415,15 @@ def test_constructor_with_coords(self): assert not ds.data_vars self.assertItemsEqual(ds.coords.keys(), ['a']) - mindex = pd.MultiIndex.from_product([['a', 'b'], [1, 2]], - names=('level_1', 'level_2')) + mindex = pd.MultiIndex.from_product( + [['a', 'b'], [1, 2]], names=('level_1', 'level_2')) with raises_regex(ValueError, 'conflicting MultiIndex'): Dataset({}, {'x': mindex, 'y': mindex}) Dataset({}, {'x': mindex, 'level_1': range(4)}) def test_properties(self): ds = create_test_data() - assert ds.dims == \ - {'dim1': 8, 'dim2': 9, 'dim3': 10, 'time': 20} + assert ds.dims == {'dim1': 8, 'dim2': 9, 'dim3': 10, 'time': 20} assert list(ds.dims) == sorted(ds.dims) assert ds.sizes == ds.dims @@ -420,7 +442,7 @@ def test_properties(self): with pytest.warns(FutureWarning): assert len(ds) == 7 with pytest.warns(FutureWarning): - assert bool(ds) == True + assert bool(ds) self.assertItemsEqual(ds.data_vars, ['var1', 'var2', 'var3']) self.assertItemsEqual(ds.data_vars.keys(), ['var1', 'var2', 'var3']) @@ -440,20 +462,33 @@ def test_properties(self): assert 'dim1' not in ds.coords assert len(ds.coords) == 4 - assert Dataset({'x': np.int64(1), - 'y': np.float32([1, 2])}).nbytes == 16 + assert Dataset({ + 'x': np.int64(1), + 'y': np.float32([1, 2]) + }).nbytes == 16 def test_get_index(self): - ds = Dataset({'foo': (('x', 'y'), np.zeros((2, 3)))}, - coords={'x': ['a', 'b']}) + ds = Dataset( + { + 'foo': (('x', 'y'), np.zeros((2, 3))) + }, coords={ + 'x': ['a', 'b'] + }) assert ds.get_index('x').equals(pd.Index(['a', 'b'])) assert ds.get_index('y').equals(pd.Index([0, 1, 2])) with pytest.raises(KeyError): ds.get_index('z') def test_attr_access(self): - ds = Dataset({'tmin': ('x', [42], {'units': 'Celcius'})}, - attrs={'title': 'My test data'}) + ds = Dataset( + { + 'tmin': ('x', [42], { + 'units': 'Celcius' + }) + }, + attrs={ + 'title': 'My test data' + }) assert_identical(ds.tmin, ds['tmin']) assert_identical(ds.tmin.x, ds.x) @@ -471,10 +506,16 @@ def test_attr_access(self): def test_variable(self): a = Dataset() d = np.random.random((10, 3)) - a['foo'] = (('time', 'x',), d) + a['foo'] = (( + 'time', + 'x', + ), d) assert 'foo' in a.variables assert 'foo' in a - a['bar'] = (('time', 'x',), d) + a['bar'] = (( + 'time', + 'x', + ), d) # order of creation is preserved assert list(a.variables) == ['foo', 'bar'] assert_array_equal(a['foo'].values, d) @@ -484,7 +525,7 @@ def test_variable(self): def test_modify_inplace(self): a = Dataset() - vec = np.random.random((10,)) + vec = np.random.random((10, )) attributes = {'foo': 'bar'} a['x'] = ('x', vec, attributes) assert 'x' in a.coords @@ -500,7 +541,10 @@ def test_modify_inplace(self): with pytest.raises(ValueError): # now it shouldn't, since there is a conflicting length a['x'] = ('x', vec[:4]) - arr = np.random.random((10, 1,)) + arr = np.random.random(( + 10, + 1, + )) scal = np.array(0) with pytest.raises(ValueError): a['y'] = ('y', arr) @@ -512,12 +556,12 @@ def test_coords_properties(self): # use an OrderedDict for coordinates to ensure order across python # versions # use int64 for repr consistency on windows - data = Dataset(OrderedDict([('x', ('x', np.array([-1, -2], 'int64'))), - ('y', ('y', np.array([0, 1, 2], 'int64'))), - ('foo', (['x', 'y'], - np.random.randn(2, 3)))]), - OrderedDict([('a', ('x', np.array([4, 5], 'int64'))), - ('b', np.int64(-10))])) + data = Dataset( + OrderedDict([('x', ('x', np.array([-1, -2], 'int64'))), + ('y', ('y', np.array([0, 1, 2], 'int64'))), + ('foo', (['x', 'y'], np.random.randn(2, 3)))]), + OrderedDict([('a', ('x', np.array([4, 5], 'int64'))), + ('b', np.int64(-10))])) assert 4 == len(data.coords) @@ -548,10 +592,14 @@ def test_coords_properties(self): assert {'x': 2, 'y': 3} == data.coords.dims def test_coords_modify(self): - data = Dataset({'x': ('x', [-1, -2]), - 'y': ('y', [0, 1, 2]), - 'foo': (['x', 'y'], np.random.randn(2, 3))}, - {'a': ('x', [4, 5]), 'b': -10}) + data = Dataset({ + 'x': ('x', [-1, -2]), + 'y': ('y', [0, 1, 2]), + 'foo': (['x', 'y'], np.random.randn(2, 3)) + }, { + 'a': ('x', [4, 5]), + 'b': -10 + }) actual = data.copy(deep=True) actual.coords['x'] = ('x', ['a', 'b']) @@ -594,15 +642,22 @@ def test_coords_setitem_multiindex(self): data.coords['level_1'] = range(4) def test_coords_set(self): - one_coord = Dataset({'x': ('x', [0]), - 'yy': ('x', [1]), - 'zzz': ('x', [2])}) - two_coords = Dataset({'zzz': ('x', [2])}, - {'x': ('x', [0]), - 'yy': ('x', [1])}) - all_coords = Dataset(coords={'x': ('x', [0]), - 'yy': ('x', [1]), - 'zzz': ('x', [2])}) + one_coord = Dataset({ + 'x': ('x', [0]), + 'yy': ('x', [1]), + 'zzz': ('x', [2]) + }) + two_coords = Dataset({ + 'zzz': ('x', [2]) + }, { + 'x': ('x', [0]), + 'yy': ('x', [1]) + }) + all_coords = Dataset(coords={ + 'x': ('x', [0]), + 'yy': ('x', [1]), + 'zzz': ('x', [2]) + }) actual = one_coord.set_coords('x') assert_identical(one_coord, actual) @@ -644,11 +699,15 @@ def test_coords_to_dataset(self): def test_coords_merge(self): orig_coords = Dataset(coords={'a': ('x', [1, 2]), 'x': [0, 1]}).coords - other_coords = Dataset(coords={'b': ('x', ['a', 'b']), - 'x': [0, 1]}).coords - expected = Dataset(coords={'a': ('x', [1, 2]), - 'b': ('x', ['a', 'b']), - 'x': [0, 1]}) + other_coords = Dataset(coords={ + 'b': ('x', ['a', 'b']), + 'x': [0, 1] + }).coords + expected = Dataset(coords={ + 'a': ('x', [1, 2]), + 'b': ('x', ['a', 'b']), + 'x': [0, 1] + }) actual = orig_coords.merge(other_coords) assert_identical(expected, actual) actual = other_coords.merge(orig_coords) @@ -700,7 +759,7 @@ def test_coords_merge_mismatched_shape(self): def test_data_vars_properties(self): ds = Dataset() - ds['foo'] = (('x',), [1.0]) + ds['foo'] = (('x', ), [1.0]) ds['bar'] = 2.0 assert set(ds.data_vars) == {'foo', 'bar'} @@ -769,7 +828,7 @@ def test_chunk(self): else: assert isinstance(v.data, da.Array) - expected_chunks = {'dim1': (8,), 'dim2': (9,), 'dim3': (10,)} + expected_chunks = {'dim1': (8, ), 'dim2': (9, ), 'dim3': (10, )} assert reblocked.chunks == expected_chunks reblocked = data.chunk({'time': 5, 'dim1': 5, 'dim2': 5, 'dim3': 5}) @@ -866,21 +925,24 @@ def test_isel_fancy(self): pdim1 = [1, 2, 3] pdim2 = [4, 5, 1] pdim3 = [1, 2, 3] - actual = data.isel(dim1=(('test_coord', ), pdim1), - dim2=(('test_coord', ), pdim2), - dim3=(('test_coord', ), pdim3)) + actual = data.isel( + dim1=(('test_coord', ), pdim1), + dim2=(('test_coord', ), pdim2), + dim3=(('test_coord', ), pdim3)) assert 'test_coord' in actual.dims assert actual.coords['test_coord'].shape == (len(pdim1), ) # Should work with DataArray - actual = data.isel(dim1=DataArray(pdim1, dims='test_coord'), - dim2=(('test_coord', ), pdim2), - dim3=(('test_coord', ), pdim3)) + actual = data.isel( + dim1=DataArray(pdim1, dims='test_coord'), + dim2=(('test_coord', ), pdim2), + dim3=(('test_coord', ), pdim3)) assert 'test_coord' in actual.dims assert actual.coords['test_coord'].shape == (len(pdim1), ) - expected = data.isel(dim1=(('test_coord', ), pdim1), - dim2=(('test_coord', ), pdim2), - dim3=(('test_coord', ), pdim3)) + expected = data.isel( + dim1=(('test_coord', ), pdim1), + dim2=(('test_coord', ), pdim2), + dim3=(('test_coord', ), pdim3)) assert_identical(actual, expected) # DataArray with coordinate @@ -895,11 +957,12 @@ def test_isel_fancy(self): assert 'time' in actual.coords assert 'dim2' in actual.coords assert 'dim3' in actual.coords - expected = data.isel(dim1=(('a', ), pdim1), - dim2=(('b', ), pdim2), - dim3=(('c', ), pdim3)) - expected = expected.assign_coords(a=idx1['a'], b=idx2['b'], - c=idx3['c']) + expected = data.isel( + dim1=(('a', ), pdim1), + dim2=(('b', ), pdim2), + dim3=(('c', ), pdim3)) + expected = expected.assign_coords( + a=idx1['a'], b=idx2['b'], c=idx3['c']) assert_identical(actual, expected) idx1 = DataArray(pdim1, dims=['a'], coords={'a': np.random.randn(3)}) @@ -911,29 +974,28 @@ def test_isel_fancy(self): assert 'time' in actual.coords assert 'dim2' in actual.coords assert 'dim3' in actual.coords - expected = data.isel(dim1=(('a', ), pdim1), - dim2=(('a', ), pdim2), - dim3=(('a', ), pdim3)) + expected = data.isel( + dim1=(('a', ), pdim1), + dim2=(('a', ), pdim2), + dim3=(('a', ), pdim3)) expected = expected.assign_coords(a=idx1['a']) assert_identical(actual, expected) - actual = data.isel(dim1=(('points', ), pdim1), - dim2=(('points', ), pdim2)) + actual = data.isel( + dim1=(('points', ), pdim1), dim2=(('points', ), pdim2)) assert 'points' in actual.dims assert 'dim3' in actual.dims assert 'dim3' not in actual.data_vars np.testing.assert_array_equal(data['dim2'][pdim2], actual['dim2']) # test that the order of the indexers doesn't matter - assert_identical(data.isel(dim1=(('points', ), pdim1), - dim2=(('points', ), pdim2)), - data.isel(dim2=(('points', ), pdim2), - dim1=(('points', ), pdim1))) + assert_identical( + data.isel(dim1=(('points', ), pdim1), dim2=(('points', ), pdim2)), + data.isel(dim2=(('points', ), pdim2), dim1=(('points', ), pdim1))) # make sure we're raising errors in the right places - with raises_regex(IndexError, - 'Dimensions of indexers mismatch'): - data.isel(dim1=(('points', ), [1, 2]), - dim2=(('points', ), [1, 2, 3])) + with raises_regex(IndexError, 'Dimensions of indexers mismatch'): + data.isel( + dim1=(('points', ), [1, 2]), dim2=(('points', ), [1, 2, 3])) with raises_regex(TypeError, 'cannot use a Dataset'): data.isel(dim1=Dataset({'points': [1, 2]})) @@ -948,18 +1010,21 @@ def test_isel_fancy(self): stations['dim1s'] = (('station', ), [1, 2, 3]) stations['dim2s'] = (('station', ), [4, 5, 1]) - actual = data.isel(dim1=stations['dim1s'], - dim2=stations['dim2s']) + actual = data.isel(dim1=stations['dim1s'], dim2=stations['dim2s']) assert 'station' in actual.coords assert 'station' in actual.dims - assert_identical(actual['station'].drop(['dim2']), - stations['station']) + assert_identical(actual['station'].drop(['dim2']), stations['station']) with raises_regex(ValueError, 'conflicting values for '): - data.isel(dim1=DataArray([0, 1, 2], dims='station', - coords={'station': [0, 1, 2]}), - dim2=DataArray([0, 1, 2], dims='station', - coords={'station': [0, 1, 3]})) + data.isel( + dim1=DataArray( + [0, 1, 2], dims='station', coords={ + 'station': [0, 1, 2] + }), + dim2=DataArray( + [0, 1, 2], dims='station', coords={ + 'station': [0, 1, 3] + })) # multi-dimensional selection stations = Dataset() @@ -975,15 +1040,14 @@ def test_isel_fancy(self): assert 'dim2' in actual.coords assert 'a' in actual['dim2'].dims - assert_identical(actual['a'].drop(['dim2']), - stations['a']) + assert_identical(actual['a'].drop(['dim2']), stations['a']) assert_identical(actual['b'], stations['b']) expected_var1 = data['var1'].variable[stations['dim1s'].variable, stations['dim2s'].variable] expected_var2 = data['var2'].variable[stations['dim1s'].variable, stations['dim2s'].variable] - expected_var3 = data['var3'].variable[slice(None), - stations['dim1s'].variable] + expected_var3 = data['var3'].variable[slice(None), stations['dim1s'] + .variable] assert_equal(actual['a'].drop('dim2'), stations['a']) assert_array_equal(actual['var1'], expected_var1) assert_array_equal(actual['var2'], expected_var2) @@ -993,15 +1057,23 @@ def test_isel_dataarray(self): """ Test for indexing by DataArray """ data = create_test_data() # indexing with DataArray with same-name coordinates. - indexing_da = DataArray(np.arange(1, 4), dims=['dim1'], - coords={'dim1': np.random.randn(3)}) + indexing_da = DataArray( + np.arange(1, 4), + dims=['dim1'], + coords={ + 'dim1': np.random.randn(3) + }) actual = data.isel(dim1=indexing_da) assert_identical(indexing_da['dim1'], actual['dim1']) assert_identical(data['dim2'], actual['dim2']) # Conflict in the dimension coordinate - indexing_da = DataArray(np.arange(1, 4), dims=['dim2'], - coords={'dim2': np.random.randn(3)}) + indexing_da = DataArray( + np.arange(1, 4), + dims=['dim2'], + coords={ + 'dim2': np.random.randn(3) + }) with raises_regex(IndexError, "dimension coordinate 'dim2'"): actual = data.isel(dim2=indexing_da) # Also the case for DataArray @@ -1011,44 +1083,60 @@ def test_isel_dataarray(self): data['dim2'].isel(dim2=indexing_da) # same name coordinate which does not conflict - indexing_da = DataArray(np.arange(1, 4), dims=['dim2'], - coords={'dim2': data['dim2'].values[1:4]}) + indexing_da = DataArray( + np.arange(1, 4), + dims=['dim2'], + coords={ + 'dim2': data['dim2'].values[1:4] + }) actual = data.isel(dim2=indexing_da) assert_identical(actual['dim2'], indexing_da['dim2']) # Silently drop conflicted (non-dimensional) coordinate of indexer - indexing_da = DataArray(np.arange(1, 4), dims=['dim2'], - coords={'dim2': data['dim2'].values[1:4], - 'numbers': ('dim2', np.arange(2, 5))}) + indexing_da = DataArray( + np.arange(1, 4), + dims=['dim2'], + coords={ + 'dim2': data['dim2'].values[1:4], + 'numbers': ('dim2', np.arange(2, 5)) + }) actual = data.isel(dim2=indexing_da) assert_identical(actual['numbers'], data['numbers']) # boolean data array with coordinate with the same name - indexing_da = DataArray(np.arange(1, 10), dims=['dim2'], - coords={'dim2': data['dim2'].values}) + indexing_da = DataArray( + np.arange(1, 10), + dims=['dim2'], + coords={ + 'dim2': data['dim2'].values + }) indexing_da = (indexing_da < 3) actual = data.isel(dim2=indexing_da) assert_identical(actual['dim2'], data['dim2'][:2]) # boolean data array with non-dimensioncoordinate - indexing_da = DataArray(np.arange(1, 10), dims=['dim2'], - coords={'dim2': data['dim2'].values, - 'non_dim': (('dim2', ), - np.random.randn(9)), - 'non_dim2': 0}) + indexing_da = DataArray( + np.arange(1, 10), + dims=['dim2'], + coords={ + 'dim2': data['dim2'].values, + 'non_dim': (('dim2', ), np.random.randn(9)), + 'non_dim2': 0 + }) indexing_da = (indexing_da < 3) actual = data.isel(dim2=indexing_da) - assert_identical( - actual['dim2'].drop('non_dim').drop('non_dim2'), data['dim2'][:2]) - assert_identical( - actual['non_dim'], indexing_da['non_dim'][:2]) - assert_identical( - actual['non_dim2'], indexing_da['non_dim2']) + assert_identical(actual['dim2'].drop('non_dim').drop('non_dim2'), + data['dim2'][:2]) + assert_identical(actual['non_dim'], indexing_da['non_dim'][:2]) + assert_identical(actual['non_dim2'], indexing_da['non_dim2']) # non-dimension coordinate will be also attached - indexing_da = DataArray(np.arange(1, 4), dims=['dim2'], - coords={'non_dim': (('dim2', ), - np.random.randn(3))}) + indexing_da = DataArray( + np.arange(1, 4), + dims=['dim2'], + coords={ + 'non_dim': (('dim2', ), np.random.randn(3)) + }) actual = data.isel(dim2=indexing_da) assert 'non_dim' in actual assert 'non_dim' in actual.coords @@ -1062,43 +1150,42 @@ def test_isel_dataarray(self): # indexer generated from coordinates indexing_ds = Dataset({}, coords={'dim2': [0, 1, 2]}) - with raises_regex( - IndexError, "dimension coordinate 'dim2'"): + with raises_regex(IndexError, "dimension coordinate 'dim2'"): actual = data.isel(dim2=indexing_ds['dim2']) def test_sel(self): data = create_test_data() - int_slicers = {'dim1': slice(None, None, 2), - 'dim2': slice(2), - 'dim3': slice(3)} - loc_slicers = {'dim1': slice(None, None, 2), - 'dim2': slice(0, 0.5), - 'dim3': slice('a', 'c')} - assert_equal(data.isel(**int_slicers), - data.sel(**loc_slicers)) + int_slicers = { + 'dim1': slice(None, None, 2), + 'dim2': slice(2), + 'dim3': slice(3) + } + loc_slicers = { + 'dim1': slice(None, None, 2), + 'dim2': slice(0, 0.5), + 'dim3': slice('a', 'c') + } + assert_equal(data.isel(**int_slicers), data.sel(**loc_slicers)) data['time'] = ('time', pd.date_range('2000-01-01', periods=20)) - assert_equal(data.isel(time=0), - data.sel(time='2000-01-01')) - assert_equal(data.isel(time=slice(10)), - data.sel(time=slice('2000-01-01', - '2000-01-10'))) + assert_equal(data.isel(time=0), data.sel(time='2000-01-01')) + assert_equal( + data.isel(time=slice(10)), + data.sel(time=slice('2000-01-01', '2000-01-10'))) assert_equal(data, data.sel(time=slice('1999', '2005'))) times = pd.date_range('2000-01-01', periods=3) - assert_equal(data.isel(time=slice(3)), - data.sel(time=times)) - assert_equal(data.isel(time=slice(3)), - data.sel(time=(data['time.dayofyear'] <= 3))) + assert_equal(data.isel(time=slice(3)), data.sel(time=times)) + assert_equal( + data.isel(time=slice(3)), + data.sel(time=(data['time.dayofyear'] <= 3))) td = pd.to_timedelta(np.arange(3), unit='days') data = Dataset({'x': ('td', np.arange(3)), 'td': td}) assert_equal(data, data.sel(td=td)) assert_equal(data, data.sel(td=slice('3 days'))) - assert_equal(data.isel(td=0), - data.sel(td=pd.Timedelta('0 days'))) - assert_equal(data.isel(td=0), - data.sel(td=pd.Timedelta('0h'))) - assert_equal(data.isel(td=slice(1, 3)), - data.sel(td=slice('1 days', '2 days'))) + assert_equal(data.isel(td=0), data.sel(td=pd.Timedelta('0 days'))) + assert_equal(data.isel(td=0), data.sel(td=pd.Timedelta('0h'))) + assert_equal( + data.isel(td=slice(1, 3)), data.sel(td=slice('1 days', '2 days'))) def test_sel_dataarray(self): data = create_test_data() @@ -1117,59 +1204,77 @@ def test_sel_dataarray(self): # Multi-dimensional ind = DataArray([[0.0], [0.5], [1.0]], dims=['new_dim', 'new_dim2']) actual = data.sel(dim2=ind) - expected = data.isel(dim2=Variable(('new_dim', 'new_dim2'), - [[0], [1], [2]])) + expected = data.isel( + dim2=Variable(('new_dim', 'new_dim2'), [[0], [1], [2]])) assert 'new_dim' in actual.dims assert 'new_dim2' in actual.dims assert_equal(actual, expected) # with coordinate - ind = DataArray([0.0, 0.5, 1.0], dims=['new_dim'], - coords={'new_dim': ['a', 'b', 'c']}) + ind = DataArray( + [0.0, 0.5, 1.0], + dims=['new_dim'], + coords={ + 'new_dim': ['a', 'b', 'c'] + }) actual = data.sel(dim2=ind) expected = data.isel(dim2=[0, 1, 2]).rename({'dim2': 'new_dim'}) assert 'new_dim' in actual.dims assert 'new_dim' in actual.coords - assert_equal(actual.drop('new_dim').drop('dim2'), - expected.drop('new_dim')) - assert_equal(actual['new_dim'].drop('dim2'), - ind['new_dim']) + assert_equal( + actual.drop('new_dim').drop('dim2'), expected.drop('new_dim')) + assert_equal(actual['new_dim'].drop('dim2'), ind['new_dim']) # with conflicted coordinate (silently ignored) - ind = DataArray([0.0, 0.5, 1.0], dims=['dim2'], - coords={'dim2': ['a', 'b', 'c']}) + ind = DataArray( + [0.0, 0.5, 1.0], dims=['dim2'], coords={ + 'dim2': ['a', 'b', 'c'] + }) actual = data.sel(dim2=ind) expected = data.isel(dim2=[0, 1, 2]) assert_equal(actual, expected) # with conflicted coordinate (silently ignored) - ind = DataArray([0.0, 0.5, 1.0], dims=['new_dim'], - coords={'new_dim': ['a', 'b', 'c'], - 'dim2': 3}) + ind = DataArray( + [0.0, 0.5, 1.0], + dims=['new_dim'], + coords={ + 'new_dim': ['a', 'b', 'c'], + 'dim2': 3 + }) actual = data.sel(dim2=ind) assert_equal(actual['new_dim'].drop('dim2'), - ind['new_dim'].drop('dim2')) + ind['new_dim'].drop('dim2')) expected = data.isel(dim2=[0, 1, 2]) expected['dim2'] = (('new_dim'), expected['dim2'].values) - assert_equal(actual['dim2'].drop('new_dim'), - expected['dim2']) + assert_equal(actual['dim2'].drop('new_dim'), expected['dim2']) assert actual['var1'].dims == ('dim1', 'new_dim') # with non-dimensional coordinate - ind = DataArray([0.0, 0.5, 1.0], dims=['dim2'], - coords={'dim2': ['a', 'b', 'c'], - 'numbers': ('dim2', [0, 1, 2]), - 'new_dim': ('dim2', [1.1, 1.2, 1.3])}) + ind = DataArray( + [0.0, 0.5, 1.0], + dims=['dim2'], + coords={ + 'dim2': ['a', 'b', 'c'], + 'numbers': ('dim2', [0, 1, 2]), + 'new_dim': ('dim2', [1.1, 1.2, 1.3]) + }) actual = data.sel(dim2=ind) expected = data.isel(dim2=[0, 1, 2]) assert_equal(actual.drop('new_dim'), expected) assert np.allclose(actual['new_dim'].values, ind['new_dim'].values) def test_sel_dataarray_mindex(self): - midx = pd.MultiIndex.from_product([list('abc'), [0, 1]], - names=('one', 'two')) - mds = xr.Dataset({'var': (('x', 'y'), np.random.rand(6, 3))}, - coords={'x': midx, 'y': range(3)}) + midx = pd.MultiIndex.from_product( + [list('abc'), [0, 1]], names=('one', 'two')) + mds = xr.Dataset( + { + 'var': (('x', 'y'), np.random.rand(6, 3)) + }, + coords={ + 'x': midx, + 'y': range(3) + }) actual_isel = mds.isel(x=xr.DataArray(np.arange(3), dims='x')) actual_sel = mds.sel(x=DataArray(mds.indexes['x'][:3], dims='x')) @@ -1184,10 +1289,15 @@ def test_sel_dataarray_mindex(self): assert_identical(actual_isel, actual_sel) # with coordinate - actual_isel = mds.isel(x=xr.DataArray(np.arange(3), dims='z', - coords={'z': [0, 1, 2]})) - actual_sel = mds.sel(x=xr.DataArray(mds.indexes['x'][:3], dims='z', - coords={'z': [0, 1, 2]})) + actual_isel = mds.isel( + x=xr.DataArray(np.arange(3), dims='z', coords={ + 'z': [0, 1, 2] + })) + actual_sel = mds.sel( + x=xr.DataArray( + mds.indexes['x'][:3], dims='z', coords={ + 'z': [0, 1, 2] + })) assert actual_isel['x'].dims == ('z', ) assert actual_sel['x'].dims == ('z', ) assert_identical(actual_isel, actual_sel) @@ -1199,8 +1309,10 @@ def test_sel_dataarray_mindex(self): with raises_regex(ValueError, 'Vectorized selection is ' 'not available along MultiIndex variable:' ' x'): - mds.sel(x=xr.DataArray([np.array(midx[:2]), np.array(midx[-2:])], - dims=['a', 'b'])) + mds.sel( + x=xr.DataArray( + [np.array(midx[:2]), + np.array(midx[-2:])], dims=['a', 'b'])) def test_sel_drop(self): data = Dataset({'foo': ('x', [1, 2, 3])}, {'x': [0, 1, 2]}) @@ -1233,8 +1345,8 @@ def test_isel_points(self): pdim1 = [1, 2, 3] pdim2 = [4, 5, 1] pdim3 = [1, 2, 3] - actual = data.isel_points(dim1=pdim1, dim2=pdim2, dim3=pdim3, - dim='test_coord') + actual = data.isel_points( + dim1=pdim1, dim2=pdim2, dim3=pdim3, dim='test_coord') assert 'test_coord' in actual.dims assert actual.coords['test_coord'].shape == (len(pdim1), ) @@ -1245,22 +1357,20 @@ def test_isel_points(self): np.testing.assert_array_equal(data['dim2'][pdim2], actual['dim2']) # test that the order of the indexers doesn't matter - assert_identical(data.isel_points(dim1=pdim1, dim2=pdim2), - data.isel_points(dim2=pdim2, dim1=pdim1)) + assert_identical( + data.isel_points(dim1=pdim1, dim2=pdim2), + data.isel_points(dim2=pdim2, dim1=pdim1)) # make sure we're raising errors in the right places - with raises_regex(ValueError, - 'All indexers must be the same length'): + with raises_regex(ValueError, 'All indexers must be the same length'): data.isel_points(dim1=[1, 2], dim2=[1, 2, 3]) - with raises_regex(ValueError, - 'dimension bad_key does not exist'): + with raises_regex(ValueError, 'dimension bad_key does not exist'): data.isel_points(bad_key=[1, 2]) with raises_regex(TypeError, 'Indexers must be integers'): data.isel_points(dim1=[1.5, 2.2]) with raises_regex(TypeError, 'Indexers must be integers'): data.isel_points(dim1=[1, 2, 3], dim2=slice(3)) - with raises_regex(ValueError, - 'Indexers must be 1 dimensional'): + with raises_regex(ValueError, 'Indexers must be 1 dimensional'): data.isel_points(dim1=1, dim2=2) with raises_regex(ValueError, 'Existing dimension names are not valid'): @@ -1277,32 +1387,34 @@ def test_isel_points(self): stations['dim1s'] = ('station', [1, 2, 3]) stations['dim2s'] = ('station', [4, 5, 1]) - actual = data.isel_points(dim1=stations['dim1s'], - dim2=stations['dim2s'], - dim=stations['station']) + actual = data.isel_points( + dim1=stations['dim1s'], + dim2=stations['dim2s'], + dim=stations['station']) assert 'station' in actual.coords assert 'station' in actual.dims - assert_identical(actual['station'].drop(['dim2']), - stations['station']) + assert_identical(actual['station'].drop(['dim2']), stations['station']) # make sure we get the default 'points' coordinate when passed a list - actual = data.isel_points(dim1=stations['dim1s'], - dim2=stations['dim2s'], - dim=['A', 'B', 'C']) + actual = data.isel_points( + dim1=stations['dim1s'], + dim2=stations['dim2s'], + dim=['A', 'B', 'C']) assert 'points' in actual.coords assert actual.coords['points'].values.tolist() == ['A', 'B', 'C'] # test index - actual = data.isel_points(dim1=stations['dim1s'].values, - dim2=stations['dim2s'].values, - dim=pd.Index(['A', 'B', 'C'], - name='letters')) + actual = data.isel_points( + dim1=stations['dim1s'].values, + dim2=stations['dim2s'].values, + dim=pd.Index(['A', 'B', 'C'], name='letters')) assert 'letters' in actual.coords # can pass a numpy array - data.isel_points(dim1=stations['dim1s'], - dim2=stations['dim2s'], - dim=np.array([4, 5, 6])) + data.isel_points( + dim1=stations['dim1s'], + dim2=stations['dim2s'], + dim=np.array([4, 5, 6])) def test_sel_points(self): data = create_test_data() @@ -1313,10 +1425,13 @@ def test_sel_points(self): pdim1 = [1, 2, 3] pdim2 = [4, 5, 1] pdim3 = [1, 2, 3] - expected = data.isel_points(dim1=pdim1, dim2=pdim2, dim3=pdim3, - dim='test_coord') - actual = data.sel_points(dim1=data.dim1[pdim1], dim2=data.dim2[pdim2], - dim3=data.dim3[pdim3], dim='test_coord') + expected = data.isel_points( + dim1=pdim1, dim2=pdim2, dim3=pdim3, dim='test_coord') + actual = data.sel_points( + dim1=data.dim1[pdim1], + dim2=data.dim2[pdim2], + dim3=data.dim3[pdim3], + dim='test_coord') assert_identical(expected, actual) data = Dataset({'foo': (('x', 'y'), np.arange(9).reshape(3, 3))}) @@ -1325,10 +1440,12 @@ def test_sel_points(self): assert_identical(expected, actual) data.coords.update({'x': [0, 1, 2], 'y': [0, 1, 2]}) - expected.coords.update({'x': ('points', [0, 1, 2]), - 'y': ('points', [0, 1, 2])}) - actual = data.sel_points(x=[0.1, 1.1, 2.5], y=[0, 1.2, 2.0], - method='pad') + expected.coords.update({ + 'x': ('points', [0, 1, 2]), + 'y': ('points', [0, 1, 2]) + }) + actual = data.sel_points( + x=[0.1, 1.1, 2.5], y=[0, 1.2, 2.0], method='pad') assert_identical(expected, actual) with pytest.raises(KeyError): @@ -1343,58 +1460,95 @@ def test_sel_fancy(self): pdim1 = [1, 2, 3] pdim2 = [4, 5, 1] pdim3 = [1, 2, 3] - expected = data.isel(dim1=Variable(('test_coord', ), pdim1), - dim2=Variable(('test_coord', ), pdim2), - dim3=Variable(('test_coord'), pdim3)) - actual = data.sel(dim1=Variable(('test_coord', ), data.dim1[pdim1]), - dim2=Variable(('test_coord', ), data.dim2[pdim2]), - dim3=Variable(('test_coord', ), data.dim3[pdim3])) + expected = data.isel( + dim1=Variable(('test_coord', ), pdim1), + dim2=Variable(('test_coord', ), pdim2), + dim3=Variable(('test_coord'), pdim3)) + actual = data.sel( + dim1=Variable(('test_coord', ), data.dim1[pdim1]), + dim2=Variable(('test_coord', ), data.dim2[pdim2]), + dim3=Variable(('test_coord', ), data.dim3[pdim3])) assert_identical(expected, actual) # DataArray Indexer - idx_t = DataArray(data['time'][[3, 2, 1]].values, dims=['a'], - coords={'a': ['a', 'b', 'c']}) - idx_2 = DataArray(data['dim2'][[3, 2, 1]].values, dims=['a'], - coords={'a': ['a', 'b', 'c']}) - idx_3 = DataArray(data['dim3'][[3, 2, 1]].values, dims=['a'], - coords={'a': ['a', 'b', 'c']}) + idx_t = DataArray( + data['time'][[3, 2, 1]].values, + dims=['a'], + coords={ + 'a': ['a', 'b', 'c'] + }) + idx_2 = DataArray( + data['dim2'][[3, 2, 1]].values, + dims=['a'], + coords={ + 'a': ['a', 'b', 'c'] + }) + idx_3 = DataArray( + data['dim3'][[3, 2, 1]].values, + dims=['a'], + coords={ + 'a': ['a', 'b', 'c'] + }) actual = data.sel(time=idx_t, dim2=idx_2, dim3=idx_3) - expected = data.isel(time=Variable(('a', ), [3, 2, 1]), - dim2=Variable(('a', ), [3, 2, 1]), - dim3=Variable(('a', ), [3, 2, 1])) + expected = data.isel( + time=Variable(('a', ), [3, 2, 1]), + dim2=Variable(('a', ), [3, 2, 1]), + dim3=Variable(('a', ), [3, 2, 1])) expected = expected.assign_coords(a=idx_t['a']) assert_identical(expected, actual) - idx_t = DataArray(data['time'][[3, 2, 1]].values, dims=['a'], - coords={'a': ['a', 'b', 'c']}) - idx_2 = DataArray(data['dim2'][[2, 1, 3]].values, dims=['b'], - coords={'b': [0, 1, 2]}) - idx_3 = DataArray(data['dim3'][[1, 2, 1]].values, dims=['c'], - coords={'c': [0.0, 1.1, 2.2]}) + idx_t = DataArray( + data['time'][[3, 2, 1]].values, + dims=['a'], + coords={ + 'a': ['a', 'b', 'c'] + }) + idx_2 = DataArray( + data['dim2'][[2, 1, 3]].values, + dims=['b'], + coords={ + 'b': [0, 1, 2] + }) + idx_3 = DataArray( + data['dim3'][[1, 2, 1]].values, + dims=['c'], + coords={ + 'c': [0.0, 1.1, 2.2] + }) actual = data.sel(time=idx_t, dim2=idx_2, dim3=idx_3) - expected = data.isel(time=Variable(('a', ), [3, 2, 1]), - dim2=Variable(('b', ), [2, 1, 3]), - dim3=Variable(('c', ), [1, 2, 1])) - expected = expected.assign_coords(a=idx_t['a'], b=idx_2['b'], - c=idx_3['c']) + expected = data.isel( + time=Variable(('a', ), [3, 2, 1]), + dim2=Variable(('b', ), [2, 1, 3]), + dim3=Variable(('c', ), [1, 2, 1])) + expected = expected.assign_coords( + a=idx_t['a'], b=idx_2['b'], c=idx_3['c']) assert_identical(expected, actual) # test from sel_points data = Dataset({'foo': (('x', 'y'), np.arange(9).reshape(3, 3))}) data.coords.update({'x': [0, 1, 2], 'y': [0, 1, 2]}) - expected = Dataset({'foo': ('points', [0, 4, 8])}, - coords={'x': Variable(('points', ), [0, 1, 2]), - 'y': Variable(('points', ), [0, 1, 2])}) - actual = data.sel(x=Variable(('points', ), [0, 1, 2]), - y=Variable(('points', ), [0, 1, 2])) + expected = Dataset( + { + 'foo': ('points', [0, 4, 8]) + }, + coords={ + 'x': Variable(('points', ), [0, 1, 2]), + 'y': Variable(('points', ), [0, 1, 2]) + }) + actual = data.sel( + x=Variable(('points', ), [0, 1, 2]), + y=Variable(('points', ), [0, 1, 2])) assert_identical(expected, actual) - expected.coords.update({'x': ('points', [0, 1, 2]), - 'y': ('points', [0, 1, 2])}) - actual = data.sel(x=Variable(('points', ), [0.1, 1.1, 2.5]), - y=Variable(('points', ), [0, 1.2, 2.0]), - method='pad') + expected.coords.update({ + 'x': ('points', [0, 1, 2]), + 'y': ('points', [0, 1, 2]) + }) + actual = data.sel( + x=Variable(('points', ), [0.1, 1.1, 2.5]), + y=Variable(('points', ), [0, 1.2, 2.0]), + method='pad') assert_identical(expected, actual) idx_x = DataArray([0, 1, 2], dims=['a'], coords={'a': ['a', 'b', 'c']}) @@ -1447,12 +1601,16 @@ def test_loc(self): data.loc[dict(dim3='a')] = 0 def test_selection_multiindex(self): - mindex = pd.MultiIndex.from_product([['a', 'b'], [1, 2], [-1, -2]], - names=('one', 'two', 'three')) - mdata = Dataset(data_vars={'var': ('x', range(8))}, - coords={'x': mindex}) - - def test_sel(lab_indexer, pos_indexer, replaced_idx=False, + mindex = pd.MultiIndex.from_product( + [['a', 'b'], [1, 2], [-1, -2]], names=('one', 'two', 'three')) + mdata = Dataset( + data_vars={'var': ('x', range(8))}, coords={ + 'x': mindex + }) + + def test_sel(lab_indexer, + pos_indexer, + replaced_idx=False, renamed_dim=None): ds = mdata.sel(x=lab_indexer) expected_ds = mdata.isel(x=pos_indexer) @@ -1463,33 +1621,47 @@ def test_sel(lab_indexer, pos_indexer, replaced_idx=False, assert ds['var'].dims[0] == renamed_dim ds = ds.rename({renamed_dim: 'x'}) assert_identical(ds['var'].variable, - expected_ds['var'].variable) + expected_ds['var'].variable) self.assertVariableNotEqual(ds['x'], expected_ds['x']) test_sel(('a', 1, -1), 0) test_sel(('b', 2, -2), -1) test_sel(('a', 1), [0, 1], replaced_idx=True, renamed_dim='three') - test_sel(('a',), range(4), replaced_idx=True) + test_sel(('a', ), range(4), replaced_idx=True) test_sel('a', range(4), replaced_idx=True) test_sel([('a', 1, -1), ('b', 2, -2)], [0, 7]) test_sel(slice('a', 'b'), range(8)) test_sel(slice(('a', 1), ('b', 1)), range(6)) test_sel({'one': 'a', 'two': 1, 'three': -1}, 0) - test_sel({'one': 'a', 'two': 1}, [0, 1], replaced_idx=True, - renamed_dim='three') + test_sel( + { + 'one': 'a', + 'two': 1 + }, [0, 1], + replaced_idx=True, + renamed_dim='three') test_sel({'one': 'a'}, range(4), replaced_idx=True) - assert_identical(mdata.loc[{'x': {'one': 'a'}}], - mdata.sel(x={'one': 'a'})) - assert_identical(mdata.loc[{'x': 'a'}], - mdata.sel(x='a')) - assert_identical(mdata.loc[{'x': ('a', 1)}], - mdata.sel(x=('a', 1))) - assert_identical(mdata.loc[{'x': ('a', 1, -1)}], - mdata.sel(x=('a', 1, -1))) + assert_identical( + mdata.loc[{ + 'x': { + 'one': 'a' + } + }], mdata.sel(x={ + 'one': 'a' + })) + assert_identical(mdata.loc[{'x': 'a'}], mdata.sel(x='a')) + assert_identical(mdata.loc[{'x': ('a', 1)}], mdata.sel(x=('a', 1))) + assert_identical( + mdata.loc[{ + 'x': ('a', 1, -1) + }], mdata.sel(x=('a', 1, -1))) - assert_identical(mdata.sel(x={'one': 'a', 'two': 1}), - mdata.sel(one='a', two=1)) + assert_identical( + mdata.sel(x={ + 'one': 'a', + 'two': 1 + }), mdata.sel(one='a', two=1)) def test_reindex_like(self): data = create_test_data() @@ -1525,8 +1697,8 @@ def test_reindex(self): actual = data.reindex(dim1=data['dim1'].to_index()) assert_identical(actual, expected) - with raises_regex( - ValueError, 'cannot reindex or align along dimension'): + with raises_regex(ValueError, + 'cannot reindex or align along dimension'): data.reindex(dim1=data['dim1'][:5]) expected = data.isel(dim2=slice(5)) @@ -1552,18 +1724,29 @@ def test_reindex(self): assert_identical(actual, expected) # regression test for #279 - expected = Dataset({'x': ('time', np.random.randn(5))}, - {'time': range(5)}) + expected = Dataset({ + 'x': ('time', np.random.randn(5)) + }, { + 'time': range(5) + }) time2 = DataArray(np.arange(5), dims="time2") with pytest.warns(FutureWarning): actual = expected.reindex(time=time2) assert_identical(actual, expected) # another regression test - ds = Dataset({'foo': (['x', 'y'], np.zeros((3, 4)))}, - {'x': range(3), 'y': range(4)}) - expected = Dataset({'foo': (['x', 'y'], np.zeros((3, 2)))}, - {'x': [0, 1, 3], 'y': [0, 1]}) + ds = Dataset({ + 'foo': (['x', 'y'], np.zeros((3, 4))) + }, { + 'x': range(3), + 'y': range(4) + }) + expected = Dataset({ + 'foo': (['x', 'y'], np.zeros((3, 2))) + }, { + 'x': [0, 1, 3], + 'y': [0, 1] + }) expected['foo'][-1] = np.nan actual = ds.reindex(x=[0, 1, 3], y=[0, 1]) assert_identical(expected, actual) @@ -1575,8 +1758,8 @@ def test_reindex_warning(self): # DataArray with different dimension raises Future warning ind = xr.DataArray([0.0, 1.0], dims=['new_dim'], name='ind') data.reindex(dim2=ind) - assert any(["Indexer has dimensions " in - str(w.message) for w in ws]) + assert any( + ["Indexer has dimensions " in str(w.message) for w in ws]) # Should not warn ind = xr.DataArray([0.0, 1.0], dims=['dim2'], name='ind') @@ -1630,8 +1813,8 @@ def test_align(self): self.assertArrayEqual(left2['dim3'], union) assert_equal(left2['dim3'].variable, right2['dim3'].variable) - assert_identical(left2.sel(dim3=intersection), - right2.sel(dim3=intersection)) + assert_identical( + left2.sel(dim3=intersection), right2.sel(dim3=intersection)) assert np.isnan(left2['var3'][-2:]).all() assert np.isnan(right2['var3'][:2]).all() @@ -1639,16 +1822,16 @@ def test_align(self): assert_equal(left2['dim3'].variable, right2['dim3'].variable) assert_equal(left2['dim3'].variable, left['dim3'].variable) - assert_identical(left2.sel(dim3=intersection), - right2.sel(dim3=intersection)) + assert_identical( + left2.sel(dim3=intersection), right2.sel(dim3=intersection)) assert np.isnan(right2['var3'][:2]).all() left2, right2 = align(left, right, join='right') assert_equal(left2['dim3'].variable, right2['dim3'].variable) assert_equal(left2['dim3'].variable, right['dim3'].variable) - assert_identical(left2.sel(dim3=intersection), - right2.sel(dim3=intersection)) + assert_identical( + left2.sel(dim3=intersection), right2.sel(dim3=intersection)) assert np.isnan(left2['var3'][-2:]).all() @@ -1669,20 +1852,48 @@ def test_align_exact(self): xr.align(left, right, join='exact') def test_align_exclude(self): - x = Dataset({'foo': DataArray([[1, 2], [3, 4]], dims=['x', 'y'], - coords={'x': [1, 2], 'y': [3, 4]})}) - y = Dataset({'bar': DataArray([[1, 2], [3, 4]], dims=['x', 'y'], - coords={'x': [1, 3], 'y': [5, 6]})}) + x = Dataset({ + 'foo': + DataArray( + [[1, 2], [3, 4]], + dims=['x', 'y'], + coords={ + 'x': [1, 2], + 'y': [3, 4] + }) + }) + y = Dataset({ + 'bar': + DataArray( + [[1, 2], [3, 4]], + dims=['x', 'y'], + coords={ + 'x': [1, 3], + 'y': [5, 6] + }) + }) x2, y2 = align(x, y, exclude=['y'], join='outer') - expected_x2 = Dataset( - {'foo': DataArray([[1, 2], [3, 4], [np.nan, np.nan]], - dims=['x', 'y'], - coords={'x': [1, 2, 3], 'y': [3, 4]})}) - expected_y2 = Dataset( - {'bar': DataArray([[1, 2], [np.nan, np.nan], [3, 4]], - dims=['x', 'y'], - coords={'x': [1, 2, 3], 'y': [5, 6]})}) + expected_x2 = Dataset({ + 'foo': + DataArray( + [[1, 2], [3, 4], [np.nan, np.nan]], + dims=['x', 'y'], + coords={ + 'x': [1, 2, 3], + 'y': [3, 4] + }) + }) + expected_y2 = Dataset({ + 'bar': + DataArray( + [[1, 2], [np.nan, np.nan], [3, 4]], + dims=['x', 'y'], + coords={ + 'x': [1, 2, 3], + 'y': [5, 6] + }) + }) assert_identical(expected_x2, x2) assert_identical(expected_y2, y2) @@ -1690,8 +1901,10 @@ def test_align_nocopy(self): x = Dataset({'foo': DataArray([1, 2, 3], coords=[('x', [1, 2, 3])])}) y = Dataset({'foo': DataArray([1, 2], coords=[('x', [1, 2])])}) expected_x2 = x - expected_y2 = Dataset({'foo': DataArray([1, 2, np.nan], - coords=[('x', [1, 2, 3])])}) + expected_y2 = Dataset({ + 'foo': + DataArray([1, 2, np.nan], coords=[('x', [1, 2, 3])]) + }) x2, y2 = align(x, y, copy=False, join='outer') assert_identical(expected_x2, x2) @@ -1705,15 +1918,20 @@ def test_align_nocopy(self): assert_identical(expected_y2, y2) def test_align_indexes(self): - x = Dataset({'foo': DataArray([1, 2, 3], dims='x', - coords=[('x', [1, 2, 3])])}) + x = Dataset({ + 'foo': + DataArray([1, 2, 3], dims='x', coords=[('x', [1, 2, 3])]) + }) x2, = align(x, indexes={'x': [2, 3, 1]}) - expected_x2 = Dataset({'foo': DataArray([2, 3, 1], dims='x', - coords={'x': [2, 3, 1]})}) + expected_x2 = Dataset({ + 'foo': + DataArray([2, 3, 1], dims='x', coords={ + 'x': [2, 3, 1] + }) + }) assert_identical(expected_x2, x2) - def test_align_non_unique(self): x = Dataset({'foo': ('x', [3, 4, 5]), 'x': [0, 0, 1]}) x1, x2 = align(x, x) @@ -1724,12 +1942,20 @@ def test_align_non_unique(self): align(x, y) def test_broadcast(self): - ds = Dataset({'foo': 0, 'bar': ('x', [1]), 'baz': ('y', [2, 3])}, - {'c': ('x', [4])}) - expected = Dataset({'foo': (('x', 'y'), [[0, 0]]), - 'bar': (('x', 'y'), [[1, 1]]), - 'baz': (('x', 'y'), [[2, 3]])}, - {'c': ('x', [4])}) + ds = Dataset({ + 'foo': 0, + 'bar': ('x', [1]), + 'baz': ('y', [2, 3]) + }, { + 'c': ('x', [4]) + }) + expected = Dataset({ + 'foo': (('x', 'y'), [[0, 0]]), + 'bar': (('x', 'y'), [[1, 1]]), + 'baz': (('x', 'y'), [[2, 3]]) + }, { + 'c': ('x', [4]) + }) actual, = broadcast(ds) assert_identical(expected, actual) @@ -1764,43 +1990,93 @@ def test_broadcast_nocopy(self): def test_broadcast_exclude(self): x = Dataset({ - 'foo': DataArray([[1, 2], [3, 4]], dims=['x', 'y'], - coords={'x': [1, 2], 'y': [3, 4]}), - 'bar': DataArray(5), + 'foo': + DataArray( + [[1, 2], [3, 4]], + dims=['x', 'y'], + coords={ + 'x': [1, 2], + 'y': [3, 4] + }), + 'bar': + DataArray(5), }) y = Dataset({ - 'foo': DataArray([[1, 2]], dims=['z', 'y'], - coords={'z': [1], 'y': [5, 6]}), + 'foo': + DataArray( + [[1, 2]], dims=['z', 'y'], coords={ + 'z': [1], + 'y': [5, 6] + }), }) x2, y2 = broadcast(x, y, exclude=['y']) expected_x2 = Dataset({ - 'foo': DataArray([[[1, 2]], [[3, 4]]], dims=['x', 'z', 'y'], - coords={'z': [1], 'x': [1, 2], 'y': [3, 4]}), - 'bar': DataArray([[5], [5]], dims=['x', 'z'], - coords={'x': [1, 2], 'z': [1]}), + 'foo': + DataArray( + [[[1, 2]], [[3, 4]]], + dims=['x', 'z', 'y'], + coords={ + 'z': [1], + 'x': [1, 2], + 'y': [3, 4] + }), + 'bar': + DataArray( + [[5], [5]], dims=['x', 'z'], coords={ + 'x': [1, 2], + 'z': [1] + }), }) expected_y2 = Dataset({ - 'foo': DataArray([[[1, 2]], [[1, 2]]], dims=['x', 'z', 'y'], - coords={'z': [1], 'x': [1, 2], 'y': [5, 6]}), + 'foo': + DataArray( + [[[1, 2]], [[1, 2]]], + dims=['x', 'z', 'y'], + coords={ + 'z': [1], + 'x': [1, 2], + 'y': [5, 6] + }), }) assert_identical(expected_x2, x2) assert_identical(expected_y2, y2) def test_broadcast_misaligned(self): - x = Dataset({'foo': DataArray([1, 2, 3], - coords=[('x', [-1, -2, -3])])}) - y = Dataset({'bar': DataArray([[1, 2], [3, 4]], dims=['y', 'x'], - coords={'y': [1, 2], 'x': [10, -3]})}) + x = Dataset({ + 'foo': DataArray([1, 2, 3], coords=[('x', [-1, -2, -3])]) + }) + y = Dataset({ + 'bar': + DataArray( + [[1, 2], [3, 4]], + dims=['y', 'x'], + coords={ + 'y': [1, 2], + 'x': [10, -3] + }) + }) x2, y2 = broadcast(x, y) - expected_x2 = Dataset( - {'foo': DataArray([[3, 3], [2, 2], [1, 1], [np.nan, np.nan]], - dims=['x', 'y'], - coords={'y': [1, 2], 'x': [-3, -2, -1, 10]})}) - expected_y2 = Dataset( - {'bar': DataArray( + expected_x2 = Dataset({ + 'foo': + DataArray( + [[3, 3], [2, 2], [1, 1], [np.nan, np.nan]], + dims=['x', 'y'], + coords={ + 'y': [1, 2], + 'x': [-3, -2, -1, 10] + }) + }) + expected_y2 = Dataset({ + 'bar': + DataArray( [[2, 4], [np.nan, np.nan], [np.nan, np.nan], [1, 3]], - dims=['x', 'y'], coords={'y': [1, 2], 'x': [-3, -2, -1, 10]})}) + dims=['x', 'y'], + coords={ + 'y': [1, 2], + 'x': [-3, -2, -1, 10] + }) + }) assert_identical(expected_x2, x2) assert_identical(expected_y2, y2) @@ -1822,8 +2098,8 @@ def test_drop_variables(self): assert_identical(data, data.drop([])) - expected = Dataset(dict((k, data[k]) for k in data.variables - if k != 'time')) + expected = Dataset( + dict((k, data[k]) for k in data.variables if k != 'time')) actual = data.drop('time') assert_identical(expected, actual) actual = data.drop(['time']) @@ -1833,8 +2109,10 @@ def test_drop_variables(self): data.drop('not_found_here') def test_drop_index_labels(self): - data = Dataset({'A': (['x', 'y'], np.random.randn(2, 3)), - 'x': ['a', 'b']}) + data = Dataset({ + 'A': (['x', 'y'], np.random.randn(2, 3)), + 'x': ['a', 'b'] + }) actual = data.drop(['a'], 'x') expected = data.isel(x=[1]) @@ -1848,8 +2126,7 @@ def test_drop_index_labels(self): # not contained in axis data.drop(['c'], dim='x') - with raises_regex( - ValueError, 'does not have coordinate labels'): + with raises_regex(ValueError, 'does not have coordinate labels'): data.drop(1, 'y') def test_copy(self): @@ -1889,10 +2166,11 @@ def test_rename(self): if name in dims: dims[dims.index(name)] = newname - self.assertVariableEqual(Variable(dims, v.values, v.attrs), - renamed[k].variable.to_base_variable()) + self.assertVariableEqual( + Variable(dims, v.values, v.attrs), + renamed[k].variable.to_base_variable()) assert v.encoding == renamed[k].encoding - assert type(v) == type(renamed.variables[k]) + assert type(v) == type(renamed.variables[k]) # noqa: E721 assert 'var1' not in renamed assert 'dim2' not in renamed @@ -1940,8 +2218,12 @@ def test_rename_inplace(self): def test_swap_dims(self): original = Dataset({'x': [1, 2, 3], 'y': ('x', list('abc')), 'z': 42}) - expected = Dataset({'z': 42}, - {'x': ('y', [1, 2, 3]), 'y': list('abc')}) + expected = Dataset({ + 'z': 42 + }, { + 'x': ('y', [1, 2, 3]), + 'y': list('abc') + }) actual = original.swap_dims({'x': 'y'}) assert_identical(expected, actual) assert isinstance(actual.variables['y'], IndexVariable) @@ -1960,13 +2242,20 @@ def test_swap_dims(self): original.swap_dims({'x': 'z'}) def test_expand_dims_error(self): - original = Dataset({'x': ('a', np.random.randn(3)), - 'y': (['b', 'a'], np.random.randn(4, 3)), - 'z': ('a', np.random.randn(3))}, - coords={'a': np.linspace(0, 1, 3), - 'b': np.linspace(0, 1, 4), - 'c': np.linspace(0, 1, 5)}, - attrs={'key': 'entry'}) + original = Dataset( + { + 'x': ('a', np.random.randn(3)), + 'y': (['b', 'a'], np.random.randn(4, 3)), + 'z': ('a', np.random.randn(3)) + }, + coords={ + 'a': np.linspace(0, 1, 3), + 'b': np.linspace(0, 1, 4), + 'c': np.linspace(0, 1, 5) + }, + attrs={ + 'key': 'entry' + }) with raises_regex(ValueError, 'already exists'): original.expand_dims(dim=['x']) @@ -1978,20 +2267,34 @@ def test_expand_dims_error(self): original.expand_dims(dim=['z']) def test_expand_dims(self): - original = Dataset({'x': ('a', np.random.randn(3)), - 'y': (['b', 'a'], np.random.randn(4, 3))}, - coords={'a': np.linspace(0, 1, 3), - 'b': np.linspace(0, 1, 4), - 'c': np.linspace(0, 1, 5)}, - attrs={'key': 'entry'}) + original = Dataset( + { + 'x': ('a', np.random.randn(3)), + 'y': (['b', 'a'], np.random.randn(4, 3)) + }, + coords={ + 'a': np.linspace(0, 1, 3), + 'b': np.linspace(0, 1, 4), + 'c': np.linspace(0, 1, 5) + }, + attrs={ + 'key': 'entry' + }) actual = original.expand_dims(['z'], [1]) - expected = Dataset({'x': original['x'].expand_dims('z', 1), - 'y': original['y'].expand_dims('z', 1)}, - coords={'a': np.linspace(0, 1, 3), - 'b': np.linspace(0, 1, 4), - 'c': np.linspace(0, 1, 5)}, - attrs={'key': 'entry'}) + expected = Dataset( + { + 'x': original['x'].expand_dims('z', 1), + 'y': original['y'].expand_dims('z', 1) + }, + coords={ + 'a': np.linspace(0, 1, 3), + 'b': np.linspace(0, 1, 4), + 'c': np.linspace(0, 1, 5) + }, + attrs={ + 'key': 'entry' + }) assert_identical(expected, actual) # make sure squeeze restores the original data set. roundtripped = actual.squeeze('z') @@ -1999,12 +2302,19 @@ def test_expand_dims(self): # another test with a negative axis actual = original.expand_dims(['z'], [-1]) - expected = Dataset({'x': original['x'].expand_dims('z', -1), - 'y': original['y'].expand_dims('z', -1)}, - coords={'a': np.linspace(0, 1, 3), - 'b': np.linspace(0, 1, 4), - 'c': np.linspace(0, 1, 5)}, - attrs={'key': 'entry'}) + expected = Dataset( + { + 'x': original['x'].expand_dims('z', -1), + 'y': original['y'].expand_dims('z', -1) + }, + coords={ + 'a': np.linspace(0, 1, 3), + 'b': np.linspace(0, 1, 4), + 'c': np.linspace(0, 1, 5) + }, + attrs={ + 'key': 'entry' + }) assert_identical(expected, actual) # make sure squeeze restores the original data set. roundtripped = actual.squeeze('z') @@ -2059,33 +2369,41 @@ def test_reorder_levels(self): ds.reorder_levels(x=['level_1', 'level_2']) def test_stack(self): - ds = Dataset({'a': ('x', [0, 1]), - 'b': (('x', 'y'), [[0, 1], [2, 3]]), - 'y': ['a', 'b']}) - - exp_index = pd.MultiIndex.from_product([[0, 1], ['a', 'b']], - names=['x', 'y']) - expected = Dataset({'a': ('z', [0, 0, 1, 1]), - 'b': ('z', [0, 1, 2, 3]), - 'z': exp_index}) + ds = Dataset({ + 'a': ('x', [0, 1]), + 'b': (('x', 'y'), [[0, 1], [2, 3]]), + 'y': ['a', 'b'] + }) + + exp_index = pd.MultiIndex.from_product( + [[0, 1], ['a', 'b']], names=['x', 'y']) + expected = Dataset({ + 'a': ('z', [0, 0, 1, 1]), + 'b': ('z', [0, 1, 2, 3]), + 'z': exp_index + }) actual = ds.stack(z=['x', 'y']) assert_identical(expected, actual) - exp_index = pd.MultiIndex.from_product([['a', 'b'], [0, 1]], - names=['y', 'x']) - expected = Dataset({'a': ('z', [0, 1, 0, 1]), - 'b': ('z', [0, 2, 1, 3]), - 'z': exp_index}) + exp_index = pd.MultiIndex.from_product( + [['a', 'b'], [0, 1]], names=['y', 'x']) + expected = Dataset({ + 'a': ('z', [0, 1, 0, 1]), + 'b': ('z', [0, 2, 1, 3]), + 'z': exp_index + }) actual = ds.stack(z=['y', 'x']) assert_identical(expected, actual) def test_unstack(self): - index = pd.MultiIndex.from_product([[0, 1], ['a', 'b']], - names=['x', 'y']) + index = pd.MultiIndex.from_product( + [[0, 1], ['a', 'b']], names=['x', 'y']) ds = Dataset({'b': ('z', [0, 1, 2, 3]), 'z': index}) - expected = Dataset({'b': (('x', 'y'), [[0, 1], [2, 3]]), - 'x': [0, 1], - 'y': ['a', 'b']}) + expected = Dataset({ + 'b': (('x', 'y'), [[0, 1], [2, 3]]), + 'x': [0, 1], + 'y': ['a', 'b'] + }) actual = ds.unstack('z') assert_identical(actual, expected) @@ -2097,10 +2415,12 @@ def test_unstack_errors(self): ds.unstack('x') def test_stack_unstack(self): - ds = Dataset({'a': ('x', [0, 1]), - 'b': (('x', 'y'), [[0, 1], [2, 3]]), - 'x': [0, 1], - 'y': ['a', 'b']}) + ds = Dataset({ + 'a': ('x', [0, 1]), + 'b': (('x', 'y'), [[0, 1], [2, 3]]), + 'x': [0, 1], + 'y': ['a', 'b'] + }) actual = ds.stack(z=['x', 'y']).unstack('z') assert actual.broadcast_equals(ds) @@ -2133,8 +2453,12 @@ def test_update(self): def test_update_auto_align(self): ds = Dataset({'x': ('t', [3, 4])}, {'t': [0, 1]}) - expected = Dataset({'x': ('t', [3, 4]), 'y': ('t', [np.nan, 5])}, - {'t': [0, 1]}) + expected = Dataset({ + 'x': ('t', [3, 4]), + 'y': ('t', [np.nan, 5]) + }, { + 't': [0, 1] + }) actual = ds.copy() other = {'y': ('t', [5]), 't': [1]} with raises_regex(ValueError, 'conflicting sizes'): @@ -2145,8 +2469,12 @@ def test_update_auto_align(self): actual = ds.copy() other = Dataset({'y': ('t', [5]), 't': [100]}) actual.update(other) - expected = Dataset({'x': ('t', [3, 4]), 'y': ('t', [np.nan] * 2)}, - {'t': [0, 1]}) + expected = Dataset({ + 'x': ('t', [3, 4]), + 'y': ('t', [np.nan] * 2) + }, { + 't': [0, 1] + }) assert_identical(expected, actual) def test_getitem(self): @@ -2163,10 +2491,12 @@ def test_getitem(self): assert_equal(expected, actual) actual = data['numbers'] - expected = DataArray(data['numbers'].variable, - {'dim3': data['dim3'], - 'numbers': data['numbers']}, - dims='dim3', name='numbers') + expected = DataArray( + data['numbers'].variable, + {'dim3': data['dim3'], + 'numbers': data['numbers']}, + dims='dim3', + name='numbers') assert_identical(expected, actual) actual = data[dict(dim1=0)] @@ -2196,16 +2526,19 @@ def test_virtual_variables_default_coords(self): def test_virtual_variables_time(self): # access virtual variables data = create_test_data() - expected = DataArray(1 + np.arange(20), coords=[data['time']], - dims='time', name='dayofyear') + expected = DataArray( + 1 + np.arange(20), + coords=[data['time']], + dims='time', + name='dayofyear') assert_array_equal(data['time.month'].values, - data.variables['time'].to_index().month) + data.variables['time'].to_index().month) assert_array_equal(data['time.season'].values, 'DJF') # test virtual variable math assert_array_equal(data['time.dayofyear'] + 1, 2 + np.arange(20)) - assert_array_equal(np.sin(data['time.dayofyear']), - np.sin(1 + np.arange(20))) + assert_array_equal( + np.sin(data['time.dayofyear']), np.sin(1 + np.arange(20))) # ensure they become coordinates expected = Dataset({}, {'dayofyear': data['time.dayofyear']}) actual = data[['time.dayofyear']] @@ -2225,17 +2558,24 @@ def test_virtual_variable_same_name(self): def test_virtual_variable_multiindex(self): # access multi-index levels as virtual variables data = create_test_multiindex() - expected = DataArray(['a', 'a', 'b', 'b'], name='level_1', - coords=[data['x'].to_index()], dims='x') + expected = DataArray( + ['a', 'a', 'b', 'b'], + name='level_1', + coords=[data['x'].to_index()], + dims='x') assert_identical(expected, data['level_1']) # combine multi-index level and datetime dr_index = pd.date_range('1/1/2011', periods=4, freq='H') - mindex = pd.MultiIndex.from_arrays([['a', 'a', 'b', 'b'], dr_index], - names=('level_str', 'level_date')) + mindex = pd.MultiIndex.from_arrays( + [['a', 'a', 'b', 'b'], dr_index], + names=('level_str', 'level_date')) data = Dataset({}, {'x': mindex}) - expected = DataArray(mindex.get_level_values('level_date').hour, - name='hour', coords=[mindex], dims='x') + expected = DataArray( + mindex.get_level_values('level_date').hour, + name='hour', + coords=[mindex], + dims='x') assert_identical(expected, data['level_date.hour']) # attribute style access @@ -2250,7 +2590,8 @@ def test_slice_virtual_variable(self): data = create_test_data() self.assertVariableEqual(data['time.dayofyear'][:10].variable, Variable(['time'], 1 + np.arange(10))) - self.assertVariableEqual(data['time.dayofyear'][0].variable, Variable([], 1)) + self.assertVariableEqual(data['time.dayofyear'][0].variable, + Variable([], 1)) def test_setitem(self): # assign a variable @@ -2266,8 +2607,7 @@ def test_setitem(self): data2['B'] = dv assert_identical(data1, data2) # can't assign an ND array without dimensions - with raises_regex(ValueError, - 'without explicit dimension names'): + with raises_regex(ValueError, 'without explicit dimension names'): data2['C'] = var.values.reshape(2, 4) # but can assign a 1D array data1['C'] = var.values @@ -2325,9 +2665,12 @@ def test_setitem_auto_align(self): def test_setitem_align_new_indexes(self): ds = Dataset({'foo': ('x', [1, 2, 3])}, {'x': [0, 1, 2]}) ds['bar'] = DataArray([2, 3, 4], [('x', [1, 2, 3])]) - expected = Dataset({'foo': ('x', [1, 2, 3]), - 'bar': ('x', [np.nan, 2, 3])}, - {'x': [0, 1, 2]}) + expected = Dataset({ + 'foo': ('x', [1, 2, 3]), + 'bar': ('x', [np.nan, 2, 3]) + }, { + 'x': [0, 1, 2] + }) assert_identical(ds, expected) def test_assign(self): @@ -2379,8 +2722,12 @@ def test_assign_multiindex_level(self): def test_setitem_original_non_unique_index(self): # regression test for GH943 - original = Dataset({'data': ('x', np.arange(5))}, - coords={'x': [0, 1, 2, 0, 1]}) + original = Dataset( + { + 'data': ('x', np.arange(5)) + }, coords={ + 'x': [0, 1, 2, 0, 1] + }) expected = Dataset({'data': ('x', np.arange(5))}, {'x': range(5)}) actual = original.copy() @@ -2399,8 +2746,10 @@ def test_setitem_both_non_unique_index(self): # regression test for GH956 names = ['joaquin', 'manolo', 'joaquin'] values = np.random.randint(0, 256, (3, 4, 4)) - array = DataArray(values, dims=['name', 'row', 'column'], - coords=[names, range(4), range(4)]) + array = DataArray( + values, + dims=['name', 'row', 'column'], + coords=[names, range(4), range(4)]) expected = Dataset({'first': array, 'second': array}) actual = array.rename('first').to_dataset() actual['second'] = array @@ -2425,10 +2774,13 @@ def test_delitem(self): 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.dims)] if args else [] - expected = Dataset(dict((k, v.squeeze(*get_args(v))) - for k, v in iteritems(data.variables))) + + expected = Dataset( + dict((k, v.squeeze(*get_args(v))) + for k, v in iteritems(data.variables))) expected.set_coords(data.coords, inplace=True) assert_identical(expected, data.squeeze(*args)) # invalid squeeze @@ -2454,21 +2806,23 @@ def test_squeeze_drop(self): selected = data.squeeze(dim='y', drop=True) assert_identical(expected, selected) - data = Dataset({'foo': (('x',), [])}, {'x': []}) + data = Dataset({'foo': (('x', ), [])}, {'x': []}) selected = data.squeeze(drop=True) assert_identical(data, selected) def test_groupby(self): - data = Dataset({'z': (['x', 'y'], np.random.randn(3, 5))}, - {'x': ('x', list('abc')), - 'c': ('x', [0, 1, 0]), - 'y': range(5)}) + data = Dataset({ + 'z': (['x', 'y'], np.random.randn(3, 5)) + }, { + 'x': ('x', list('abc')), + 'c': ('x', [0, 1, 0]), + 'y': range(5) + }) groupby = data.groupby('x') assert len(groupby) == 3 expected_groups = {'a': 0, 'b': 1, 'c': 2} assert groupby.groups == expected_groups - expected_items = [('a', data.isel(x=0)), - ('b', data.isel(x=1)), + expected_items = [('a', data.isel(x=0)), ('b', data.isel(x=1)), ('c', data.isel(x=2))] for actual, expected in zip(groupby, expected_items): assert actual[0] == expected[0] @@ -2510,10 +2864,12 @@ def test_groupby_errors(self): data.groupby(data.coords['dim1'].to_index()) def test_groupby_reduce(self): - data = Dataset({'xy': (['x', 'y'], np.random.randn(3, 4)), - 'xonly': ('x', np.random.randn(3)), - 'yonly': ('y', np.random.randn(4)), - 'letters': ('y', ['a', 'a', 'b', 'b'])}) + data = Dataset({ + 'xy': (['x', 'y'], np.random.randn(3, 4)), + 'xonly': ('x', np.random.randn(3)), + 'yonly': ('y', np.random.randn(4)), + 'letters': ('y', ['a', 'a', 'b', 'b']) + }) expected = data.mean('y') expected['yonly'] = expected['yonly'].variable.set_dims({'x': 3}) @@ -2524,10 +2880,15 @@ def test_groupby_reduce(self): self.assertDatasetAllClose(expected, actual) letters = data['letters'] - expected = Dataset({'xy': data['xy'].groupby(letters).mean(), - 'xonly': (data['xonly'].mean().variable - .set_dims({'letters': 2})), - 'yonly': data['yonly'].groupby(letters).mean()}) + expected = Dataset({ + 'xy': + data['xy'].groupby(letters).mean(), + 'xonly': (data['xonly'].mean().variable.set_dims({ + 'letters': 2 + })), + 'yonly': + data['yonly'].groupby(letters).mean() + }) actual = data.groupby('letters').mean() self.assertDatasetAllClose(expected, actual) @@ -2557,8 +2918,8 @@ def reorder_dims(x): grouped = ds.groupby('numbers') zeros = DataArray([0, 0, 0, 0], [('numbers', range(4))]) - expected = ((ds + Variable('dim3', np.zeros(10))) - .transpose('dim3', 'dim1', 'dim2', 'time')) + expected = ((ds + Variable('dim3', np.zeros(10))).transpose( + 'dim3', 'dim1', 'dim2', 'time')) actual = grouped + zeros assert_equal(expected, actual) @@ -2576,24 +2937,31 @@ def reorder_dims(x): with raises_regex(TypeError, 'in-place operations'): ds += grouped - ds = Dataset({'x': ('time', np.arange(100)), - 'time': pd.date_range('2000-01-01', periods=100)}) + ds = Dataset({ + 'x': ('time', np.arange(100)), + 'time': pd.date_range('2000-01-01', periods=100) + }) with raises_regex(ValueError, 'incompat.* grouped binary'): ds + ds.groupby('time.month') def test_groupby_math_virtual(self): - ds = Dataset({'x': ('t', [1, 2, 3])}, - {'t': pd.date_range('20100101', periods=3)}) + ds = Dataset({ + 'x': ('t', [1, 2, 3]) + }, { + 't': pd.date_range('20100101', periods=3) + }) grouped = ds.groupby('t.day') actual = grouped - grouped.mean() - expected = Dataset({'x': ('t', [0, 0, 0])}, - ds[['t', 't.day']]) + expected = Dataset({'x': ('t', [0, 0, 0])}, ds[['t', 't.day']]) assert_identical(actual, expected) def test_groupby_nan(self): # nan should be excluded from groupby - ds = Dataset({'foo': ('x', [1, 2, 3, 4])}, - {'bar': ('x', [1, 1, 2, np.nan])}) + ds = Dataset({ + 'foo': ('x', [1, 2, 3, 4]) + }, { + 'bar': ('x', [1, 1, 2, np.nan]) + }) actual = ds.groupby('bar').mean() expected = Dataset({'foo': ('bar', [1.5, 3]), 'bar': [1, 2]}) assert_identical(actual, expected) @@ -2615,9 +2983,13 @@ def test_groupby_order(self): def test_resample_and_first(self): times = pd.date_range('2000-01-01', freq='6H', periods=10) - ds = Dataset({'foo': (['time', 'x', 'y'], np.random.randn(10, 5, 3)), - 'bar': ('time', np.random.randn(10), {'meta': 'data'}), - 'time': times}) + ds = Dataset({ + 'foo': (['time', 'x', 'y'], np.random.randn(10, 5, 3)), + 'bar': ('time', np.random.randn(10), { + 'meta': 'data' + }), + 'time': times + }) actual = ds.resample(time='1D').first(keep_attrs=True) expected = ds.isel(time=[0, 4, 8]) @@ -2627,19 +2999,30 @@ def test_resample_and_first(self): expected_time = pd.date_range('2000-01-01', freq='3H', periods=19) expected = ds.reindex(time=expected_time) actual = ds.resample(time='3H') - for how in ['mean', 'sum', 'first', 'last', ]: + for how in [ + 'mean', + 'sum', + 'first', + 'last', + ]: method = getattr(actual, how) result = method() assert_equal(expected, result) - for method in [np.mean, ]: + for method in [ + np.mean, + ]: result = actual.reduce(method) assert_equal(expected, result) def test_resample_by_mean_with_keep_attrs(self): times = pd.date_range('2000-01-01', freq='6H', periods=10) - ds = Dataset({'foo': (['time', 'x', 'y'], np.random.randn(10, 5, 3)), - 'bar': ('time', np.random.randn(10), {'meta': 'data'}), - 'time': times}) + ds = Dataset({ + 'foo': (['time', 'x', 'y'], np.random.randn(10, 5, 3)), + 'bar': ('time', np.random.randn(10), { + 'meta': 'data' + }), + 'time': times + }) ds.attrs['dsmeta'] = 'dsdata' resampled_ds = ds.resample(time='1D').mean(keep_attrs=True) @@ -2653,9 +3036,13 @@ def test_resample_by_mean_with_keep_attrs(self): def test_resample_by_mean_discarding_attrs(self): times = pd.date_range('2000-01-01', freq='6H', periods=10) - ds = Dataset({'foo': (['time', 'x', 'y'], np.random.randn(10, 5, 3)), - 'bar': ('time', np.random.randn(10), {'meta': 'data'}), - 'time': times}) + ds = Dataset({ + 'foo': (['time', 'x', 'y'], np.random.randn(10, 5, 3)), + 'bar': ('time', np.random.randn(10), { + 'meta': 'data' + }), + 'time': times + }) ds.attrs['dsmeta'] = 'dsdata' resampled_ds = ds.resample(time='1D').mean(keep_attrs=False) @@ -2665,9 +3052,13 @@ def test_resample_by_mean_discarding_attrs(self): def test_resample_by_last_discarding_attrs(self): times = pd.date_range('2000-01-01', freq='6H', periods=10) - ds = Dataset({'foo': (['time', 'x', 'y'], np.random.randn(10, 5, 3)), - 'bar': ('time', np.random.randn(10), {'meta': 'data'}), - 'time': times}) + ds = Dataset({ + 'foo': (['time', 'x', 'y'], np.random.randn(10, 5, 3)), + 'bar': ('time', np.random.randn(10), { + 'meta': 'data' + }), + 'time': times + }) ds.attrs['dsmeta'] = 'dsdata' resampled_ds = ds.resample(time='1D').last(keep_attrs=False) @@ -2683,14 +3074,13 @@ def test_resample_drop_nondim_coords(self): data = np.tile(np.arange(5), (6, 3, 1)) xx, yy = np.meshgrid(xs * 5, ys * 2.5) tt = np.arange(len(times), dtype=int) - array = DataArray(data, - {'time': times, 'x': xs, 'y': ys}, - ('x', 'y', 'time')) + array = DataArray(data, {'time': times, + 'x': xs, + 'y': ys}, ('x', 'y', 'time')) xcoord = DataArray(xx.T, {'x': xs, 'y': ys}, ('x', 'y')) ycoord = DataArray(yy.T, {'x': xs, 'y': ys}, ('x', 'y')) tcoord = DataArray(tt, {'time': times}, ('time', )) - ds = Dataset({'data': array, 'xc': xcoord, - 'yc': ycoord, 'tc': tcoord}) + ds = Dataset({'data': array, 'xc': xcoord, 'yc': ycoord, 'tc': tcoord}) ds = ds.set_coords(['xc', 'yc', 'tc']) # Re-sample @@ -2708,9 +3098,13 @@ def test_resample_drop_nondim_coords(self): def test_resample_old_vs_new_api(self): times = pd.date_range('2000-01-01', freq='6H', periods=10) - ds = Dataset({'foo': (['time', 'x', 'y'], np.random.randn(10, 5, 3)), - 'bar': ('time', np.random.randn(10), {'meta': 'data'}), - 'time': times}) + ds = Dataset({ + 'foo': (['time', 'x', 'y'], np.random.randn(10, 5, 3)), + 'bar': ('time', np.random.randn(10), { + 'meta': 'data' + }), + 'time': times + }) ds.attrs['dsmeta'] = 'dsdata' for method in ['mean', 'sum', 'count', 'first', 'last']: @@ -2723,8 +3117,12 @@ def test_resample_old_vs_new_api(self): assert_identical(new_api, old_api) def test_to_array(self): - ds = Dataset(OrderedDict([('a', 1), ('b', ('x', [1, 2, 3]))]), - coords={'c': 42}, attrs={'Conventions': 'None'}) + ds = Dataset( + OrderedDict([('a', 1), ('b', ('x', [1, 2, 3]))]), + coords={'c': 42}, + attrs={ + 'Conventions': 'None' + }) data = [[1, 1, 1], [1, 2, 3]] coords = {'c': 42, 'variable': ['a', 'b']} dims = ('variable', 'x') @@ -2740,11 +3138,12 @@ 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')) + 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) @@ -2768,8 +3167,8 @@ def test_to_and_from_dataframe(self): assert expected.equals(actual) # check roundtrip - assert_identical(ds.assign_coords(x=[0, 1]), - Dataset.from_dataframe(actual)) + assert_identical( + ds.assign_coords(x=[0, 1]), Dataset.from_dataframe(actual)) # check pathological cases df = pd.DataFrame([1]) @@ -2785,23 +3184,30 @@ def test_to_and_from_dataframe(self): # GH697 df = pd.DataFrame({'A': []}) actual = Dataset.from_dataframe(df) - expected = Dataset({'A': DataArray([], dims=('index',))}, - {'index': []}) + expected = Dataset({ + 'A': DataArray([], dims=('index', )) + }, { + 'index': [] + }) assert_identical(expected, actual) # regression test for GH278 # use int64 to ensure consistent results for the pandas .equals method # on windows (which requires the same dtype) - ds = Dataset({'x': pd.Index(['bar']), - 'a': ('y', np.array([1], 'int64'))}).isel(x=0) + ds = Dataset({ + 'x': pd.Index(['bar']), + 'a': ('y', np.array([1], 'int64')) + }).isel(x=0) # use .loc to ensure consistent results on Python 3 actual = ds.to_dataframe().loc[:, ['a', 'x']] - expected = pd.DataFrame([[1, 'bar']], index=pd.Index([0], name='y'), - columns=['a', 'x']) + expected = pd.DataFrame( + [[1, 'bar']], index=pd.Index([0], name='y'), columns=['a', 'x']) assert expected.equals(actual), (expected, actual) - ds = Dataset({'x': np.array([0], 'int64'), - 'y': np.array([1], 'int64')}) + ds = Dataset({ + 'x': np.array([0], 'int64'), + 'y': np.array([1], 'int64') + }) actual = ds.to_dataframe() idx = pd.MultiIndex.from_arrays([[0], [1]], names=['x', 'y']) expected = pd.DataFrame([[]], index=idx) @@ -2816,18 +3222,25 @@ def test_from_dataframe_non_unique_columns(self): def test_convert_dataframe_with_many_types_and_multiindex(self): # regression test for GH737 - df = pd.DataFrame({'a': list('abc'), - 'b': list(range(1, 4)), - 'c': np.arange(3, 6).astype('u1'), - 'd': np.arange(4.0, 7.0, dtype='float64'), - 'e': [True, False, True], - 'f': pd.Categorical(list('abc')), - 'g': pd.date_range('20130101', periods=3), - 'h': pd.date_range('20130101', - periods=3, - tz='US/Eastern')}) - df.index = pd.MultiIndex.from_product([['a'], range(3)], - names=['one', 'two']) + df = pd.DataFrame({ + 'a': + list('abc'), + 'b': + list(range(1, 4)), + 'c': + np.arange(3, 6).astype('u1'), + 'd': + np.arange(4.0, 7.0, dtype='float64'), + 'e': [True, False, True], + 'f': + pd.Categorical(list('abc')), + 'g': + pd.date_range('20130101', periods=3), + 'h': + pd.date_range('20130101', periods=3, tz='US/Eastern') + }) + df.index = pd.MultiIndex.from_product( + [['a'], range(3)], names=['one', 'two']) roundtripped = Dataset.from_dataframe(df).to_dataframe() # we can't do perfectly, but we should be at least as faithful as # np.asarray @@ -2845,20 +3258,33 @@ def test_to_and_from_dict(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 = {'coords': {'t': {'dims': ('t',), - 'data': t, - 'attrs': {}}}, - 'attrs': {}, - 'dims': {'t': 10}, - 'data_vars': {'a': {'dims': ('t',), - 'data': x.tolist(), - 'attrs': {}}, - 'b': {'dims': ('t',), - 'data': y.tolist(), - 'attrs': {}}}} + ds = Dataset( + OrderedDict([('a', ('t', x)), ('b', ('t', y)), ('t', ('t', t))])) + expected = { + 'coords': { + 't': { + 'dims': ('t', ), + 'data': t, + 'attrs': {} + } + }, + 'attrs': {}, + 'dims': { + 't': 10 + }, + 'data_vars': { + 'a': { + 'dims': ('t', ), + 'data': x.tolist(), + 'attrs': {} + }, + 'b': { + 'dims': ('t', ), + 'data': y.tolist(), + 'attrs': {} + } + } + } actual = ds.to_dict() @@ -2878,22 +3304,58 @@ def test_to_and_from_dict(self): # this one has no attrs field, the dims are strings, and x, y are # np.arrays - d = {'coords': {'t': {'dims': 't', 'data': t}}, - 'dims': 't', - 'data_vars': {'a': {'dims': 't', 'data': x}, - 'b': {'dims': 't', 'data': y}}} + d = { + 'coords': { + 't': { + 'dims': 't', + 'data': t + } + }, + 'dims': 't', + 'data_vars': { + 'a': { + 'dims': 't', + 'data': x + }, + 'b': { + 'dims': 't', + 'data': y + } + } + } assert_identical(ds, Dataset.from_dict(d)) # this is kind of a flattened version with no coords, or data_vars - d = {'a': {'dims': 't', 'data': x}, - 't': {'data': t, 'dims': 't'}, - 'b': {'dims': 't', 'data': y}} + d = { + 'a': { + 'dims': 't', + 'data': x + }, + 't': { + 'data': t, + 'dims': 't' + }, + 'b': { + 'dims': 't', + 'data': y + } + } assert_identical(ds, Dataset.from_dict(d)) # this one is missing some necessary information - d = {'a': {'data': x}, - 't': {'data': t, 'dims': 't'}, - 'b': {'dims': 't', 'data': y}} + d = { + 'a': { + 'data': x + }, + 't': { + 'data': t, + 'dims': 't' + }, + 'b': { + 'dims': 't', + 'data': y + } + } with raises_regex(ValueError, "cannot convert dict " "without the key 'dims'"): Dataset.from_dict(d) @@ -2903,10 +3365,9 @@ def test_to_and_from_dict_with_time_dim(self): y = np.random.randn(10, 3) t = pd.date_range('20130101', periods=10) lat = [77.7, 83.2, 76] - ds = Dataset(OrderedDict([('a', (['t', 'lat'], x)), - ('b', (['t', 'lat'], y)), - ('t', ('t', t)), - ('lat', ('lat', lat))])) + ds = Dataset( + OrderedDict([('a', (['t', 'lat'], x)), ('b', (['t', 'lat'], y)), + ('t', ('t', t)), ('lat', ('lat', lat))])) roundtripped = Dataset.from_dict(ds.to_dict()) assert_identical(ds, roundtripped) @@ -2918,10 +3379,9 @@ def test_to_and_from_dict_with_nan_nat(self): t[2] = np.nan lat = [77.7, 83.2, 76] - ds = Dataset(OrderedDict([('a', (['t', 'lat'], x)), - ('b', (['t', 'lat'], y)), - ('t', ('t', t)), - ('lat', ('lat', lat))])) + ds = Dataset( + OrderedDict([('a', (['t', 'lat'], x)), ('b', (['t', 'lat'], y)), + ('t', ('t', t)), ('lat', ('lat', lat))])) roundtripped = Dataset.from_dict(ds.to_dict()) assert_identical(ds, roundtripped) @@ -2930,15 +3390,19 @@ def test_to_dict_with_numpy_attrs(self): x = np.random.randn(10) y = np.random.randn(10) t = list('abcdefghij') - attrs = {'created': np.float64(1998), - 'coords': np.array([37, -110.1, 100]), - 'maintainer': 'bar'} - ds = Dataset(OrderedDict([('a', ('t', x, attrs)), - ('b', ('t', y, attrs)), - ('t', ('t', t))])) - expected_attrs = {'created': np.asscalar(attrs['created']), - 'coords': attrs['coords'].tolist(), - 'maintainer': 'bar'} + attrs = { + 'created': np.float64(1998), + 'coords': np.array([37, -110.1, 100]), + 'maintainer': 'bar' + } + ds = Dataset( + OrderedDict([('a', ('t', x, attrs)), ('b', ('t', y, attrs)), + ('t', ('t', t))])) + expected_attrs = { + 'created': np.asscalar(attrs['created']), + 'coords': attrs['coords'].tolist(), + 'maintainer': 'bar' + } actual = ds.to_dict() # check that they are identical @@ -3027,8 +3491,7 @@ def test_dropna(self): ds.dropna('a', how=None) def test_fillna(self): - ds = Dataset({'a': ('x', [np.nan, 1, np.nan, 3])}, - {'x': [0, 1, 2, 3]}) + ds = Dataset({'a': ('x', [np.nan, 1, np.nan, 3])}, {'x': [0, 1, 2, 3]}) # fill with -1 actual = ds.fillna(-1) @@ -3063,8 +3526,12 @@ def test_fillna(self): # okay to only include some data variables ds['b'] = np.nan actual = ds.fillna({'a': -1}) - expected = Dataset({'a': ('x', [-1, 1, -1, 3]), 'b': np.nan}, - {'x': [0, 1, 2, 3]}) + expected = Dataset({ + 'a': ('x', [-1, 1, -1, 3]), + 'b': np.nan + }, { + 'x': [0, 1, 2, 3] + }) assert_identical(expected, actual) # but new data variables is not okay @@ -3202,17 +3669,21 @@ def test_where_drop(self): ds.where(np.arange(5) > 1, drop=True) # 1d with odd coordinates - array = DataArray(np.array([2, 7, 1, 8, 3]), - coords=[np.array([3, 1, 4, 5, 9])], dims=['x']) - expected = DataArray(np.array([7, 8, 3]), coords=[np.array([1, 5, 9])], - dims=['x']) + array = DataArray( + np.array([2, 7, 1, 8, 3]), + coords=[np.array([3, 1, 4, 5, 9])], + dims=['x']) + expected = DataArray( + np.array([7, 8, 3]), coords=[np.array([1, 5, 9])], dims=['x']) actual = array.where(array > 2, drop=True) assert_identical(expected, actual) # 1d multiple variables ds = Dataset({'a': (('x'), [0, 1, 2, 3]), 'b': (('x'), [4, 5, 6, 7])}) - expected = Dataset({'a': (('x'), [np.nan, 1, 2, 3]), - 'b': (('x'), [4, 5, 6, np.nan])}) + expected = Dataset({ + 'a': (('x'), [np.nan, 1, 2, 3]), + 'b': (('x'), [4, 5, 6, np.nan]) + }) actual = ds.where((ds > 0) & (ds < 7), drop=True) assert_identical(expected, actual) @@ -3223,28 +3694,44 @@ def test_where_drop(self): assert_identical(expected, actual) # 2d with odd coordinates - ds = Dataset({'a': (('x', 'y'), [[0, 1], [2, 3]])}, coords={ - 'x': [4, 3], 'y': [1, 2], - 'z': (['x', 'y'], [[np.e, np.pi], [np.pi * np.e, np.pi * 3]])}) - expected = Dataset({'a': (('x', 'y'), [[3]])}, - coords={'x': [3], 'y': [2], - 'z': (['x', 'y'], [[np.pi * 3]])}) + ds = Dataset( + { + 'a': (('x', 'y'), [[0, 1], [2, 3]]) + }, + coords={ + 'x': [4, 3], + 'y': [1, 2], + 'z': (['x', 'y'], [[np.e, np.pi], [np.pi * np.e, np.pi * 3]]) + }) + expected = Dataset( + { + 'a': (('x', 'y'), [[3]]) + }, + coords={ + 'x': [3], + 'y': [2], + 'z': (['x', 'y'], [[np.pi * 3]]) + }) actual = ds.where(ds > 2, drop=True) assert_identical(expected, actual) # 2d multiple variables - ds = Dataset({'a': (('x', 'y'), [[0, 1], [2, 3]]), - 'b': (('x', 'y'), [[4, 5], [6, 7]])}) - expected = Dataset({'a': (('x', 'y'), [[np.nan, 1], [2, 3]]), - 'b': (('x', 'y'), [[4, 5], [6, 7]])}) + ds = Dataset({ + 'a': (('x', 'y'), [[0, 1], [2, 3]]), + 'b': (('x', 'y'), [[4, 5], [6, 7]]) + }) + expected = Dataset({ + 'a': (('x', 'y'), [[np.nan, 1], [2, 3]]), + 'b': (('x', 'y'), [[4, 5], [6, 7]]) + }) actual = ds.where(ds > 0, drop=True) assert_identical(expected, actual) def test_where_drop_empty(self): # regression test for GH1341 - array = DataArray(np.random.rand(100, 10), - dims=['nCells', 'nVertLevels']) - mask = DataArray(np.zeros((100,), dtype='bool'), dims='nCells') + array = DataArray( + np.random.rand(100, 10), dims=['nCells', 'nVertLevels']) + mask = DataArray(np.zeros((100, ), dtype='bool'), dims='nCells') actual = array.where(mask, drop=True) expected = DataArray(np.zeros((0, 10)), dims=['nCells', 'nVertLevels']) assert_identical(expected, actual) @@ -3261,16 +3748,15 @@ def test_reduce(self): assert len(data.mean().coords) == 0 actual = data.max() - expected = Dataset(dict((k, v.max()) - for k, v in iteritems(data.data_vars))) + expected = Dataset( + dict((k, v.max()) for k, v in iteritems(data.data_vars))) assert_equal(expected, actual) - assert_equal(data.min(dim=['dim1']), - data.min(dim='dim1')) + assert_equal(data.min(dim=['dim1']), data.min(dim='dim1')) - for reduct, expected in [('dim2', ['dim1', 'dim3', 'time']), - (['dim2', 'time'], ['dim1', 'dim3']), - (('dim2', 'time'), ['dim1', 'dim3']), + for reduct, expected in [('dim2', ['dim1', 'dim3', 'time']), ([ + 'dim2', 'time' + ], ['dim1', 'dim3']), (('dim2', 'time'), ['dim1', 'dim3']), ((), ['dim1', 'dim2', 'dim3', 'time'])]: actual = data.min(dim=reduct).dims print(reduct, actual, expected) @@ -3279,12 +3765,15 @@ def test_reduce(self): assert_equal(data.mean(dim=[]), data) # uint support - data = xr.Dataset({'a': (('x', 'y'), - np.arange(6).reshape(3, 2).astype('uint')), - 'b': (('x', ), np.array([0.1, 0.2, np.nan]))}) + data = xr.Dataset({ + 'a': (('x', 'y'), np.arange(6).reshape(3, 2).astype('uint')), + 'b': (('x', ), np.array([0.1, 0.2, np.nan])) + }) actual = data.mean('x', skipna=True) - expected = xr.Dataset({'a': data['a'].mean('x'), - 'b': data['b'].mean('x', skipna=True)}) + expected = xr.Dataset({ + 'a': data['a'].mean('x'), + 'b': data['b'].mean('x', skipna=True) + }) assert_identical(actual, expected) def test_reduce_bad_dim(self): @@ -3305,12 +3794,11 @@ def test_reduce_cumsum_test_dims(self): getattr(data, cumfunc)(dim='bad_dim') # ensure dimensions are correct - for reduct, expected in [ - ('dim1', ['dim1', 'dim2', 'dim3', 'time']), - ('dim2', ['dim1', 'dim2', 'dim3', 'time']), - ('dim3', ['dim1', 'dim2', 'dim3', 'time']), - ('time', ['dim1', 'dim2', 'dim3']) - ]: + for reduct, expected in [('dim1', [ + 'dim1', 'dim2', 'dim3', 'time' + ]), ('dim2', ['dim1', 'dim2', 'dim3', + 'time']), ('dim3', ['dim1', 'dim2', 'dim3', 'time']), + ('time', ['dim1', 'dim2', 'dim3'])]: actual = getattr(data, cumfunc)(dim=reduct).dims print(reduct, actual, expected) self.assertItemsEqual(actual, expected) @@ -3326,8 +3814,7 @@ def test_reduce_non_numeric(self): assert 'var4' not in data1.mean() assert_equal(data1.mean(), data2.mean()) - assert_equal(data1.mean(dim='dim1'), - data2.mean(dim='dim1')) + assert_equal(data1.mean(dim='dim1'), data2.mean(dim='dim1')) def test_reduce_strings(self): expected = Dataset({'x': 'a'}) @@ -3408,7 +3895,6 @@ def test_reduce_scalars(self): assert_identical(expected, actual) def test_reduce_only_one_axis(self): - def mean_only_one_axis(x, axis): if not isinstance(axis, integer_types): raise TypeError('non-integer axis') @@ -3435,8 +3921,8 @@ def test_quantile(self): assert 'quantile' in ds_quantile for var, dar in ds.data_vars.items(): assert var in ds_quantile - assert_identical( - ds_quantile[var], dar.quantile(q, dim=dim)) + assert_identical(ds_quantile[var], dar.quantile( + q, dim=dim)) dim = ['dim1', 'dim2'] ds_quantile = ds.quantile(q, dim=dim) assert 'dim3' in ds_quantile.dims @@ -3475,8 +3961,8 @@ def test_apply(self): actual = data.apply(lambda x: x.mean(keep_attrs=True), keep_attrs=True) assert_identical(expected, actual) - assert_identical(data.apply(lambda x: x, keep_attrs=True), - data.drop('time')) + assert_identical( + data.apply(lambda x: x, keep_attrs=True), data.drop('time')) def scale(x, multiple=1): return multiple * x @@ -3490,11 +3976,10 @@ def scale(x, multiple=1): assert_equal(expected, actual) def make_example_math_dataset(self): - variables = OrderedDict( - [('bar', ('x', np.arange(100, 400, 100))), - ('foo', (('x', 'y'), 1.0 * np.arange(12).reshape(3, 4)))]) - coords = {'abc': ('x', ['a', 'b', 'c']), - 'y': 10 * np.arange(4)} + variables = OrderedDict([('bar', ('x', np.arange(100, 400, 100))), + ('foo', (('x', 'y'), + 1.0 * np.arange(12).reshape(3, 4)))]) + coords = {'abc': ('x', ['a', 'b', 'c']), 'y': 10 * np.arange(4)} ds = Dataset(variables, coords) ds['foo'][0, 0] = np.nan return ds @@ -3518,9 +4003,10 @@ def test_unary_ops(self): assert_identical(ds.apply(abs), abs(ds)) assert_identical(ds.apply(lambda x: x + 4), ds + 4) - for func in [lambda x: x.isnull(), - lambda x: x.round(), - lambda x: x.astype(int)]: + for func in [ + lambda x: x.isnull(), lambda x: x.round(), + lambda x: x.astype(int) + ]: assert_identical(ds.apply(func), func(ds)) assert_identical(ds.isnull(), ~ds.notnull()) @@ -3628,8 +4114,10 @@ def test_dataset_math_errors(self): assert_identical(actual, ds) def test_dataset_transpose(self): - ds = Dataset({'a': (('x', 'y'), np.random.randn(3, 4)), - 'b': (('y', 'x'), np.random.randn(4, 3))}) + ds = Dataset({ + 'a': (('x', 'y'), np.random.randn(3, 4)), + 'b': (('y', 'x'), np.random.randn(4, 3)) + }) actual = ds.transpose() expected = ds.apply(lambda x: x.transpose()) @@ -3690,12 +4178,12 @@ def test_dataset_diff_n1(self): ds = create_test_data(seed=1) actual = ds.diff('dim2') expected = dict() - expected['var1'] = DataArray(np.diff(ds['var1'].values, axis=1), - {'dim2': ds['dim2'].values[1:]}, - ['dim1', 'dim2']) - expected['var2'] = DataArray(np.diff(ds['var2'].values, axis=1), - {'dim2': ds['dim2'].values[1:]}, - ['dim1', 'dim2']) + expected['var1'] = DataArray( + np.diff(ds['var1'].values, axis=1), + {'dim2': ds['dim2'].values[1:]}, ['dim1', 'dim2']) + expected['var2'] = DataArray( + np.diff(ds['var2'].values, axis=1), + {'dim2': ds['dim2'].values[1:]}, ['dim1', 'dim2']) expected['var3'] = ds['var3'] expected = Dataset(expected, coords={'time': ds['time'].values}) expected.coords['numbers'] = ('dim3', ds['numbers'].values) @@ -3705,12 +4193,12 @@ def test_dataset_diff_n2(self): ds = create_test_data(seed=1) actual = ds.diff('dim2', n=2) expected = dict() - expected['var1'] = DataArray(np.diff(ds['var1'].values, axis=1, n=2), - {'dim2': ds['dim2'].values[2:]}, - ['dim1', 'dim2']) - expected['var2'] = DataArray(np.diff(ds['var2'].values, axis=1, n=2), - {'dim2': ds['dim2'].values[2:]}, - ['dim1', 'dim2']) + expected['var1'] = DataArray( + np.diff(ds['var1'].values, axis=1, n=2), + {'dim2': ds['dim2'].values[2:]}, ['dim1', 'dim2']) + expected['var2'] = DataArray( + np.diff(ds['var2'].values, axis=1, n=2), + {'dim2': ds['dim2'].values[2:]}, ['dim1', 'dim2']) expected['var3'] = ds['var3'] expected = Dataset(expected, coords={'time': ds['time'].values}) expected.coords['numbers'] = ('dim3', ds['numbers'].values) @@ -3773,10 +4261,15 @@ def test_filter_by_attrs(self): precip = dict(standard_name='convective_precipitation_flux') temp0 = dict(standard_name='air_potential_temperature', height='0 m') temp10 = dict(standard_name='air_potential_temperature', height='10 m') - ds = Dataset({'temperature_0': (['t'], [0], temp0), - 'temperature_10': (['t'], [0], temp10), - 'precipitation': (['t'], [0], precip)}, - coords={'time': (['t'], [0], dict(axis='T'))}) + ds = Dataset( + { + 'temperature_0': (['t'], [0], temp0), + 'temperature_10': (['t'], [0], temp10), + 'precipitation': (['t'], [0], precip) + }, + coords={ + 'time': (['t'], [0], dict(axis='T')) + }) # Test return empty Dataset. ds.filter_by_attrs(standard_name='invalid_standard_name') @@ -3784,9 +4277,10 @@ def test_filter_by_attrs(self): assert not bool(new_ds.data_vars) # Test return one DataArray. - new_ds = ds.filter_by_attrs(standard_name='convective_precipitation_flux') - assert (new_ds['precipitation'].standard_name - == 'convective_precipitation_flux') + new_ds = ds.filter_by_attrs( + standard_name='convective_precipitation_flux') + assert (new_ds['precipitation'].standard_name == + 'convective_precipitation_flux') assert_equal(new_ds['precipitation'], ds['precipitation']) @@ -3841,10 +4335,18 @@ def test_binary_op_join_setting(self): def test_full_like(self): # For more thorough tests, see test_variable.py # Note: testing data_vars with mismatched dtypes - ds = Dataset({ - 'd1': DataArray([1, 2, 3], dims=['x'], coords={'x': [10, 20, 30]}), - 'd2': DataArray([1.1, 2.2, 3.3], dims=['y']) - }, attrs={'foo': 'bar'}) + ds = Dataset( + { + 'd1': + DataArray([1, 2, 3], dims=['x'], coords={ + 'x': [10, 20, 30] + }), + 'd2': + DataArray([1.1, 2.2, 3.3], dims=['y']) + }, + attrs={ + 'foo': 'bar' + }) actual = full_like(ds, 2) expect = ds.copy(deep=True) @@ -3868,9 +4370,14 @@ def test_combine_first(self): dsx1 = DataArray([1, 1], [('x', ['b', 'c'])]).to_dataset(name='dsx1') actual = dsx0.combine_first(dsx1) - expected = Dataset({'dsx0': ('x', [0, 0, np.nan]), - 'dsx1': ('x', [np.nan, 1, 1])}, - coords={'x': ['a', 'b', 'c']}) + expected = Dataset( + { + 'dsx0': ('x', [0, 0, np.nan]), + 'dsx1': ('x', [np.nan, 1, 1]) + }, + coords={ + 'x': ['a', 'b', 'c'] + }) assert_equal(actual, expected) assert_equal(actual, xr.merge([dsx0, dsx1])) @@ -3882,23 +4389,29 @@ def test_combine_first(self): assert_equal(actual, expected) def test_sortby(self): - ds = Dataset({'A': DataArray([[1, 2], [3, 4], [5, 6]], - [('x', ['c', 'b', 'a']), - ('y', [1, 0])]), - 'B': DataArray([[5, 6], [7, 8], [9, 10]], - dims=['x', 'y'])}) - - sorted1d = Dataset({'A': DataArray([[5, 6], [3, 4], [1, 2]], - [('x', ['a', 'b', 'c']), - ('y', [1, 0])]), - 'B': DataArray([[9, 10], [7, 8], [5, 6]], - dims=['x', 'y'])}) - - sorted2d = Dataset({'A': DataArray([[6, 5], [4, 3], [2, 1]], - [('x', ['a', 'b', 'c']), - ('y', [0, 1])]), - 'B': DataArray([[10, 9], [8, 7], [6, 5]], - dims=['x', 'y'])}) + ds = Dataset({ + 'A': + DataArray([[1, 2], [3, 4], [5, 6]], [('x', ['c', 'b', 'a']), + ('y', [1, 0])]), + 'B': + DataArray([[5, 6], [7, 8], [9, 10]], dims=['x', 'y']) + }) + + sorted1d = Dataset({ + 'A': + DataArray([[5, 6], [3, 4], [1, 2]], [('x', ['a', 'b', 'c']), + ('y', [1, 0])]), + 'B': + DataArray([[9, 10], [7, 8], [5, 6]], dims=['x', 'y']) + }) + + sorted2d = Dataset({ + 'A': + DataArray([[6, 5], [4, 3], [2, 1]], [('x', ['a', 'b', 'c']), + ('y', [0, 1])]), + 'B': + DataArray([[10, 9], [8, 7], [6, 5]], dims=['x', 'y']) + }) expected = sorted1d dax = DataArray([100, 99, 98], [('x', ['c', 'b', 'a'])]) @@ -3946,18 +4459,23 @@ def test_sortby(self): # test pandas.MultiIndex indices = (('b', 1), ('b', 0), ('a', 1), ('a', 0)) midx = pd.MultiIndex.from_tuples(indices, names=['one', 'two']) - ds_midx = Dataset({'A': DataArray([[1, 2], [3, 4], [5, 6], [7, 8]], - [('x', midx), ('y', [1, 0])]), - 'B': DataArray([[5, 6], [7, 8], [9, 10], [11, 12]], - dims=['x', 'y'])}) + ds_midx = Dataset({ + 'A': + DataArray([[1, 2], [3, 4], [5, 6], [7, 8]], [('x', midx), + ('y', [1, 0])]), + 'B': + DataArray([[5, 6], [7, 8], [9, 10], [11, 12]], dims=['x', 'y']) + }) actual = ds_midx.sortby('x') - midx_reversed = pd.MultiIndex.from_tuples(tuple(reversed(indices)), - names=['one', 'two']) - expected = Dataset({'A': DataArray([[7, 8], [5, 6], [3, 4], [1, 2]], - [('x', midx_reversed), - ('y', [1, 0])]), - 'B': DataArray([[11, 12], [9, 10], [7, 8], [5, 6]], - dims=['x', 'y'])}) + midx_reversed = pd.MultiIndex.from_tuples( + tuple(reversed(indices)), names=['one', 'two']) + expected = Dataset({ + 'A': + DataArray([[7, 8], [5, 6], [3, 4], [1, 2]], [('x', midx_reversed), + ('y', [1, 0])]), + 'B': + DataArray([[11, 12], [9, 10], [7, 8], [5, 6]], dims=['x', 'y']) + }) assert_equal(actual, expected) # multi-dim sort by coordinate objects @@ -3971,8 +4489,10 @@ def test_sortby(self): def test_attribute_access(self): ds = create_test_data(seed=1) - for key in ['var1', 'var2', 'var3', 'time', 'dim1', - 'dim2', 'dim3', 'numbers']: + for key in [ + 'var1', 'var2', 'var3', 'time', 'dim1', 'dim2', 'dim3', + 'numbers' + ]: assert_equal(ds[key], getattr(ds, key)) assert key in dir(ds) @@ -3986,8 +4506,9 @@ def test_attribute_access(self): def test_ipython_key_completion(self): ds = create_test_data(seed=1) actual = ds._ipython_key_completions_() - expected = ['var1', 'var2', 'var3', 'time', 'dim1', - 'dim2', 'dim3', 'numbers'] + expected = [ + 'var1', 'var2', 'var3', 'time', 'dim1', 'dim2', 'dim3', 'numbers' + ] for item in actual: ds[item] # should not raise assert sorted(actual) == sorted(expected) @@ -4002,8 +4523,10 @@ def test_ipython_key_completion(self): # MultiIndex ds_midx = ds.stack(dim12=['dim1', 'dim2']) actual = ds_midx._ipython_key_completions_() - expected = ['var1', 'var2', 'var3', 'time', 'dim1', - 'dim2', 'dim3', 'numbers', 'dim12'] + expected = [ + 'var1', 'var2', 'var3', 'time', 'dim1', 'dim2', 'dim3', 'numbers', + 'dim12' + ] for item in actual: ds_midx[item] # should not raise assert sorted(actual) == sorted(expected) @@ -4028,6 +4551,7 @@ def test_ipython_key_completion(self): ds.data_vars[item] # should not raise assert sorted(actual) == sorted(expected) + # Py.test tests @@ -4038,8 +4562,7 @@ def data_set(seed=None): def test_dir_expected_attrs(data_set): - some_expected_attrs = {'pipe', 'mean', 'isnull', 'var1', - 'dim2', 'numbers'} + some_expected_attrs = {'pipe', 'mean', 'isnull', 'var1', 'dim2', 'numbers'} result = dir(data_set) assert set(result) >= some_expected_attrs @@ -4060,21 +4583,27 @@ def test_dir_unicode(data_set): @pytest.fixture(params=[1]) def ds(request): if request.param == 1: - return Dataset({'z1': (['y', 'x'], np.random.randn(2, 8)), - 'z2': (['time', 'y'], np.random.randn(10, 2))}, - {'x': ('x', np.linspace(0, 1.0, 8)), - 'time': ('time', np.linspace(0, 1.0, 10)), - 'c': ('y', ['a', 'b']), - 'y': range(2)}) + return Dataset({ + 'z1': (['y', 'x'], np.random.randn(2, 8)), + 'z2': (['time', 'y'], np.random.randn(10, 2)) + }, { + 'x': ('x', np.linspace(0, 1.0, 8)), + 'time': ('time', np.linspace(0, 1.0, 10)), + 'c': ('y', ['a', 'b']), + 'y': range(2) + }) if request.param == 2: - return Dataset({'z1': (['time', 'y'], np.random.randn(10, 2)), - 'z2': (['time'], np.random.randn(10)), - 'z3': (['x', 'time'], np.random.randn(8, 10))}, - {'x': ('x', np.linspace(0, 1.0, 8)), - 'time': ('time', np.linspace(0, 1.0, 10)), - 'c': ('y', ['a', 'b']), - 'y': range(2)}) + return Dataset({ + 'z1': (['time', 'y'], np.random.randn(10, 2)), + 'z2': (['time'], np.random.randn(10)), + 'z3': (['x', 'time'], np.random.randn(8, 10)) + }, { + 'x': ('x', np.linspace(0, 1.0, 8)), + 'time': ('time', np.linspace(0, 1.0, 10)), + 'c': ('y', ['a', 'b']), + 'y': range(2) + }) def test_rolling_properties(ds): @@ -4109,8 +4638,8 @@ def test_rolling_wrapped_bottleneck(ds, name, center, min_periods, key): if key is 'z1': # z1 does not depend on 'Time' axis. Stored as it is. expected = ds[key] elif key is 'z2': - expected = getattr(bn, func_name)(ds[key].values, window=7, axis=0, - min_count=min_periods) + expected = getattr(bn, func_name)( + ds[key].values, window=7, axis=0, min_count=min_periods) assert_array_equal(actual[key].values, expected) # Test center @@ -4123,23 +4652,25 @@ def test_rolling_wrapped_bottleneck(ds, name, center, min_periods, key): @pytest.mark.parametrize('min_periods', (None, 1, 2, 3)) @pytest.mark.parametrize('window', (1, 2, 3, 4)) def test_rolling_pandas_compat(center, window, min_periods): - df = pd.DataFrame({'x': np.random.randn(20), 'y': np.random.randn(20), - 'time': np.linspace(0, 1, 20)}) + df = pd.DataFrame({ + 'x': np.random.randn(20), + 'y': np.random.randn(20), + 'time': np.linspace(0, 1, 20) + }) ds = Dataset.from_dataframe(df) if min_periods is not None and window < min_periods: min_periods = window - df_rolling = df.rolling(window, center=center, - min_periods=min_periods).mean() - ds_rolling = ds.rolling(index=window, center=center, - min_periods=min_periods).mean() + df_rolling = df.rolling( + window, center=center, min_periods=min_periods).mean() + ds_rolling = ds.rolling( + index=window, center=center, min_periods=min_periods).mean() # pandas does some fancy stuff in the last position, # we're not going to do that yet! np.testing.assert_allclose(df_rolling['x'].values[:-1], ds_rolling['x'].values[:-1]) - np.testing.assert_allclose(df_rolling.index, - ds_rolling['index']) + np.testing.assert_allclose(df_rolling.index, ds_rolling['index']) @pytest.mark.slow @@ -4157,8 +4688,8 @@ def test_rolling_reduce(ds, center, min_periods, window, name): if name == 'std' and window == 1: pytest.skip('std with window == 1 is unstable in bottleneck') - rolling_obj = ds.rolling(time=window, center=center, - min_periods=min_periods) + rolling_obj = ds.rolling( + time=window, center=center, min_periods=min_periods) # add nan prefix to numpy methods to get similar behavior as bottleneck actual = rolling_obj.reduce(getattr(np, 'nan%s' % name)) From f94a0a9551ba16805aca68d794e0feff00c742c2 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 17:25:14 -0500 Subject: [PATCH 26/41] autopep8 all the things --- xarray/core/dataset.py | 6 +- xarray/core/missing.py | 3 + xarray/core/utils.py | 1 + xarray/tests/test_backends.py | 12 +- xarray/tests/test_coding_times.py | 5 +- xarray/tests/test_conventions.py | 2 +- xarray/tests/test_dataset.py | 6 +- xarray/tests/test_distributed.py | 2 + xarray/tests/test_indexing.py | 2 +- xarray/tests/test_missing.py | 4 +- xarray/tests/test_plot.py | 187 ++++++------ xarray/tests/test_utils.py | 12 +- xarray/tests/test_variable.py | 478 ++++++++++++++++-------------- 13 files changed, 393 insertions(+), 327 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 5e75e97ef31..62ad2b9b653 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -1573,7 +1573,8 @@ def relevant_keys(mapping): dim_name = dim dim_coord = None - reordered = self.transpose(*(list(indexer_dims) + list(non_indexed_dims))) + reordered = self.transpose( + *(list(indexer_dims) + list(non_indexed_dims))) variables = OrderedDict() @@ -3383,7 +3384,8 @@ def rank(self, dim, pct=False, keep_attrs=False): Variables that do not depend on `dim` are dropped. """ if dim not in self.dims: - raise ValueError('Dataset does not contain the dimension: %s' % dim) + raise ValueError( + 'Dataset does not contain the dimension: %s' % dim) variables = OrderedDict() for name, var in iteritems(self.variables): diff --git a/xarray/core/missing.py b/xarray/core/missing.py index cbf0df958d5..e26e976a11b 100644 --- a/xarray/core/missing.py +++ b/xarray/core/missing.py @@ -41,6 +41,7 @@ class NumpyInterpolator(BaseInterpolator): -------- numpy.interp ''' + def __init__(self, xi, yi, method='linear', fill_value=None, **kwargs): if method != 'linear': @@ -83,6 +84,7 @@ class ScipyInterpolator(BaseInterpolator): -------- scipy.interpolate.interp1d ''' + def __init__(self, xi, yi, method=None, fill_value=None, assume_sorted=True, copy=False, bounds_error=False, **kwargs): from scipy.interpolate import interp1d @@ -118,6 +120,7 @@ class SplineInterpolator(BaseInterpolator): -------- scipy.interpolate.UnivariateSpline ''' + def __init__(self, xi, yi, method='spline', fill_value=None, order=3, **kwargs): from scipy.interpolate import UnivariateSpline diff --git a/xarray/core/utils.py b/xarray/core/utils.py index 7edca42b4dd..de6b5825390 100644 --- a/xarray/core/utils.py +++ b/xarray/core/utils.py @@ -542,6 +542,7 @@ class HiddenKeyDict(MutableMapping): Acts like a normal dictionary, but hides certain keys. ''' # ``__init__`` method required to create instance from class. + def __init__(self, data, hidden_keys): self._data = data if type(hidden_keys) not in (list, tuple): diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 375107a1943..5404c0e8a81 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -2193,9 +2193,9 @@ def test_utm(self): # Tests expected = DataArray(data, dims=('band', 'y', 'x'), coords={ - 'band': [1, 2, 3], - 'y': -np.arange(ny) * 2000 + 80000 + dy / 2, - 'x': np.arange(nx) * 1000 + 5000 + dx / 2, + 'band': [1, 2, 3], + 'y': -np.arange(ny) * 2000 + 80000 + dy / 2, + 'x': np.arange(nx) * 1000 + 5000 + dx / 2, }) with xr.open_rasterio(tmp_file) as rioda: assert_allclose(rioda, expected) @@ -2362,9 +2362,9 @@ def test_caching(self): # ref expected = DataArray( data, dims=('band', 'y', 'x'), coords={ - 'x': (np.arange(nx) * 0.5 + 1) + dx / 2, - 'y': (-np.arange(ny) * 2 + 2) + dy / 2, - 'band': [1, 2, 3]}) + 'x': (np.arange(nx) * 0.5 + 1) + dx / 2, + 'y': (-np.arange(ny) * 2 + 2) + dy / 2, + 'band': [1, 2, 3]}) # Cache is the default with xr.open_rasterio(tmp_file) as actual: diff --git a/xarray/tests/test_coding_times.py b/xarray/tests/test_coding_times.py index b35959cab1a..f8ac3d3b58b 100644 --- a/xarray/tests/test_coding_times.py +++ b/xarray/tests/test_coding_times.py @@ -9,8 +9,7 @@ from xarray import Variable, coding from . import ( - TestCase, requires_netCDF4, assert_equal, assert_identical, - assert_array_equal) + TestCase, requires_netCDF4, assert_array_equal) import pytest @@ -219,7 +218,7 @@ def test_decode_non_standard_calendar_fallback(self): calendar=calendar) assert len(w) == 1 assert 'Unable to decode time axis' in \ - str(w[0].message) + str(w[0].message) assert actual.dtype == np.dtype('O') assert_array_equal(actual, expected) diff --git a/xarray/tests/test_conventions.py b/xarray/tests/test_conventions.py index 7f1e16f9d6d..6a509368017 100644 --- a/xarray/tests/test_conventions.py +++ b/xarray/tests/test_conventions.py @@ -143,7 +143,7 @@ def test_booltype_array(self): bx = conventions.BoolTypeArray(x) assert bx.dtype == np.bool assert_array_equal(bx, np.array([True, False, True, True, False], - dtype=np.bool)) + dtype=np.bool)) class TestNativeEndiannessArray(TestCase): diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 6c3f31b9f48..ad5052e4189 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -880,7 +880,7 @@ def test_isel(self): for d in data.dims: if d in slicers: assert ret.dims[d] == \ - np.arange(data.dims[d])[slicers[d]].size + np.arange(data.dims[d])[slicers[d]].size else: assert data.dims[d] == ret.dims[d] # Verify that the data is what we expect @@ -3757,7 +3757,7 @@ def test_reduce(self): for reduct, expected in [('dim2', ['dim1', 'dim3', 'time']), ([ 'dim2', 'time' ], ['dim1', 'dim3']), (('dim2', 'time'), ['dim1', 'dim3']), - ((), ['dim1', 'dim2', 'dim3', 'time'])]: + ((), ['dim1', 'dim2', 'dim3', 'time'])]: actual = data.min(dim=reduct).dims print(reduct, actual, expected) self.assertItemsEqual(actual, expected) @@ -3798,7 +3798,7 @@ def test_reduce_cumsum_test_dims(self): 'dim1', 'dim2', 'dim3', 'time' ]), ('dim2', ['dim1', 'dim2', 'dim3', 'time']), ('dim3', ['dim1', 'dim2', 'dim3', 'time']), - ('time', ['dim1', 'dim2', 'dim3'])]: + ('time', ['dim1', 'dim2', 'dim3'])]: actual = getattr(data, cumfunc)(dim=reduct).dims print(reduct, actual, expected) self.assertItemsEqual(actual, expected) diff --git a/xarray/tests/test_distributed.py b/xarray/tests/test_distributed.py index 478b669790e..1d450ff51d4 100644 --- a/xarray/tests/test_distributed.py +++ b/xarray/tests/test_distributed.py @@ -40,6 +40,7 @@ def test_dask_distributed_netcdf_integration_test(loop, engine): computed = restored.compute() assert_allclose(original, computed) + @requires_zarr def test_dask_distributed_zarr_integration_test(loop): with cluster() as (s, _): @@ -52,6 +53,7 @@ def test_dask_distributed_zarr_integration_test(loop): computed = restored.compute() assert_allclose(original, computed) + @pytest.mark.skipif(distributed.__version__ <= '1.19.3', reason='Need recent distributed version to clean up get') @gen_cluster(client=True, timeout=None) diff --git a/xarray/tests/test_indexing.py b/xarray/tests/test_indexing.py index 96f9c4e5dd7..3d93afb26d4 100644 --- a/xarray/tests/test_indexing.py +++ b/xarray/tests/test_indexing.py @@ -35,7 +35,7 @@ def test_expanded_indexer(self): j = indexing.expanded_indexer(i, x.ndim) assert_array_equal(x[i], x[j]) assert_array_equal(self.set_to_zero(x, i), - self.set_to_zero(x, j)) + self.set_to_zero(x, j)) with raises_regex(IndexError, 'too many indices'): indexing.expanded_indexer(I[1, 2, 3], 2) diff --git a/xarray/tests/test_missing.py b/xarray/tests/test_missing.py index 209e8de8663..ce735d720d0 100644 --- a/xarray/tests/test_missing.py +++ b/xarray/tests/test_missing.py @@ -62,7 +62,7 @@ def make_interpolate_example_data(shape, frac_nan, seed=12345, coords = {'time': (pd.Timestamp('2000-01-01') + deltas).sort_values()} else: coords = {'time': pd.date_range('2000-01-01', freq='D', - periods=shape[0])} + periods=shape[0])} da = xr.DataArray(vals, dims=('time', 'x'), coords=coords) df = da.to_pandas() @@ -254,7 +254,7 @@ def test_interpolate_limits(): actual = da.interpolate_na(dim='x', limit=2) expected = xr.DataArray(np.array([1, 2, 3, 4, np.nan, 6], - dtype=np.float64), dims='x') + dtype=np.float64), dims='x') assert_equal(actual, expected) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index ec1d79d2198..7306a36e6f5 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -20,13 +20,11 @@ import xarray.plot as xplt from xarray.plot.plot import _infer_interval_breaks -from xarray.plot.utils import (_determine_cmap_params, - _build_discrete_cmap, +from xarray.plot.utils import (_determine_cmap_params, _build_discrete_cmap, _color_palette, import_seaborn) -from . import ( - TestCase, requires_matplotlib, requires_seaborn, raises_regex, - assert_equal, assert_array_equal) +from . import (TestCase, requires_matplotlib, requires_seaborn, raises_regex, + assert_equal, assert_array_equal) @pytest.mark.flaky @@ -66,7 +64,6 @@ def easy_array(shape, start=0, stop=1): @requires_matplotlib class PlotTestCase(TestCase): - def tearDown(self): # Remove all matplotlib figures plt.close('all') @@ -89,7 +86,6 @@ def contourf_called(self, plotmethod): class TestPlot(PlotTestCase): - def setUp(self): self.darray = DataArray(easy_array((2, 3, 4))) @@ -113,7 +109,7 @@ def test_2d_line_accepts_legend_kw(self): assert plt.gca().get_legend() # check whether legend title is set assert plt.gca().get_legend().get_title().get_text() \ - == 'dim_1' + == 'dim_1' def test_2d_line_accepts_x_kw(self): self.darray[:, :, 0].plot.line(x='dim_0') @@ -125,11 +121,11 @@ def test_2d_line_accepts_x_kw(self): def test_2d_line_accepts_hue_kw(self): self.darray[:, :, 0].plot.line(hue='dim_0') assert plt.gca().get_legend().get_title().get_text() \ - == 'dim_0' + == 'dim_0' plt.cla() self.darray[:, :, 0].plot.line(hue='dim_1') assert plt.gca().get_legend().get_title().get_text() \ - == 'dim_1' + == 'dim_1' def test_2d_before_squeeze(self): a = DataArray(easy_array((1, 5))) @@ -147,9 +143,13 @@ def test2d_nonuniform_calls_contourf(self): def test2d_1d_2d_coordinates_contourf(self): sz = (20, 10) depth = easy_array(sz) - a = DataArray(easy_array(sz), dims=['z', 'time'], - coords={'depth': (['z', 'time'], depth), - 'time': np.linspace(0, 1, sz[1])}) + a = DataArray( + easy_array(sz), + dims=['z', 'time'], + coords={ + 'depth': (['z', 'time'], depth), + 'time': np.linspace(0, 1, sz[1]) + }) a.plot.contourf(x='time', y='depth') @@ -182,8 +182,9 @@ def test_datetime_dimension(self): nrow = 3 ncol = 4 time = pd.date_range('2000-01-01', periods=nrow) - a = DataArray(easy_array((nrow, ncol)), - coords=[('time', time), ('y', range(ncol))]) + a = DataArray( + easy_array((nrow, ncol)), + coords=[('time', time), ('y', range(ncol))]) a.plot() ax = plt.gca() assert ax.has_data() @@ -210,13 +211,18 @@ def test_subplot_kws(self): a = easy_array((10, 15, 4)) d = DataArray(a, dims=['y', 'x', 'z']) d.coords['z'] = list('abcd') - g = d.plot(x='x', y='y', col='z', col_wrap=2, cmap='cool', - subplot_kws=dict(facecolor='r')) + g = d.plot( + x='x', + y='y', + col='z', + col_wrap=2, + cmap='cool', + subplot_kws=dict(facecolor='r')) for ax in g.axes.flat: try: # mpl V2 assert ax.get_facecolor()[0:3] == \ - mpl.colors.to_rgb('r') + mpl.colors.to_rgb('r') except AttributeError: assert ax.get_axis_bgcolor() == 'r' @@ -261,11 +267,10 @@ def test_convenient_facetgrid_4d(self): class TestPlot1D(PlotTestCase): - def setUp(self): d = [0, 1.1, 0, 2] - self.darray = DataArray(d, coords={'period': range(len(d))}, - dims='period') + self.darray = DataArray( + d, coords={'period': range(len(d))}, dims='period') def test_xlabel_is_index_name(self): self.darray.plot() @@ -287,8 +292,7 @@ def test_can_pass_in_axis(self): self.pass_in_axis(self.darray.plot.line) def test_nonnumeric_index_raises_typeerror(self): - a = DataArray([1, 2, 3], {'letter': ['a', 'b', 'c']}, - dims='letter') + a = DataArray([1, 2, 3], {'letter': ['a', 'b', 'c']}, dims='letter') with raises_regex(TypeError, r'[Pp]lot'): a.plot.line() @@ -316,7 +320,6 @@ def test_slice_in_title(self): class TestPlotHistogram(PlotTestCase): - def setUp(self): self.darray = DataArray(easy_array((2, 3, 4))) @@ -356,7 +359,6 @@ def test_plot_nans(self): @requires_matplotlib class TestDetermineCmapParams(TestCase): - def setUp(self): self.data = np.linspace(0, 1, num=100) @@ -389,8 +391,8 @@ def test_integer_levels(self): assert cmap_params['extend'] == 'neither' # with min max we are more strict - cmap_params = _determine_cmap_params(data, levels=5, vmin=0, vmax=5, - cmap='Blues') + cmap_params = _determine_cmap_params( + data, levels=5, vmin=0, vmax=5, cmap='Blues') assert cmap_params['vmin'] == 0 assert cmap_params['vmax'] == 5 assert cmap_params['vmin'] == cmap_params['levels'][0] @@ -400,18 +402,17 @@ def test_integer_levels(self): assert cmap_params['cmap'].N == 4 assert cmap_params['norm'].N == 5 - cmap_params = _determine_cmap_params(data, levels=5, - vmin=0.5, vmax=1.5) + cmap_params = _determine_cmap_params( + data, levels=5, vmin=0.5, vmax=1.5) assert cmap_params['cmap'].name == 'viridis' assert cmap_params['extend'] == 'max' - cmap_params = _determine_cmap_params(data, levels=5, - vmin=1.5) + cmap_params = _determine_cmap_params(data, levels=5, vmin=1.5) assert cmap_params['cmap'].name == 'viridis' assert cmap_params['extend'] == 'min' - cmap_params = _determine_cmap_params(data, levels=5, - vmin=1.3, vmax=1.5) + cmap_params = _determine_cmap_params( + data, levels=5, vmin=1.3, vmax=1.5) assert cmap_params['cmap'].name == 'viridis' assert cmap_params['extend'] == 'both' @@ -420,8 +421,8 @@ def test_list_levels(self): orig_levels = [0, 1, 2, 3, 4, 5] # vmin and vmax should be ignored if levels are explicitly provided - cmap_params = _determine_cmap_params(data, levels=orig_levels, - vmin=0, vmax=3) + cmap_params = _determine_cmap_params( + data, levels=orig_levels, vmin=0, vmax=3) assert cmap_params['vmin'] == 0 assert cmap_params['vmax'] == 5 assert cmap_params['cmap'].N == 5 @@ -507,7 +508,6 @@ def test_divergentcontrol(self): @requires_matplotlib class TestDiscreteColorMap(TestCase): - def setUp(self): x = np.arange(start=0, stop=10, step=2) y = np.arange(start=9, stop=-7, step=-3) @@ -541,10 +541,10 @@ def test_build_discrete_cmap(self): @pytest.mark.slow def test_discrete_colormap_list_of_levels(self): - for extend, levels in [('max', [-1, 2, 4, 8, 10]), - ('both', [2, 5, 10, 11]), - ('neither', [0, 5, 10, 15]), - ('min', [2, 5, 10, 15])]: + for extend, levels in [('max', [-1, 2, 4, 8, 10]), ('both', + [2, 5, 10, 11]), + ('neither', [0, 5, 10, 15]), ('min', + [2, 5, 10, 15])]: for kind in ['imshow', 'pcolormesh', 'contourf', 'contour']: primitive = getattr(self.darray.plot, kind)(levels=levels) assert_array_equal(levels, primitive.norm.boundaries) @@ -558,16 +558,15 @@ def test_discrete_colormap_list_of_levels(self): @pytest.mark.slow def test_discrete_colormap_int_levels(self): - for extend, levels, vmin, vmax in [('neither', 7, None, None), - ('neither', 7, None, 20), - ('both', 7, 4, 8), - ('min', 10, 4, 15)]: + for extend, levels, vmin, vmax in [('neither', 7, None, + None), ('neither', 7, None, 20), + ('both', 7, 4, 8), ('min', 10, 4, + 15)]: for kind in ['imshow', 'pcolormesh', 'contourf', 'contour']: - primitive = getattr(self.darray.plot, kind)(levels=levels, - vmin=vmin, - vmax=vmax) + primitive = getattr(self.darray.plot, kind)( + levels=levels, vmin=vmin, vmax=vmax) assert levels >= \ - len(primitive.norm.boundaries) - 1 + len(primitive.norm.boundaries) - 1 if vmax is None: assert primitive.norm.vmax >= self.data_max else: @@ -598,8 +597,7 @@ class Common2dMixin: """ def setUp(self): - da = DataArray(easy_array( - (10, 15), start=-1), dims=['y', 'x']) + da = DataArray(easy_array((10, 15), start=-1), dims=['y', 'x']) # add 2d coords ds = da.to_dataset(name='testvar') x, y = np.meshgrid(da.x.values, da.y.values) @@ -627,8 +625,7 @@ def test_3d_raises_valueerror(self): self.plotfunc(a) def test_nonnumeric_index_raises_typeerror(self): - a = DataArray(easy_array((3, 2)), - coords=[['a', 'b', 'c'], ['d', 'e']]) + a = DataArray(easy_array((3, 2)), coords=[['a', 'b', 'c'], ['d', 'e']]) with raises_regex(TypeError, r'[Pp]lot'): self.plotfunc(a) @@ -651,8 +648,8 @@ def test_xyincrease_true_changes_axes(self): def test_x_ticks_are_rotated_for_time(self): time = pd.date_range('2000-01-01', '2000-01-10') - a = DataArray(np.random.randn(2, len(time)), - [('xx', [1, 2]), ('t', time)]) + a = DataArray( + np.random.randn(2, len(time)), [('xx', [1, 2]), ('t', time)]) a.plot(x='t') rotation = plt.gca().get_xticklabels()[0].get_rotation() assert rotation != 0 @@ -692,8 +689,7 @@ def test_default_cmap(self): @requires_seaborn def test_seaborn_palette_as_cmap(self): - cmap_name = self.plotmethod( - levels=2, cmap='husl').get_cmap().name + cmap_name = self.plotmethod(levels=2, cmap='husl').get_cmap().name assert 'husl' == cmap_name def test_can_change_default_cmap(self): @@ -703,7 +699,7 @@ def test_can_change_default_cmap(self): def test_diverging_color_limits(self): artist = self.plotmethod() vmin, vmax = artist.get_clim() - assert round(abs(-vmin-vmax), 7) == 0 + assert round(abs(-vmin - vmax), 7) == 0 def test_xy_strings(self): self.plotmethod('y', 'x') @@ -723,14 +719,13 @@ def test_positional_coord_string(self): assert 'y' == ax.get_ylabel() def test_bad_x_string_exception(self): - with raises_regex( - ValueError, 'x and y must be coordinate variables'): + with raises_regex(ValueError, 'x and y must be coordinate variables'): self.plotmethod('not_a_real_dim', 'y') - with raises_regex( - ValueError, 'x must be a dimension name if y is not supplied'): + with raises_regex(ValueError, + 'x must be a dimension name if y is not supplied'): self.plotmethod(x='not_a_real_dim') - with raises_regex( - ValueError, 'y must be a dimension name if x is not supplied'): + with raises_regex(ValueError, + 'y must be a dimension name if x is not supplied'): self.plotmethod(y='not_a_real_dim') self.darray.coords['z'] = 100 @@ -793,14 +788,20 @@ def test_colorbar_kwargs(self): assert 'MyLabel' in alltxt assert 'testvar' not in alltxt # you can use mapping types as well - self.plotmethod(add_colorbar=True, cbar_kwargs=(('label', 'MyLabel'),)) + self.plotmethod( + add_colorbar=True, cbar_kwargs=(('label', 'MyLabel'), )) alltxt = text_in_fig() assert 'MyLabel' in alltxt assert 'testvar' not in alltxt # change cbar ax fig, (ax, cax) = plt.subplots(1, 2) - self.plotmethod(ax=ax, cbar_ax=cax, add_colorbar=True, - cbar_kwargs={'label':'MyBar'}) + self.plotmethod( + ax=ax, + cbar_ax=cax, + add_colorbar=True, + cbar_kwargs={ + 'label': 'MyBar' + }) assert ax.has_data() assert cax.has_data() alltxt = text_in_fig() @@ -808,8 +809,13 @@ def test_colorbar_kwargs(self): assert 'testvar' not in alltxt # note that there are two ways to achieve this fig, (ax, cax) = plt.subplots(1, 2) - self.plotmethod(ax=ax, add_colorbar=True, - cbar_kwargs={'label': 'MyBar', 'cax': cax}) + self.plotmethod( + ax=ax, + add_colorbar=True, + cbar_kwargs={ + 'label': 'MyBar', + 'cax': cax + }) assert ax.has_data() assert cax.has_data() alltxt = text_in_fig() @@ -819,8 +825,13 @@ def test_colorbar_kwargs(self): self.plotmethod(add_colorbar=False) assert 'testvar' not in text_in_fig() # check that error is raised - pytest.raises(ValueError, self.plotmethod, - add_colorbar=False, cbar_kwargs={'label': 'label'}) + pytest.raises( + ValueError, + self.plotmethod, + add_colorbar=False, + cbar_kwargs={ + 'label': 'label' + }) def test_verbose_facetgrid(self): a = easy_array((10, 15, 3)) @@ -948,6 +959,7 @@ def test_colors(self): # when seaborn is used, instead we get an rgb tuple def _color_as_tuple(c): return tuple(c[:3]) + artist = self.plotmethod(colors='k') assert _color_as_tuple(artist.cmap.colors[0]) == \ (0.0, 0.0, 0.0) @@ -956,8 +968,8 @@ def _color_as_tuple(c): assert _color_as_tuple(artist.cmap.colors[1]) == \ (0.0, 0.0, 1.0) - artist = self.darray.plot.contour(levels=[-0.5, 0., 0.5, 1.], - colors=['k', 'r', 'w', 'b']) + artist = self.darray.plot.contour( + levels=[-0.5, 0., 0.5, 1.], colors=['k', 'r', 'w', 'b']) assert _color_as_tuple(artist.cmap.colors[1]) == \ (1.0, 0.0, 0.0) assert _color_as_tuple(artist.cmap.colors[2]) == \ @@ -1077,14 +1089,16 @@ def test_plot_rgb_image_explicit(self): DataArray( easy_array((10, 15, 3), start=0), dims=['y', 'x', 'band'], - ).plot.imshow(y='y', x='x', rgb='band') + ).plot.imshow( + y='y', x='x', rgb='band') assert 0 == len(find_possible_colorbars()) def test_plot_rgb_faceted(self): DataArray( easy_array((2, 2, 10, 15, 3), start=0), dims=['a', 'b', 'y', 'x', 'band'], - ).plot.imshow(row='a', col='b') + ).plot.imshow( + row='a', col='b') assert 0 == len(find_possible_colorbars()) def test_plot_rgba_image_transposed(self): @@ -1114,11 +1128,12 @@ def test_rgb_errors_bad_dim_sizes(self): class TestFacetGrid(PlotTestCase): - def setUp(self): d = easy_array((10, 15, 3)) - self.darray = DataArray(d, dims=['y', 'x', 'z'], - coords={'z': ['a', 'b', 'c']}) + self.darray = DataArray( + d, dims=['y', 'x', 'z'], coords={ + 'z': ['a', 'b', 'c'] + }) self.g = xplt.FacetGrid(self.darray, col='z') @pytest.mark.slow @@ -1331,20 +1346,21 @@ def test_facetgrid_colorbar(self): @pytest.mark.slow def test_facetgrid_polar(self): # test if polar projection in FacetGrid does not raise an exception - self.darray.plot.pcolormesh(col='z', - subplot_kws=dict(projection='polar'), - sharex=False, sharey=False) + self.darray.plot.pcolormesh( + col='z', + subplot_kws=dict(projection='polar'), + sharex=False, + sharey=False) class TestFacetGrid4d(PlotTestCase): - def setUp(self): a = easy_array((10, 15, 3, 2)) darray = DataArray(a, dims=['y', 'x', 'col', 'row']) - darray.coords['col'] = np.array(['col' + str(x) for x in - darray.coords['col'].values]) - darray.coords['row'] = np.array(['row' + str(x) for x in - darray.coords['row'].values]) + darray.coords['col'] = np.array( + ['col' + str(x) for x in darray.coords['col'].values]) + darray.coords['row'] = np.array( + ['row' + str(x) for x in darray.coords['row'].values]) self.darray = darray @@ -1365,7 +1381,6 @@ def test_default_labels(self): class TestDatetimePlot(PlotTestCase): - def setUp(self): ''' Create a DataArray with a time-axis that contains datetime objects. diff --git a/xarray/tests/test_utils.py b/xarray/tests/test_utils.py index 2353e1b5259..1a008eff180 100644 --- a/xarray/tests/test_utils.py +++ b/xarray/tests/test_utils.py @@ -83,7 +83,7 @@ def test_unsafe(self): def test_ordered_dict_intersection(self): assert {'b': 'B'} == \ - utils.ordered_dict_intersection(self.x, self.y) + utils.ordered_dict_intersection(self.x, self.y) assert {} == utils.ordered_dict_intersection(self.x, self.z) def test_dict_equiv(self): @@ -121,14 +121,14 @@ def test_frozen(self): x.update(self.y) assert x.mapping == self.x assert repr(x) in ("Frozen({'a': 'A', 'b': 'B'})", - "Frozen({'b': 'B', 'a': 'A'})") + "Frozen({'b': 'B', 'a': 'A'})") def test_sorted_keys_dict(self): x = {'a': 1, 'b': 2, 'c': 3} y = utils.SortedKeysDict(x) self.assertItemsEqual(y, ['a', 'b', 'c']) assert repr(utils.SortedKeysDict()) == \ - "SortedKeysDict({})" + "SortedKeysDict({})" def test_chain_map(self): m = utils.ChainMap({'x': 0, 'y': 1}, {'x': -100, 'z': 2}) @@ -155,13 +155,13 @@ def test_sorted_uniform(self): assert utils.is_uniform_spaced(np.arange(5)) def test_sorted_not_uniform(self): - assert False == utils.is_uniform_spaced([-2, 1, 89]) + assert not utils.is_uniform_spaced([-2, 1, 89]) def test_not_sorted_uniform(self): - assert False == utils.is_uniform_spaced([1, -1, 3]) + assert not utils.is_uniform_spaced([1, -1, 3]) def test_not_sorted_not_uniform(self): - assert False == utils.is_uniform_spaced([4, 1, 89]) + assert not utils.is_uniform_spaced([4, 1, 89]) def test_two_numbers(self): assert utils.is_uniform_spaced([0, 1.7]) diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index 41baf41eeae..709b306986a 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -15,18 +15,16 @@ from xarray import Variable, IndexVariable, Coordinate, Dataset from xarray.core import indexing from xarray.core.variable import as_variable, as_compatible_data -from xarray.core.indexing import (PandasIndexAdapter, LazilyIndexedArray, - BasicIndexer, OuterIndexer, - VectorizedIndexer, NumpyIndexingAdapter, - CopyOnWriteArray, MemoryCachedArray, - DaskIndexingAdapter) +from xarray.core.indexing import ( + PandasIndexAdapter, LazilyIndexedArray, BasicIndexer, OuterIndexer, + VectorizedIndexer, NumpyIndexingAdapter, CopyOnWriteArray, + MemoryCachedArray, DaskIndexingAdapter) from xarray.core.pycompat import PY3, OrderedDict from xarray.core.common import full_like, zeros_like, ones_like from xarray.core.utils import NDArrayMixin -from . import ( - TestCase, source_ndarray, requires_dask, raises_regex, assert_identical, - assert_equal, assert_array_equal) +from . import (TestCase, source_ndarray, requires_dask, raises_regex, + assert_identical, assert_array_equal) from xarray.tests import requires_bottleneck @@ -35,10 +33,10 @@ class VariableSubclassTestCases(object): def test_properties(self): data = 0.5 * np.arange(10) v = self.cls(['time'], data, {'foo': 'bar'}) - assert v.dims == ('time',) + assert v.dims == ('time', ) assert_array_equal(v.values, data) assert v.dtype == float - assert v.shape == (10,) + assert v.shape == (10, ) assert v.size == 10 assert v.sizes == {'time': 10} assert v.nbytes == 80 @@ -110,25 +108,29 @@ def test_getitem_1d_fancy(self): def test_getitem_with_mask(self): v = self.cls(['x'], [0, 1, 2]) assert_identical(v._getitem_with_mask(-1), Variable((), np.nan)) - assert_identical(v._getitem_with_mask([0, -1, 1]), - self.cls(['x'], [0, np.nan, 1])) - assert_identical(v._getitem_with_mask(slice(2)), - self.cls(['x'], [0, 1])) - assert_identical(v._getitem_with_mask([0, -1, 1], fill_value=-99), - self.cls(['x'], [0, -99, 1])) + assert_identical( + v._getitem_with_mask([0, -1, 1]), self.cls(['x'], [0, np.nan, 1])) + assert_identical( + v._getitem_with_mask(slice(2)), self.cls(['x'], [0, 1])) + assert_identical( + v._getitem_with_mask([0, -1, 1], fill_value=-99), + self.cls(['x'], [0, -99, 1])) def test_getitem_with_mask_size_zero(self): v = self.cls(['x'], []) assert_identical(v._getitem_with_mask(-1), Variable((), np.nan)) - assert_identical(v._getitem_with_mask([-1, -1, -1]), - self.cls(['x'], [np.nan, np.nan, np.nan])) + assert_identical( + v._getitem_with_mask([-1, -1, -1]), + self.cls(['x'], [np.nan, np.nan, np.nan])) def test_getitem_with_mask_nd_indexer(self): v = self.cls(['x'], [0, 1, 2]) indexer = Variable(('x', 'y'), [[0, -1], [-1, 2]]) assert_identical(v._getitem_with_mask(indexer, fill_value=-1), indexer) - def _assertIndexedLikeNDArray(self, variable, expected_value0, + def _assertIndexedLikeNDArray(self, + variable, + expected_value0, expected_dtype=None): """Given a 1-dimensional variable, verify that the variable is indexed like a numpy.ndarray. @@ -152,14 +154,12 @@ def _assertIndexedLikeNDArray(self, variable, expected_value0, assert variable[0].values.dtype == expected_dtype def test_index_0d_int(self): - for value, dtype in [(0, np.int_), - (np.int32(0), np.int32)]: + for value, dtype in [(0, np.int_), (np.int32(0), np.int32)]: x = self.cls(['x'], [value]) self._assertIndexedLikeNDArray(x, value, dtype) def test_index_0d_float(self): - for value, dtype in [(0.5, np.float_), - (np.float32(0.5), np.float32)]: + for value, dtype in [(0.5, np.float_), (np.float32(0.5), np.float32)]: x = self.cls(['x'], [value]) self._assertIndexedLikeNDArray(x, value, dtype) @@ -184,12 +184,12 @@ def test_index_0d_timedelta64(self): td = timedelta(hours=1) x = self.cls(['x'], [np.timedelta64(td)]) - self._assertIndexedLikeNDArray( - x, np.timedelta64(td), 'timedelta64[ns]') + self._assertIndexedLikeNDArray(x, np.timedelta64(td), + 'timedelta64[ns]') x = self.cls(['x'], pd.to_timedelta([td])) - self._assertIndexedLikeNDArray( - x, np.timedelta64(td), 'timedelta64[ns]') + self._assertIndexedLikeNDArray(x, np.timedelta64(td), + 'timedelta64[ns]') def test_index_0d_not_a_time(self): d = np.datetime64('NaT', 'ns') @@ -197,7 +197,6 @@ def test_index_0d_not_a_time(self): self._assertIndexedLikeNDArray(x, d) def test_index_0d_object(self): - class HashableItemWrapper(object): def __init__(self, item): self.item = item @@ -216,7 +215,7 @@ def __repr__(self): self._assertIndexedLikeNDArray(x, item, expected_dtype=False) def test_0d_object_array_with_list(self): - listarray = np.empty((1,), dtype=object) + listarray = np.empty((1, ), dtype=object) listarray[0] = [1, 2, 3] x = self.cls('x', listarray) assert_array_equal(x.data, listarray) @@ -226,8 +225,10 @@ def test_0d_object_array_with_list(self): def test_index_and_concat_datetime(self): # regression test for #125 date_range = pd.date_range('2011-09-01', periods=10) - for dates in [date_range, date_range.values, - date_range.to_pydatetime()]: + for dates in [ + date_range, date_range.values, + date_range.to_pydatetime() + ]: expected = self.cls('t', dates) for times in [[expected[i] for i in range(10)], [expected[i:(i + 1)] for i in range(10)], @@ -284,8 +285,8 @@ def test_pandas_data(self): assert v[0].values == v.values[0] def test_pandas_period_index(self): - v = self.cls(['x'], pd.period_range(start='2000', periods=20, - freq='B')) + v = self.cls(['x'], pd.period_range( + start='2000', periods=20, freq='B')) v = v.load() # for dask-based Variable assert v[0] == pd.Period('2000', freq='B') assert "Period('2000-01-03', 'B')" in repr(v) @@ -349,20 +350,23 @@ def test_array_interface(self): assert_array_equal(v.astype(float), x.astype(float)) # think this is a break, that argsort changes the type assert_identical(v.argsort(), v.to_base_variable()) - assert_identical(v.clip(2, 3), - self.cls('x', x.clip(2, 3)).to_base_variable()) + assert_identical( + v.clip(2, 3), + self.cls('x', x.clip(2, 3)).to_base_variable()) # test ufuncs - assert_identical(np.sin(v), - self.cls(['x'], np.sin(x)).to_base_variable()) + assert_identical( + np.sin(v), + self.cls(['x'], np.sin(x)).to_base_variable()) assert isinstance(np.sin(v), Variable) assert not isinstance(np.sin(v), IndexVariable) def example_1d_objects(self): - for data in [range(3), - 0.5 * np.arange(3), - 0.5 * np.arange(3, dtype=np.float32), - pd.date_range('2000-01-01', periods=3), - np.array(['a', 'b', 'c'], dtype=object)]: + for data in [ + range(3), 0.5 * np.arange(3), + 0.5 * np.arange(3, dtype=np.float32), + pd.date_range('2000-01-01', periods=3), + np.array(['a', 'b', 'c'], dtype=object) + ]: yield (self.cls('x', data), data) def test___array__(self): @@ -397,16 +401,19 @@ def test_eq_all_dtypes(self): def test_encoding_preserved(self): expected = self.cls('x', range(3), {'foo': 1}, {'bar': 2}) - for actual in [expected.T, - expected[...], - expected.squeeze(), - expected.isel(x=slice(None)), - expected.set_dims({'x': 3}), - expected.copy(deep=True), - expected.copy(deep=False)]: + for actual in [ + expected.T, expected[...], + expected.squeeze(), + expected.isel(x=slice(None)), + expected.set_dims({ + 'x': 3 + }), + expected.copy(deep=True), + expected.copy(deep=False) + ]: assert_identical(expected.to_base_variable(), - actual.to_base_variable()) + actual.to_base_variable()) assert expected.encoding == actual.encoding def test_concat(self): @@ -414,18 +421,22 @@ def test_concat(self): y = np.arange(5, 10) v = self.cls(['a'], x) w = self.cls(['a'], y) - assert_identical(Variable(['b', 'a'], np.array([x, y])), - Variable.concat([v, w], 'b')) - assert_identical(Variable(['b', 'a'], np.array([x, y])), - Variable.concat((v, w), 'b')) - assert_identical(Variable(['b', 'a'], np.array([x, y])), - Variable.concat((v, w), 'b')) + assert_identical( + Variable(['b', 'a'], np.array([x, y])), Variable.concat([v, w], + 'b')) + assert_identical( + Variable(['b', 'a'], np.array([x, y])), Variable.concat((v, w), + 'b')) + assert_identical( + Variable(['b', 'a'], np.array([x, y])), Variable.concat((v, w), + 'b')) with raises_regex(ValueError, 'inconsistent dimensions'): Variable.concat([v, Variable(['c'], y)], 'b') # test indexers actual = Variable.concat( [v, w], - positions=[np.arange(0, 10, 2), np.arange(1, 10, 2)], + positions=[np.arange(0, 10, 2), + np.arange(1, 10, 2)], dim='a') expected = Variable('a', np.array([x, y]).ravel(order='F')) assert_identical(expected, actual) @@ -443,8 +454,9 @@ def test_concat_attrs(self): # different or conflicting attributes should be removed v = self.cls('a', np.arange(5), {'foo': 'bar'}) w = self.cls('a', np.ones(5)) - expected = self.cls( - 'a', np.concatenate([np.arange(5), np.ones(5)])).to_base_variable() + expected = self.cls('a', + np.concatenate([np.arange(5), + np.ones(5)])).to_base_variable() assert_identical(expected, Variable.concat([v, w], 'a')) w.attrs['foo'] = 2 assert_identical(expected, Variable.concat([v, w], 'a')) @@ -458,8 +470,8 @@ def test_concat_fixed_len_str(self): x = self.cls('animal', np.array(['horse'], dtype=kind)) y = self.cls('animal', np.array(['aardvark'], dtype=kind)) actual = Variable.concat([x, y], 'animal') - expected = Variable( - 'animal', np.array(['horse', 'aardvark'], dtype=kind)) + expected = Variable('animal', + np.array(['horse', 'aardvark'], dtype=kind)) self.assertVariableEqual(expected, actual) def test_concat_number_strings(self): @@ -482,15 +494,15 @@ def test_copy(self): if self.cls is Variable: if deep: assert source_ndarray(v.values) is not \ - source_ndarray(w.values) + source_ndarray(w.values) else: assert source_ndarray(v.values) is \ - source_ndarray(w.values) + source_ndarray(w.values) assert_identical(v, copy(v)) def test_copy_index(self): - midx = pd.MultiIndex.from_product([['a', 'b'], [1, 2], [-1, -2]], - names=('one', 'two', 'three')) + midx = pd.MultiIndex.from_product( + [['a', 'b'], [1, 2], [-1, -2]], names=('one', 'two', 'three')) v = self.cls('x', midx) for deep in [True, False]: w = v.copy(deep=deep) @@ -506,8 +518,8 @@ def test_real_and_imag(self): expected_im = self.cls('x', -np.arange(3), {'foo': 'bar'}) assert_identical(v.imag, expected_im) - expected_abs = self.cls( - 'x', np.sqrt(2 * np.arange(3) ** 2)).to_base_variable() + expected_abs = self.cls('x', np.sqrt( + 2 * np.arange(3) ** 2)).to_base_variable() self.assertVariableAllClose(abs(v), expected_abs) def test_aggregate_complex(self): @@ -523,9 +535,11 @@ def test_pandas_cateogrical_dtype(self): assert v.dtype == 'int64' def test_pandas_datetime64_with_tz(self): - data = pd.date_range(start='2000-01-01', - tz=pytz.timezone('America/New_York'), - periods=10, freq='1h') + data = pd.date_range( + start='2000-01-01', + tz=pytz.timezone('America/New_York'), + periods=10, + freq='1h') v = self.cls('x', data) print(v) # should not error if 'America/New_York' in str(data.dtype): @@ -648,7 +662,7 @@ def test_getitem_fancy(self): # along diagonal ind = Variable(['a'], [0, 1]) v_new = v[ind, ind] - assert v_new.dims == ('a',) + assert v_new.dims == ('a', ) assert_array_equal(v_new, v_data[[0, 1], [0, 1]]) # with integer @@ -677,7 +691,7 @@ def test_getitem_fancy(self): # slice matches explicit dimension ind = Variable(['y'], [0, 1]) v_new = v[ind, :2] - assert v_new.dims == ('y',) + assert v_new.dims == ('y', ) assert_array_equal(v_new, v_data[[0, 1], [0, 1]]) # with multiple slices @@ -759,9 +773,9 @@ def test_numpy_same_methods(self): def test_datetime64_conversion_scalar(self): expected = np.datetime64('2000-01-01', 'ns') for values in [ - np.datetime64('2000-01-01'), - pd.Timestamp('2000-01-01T00'), - datetime(2000, 1, 1), + np.datetime64('2000-01-01'), + pd.Timestamp('2000-01-01T00'), + datetime(2000, 1, 1), ]: v = Variable([], values) assert v.dtype == np.dtype('datetime64[ns]') @@ -771,9 +785,9 @@ def test_datetime64_conversion_scalar(self): def test_timedelta64_conversion_scalar(self): expected = np.timedelta64(24 * 60 * 60 * 10 ** 9, 'ns') for values in [ - np.timedelta64(1, 'D'), - pd.Timedelta('1 day'), - timedelta(days=1), + np.timedelta64(1, 'D'), + pd.Timedelta('1 day'), + timedelta(days=1), ]: v = Variable([], values) assert v.dtype == np.dtype('timedelta64[ns]') @@ -803,10 +817,16 @@ def test_0d_timedelta(self): def test_equals_and_identical(self): d = np.random.rand(10, 3) d[0, 0] = np.nan - v1 = Variable(('dim1', 'dim2'), data=d, - attrs={'att1': 3, 'att2': [1, 2, 3]}) - v2 = Variable(('dim1', 'dim2'), data=d, - attrs={'att1': 3, 'att2': [1, 2, 3]}) + v1 = Variable( + ('dim1', 'dim2'), data=d, attrs={ + 'att1': 3, + 'att2': [1, 2, 3] + }) + v2 = Variable( + ('dim1', 'dim2'), data=d, attrs={ + 'att1': 3, + 'att2': [1, 2, 3] + }) assert v1.equals(v2) assert v1.identical(v2) @@ -867,8 +887,10 @@ def test_no_conflicts(self): def test_as_variable(self): data = np.arange(10) expected = Variable('x', data) - expected_extra = Variable('x', data, attrs={'myattr': 'val'}, - encoding={'scale_factor': 1}) + expected_extra = Variable( + 'x', data, attrs={'myattr': 'val'}, encoding={ + 'scale_factor': 1 + }) assert_identical(expected, as_variable(expected)) @@ -899,8 +921,7 @@ def test_as_variable(self): with raises_regex(TypeError, 'tuples to convert'): as_variable(tuple(data)) - with raises_regex( - TypeError, 'without an explicit list of dimensions'): + with raises_regex(TypeError, 'without an explicit list of dimensions'): as_variable(data) actual = as_variable(data, name='x') @@ -912,11 +933,9 @@ def test_as_variable(self): data = np.arange(9).reshape((3, 3)) expected = Variable(('x', 'y'), data) - with raises_regex( - ValueError, 'without explicit dimension names'): + with raises_regex(ValueError, 'without explicit dimension names'): as_variable(data, name='x') - with raises_regex( - ValueError, 'has more than 1-dimension'): + with raises_regex(ValueError, 'has more than 1-dimension'): as_variable(expected, name='x') def test_repr(self): @@ -1015,8 +1034,7 @@ def test_items(self): assert_identical(v, v[...]) assert_identical(Variable(['y'], data[0]), v[0]) assert_identical(Variable(['x'], data[:, 0]), v[:, 0]) - assert_identical(Variable(['x', 'y'], data[:3, :2]), - v[:3, :2]) + assert_identical(Variable(['x', 'y'], data[:3, :2]), v[:3, :2]) # test array indexing x = Variable(['x'], np.arange(10)) y = Variable(['y'], np.arange(11)) @@ -1065,8 +1083,9 @@ def test_getitem_basic(self): def test_getitem_with_mask_2d_input(self): v = Variable(('x', 'y'), [[0, 1, 2], [3, 4, 5]]) - assert_identical(v._getitem_with_mask(([-1, 0], [1, -1])), - Variable(('x', 'y'), [[np.nan, np.nan], [1, np.nan]])) + assert_identical( + v._getitem_with_mask(([-1, 0], [1, -1])), + Variable(('x', 'y'), [[np.nan, np.nan], [1, np.nan]])) assert_identical(v._getitem_with_mask((slice(2), [0, 1, 2])), v) def test_isel(self): @@ -1169,7 +1188,7 @@ def test_transpose(self): def test_transpose_0d(self): for value in [ 3.5, - ('a', 1), + ('a', 1), np.datetime64('2000-01-01'), np.timedelta64(1, 'h'), None, @@ -1196,7 +1215,7 @@ def test_squeeze(self): def test_get_axis_num(self): v = Variable(['x', 'y', 'z'], np.random.randn(2, 3, 4)) assert v.get_axis_num('x') == 0 - assert v.get_axis_num(['x']) == (0,) + assert v.get_axis_num(['x']) == (0, ) assert v.get_axis_num(['x', 'y']) == (0, 1) assert v.get_axis_num(['z', 'y', 'x']) == (2, 1, 0) with raises_regex(ValueError, 'not found in array dim'): @@ -1225,8 +1244,8 @@ def test_set_dims(self): def test_set_dims_object_dtype(self): v = Variable([], ('a', 1)) - actual = v.set_dims(('x',), (3,)) - exp_values = np.empty((3,), dtype=object) + actual = v.set_dims(('x', ), (3, )) + exp_values = np.empty((3, ), dtype=object) for i in range(3): exp_values[i] = ('a', 1) expected = Variable(['x'], exp_values) @@ -1238,14 +1257,14 @@ def test_stack(self): expected = Variable('z', [0, 1, 2, 3], v.attrs) assert_identical(actual, expected) - actual = v.stack(z=('x',)) + actual = v.stack(z=('x', )) expected = Variable(('y', 'z'), v.data.T, v.attrs) assert_identical(actual, expected) - actual = v.stack(z=(),) + actual = v.stack(z=(), ) assert_identical(actual, v) - actual = v.stack(X=('x',), Y=('y',)).transpose('X', 'Y') + actual = v.stack(X=('x', ), Y=('y', )).transpose('X', 'Y') expected = Variable(('X', 'Y'), v.data, v.attrs) assert_identical(actual, expected) @@ -1253,9 +1272,9 @@ def test_stack_errors(self): v = Variable(['x', 'y'], [[0, 1], [2, 3]], {'foo': 'bar'}) with raises_regex(ValueError, 'invalid existing dim'): - v.stack(z=('x1',)) + v.stack(z=('x1', )) with raises_regex(ValueError, 'cannot create a new dim'): - v.stack(x=('x',)) + v.stack(x=('x', )) def test_unstack(self): v = Variable('z', [0, 1, 2, 3], {'foo': 'bar'}) @@ -1276,7 +1295,7 @@ def test_unstack_errors(self): with raises_regex(ValueError, 'invalid existing dim'): v.unstack(foo={'x': 4}) with raises_regex(ValueError, 'cannot create a new dim'): - v.stack(z=('z',)) + v.stack(z=('z', )) with raises_regex(ValueError, 'the product of the new dim'): v.unstack(z={'x': 5}) @@ -1300,30 +1319,27 @@ def test_broadcasting_math(self): x = np.random.randn(2, 3) v = Variable(['a', 'b'], x) # 1d to 2d broadcasting - assert_identical( - v * v, - Variable(['a', 'b'], np.einsum('ab,ab->ab', x, x))) - assert_identical( - v * v[0], - Variable(['a', 'b'], np.einsum('ab,b->ab', x, x[0]))) - assert_identical( - v[0] * v, - Variable(['b', 'a'], np.einsum('b,ab->ba', x[0], x))) - assert_identical( - v[0] * v[:, 0], - Variable(['b', 'a'], np.einsum('b,a->ba', x[0], x[:, 0]))) + assert_identical(v * v, + Variable(['a', 'b'], np.einsum('ab,ab->ab', x, x))) + assert_identical(v * v[0], + Variable(['a', 'b'], np.einsum('ab,b->ab', x, x[0]))) + assert_identical(v[0] * v, + Variable(['b', 'a'], np.einsum('b,ab->ba', x[0], x))) + assert_identical(v[0] * v[:, 0], + Variable(['b', 'a'], + np.einsum('b,a->ba', x[0], x[:, 0]))) # higher dim broadcasting y = np.random.randn(3, 4, 5) w = Variable(['b', 'c', 'd'], y) - assert_identical( - v * w, Variable(['a', 'b', 'c', 'd'], - np.einsum('ab,bcd->abcd', x, y))) - assert_identical( - w * v, Variable(['b', 'c', 'd', 'a'], - np.einsum('bcd,ab->bcda', y, x))) - assert_identical( - v * w[0], Variable(['a', 'b', 'c', 'd'], - np.einsum('ab,cd->abcd', x, y[0]))) + assert_identical(v * w, + Variable(['a', 'b', 'c', 'd'], + np.einsum('ab,bcd->abcd', x, y))) + assert_identical(w * v, + Variable(['b', 'c', 'd', 'a'], + np.einsum('bcd,ab->bcda', y, x))) + assert_identical(v * w[0], + Variable(['a', 'b', 'c', 'd'], + np.einsum('ab,cd->abcd', x, y[0]))) def test_broadcasting_failures(self): a = Variable(['x'], np.arange(10)) @@ -1349,14 +1365,13 @@ def test_inplace_math(self): def test_reduce(self): v = Variable(['x', 'y'], self.d, {'ignored': 'attributes'}) - assert_identical(v.reduce(np.std, 'x'), - Variable(['y'], self.d.std(axis=0))) - assert_identical(v.reduce(np.std, axis=0), - v.reduce(np.std, dim='x')) - assert_identical(v.reduce(np.std, ['y', 'x']), - Variable([], self.d.std(axis=(0, 1)))) - assert_identical(v.reduce(np.std), - Variable([], self.d.std())) + assert_identical( + v.reduce(np.std, 'x'), Variable(['y'], self.d.std(axis=0))) + assert_identical(v.reduce(np.std, axis=0), v.reduce(np.std, dim='x')) + assert_identical( + v.reduce(np.std, ['y', 'x']), Variable( + [], self.d.std(axis=(0, 1)))) + assert_identical(v.reduce(np.std), Variable([], self.d.std())) assert_identical( v.reduce(np.mean, 'x').reduce(np.std, 'y'), Variable([], self.d.mean(axis=0).std())) @@ -1365,8 +1380,9 @@ def test_reduce(self): with raises_regex(ValueError, 'cannot supply both'): v.mean(dim='x', axis=0) - @pytest.mark.skipif(LooseVersion(np.__version__) < LooseVersion('1.10.0'), - reason='requires numpy version 1.10.0 or later') + @pytest.mark.skipif( + LooseVersion(np.__version__) < LooseVersion('1.10.0'), + reason='requires numpy version 1.10.0 or later') def test_quantile(self): v = Variable(['x', 'y'], self.d) for q in [0.25, [0.50], [0.25, 0.75]]: @@ -1374,8 +1390,8 @@ def test_quantile(self): [None, 'x', ['x'], ['x', 'y']]): actual = v.quantile(q, dim=dim) - expected = np.nanpercentile(self.d, np.array(q) * 100, - axis=axis) + expected = np.nanpercentile( + self.d, np.array(q) * 100, axis=axis) np.testing.assert_allclose(actual.values, expected) @requires_dask @@ -1433,10 +1449,10 @@ def test_reduce_funcs(self): assert_identical(np.mean(v), Variable([], 2)) assert_identical(v.prod(), Variable([], 6)) - assert_identical(v.cumsum(axis=0), - Variable('x', np.array([1, 1, 3, 6]))) - assert_identical(v.cumprod(axis=0), - Variable('x', np.array([1, 1, 2, 6]))) + assert_identical( + v.cumsum(axis=0), Variable('x', np.array([1, 1, 3, 6]))) + assert_identical( + v.cumprod(axis=0), Variable('x', np.array([1, 1, 2, 6]))) assert_identical(v.var(), Variable([], 2.0 / 3)) if LooseVersion(np.__version__) < '1.9': @@ -1452,8 +1468,7 @@ def test_reduce_funcs(self): v = Variable('t', pd.date_range('2000-01-01', periods=3)) with pytest.raises(NotImplementedError): v.max(skipna=True) - assert_identical( - v.max(), Variable([], pd.Timestamp('2000-01-03'))) + assert_identical(v.max(), Variable([], pd.Timestamp('2000-01-03'))) def test_reduce_keep_attrs(self): _attrs = {'units': 'test', 'long_name': 'testing'} @@ -1515,36 +1530,43 @@ def assert_assigned_2d(array, key_x, key_y, values): assert_array_equal(expected, v) # 1d vectorized indexing - assert_assigned_2d(np.random.randn(4, 3), - key_x=Variable(['a'], [0, 1]), - key_y=Variable(['a'], [0, 1]), - values=0) - assert_assigned_2d(np.random.randn(4, 3), - key_x=Variable(['a'], [0, 1]), - key_y=Variable(['a'], [0, 1]), - values=Variable((), 0)) - assert_assigned_2d(np.random.randn(4, 3), - key_x=Variable(['a'], [0, 1]), - key_y=Variable(['a'], [0, 1]), - values=Variable(('a'), [3, 2])) - assert_assigned_2d(np.random.randn(4, 3), - key_x=slice(None), - key_y=Variable(['a'], [0, 1]), - values=Variable(('a'), [3, 2])) + assert_assigned_2d( + np.random.randn(4, 3), + key_x=Variable(['a'], [0, 1]), + key_y=Variable(['a'], [0, 1]), + values=0) + assert_assigned_2d( + np.random.randn(4, 3), + key_x=Variable(['a'], [0, 1]), + key_y=Variable(['a'], [0, 1]), + values=Variable((), 0)) + assert_assigned_2d( + np.random.randn(4, 3), + key_x=Variable(['a'], [0, 1]), + key_y=Variable(['a'], [0, 1]), + values=Variable(('a'), [3, 2])) + assert_assigned_2d( + np.random.randn(4, 3), + key_x=slice(None), + key_y=Variable(['a'], [0, 1]), + values=Variable(('a'), [3, 2])) # 2d-vectorized indexing - assert_assigned_2d(np.random.randn(4, 3), - key_x=Variable(['a', 'b'], [[0, 1]]), - key_y=Variable(['a', 'b'], [[1, 0]]), - values=0) - assert_assigned_2d(np.random.randn(4, 3), - key_x=Variable(['a', 'b'], [[0, 1]]), - key_y=Variable(['a', 'b'], [[1, 0]]), - values=[0]) - assert_assigned_2d(np.random.randn(5, 4), - key_x=Variable(['a', 'b'], [[0, 1], [2, 3]]), - key_y=Variable(['a', 'b'], [[1, 0], [3, 3]]), - values=[2, 3]) + assert_assigned_2d( + np.random.randn(4, 3), + key_x=Variable(['a', 'b'], [[0, 1]]), + key_y=Variable(['a', 'b'], [[1, 0]]), + values=0) + assert_assigned_2d( + np.random.randn(4, 3), + key_x=Variable(['a', 'b'], [[0, 1]]), + key_y=Variable(['a', 'b'], [[1, 0]]), + values=[0]) + assert_assigned_2d( + np.random.randn(5, 4), + key_x=Variable(['a', 'b'], [[0, 1], [2, 3]]), + key_y=Variable(['a', 'b'], [[1, 0], [3, 3]]), + values=[2, 3]) # vindex with slice v = Variable(['x', 'y', 'z'], np.ones((4, 3, 2))) @@ -1622,8 +1644,9 @@ def test_getitem_with_mask_nd_indexer(self): import dask.array as da v = Variable(['x'], da.arange(3, chunks=3)) indexer = Variable(('x', 'y'), [[0, -1], [-1, 2]]) - assert_identical(v._getitem_with_mask(indexer, fill_value=-1), - self.cls(('x', 'y'), [[0, -1], [-1, 2]])) + assert_identical( + v._getitem_with_mask(indexer, fill_value=-1), + self.cls(('x', 'y'), [[0, -1], [-1, 2]])) class TestIndexVariable(TestCase, VariableSubclassTestCases): @@ -1661,16 +1684,16 @@ def test_name(self): coord.name = 'y' def test_level_names(self): - midx = pd.MultiIndex.from_product([['a', 'b'], [1, 2]], - names=['level_1', 'level_2']) + midx = pd.MultiIndex.from_product( + [['a', 'b'], [1, 2]], names=['level_1', 'level_2']) x = IndexVariable('x', midx) assert x.level_names == midx.names assert IndexVariable('y', [10.0]).level_names is None def test_get_level_variable(self): - midx = pd.MultiIndex.from_product([['a', 'b'], [1, 2]], - names=['level_1', 'level_2']) + midx = pd.MultiIndex.from_product( + [['a', 'b'], [1, 2]], names=['level_1', 'level_2']) x = IndexVariable('x', midx) level_1 = IndexVariable('x', midx.get_level_values('level_1')) assert_identical(x.get_level_variable('level_1'), level_1) @@ -1680,8 +1703,10 @@ def test_get_level_variable(self): def test_concat_periods(self): periods = pd.period_range('2000-01-01', periods=10) - coords = [IndexVariable('t', periods[:5]), - IndexVariable('t', periods[5:])] + coords = [ + IndexVariable('t', periods[:5]), + IndexVariable('t', periods[5:]) + ] expected = IndexVariable('t', periods) actual = IndexVariable.concat(coords, dim='t') assert actual.identical(expected) @@ -1728,12 +1753,14 @@ class TestAsCompatibleData(TestCase): def test_unchanged_types(self): types = (np.asarray, PandasIndexAdapter, indexing.LazilyIndexedArray) for t in types: - for data in [np.arange(3), - pd.date_range('2000-01-01', periods=3), - pd.date_range('2000-01-01', periods=3).values]: + for data in [ + np.arange(3), + pd.date_range('2000-01-01', periods=3), + pd.date_range('2000-01-01', periods=3).values + ]: x = t(data) assert source_ndarray(x) is \ - source_ndarray(as_compatible_data(x)) + source_ndarray(as_compatible_data(x)) def test_converted_types(self): for input_array in [[[0, 1, 2]], pd.DataFrame([[0, 1, 2]])]: @@ -1784,8 +1811,12 @@ def test_datetime(self): def test_full_like(self): # For more thorough tests, see test_variable.py - orig = Variable(dims=('x', 'y'), data=[[1.5, 2.0], [3.1, 4.3]], - attrs={'foo': 'bar'}) + orig = Variable( + dims=('x', 'y'), + data=[[1.5, 2.0], [3.1, 4.3]], + attrs={ + 'foo': 'bar' + }) expect = orig.copy(deep=True) expect.values = [[2.0, 2.0], [2.0, 2.0]] @@ -1798,8 +1829,12 @@ def test_full_like(self): @requires_dask def test_full_like_dask(self): - orig = Variable(dims=('x', 'y'), data=[[1.5, 2.0], [3.1, 4.3]], - attrs={'foo': 'bar'}).chunk(((1, 1), (2,))) + orig = Variable( + dims=('x', 'y'), + data=[[1.5, 2.0], [3.1, 4.3]], + attrs={ + 'foo': 'bar' + }).chunk(((1, 1), (2, ))) def check(actual, expect_dtype, expect_values): assert actual.dtype == expect_dtype @@ -1809,11 +1844,11 @@ def check(actual, expect_dtype, expect_values): assert actual.chunks == orig.chunks assert_array_equal(actual.values, expect_values) - check(full_like(orig, 2), - orig.dtype, np.full_like(orig.values, 2)) + check(full_like(orig, 2), orig.dtype, np.full_like(orig.values, 2)) # override dtype - check(full_like(orig, True, dtype=bool), - bool, np.full_like(orig.values, True, dtype=bool)) + check( + full_like(orig, True, dtype=bool), bool, + np.full_like(orig.values, True, dtype=bool)) # Check that there's no array stored inside dask # (e.g. we didn't create a numpy array and then we chunked it!) @@ -1826,20 +1861,26 @@ def check(actual, expect_dtype, expect_values): assert not isinstance(v, np.ndarray) def test_zeros_like(self): - orig = Variable(dims=('x', 'y'), data=[[1.5, 2.0], [3.1, 4.3]], - attrs={'foo': 'bar'}) - assert_identical(zeros_like(orig), - full_like(orig, 0)) - assert_identical(zeros_like(orig, dtype=int), - full_like(orig, 0, dtype=int)) + orig = Variable( + dims=('x', 'y'), + data=[[1.5, 2.0], [3.1, 4.3]], + attrs={ + 'foo': 'bar' + }) + assert_identical(zeros_like(orig), full_like(orig, 0)) + assert_identical( + zeros_like(orig, dtype=int), full_like(orig, 0, dtype=int)) def test_ones_like(self): - orig = Variable(dims=('x', 'y'), data=[[1.5, 2.0], [3.1, 4.3]], - attrs={'foo': 'bar'}) - assert_identical(ones_like(orig), - full_like(orig, 1)) - assert_identical(ones_like(orig, dtype=int), - full_like(orig, 1, dtype=int)) + orig = Variable( + dims=('x', 'y'), + data=[[1.5, 2.0], [3.1, 4.3]], + attrs={ + 'foo': 'bar' + }) + assert_identical(ones_like(orig), full_like(orig, 1)) + assert_identical( + ones_like(orig, dtype=int), full_like(orig, 1, dtype=int)) def test_unsupported_type(self): # Non indexable type @@ -1872,8 +1913,8 @@ def setUp(self): self.d = np.random.random((10, 3)).astype(np.float64) def check_orthogonal_indexing(self, v): - assert np.allclose(v.isel(x=[8, 3], y=[2, 1]), - self.d[[8, 3]][:, [2, 1]]) + assert np.allclose( + v.isel(x=[8, 3], y=[2, 1]), self.d[[8, 3]][:, [2, 1]]) def check_vectorized_indexing(self, v): ind_x = Variable('z', [0, 2]) @@ -1886,8 +1927,9 @@ def test_NumpyIndexingAdapter(self): self.check_vectorized_indexing(v) # could not doubly wrapping with raises_regex(TypeError, 'NumpyIndexingAdapter only wraps '): - v = Variable(dims=('x', 'y'), data=NumpyIndexingAdapter( - NumpyIndexingAdapter(self.d))) + v = Variable( + dims=('x', 'y'), + data=NumpyIndexingAdapter(NumpyIndexingAdapter(self.d))) def test_LazilyIndexedArray(self): v = Variable(dims=('x', 'y'), data=LazilyIndexedArray(self.d)) @@ -1895,12 +1937,14 @@ def test_LazilyIndexedArray(self): with raises_regex(NotImplementedError, 'Vectorized indexing for '): self.check_vectorized_indexing(v) # doubly wrapping - v = Variable(dims=('x', 'y'), - data=LazilyIndexedArray(LazilyIndexedArray(self.d))) + v = Variable( + dims=('x', 'y'), + data=LazilyIndexedArray(LazilyIndexedArray(self.d))) self.check_orthogonal_indexing(v) # hierarchical wrapping - v = Variable(dims=('x', 'y'), - data=LazilyIndexedArray(NumpyIndexingAdapter(self.d))) + v = Variable( + dims=('x', 'y'), + data=LazilyIndexedArray(NumpyIndexingAdapter(self.d))) self.check_orthogonal_indexing(v) def test_CopyOnWriteArray(self): @@ -1908,8 +1952,8 @@ def test_CopyOnWriteArray(self): self.check_orthogonal_indexing(v) self.check_vectorized_indexing(v) # doubly wrapping - v = Variable(dims=('x', 'y'), - data=CopyOnWriteArray(LazilyIndexedArray(self.d))) + v = Variable( + dims=('x', 'y'), data=CopyOnWriteArray(LazilyIndexedArray(self.d))) self.check_orthogonal_indexing(v) with raises_regex(NotImplementedError, 'Vectorized indexing for '): self.check_vectorized_indexing(v) @@ -1919,8 +1963,8 @@ def test_MemoryCachedArray(self): self.check_orthogonal_indexing(v) self.check_vectorized_indexing(v) # doubly wrapping - v = Variable(dims=('x', 'y'), - data=CopyOnWriteArray(MemoryCachedArray(self.d))) + v = Variable( + dims=('x', 'y'), data=CopyOnWriteArray(MemoryCachedArray(self.d))) self.check_orthogonal_indexing(v) self.check_vectorized_indexing(v) @@ -1932,7 +1976,7 @@ def test_DaskIndexingAdapter(self): self.check_orthogonal_indexing(v) self.check_vectorized_indexing(v) # doubly wrapping - v = Variable(dims=('x', 'y'), - data=CopyOnWriteArray(DaskIndexingAdapter(da))) + v = Variable( + dims=('x', 'y'), data=CopyOnWriteArray(DaskIndexingAdapter(da))) self.check_orthogonal_indexing(v) self.check_vectorized_indexing(v) From 2b89cbd7355d254fa1fbe7f873acd7ca9c55b86c Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 17:30:28 -0500 Subject: [PATCH 27/41] lint final --- xarray/tests/test_accessors.py | 74 ++++++++++++++++++++++++---------- xarray/tests/test_dataarray.py | 4 +- xarray/tests/test_ufuncs.py | 32 +++++++++------ xarray/tests/test_variable.py | 10 ++--- 4 files changed, 79 insertions(+), 41 deletions(-) diff --git a/xarray/tests/test_accessors.py b/xarray/tests/test_accessors.py index 454c037fde1..be1cd66e417 100644 --- a/xarray/tests/test_accessors.py +++ b/xarray/tests/test_accessors.py @@ -7,7 +7,7 @@ import pandas as pd from . import (TestCase, requires_dask, raises_regex, assert_equal, - assert_identical, assert_allclose, assert_array_equal) + assert_array_equal) class TestDatetimeAccessor(TestCase): @@ -18,24 +18,53 @@ def setUp(self): lats = np.linspace(0, 20, 10) self.times = pd.date_range(start="2000/01/01", freq='H', periods=nt) - self.data = xr.DataArray(data, coords=[lons, lats, self.times], - dims=['lon', 'lat', 'time'], name='data') + self.data = xr.DataArray( + data, + coords=[lons, lats, self.times], + dims=['lon', 'lat', 'time'], + name='data') self.times_arr = np.random.choice(self.times, size=(10, 10, nt)) - self.times_data = xr.DataArray(self.times_arr, - coords=[lons, lats, self.times], - dims=['lon', 'lat', 'time'], - name='data') + self.times_data = xr.DataArray( + self.times_arr, + coords=[lons, lats, self.times], + dims=['lon', 'lat', 'time'], + name='data') def test_field_access(self): - years = xr.DataArray(self.times.year, name='year', - coords=[self.times, ], dims=['time', ]) - months = xr.DataArray(self.times.month, name='month', - coords=[self.times, ], dims=['time', ]) - days = xr.DataArray(self.times.day, name='day', - coords=[self.times, ], dims=['time', ]) - hours = xr.DataArray(self.times.hour, name='hour', - coords=[self.times, ], dims=['time', ]) + years = xr.DataArray( + self.times.year, + name='year', + coords=[ + self.times, + ], + dims=[ + 'time', + ]) + months = xr.DataArray( + self.times.month, + name='month', + coords=[ + self.times, + ], + dims=[ + 'time', + ]) + days = xr.DataArray( + self.times.day, name='day', coords=[ + self.times, + ], dims=[ + 'time', + ]) + hours = xr.DataArray( + self.times.hour, + name='hour', + coords=[ + self.times, + ], + dims=[ + 'time', + ]) assert_equal(years, self.data.time.dt.year) assert_equal(months, self.data.time.dt.month) @@ -59,10 +88,11 @@ def test_dask_field_access(self): days = self.times_data.dt.day dask_times_arr = da.from_array(self.times_arr, chunks=(5, 5, 50)) - dask_times_2d = xr.DataArray(dask_times_arr, - coords=self.data.coords, - dims=self.data.dims, - name='data') + dask_times_2d = xr.DataArray( + dask_times_arr, + coords=self.data.coords, + dims=self.data.dims, + name='data') dask_year = dask_times_2d.dt.year dask_month = dask_times_2d.dt.month dask_day = dask_times_2d.dt.day @@ -90,8 +120,10 @@ def test_dask_field_access(self): def test_seasons(self): dates = pd.date_range(start="2000/01/01", freq="M", periods=12) dates = xr.DataArray(dates) - seasons = ["DJF", "DJF", "MAM", "MAM", "MAM", "JJA", "JJA", "JJA", - "SON", "SON", "SON", "DJF"] + seasons = [ + "DJF", "DJF", "MAM", "MAM", "MAM", "JJA", "JJA", "JJA", "SON", + "SON", "SON", "DJF" + ] seasons = xr.DataArray(seasons) assert_array_equal(seasons.values, dates.dt.season.values) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 63be8018a82..b00276b3cff 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -2879,7 +2879,7 @@ def test_to_and_from_iris(self): iris.coords.CellMethod(method='mean', coords=('height',), intervals=(), - comments=('A cell method',)),) + comments=('A cell method',))) for coord, orginal_key in zip((actual.coords()), original.coords): original_coord = original.coords[orginal_key] @@ -2951,7 +2951,7 @@ def test_to_and_from_iris_dask(self): iris.coords.CellMethod(method='mean', coords=('height',), intervals=(), - comments=('A cell method',)),) + comments=('A cell method',))) for coord, orginal_key in zip((actual.coords()), original.coords): original_coord = original.coords[orginal_key] diff --git a/xarray/tests/test_ufuncs.py b/xarray/tests/test_ufuncs.py index 961312370b7..1b13b1d66cb 100644 --- a/xarray/tests/test_ufuncs.py +++ b/xarray/tests/test_ufuncs.py @@ -8,9 +8,7 @@ import xarray.ufuncs as xu import xarray as xr -from . import ( - TestCase, raises_regex, assert_equal, assert_identical, assert_array_equal, - assert_equal) +from . import (TestCase, raises_regex, assert_identical, assert_array_equal) class TestOps(TestCase): @@ -22,20 +20,28 @@ def assert_identical(self, a, b): assert_array_equal(a, b) def test_unary(self): - args = [0, - np.zeros(2), - xr.Variable(['x'], [0, 0]), - xr.DataArray([0, 0], dims='x'), - xr.Dataset({'y': ('x', [0, 0])})] + args = [ + 0, + np.zeros(2), + xr.Variable(['x'], [0, 0]), + xr.DataArray([0, 0], dims='x'), + xr.Dataset({ + 'y': ('x', [0, 0]) + }) + ] for a in args: self.assert_identical(a + 1, xu.cos(a)) def test_binary(self): - args = [0, - np.zeros(2), - xr.Variable(['x'], [0, 0]), - xr.DataArray([0, 0], dims='x'), - xr.Dataset({'y': ('x', [0, 0])})] + args = [ + 0, + np.zeros(2), + xr.Variable(['x'], [0, 0]), + xr.DataArray([0, 0], dims='x'), + xr.Dataset({ + 'y': ('x', [0, 0]) + }) + ] for n, t1 in enumerate(args): for t2 in args[n:]: self.assert_identical(t2 + 1, xu.maximum(t1, t2 + 1)) diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index 709b306986a..295ec6e86c3 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -1187,12 +1187,12 @@ def test_transpose(self): def test_transpose_0d(self): for value in [ - 3.5, + 3.5, ('a', 1), - np.datetime64('2000-01-01'), - np.timedelta64(1, 'h'), - None, - object(), + np.datetime64('2000-01-01'), + np.timedelta64(1, 'h'), + None, + object(), ]: variable = Variable([], value) actual = variable.transpose() From 80b921cf82b0a5e1c2ae2741011d7c10e03be84f Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 17:37:11 -0500 Subject: [PATCH 28/41] undo some of the yapf crazyness --- xarray/tests/test_accessors.py | 72 ++++++++++------------------------ 1 file changed, 20 insertions(+), 52 deletions(-) diff --git a/xarray/tests/test_accessors.py b/xarray/tests/test_accessors.py index be1cd66e417..30ea1a88c7a 100644 --- a/xarray/tests/test_accessors.py +++ b/xarray/tests/test_accessors.py @@ -18,53 +18,24 @@ def setUp(self): lats = np.linspace(0, 20, 10) self.times = pd.date_range(start="2000/01/01", freq='H', periods=nt) - self.data = xr.DataArray( - data, - coords=[lons, lats, self.times], - dims=['lon', 'lat', 'time'], - name='data') + self.data = xr.DataArray(data, coords=[lons, lats, self.times], + dims=['lon', 'lat', 'time'], name='data') self.times_arr = np.random.choice(self.times, size=(10, 10, nt)) - self.times_data = xr.DataArray( - self.times_arr, - coords=[lons, lats, self.times], - dims=['lon', 'lat', 'time'], - name='data') + self.times_data = xr.DataArray(self.times_arr, + coords=[lons, lats, self.times], + dims=['lon', 'lat', 'time'], + name='data') def test_field_access(self): - years = xr.DataArray( - self.times.year, - name='year', - coords=[ - self.times, - ], - dims=[ - 'time', - ]) - months = xr.DataArray( - self.times.month, - name='month', - coords=[ - self.times, - ], - dims=[ - 'time', - ]) - days = xr.DataArray( - self.times.day, name='day', coords=[ - self.times, - ], dims=[ - 'time', - ]) - hours = xr.DataArray( - self.times.hour, - name='hour', - coords=[ - self.times, - ], - dims=[ - 'time', - ]) + years = xr.DataArray(self.times.year, name='year', + coords=[self.times, ], dims=['time', ]) + months = xr.DataArray(self.times.month, name='month', + coords=[self.times, ], dims=['time', ]) + days = xr.DataArray(self.times.day, name='day', + coords=[self.times, ], dims=['time', ]) + hours = xr.DataArray(self.times.hour, name='hour', + coords=[self.times, ], dims=['time', ]) assert_equal(years, self.data.time.dt.year) assert_equal(months, self.data.time.dt.month) @@ -88,11 +59,10 @@ def test_dask_field_access(self): days = self.times_data.dt.day dask_times_arr = da.from_array(self.times_arr, chunks=(5, 5, 50)) - dask_times_2d = xr.DataArray( - dask_times_arr, - coords=self.data.coords, - dims=self.data.dims, - name='data') + dask_times_2d = xr.DataArray(dask_times_arr, + coords=self.data.coords, + dims=self.data.dims, + name='data') dask_year = dask_times_2d.dt.year dask_month = dask_times_2d.dt.month dask_day = dask_times_2d.dt.day @@ -120,10 +90,8 @@ def test_dask_field_access(self): def test_seasons(self): dates = pd.date_range(start="2000/01/01", freq="M", periods=12) dates = xr.DataArray(dates) - seasons = [ - "DJF", "DJF", "MAM", "MAM", "MAM", "JJA", "JJA", "JJA", "SON", - "SON", "SON", "DJF" - ] + seasons = ["DJF", "DJF", "MAM", "MAM", "MAM", "JJA", "JJA", "JJA", + "SON", "SON", "SON", "DJF"] seasons = xr.DataArray(seasons) assert_array_equal(seasons.values, dates.dt.season.values) From 7e7c71de7a2527bd2c0f126a124ea720df397668 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 19:07:53 -0500 Subject: [PATCH 29/41] @Zac-HD dtype --- xarray/core/dtypes.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/xarray/core/dtypes.py b/xarray/core/dtypes.py index eeb60243362..ccbe48edc32 100644 --- a/xarray/core/dtypes.py +++ b/xarray/core/dtypes.py @@ -22,9 +22,11 @@ def maybe_promote(dtype): # N.B. these casting rules should match pandas if np.issubdtype(dtype, np.floating): fill_value = np.nan - elif np.issubdtype(dtype, np.signedinteger): - # convert to floating point so NaN is valid - dtype = float + elif np.issubdtype(dtype, np.integer): + if dtype.itemsize <= 2: + dtype = np.float32 + else: + dtype = np.float64 fill_value = np.nan elif np.issubdtype(dtype, np.complexfloating): fill_value = np.nan + np.nan * 1j From bf6084480893705180656fafdd057b200f462797 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 19:26:55 -0500 Subject: [PATCH 30/41] remove more yapf overeagerness --- xarray/tests/test_dataset.py | 1914 ++++++++++++---------------------- 1 file changed, 692 insertions(+), 1222 deletions(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index ad5052e4189..3f1aa8436e6 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -37,11 +37,9 @@ def create_test_data(seed=None): rs = np.random.RandomState(seed) - _vars = { - 'var1': ['dim1', 'dim2'], - 'var2': ['dim1', 'dim2'], - 'var3': ['dim3', 'dim1'] - } + _vars = {'var1': ['dim1', 'dim2'], + 'var2': ['dim1', 'dim2'], + 'var3': ['dim3', 'dim1']} _dims = {'dim1': 8, 'dim2': 9, 'dim3': 10} obj = Dataset() @@ -51,18 +49,16 @@ def create_test_data(seed=None): for v, dims in sorted(_vars.items()): data = rs.normal(size=tuple(_dims[d] for d in dims)) obj[v] = (dims, data, {'foo': 'variable'}) - obj.coords['numbers'] = ('dim3', - np.array( - [0, 1, 2, 0, 0, 1, 1, 2, 2, 3], - dtype='int64')) + obj.coords['numbers'] = ('dim3', np.array([0, 1, 2, 0, 0, 1, 1, 2, 2, 3], + dtype='int64')) obj.encoding = {'foo': 'bar'} assert all(obj.data.flags.writeable for obj in obj.variables.values()) return obj def create_test_multiindex(): - mindex = pd.MultiIndex.from_product( - [['a', 'b'], [1, 2]], names=('level_1', 'level_2')) + mindex = pd.MultiIndex.from_product([['a', 'b'], [1, 2]], + names=('level_1', 'level_2')) return Dataset({}, {'x': mindex}) @@ -84,9 +80,8 @@ def lazy_inaccessible(k, v): return v data = indexing.LazilyIndexedArray(InaccessibleArray(v.values)) return Variable(v.dims, data, v.attrs) - - return dict((k, lazy_inaccessible(k, v)) - for k, v in iteritems(self._variables)) + return dict((k, lazy_inaccessible(k, v)) for + k, v in iteritems(self._variables)) class TestDataset(TestCase): @@ -158,7 +153,8 @@ def test_repr_multiindex(self): # verify that long level names are not truncated mindex = pd.MultiIndex.from_product( - [['a', 'b'], [1, 2]], names=('a_quite_long_level_name', 'level_2')) + [['a', 'b'], [1, 2]], + names=('a_quite_long_level_name', 'level_2')) data = Dataset({}, {'x': mindex}) expected = dedent("""\ @@ -256,9 +252,8 @@ def test_constructor(self): def test_constructor_invalid_dims(self): # regression for GH1120 with pytest.raises(MergeError): - Dataset( - data_vars=dict(v=('y', [1, 2, 3, 4])), - coords=dict(y=DataArray([.1, .2, .3, .4], dims='x'))) + Dataset(data_vars=dict(v=('y', [1, 2, 3, 4])), + coords=dict(y=DataArray([.1, .2, .3, .4], dims='x'))) def test_constructor_1d(self): expected = Dataset({'x': (['x'], 5.0 + np.arange(5))}) @@ -278,12 +273,9 @@ class Arbitrary(object): pass d = pd.Timestamp('2000-01-01T12') - args = [ - True, None, 3.4, np.nan, 'hello', u'uni', b'raw', - np.datetime64('2000-01-01'), d, - d.to_pydatetime(), - Arbitrary() - ] + args = [True, None, 3.4, np.nan, 'hello', u'uni', b'raw', + np.datetime64('2000-01-01'), d, d.to_pydatetime(), + Arbitrary()] for arg in args: print(arg) expected = Dataset({'x': ([], arg)}) @@ -299,12 +291,9 @@ def test_constructor_auto_align(self): b = DataArray([3, 4], [('x', [1, 2])]) # verify align uses outer join - expected = Dataset({ - 'a': ('x', [1, 2, np.nan]), - 'b': ('x', [np.nan, 3, 4]) - }, { - 'x': [0, 1, 2] - }) + expected = Dataset({'a': ('x', [1, 2, np.nan]), + 'b': ('x', [np.nan, 3, 4])}, + {'x': [0, 1, 2]}) actual = Dataset({'a': a, 'b': b}) assert_identical(expected, actual) @@ -330,8 +319,9 @@ def test_constructor_auto_align(self): def test_constructor_pandas_sequence(self): ds = self.make_example_math_dataset() - pandas_objs = OrderedDict((var_name, ds[var_name].to_pandas()) - for var_name in ['foo', 'bar']) + pandas_objs = OrderedDict( + (var_name, ds[var_name].to_pandas()) for var_name in ['foo', 'bar'] + ) ds_based_on_pandas = Dataset(pandas_objs, ds.coords, attrs=ds.attrs) del ds_based_on_pandas['x'] assert_equal(ds, ds_based_on_pandas) @@ -358,9 +348,8 @@ def test_constructor_pandas_single(self): assert_array_equal(ds_based_on_pandas[dim], pandas_obj[dim]) def test_constructor_compat(self): - data = OrderedDict([('x', DataArray(0, coords={ - 'y': 1 - })), ('y', ('z', [1, 1, 1]))]) + data = OrderedDict([('x', DataArray(0, coords={'y': 1})), + ('y', ('z', [1, 1, 1]))]) with pytest.raises(MergeError): Dataset(data, compat='equals') expected = Dataset({'x': 0}, {'y': ('z', [1, 1, 1])}) @@ -369,33 +358,21 @@ def test_constructor_compat(self): actual = Dataset(data, compat='broadcast_equals') assert_identical(expected, actual) - data = OrderedDict([('y', ('z', [1, 1, 1])), ('x', - DataArray( - 0, coords={ - 'y': 1 - }))]) + data = OrderedDict([('y', ('z', [1, 1, 1])), + ('x', DataArray(0, coords={'y': 1}))]) actual = Dataset(data) assert_identical(expected, actual) - original = Dataset({ - 'a': (('x', 'y'), np.ones((2, 3))) - }, { - 'c': (('x', 'y'), np.zeros((2, 3))), - 'x': [0, 1] - }) - expected = Dataset({ - 'a': ('x', np.ones(2)), - 'b': ('y', np.ones(3)) - }, { - 'c': (('x', 'y'), np.zeros((2, 3))), - 'x': [0, 1] - }) + original = Dataset({'a': (('x', 'y'), np.ones((2, 3)))}, + {'c': (('x', 'y'), np.zeros((2, 3))), 'x': [0, 1]}) + expected = Dataset({'a': ('x', np.ones(2)), + 'b': ('y', np.ones(3))}, + {'c': (('x', 'y'), np.zeros((2, 3))), 'x': [0, 1]}) # use an OrderedDict to ensure test results are reproducible; otherwise # the order of appearance of x and y matters for the order of # dimensions in 'c' - actual = Dataset( - OrderedDict([('a', original['a'][:, 0]), - ('b', original['a'][0].drop('x'))])) + actual = Dataset(OrderedDict([('a', original['a'][:, 0]), + ('b', original['a'][0].drop('x'))])) assert_identical(expected, actual) data = {'x': DataArray(0, coords={'y': 3}), 'y': ('z', [1, 1, 1])} @@ -415,15 +392,16 @@ def test_constructor_with_coords(self): assert not ds.data_vars self.assertItemsEqual(ds.coords.keys(), ['a']) - mindex = pd.MultiIndex.from_product( - [['a', 'b'], [1, 2]], names=('level_1', 'level_2')) + mindex = pd.MultiIndex.from_product([['a', 'b'], [1, 2]], + names=('level_1', 'level_2')) with raises_regex(ValueError, 'conflicting MultiIndex'): Dataset({}, {'x': mindex, 'y': mindex}) Dataset({}, {'x': mindex, 'level_1': range(4)}) def test_properties(self): ds = create_test_data() - assert ds.dims == {'dim1': 8, 'dim2': 9, 'dim3': 10, 'time': 20} + assert ds.dims == \ + {'dim1': 8, 'dim2': 9, 'dim3': 10, 'time': 20} assert list(ds.dims) == sorted(ds.dims) assert ds.sizes == ds.dims @@ -462,33 +440,20 @@ def test_properties(self): assert 'dim1' not in ds.coords assert len(ds.coords) == 4 - assert Dataset({ - 'x': np.int64(1), - 'y': np.float32([1, 2]) - }).nbytes == 16 + assert Dataset({'x': np.int64(1), + 'y': np.float32([1, 2])}).nbytes == 16 def test_get_index(self): - ds = Dataset( - { - 'foo': (('x', 'y'), np.zeros((2, 3))) - }, coords={ - 'x': ['a', 'b'] - }) + ds = Dataset({'foo': (('x', 'y'), np.zeros((2, 3)))}, + coords={'x': ['a', 'b']}) assert ds.get_index('x').equals(pd.Index(['a', 'b'])) assert ds.get_index('y').equals(pd.Index([0, 1, 2])) with pytest.raises(KeyError): ds.get_index('z') def test_attr_access(self): - ds = Dataset( - { - 'tmin': ('x', [42], { - 'units': 'Celcius' - }) - }, - attrs={ - 'title': 'My test data' - }) + ds = Dataset({'tmin': ('x', [42], {'units': 'Celcius'})}, + attrs={'title': 'My test data'}) assert_identical(ds.tmin, ds['tmin']) assert_identical(ds.tmin.x, ds.x) @@ -506,16 +471,10 @@ def test_attr_access(self): def test_variable(self): a = Dataset() d = np.random.random((10, 3)) - a['foo'] = (( - 'time', - 'x', - ), d) + a['foo'] = (('time', 'x',), d) assert 'foo' in a.variables assert 'foo' in a - a['bar'] = (( - 'time', - 'x', - ), d) + a['bar'] = (('time', 'x',), d) # order of creation is preserved assert list(a.variables) == ['foo', 'bar'] assert_array_equal(a['foo'].values, d) @@ -525,7 +484,7 @@ def test_variable(self): def test_modify_inplace(self): a = Dataset() - vec = np.random.random((10, )) + vec = np.random.random((10,)) attributes = {'foo': 'bar'} a['x'] = ('x', vec, attributes) assert 'x' in a.coords @@ -541,10 +500,7 @@ def test_modify_inplace(self): with pytest.raises(ValueError): # now it shouldn't, since there is a conflicting length a['x'] = ('x', vec[:4]) - arr = np.random.random(( - 10, - 1, - )) + arr = np.random.random((10, 1,)) scal = np.array(0) with pytest.raises(ValueError): a['y'] = ('y', arr) @@ -556,12 +512,12 @@ def test_coords_properties(self): # use an OrderedDict for coordinates to ensure order across python # versions # use int64 for repr consistency on windows - data = Dataset( - OrderedDict([('x', ('x', np.array([-1, -2], 'int64'))), - ('y', ('y', np.array([0, 1, 2], 'int64'))), - ('foo', (['x', 'y'], np.random.randn(2, 3)))]), - OrderedDict([('a', ('x', np.array([4, 5], 'int64'))), - ('b', np.int64(-10))])) + data = Dataset(OrderedDict([('x', ('x', np.array([-1, -2], 'int64'))), + ('y', ('y', np.array([0, 1, 2], 'int64'))), + ('foo', (['x', 'y'], + np.random.randn(2, 3)))]), + OrderedDict([('a', ('x', np.array([4, 5], 'int64'))), + ('b', np.int64(-10))])) assert 4 == len(data.coords) @@ -592,14 +548,10 @@ def test_coords_properties(self): assert {'x': 2, 'y': 3} == data.coords.dims def test_coords_modify(self): - data = Dataset({ - 'x': ('x', [-1, -2]), - 'y': ('y', [0, 1, 2]), - 'foo': (['x', 'y'], np.random.randn(2, 3)) - }, { - 'a': ('x', [4, 5]), - 'b': -10 - }) + data = Dataset({'x': ('x', [-1, -2]), + 'y': ('y', [0, 1, 2]), + 'foo': (['x', 'y'], np.random.randn(2, 3))}, + {'a': ('x', [4, 5]), 'b': -10}) actual = data.copy(deep=True) actual.coords['x'] = ('x', ['a', 'b']) @@ -642,22 +594,15 @@ def test_coords_setitem_multiindex(self): data.coords['level_1'] = range(4) def test_coords_set(self): - one_coord = Dataset({ - 'x': ('x', [0]), - 'yy': ('x', [1]), - 'zzz': ('x', [2]) - }) - two_coords = Dataset({ - 'zzz': ('x', [2]) - }, { - 'x': ('x', [0]), - 'yy': ('x', [1]) - }) - all_coords = Dataset(coords={ - 'x': ('x', [0]), - 'yy': ('x', [1]), - 'zzz': ('x', [2]) - }) + one_coord = Dataset({'x': ('x', [0]), + 'yy': ('x', [1]), + 'zzz': ('x', [2])}) + two_coords = Dataset({'zzz': ('x', [2])}, + {'x': ('x', [0]), + 'yy': ('x', [1])}) + all_coords = Dataset(coords={'x': ('x', [0]), + 'yy': ('x', [1]), + 'zzz': ('x', [2])}) actual = one_coord.set_coords('x') assert_identical(one_coord, actual) @@ -699,15 +644,11 @@ def test_coords_to_dataset(self): def test_coords_merge(self): orig_coords = Dataset(coords={'a': ('x', [1, 2]), 'x': [0, 1]}).coords - other_coords = Dataset(coords={ - 'b': ('x', ['a', 'b']), - 'x': [0, 1] - }).coords - expected = Dataset(coords={ - 'a': ('x', [1, 2]), - 'b': ('x', ['a', 'b']), - 'x': [0, 1] - }) + other_coords = Dataset(coords={'b': ('x', ['a', 'b']), + 'x': [0, 1]}).coords + expected = Dataset(coords={'a': ('x', [1, 2]), + 'b': ('x', ['a', 'b']), + 'x': [0, 1]}) actual = orig_coords.merge(other_coords) assert_identical(expected, actual) actual = other_coords.merge(orig_coords) @@ -759,7 +700,7 @@ def test_coords_merge_mismatched_shape(self): def test_data_vars_properties(self): ds = Dataset() - ds['foo'] = (('x', ), [1.0]) + ds['foo'] = (('x',), [1.0]) ds['bar'] = 2.0 assert set(ds.data_vars) == {'foo', 'bar'} @@ -828,7 +769,7 @@ def test_chunk(self): else: assert isinstance(v.data, da.Array) - expected_chunks = {'dim1': (8, ), 'dim2': (9, ), 'dim3': (10, )} + expected_chunks = {'dim1': (8,), 'dim2': (9,), 'dim3': (10,)} assert reblocked.chunks == expected_chunks reblocked = data.chunk({'time': 5, 'dim1': 5, 'dim2': 5, 'dim3': 5}) @@ -925,24 +866,21 @@ def test_isel_fancy(self): pdim1 = [1, 2, 3] pdim2 = [4, 5, 1] pdim3 = [1, 2, 3] - actual = data.isel( - dim1=(('test_coord', ), pdim1), - dim2=(('test_coord', ), pdim2), - dim3=(('test_coord', ), pdim3)) + actual = data.isel(dim1=(('test_coord', ), pdim1), + dim2=(('test_coord', ), pdim2), + dim3=(('test_coord', ), pdim3)) assert 'test_coord' in actual.dims assert actual.coords['test_coord'].shape == (len(pdim1), ) # Should work with DataArray - actual = data.isel( - dim1=DataArray(pdim1, dims='test_coord'), - dim2=(('test_coord', ), pdim2), - dim3=(('test_coord', ), pdim3)) + actual = data.isel(dim1=DataArray(pdim1, dims='test_coord'), + dim2=(('test_coord', ), pdim2), + dim3=(('test_coord', ), pdim3)) assert 'test_coord' in actual.dims assert actual.coords['test_coord'].shape == (len(pdim1), ) - expected = data.isel( - dim1=(('test_coord', ), pdim1), - dim2=(('test_coord', ), pdim2), - dim3=(('test_coord', ), pdim3)) + expected = data.isel(dim1=(('test_coord', ), pdim1), + dim2=(('test_coord', ), pdim2), + dim3=(('test_coord', ), pdim3)) assert_identical(actual, expected) # DataArray with coordinate @@ -957,12 +895,11 @@ def test_isel_fancy(self): assert 'time' in actual.coords assert 'dim2' in actual.coords assert 'dim3' in actual.coords - expected = data.isel( - dim1=(('a', ), pdim1), - dim2=(('b', ), pdim2), - dim3=(('c', ), pdim3)) - expected = expected.assign_coords( - a=idx1['a'], b=idx2['b'], c=idx3['c']) + expected = data.isel(dim1=(('a', ), pdim1), + dim2=(('b', ), pdim2), + dim3=(('c', ), pdim3)) + expected = expected.assign_coords(a=idx1['a'], b=idx2['b'], + c=idx3['c']) assert_identical(actual, expected) idx1 = DataArray(pdim1, dims=['a'], coords={'a': np.random.randn(3)}) @@ -974,28 +911,29 @@ def test_isel_fancy(self): assert 'time' in actual.coords assert 'dim2' in actual.coords assert 'dim3' in actual.coords - expected = data.isel( - dim1=(('a', ), pdim1), - dim2=(('a', ), pdim2), - dim3=(('a', ), pdim3)) + expected = data.isel(dim1=(('a', ), pdim1), + dim2=(('a', ), pdim2), + dim3=(('a', ), pdim3)) expected = expected.assign_coords(a=idx1['a']) assert_identical(actual, expected) - actual = data.isel( - dim1=(('points', ), pdim1), dim2=(('points', ), pdim2)) + actual = data.isel(dim1=(('points', ), pdim1), + dim2=(('points', ), pdim2)) assert 'points' in actual.dims assert 'dim3' in actual.dims assert 'dim3' not in actual.data_vars np.testing.assert_array_equal(data['dim2'][pdim2], actual['dim2']) # test that the order of the indexers doesn't matter - assert_identical( - data.isel(dim1=(('points', ), pdim1), dim2=(('points', ), pdim2)), - data.isel(dim2=(('points', ), pdim2), dim1=(('points', ), pdim1))) + assert_identical(data.isel(dim1=(('points', ), pdim1), + dim2=(('points', ), pdim2)), + data.isel(dim2=(('points', ), pdim2), + dim1=(('points', ), pdim1))) # make sure we're raising errors in the right places - with raises_regex(IndexError, 'Dimensions of indexers mismatch'): - data.isel( - dim1=(('points', ), [1, 2]), dim2=(('points', ), [1, 2, 3])) + with raises_regex(IndexError, + 'Dimensions of indexers mismatch'): + data.isel(dim1=(('points', ), [1, 2]), + dim2=(('points', ), [1, 2, 3])) with raises_regex(TypeError, 'cannot use a Dataset'): data.isel(dim1=Dataset({'points': [1, 2]})) @@ -1010,21 +948,18 @@ def test_isel_fancy(self): stations['dim1s'] = (('station', ), [1, 2, 3]) stations['dim2s'] = (('station', ), [4, 5, 1]) - actual = data.isel(dim1=stations['dim1s'], dim2=stations['dim2s']) + actual = data.isel(dim1=stations['dim1s'], + dim2=stations['dim2s']) assert 'station' in actual.coords assert 'station' in actual.dims - assert_identical(actual['station'].drop(['dim2']), stations['station']) + assert_identical(actual['station'].drop(['dim2']), + stations['station']) with raises_regex(ValueError, 'conflicting values for '): - data.isel( - dim1=DataArray( - [0, 1, 2], dims='station', coords={ - 'station': [0, 1, 2] - }), - dim2=DataArray( - [0, 1, 2], dims='station', coords={ - 'station': [0, 1, 3] - })) + data.isel(dim1=DataArray([0, 1, 2], dims='station', + coords={'station': [0, 1, 2]}), + dim2=DataArray([0, 1, 2], dims='station', + coords={'station': [0, 1, 3]})) # multi-dimensional selection stations = Dataset() @@ -1040,14 +975,15 @@ def test_isel_fancy(self): assert 'dim2' in actual.coords assert 'a' in actual['dim2'].dims - assert_identical(actual['a'].drop(['dim2']), stations['a']) + assert_identical(actual['a'].drop(['dim2']), + stations['a']) assert_identical(actual['b'], stations['b']) expected_var1 = data['var1'].variable[stations['dim1s'].variable, stations['dim2s'].variable] expected_var2 = data['var2'].variable[stations['dim1s'].variable, stations['dim2s'].variable] - expected_var3 = data['var3'].variable[slice(None), stations['dim1s'] - .variable] + expected_var3 = data['var3'].variable[slice(None), + stations['dim1s'].variable] assert_equal(actual['a'].drop('dim2'), stations['a']) assert_array_equal(actual['var1'], expected_var1) assert_array_equal(actual['var2'], expected_var2) @@ -1057,23 +993,15 @@ def test_isel_dataarray(self): """ Test for indexing by DataArray """ data = create_test_data() # indexing with DataArray with same-name coordinates. - indexing_da = DataArray( - np.arange(1, 4), - dims=['dim1'], - coords={ - 'dim1': np.random.randn(3) - }) + indexing_da = DataArray(np.arange(1, 4), dims=['dim1'], + coords={'dim1': np.random.randn(3)}) actual = data.isel(dim1=indexing_da) assert_identical(indexing_da['dim1'], actual['dim1']) assert_identical(data['dim2'], actual['dim2']) # Conflict in the dimension coordinate - indexing_da = DataArray( - np.arange(1, 4), - dims=['dim2'], - coords={ - 'dim2': np.random.randn(3) - }) + indexing_da = DataArray(np.arange(1, 4), dims=['dim2'], + coords={'dim2': np.random.randn(3)}) with raises_regex(IndexError, "dimension coordinate 'dim2'"): actual = data.isel(dim2=indexing_da) # Also the case for DataArray @@ -1083,60 +1011,44 @@ def test_isel_dataarray(self): data['dim2'].isel(dim2=indexing_da) # same name coordinate which does not conflict - indexing_da = DataArray( - np.arange(1, 4), - dims=['dim2'], - coords={ - 'dim2': data['dim2'].values[1:4] - }) + indexing_da = DataArray(np.arange(1, 4), dims=['dim2'], + coords={'dim2': data['dim2'].values[1:4]}) actual = data.isel(dim2=indexing_da) assert_identical(actual['dim2'], indexing_da['dim2']) # Silently drop conflicted (non-dimensional) coordinate of indexer - indexing_da = DataArray( - np.arange(1, 4), - dims=['dim2'], - coords={ - 'dim2': data['dim2'].values[1:4], - 'numbers': ('dim2', np.arange(2, 5)) - }) + indexing_da = DataArray(np.arange(1, 4), dims=['dim2'], + coords={'dim2': data['dim2'].values[1:4], + 'numbers': ('dim2', np.arange(2, 5))}) actual = data.isel(dim2=indexing_da) assert_identical(actual['numbers'], data['numbers']) # boolean data array with coordinate with the same name - indexing_da = DataArray( - np.arange(1, 10), - dims=['dim2'], - coords={ - 'dim2': data['dim2'].values - }) + indexing_da = DataArray(np.arange(1, 10), dims=['dim2'], + coords={'dim2': data['dim2'].values}) indexing_da = (indexing_da < 3) actual = data.isel(dim2=indexing_da) assert_identical(actual['dim2'], data['dim2'][:2]) # boolean data array with non-dimensioncoordinate - indexing_da = DataArray( - np.arange(1, 10), - dims=['dim2'], - coords={ - 'dim2': data['dim2'].values, - 'non_dim': (('dim2', ), np.random.randn(9)), - 'non_dim2': 0 - }) + indexing_da = DataArray(np.arange(1, 10), dims=['dim2'], + coords={'dim2': data['dim2'].values, + 'non_dim': (('dim2', ), + np.random.randn(9)), + 'non_dim2': 0}) indexing_da = (indexing_da < 3) actual = data.isel(dim2=indexing_da) - assert_identical(actual['dim2'].drop('non_dim').drop('non_dim2'), - data['dim2'][:2]) - assert_identical(actual['non_dim'], indexing_da['non_dim'][:2]) - assert_identical(actual['non_dim2'], indexing_da['non_dim2']) + assert_identical( + actual['dim2'].drop('non_dim').drop('non_dim2'), data['dim2'][:2]) + assert_identical( + actual['non_dim'], indexing_da['non_dim'][:2]) + assert_identical( + actual['non_dim2'], indexing_da['non_dim2']) # non-dimension coordinate will be also attached - indexing_da = DataArray( - np.arange(1, 4), - dims=['dim2'], - coords={ - 'non_dim': (('dim2', ), np.random.randn(3)) - }) + indexing_da = DataArray(np.arange(1, 4), dims=['dim2'], + coords={'non_dim': (('dim2', ), + np.random.randn(3))}) actual = data.isel(dim2=indexing_da) assert 'non_dim' in actual assert 'non_dim' in actual.coords @@ -1150,42 +1062,43 @@ def test_isel_dataarray(self): # indexer generated from coordinates indexing_ds = Dataset({}, coords={'dim2': [0, 1, 2]}) - with raises_regex(IndexError, "dimension coordinate 'dim2'"): + with raises_regex( + IndexError, "dimension coordinate 'dim2'"): actual = data.isel(dim2=indexing_ds['dim2']) def test_sel(self): data = create_test_data() - int_slicers = { - 'dim1': slice(None, None, 2), - 'dim2': slice(2), - 'dim3': slice(3) - } - loc_slicers = { - 'dim1': slice(None, None, 2), - 'dim2': slice(0, 0.5), - 'dim3': slice('a', 'c') - } - assert_equal(data.isel(**int_slicers), data.sel(**loc_slicers)) + int_slicers = {'dim1': slice(None, None, 2), + 'dim2': slice(2), + 'dim3': slice(3)} + loc_slicers = {'dim1': slice(None, None, 2), + 'dim2': slice(0, 0.5), + 'dim3': slice('a', 'c')} + assert_equal(data.isel(**int_slicers), + data.sel(**loc_slicers)) data['time'] = ('time', pd.date_range('2000-01-01', periods=20)) - assert_equal(data.isel(time=0), data.sel(time='2000-01-01')) - assert_equal( - data.isel(time=slice(10)), - data.sel(time=slice('2000-01-01', '2000-01-10'))) + assert_equal(data.isel(time=0), + data.sel(time='2000-01-01')) + assert_equal(data.isel(time=slice(10)), + data.sel(time=slice('2000-01-01', + '2000-01-10'))) assert_equal(data, data.sel(time=slice('1999', '2005'))) times = pd.date_range('2000-01-01', periods=3) - assert_equal(data.isel(time=slice(3)), data.sel(time=times)) - assert_equal( - data.isel(time=slice(3)), - data.sel(time=(data['time.dayofyear'] <= 3))) + assert_equal(data.isel(time=slice(3)), + data.sel(time=times)) + assert_equal(data.isel(time=slice(3)), + data.sel(time=(data['time.dayofyear'] <= 3))) td = pd.to_timedelta(np.arange(3), unit='days') data = Dataset({'x': ('td', np.arange(3)), 'td': td}) assert_equal(data, data.sel(td=td)) assert_equal(data, data.sel(td=slice('3 days'))) - assert_equal(data.isel(td=0), data.sel(td=pd.Timedelta('0 days'))) - assert_equal(data.isel(td=0), data.sel(td=pd.Timedelta('0h'))) - assert_equal( - data.isel(td=slice(1, 3)), data.sel(td=slice('1 days', '2 days'))) + assert_equal(data.isel(td=0), + data.sel(td=pd.Timedelta('0 days'))) + assert_equal(data.isel(td=0), + data.sel(td=pd.Timedelta('0h'))) + assert_equal(data.isel(td=slice(1, 3)), + data.sel(td=slice('1 days', '2 days'))) def test_sel_dataarray(self): data = create_test_data() @@ -1204,77 +1117,59 @@ def test_sel_dataarray(self): # Multi-dimensional ind = DataArray([[0.0], [0.5], [1.0]], dims=['new_dim', 'new_dim2']) actual = data.sel(dim2=ind) - expected = data.isel( - dim2=Variable(('new_dim', 'new_dim2'), [[0], [1], [2]])) + expected = data.isel(dim2=Variable(('new_dim', 'new_dim2'), + [[0], [1], [2]])) assert 'new_dim' in actual.dims assert 'new_dim2' in actual.dims assert_equal(actual, expected) # with coordinate - ind = DataArray( - [0.0, 0.5, 1.0], - dims=['new_dim'], - coords={ - 'new_dim': ['a', 'b', 'c'] - }) + ind = DataArray([0.0, 0.5, 1.0], dims=['new_dim'], + coords={'new_dim': ['a', 'b', 'c']}) actual = data.sel(dim2=ind) expected = data.isel(dim2=[0, 1, 2]).rename({'dim2': 'new_dim'}) assert 'new_dim' in actual.dims assert 'new_dim' in actual.coords - assert_equal( - actual.drop('new_dim').drop('dim2'), expected.drop('new_dim')) - assert_equal(actual['new_dim'].drop('dim2'), ind['new_dim']) + assert_equal(actual.drop('new_dim').drop('dim2'), + expected.drop('new_dim')) + assert_equal(actual['new_dim'].drop('dim2'), + ind['new_dim']) # with conflicted coordinate (silently ignored) - ind = DataArray( - [0.0, 0.5, 1.0], dims=['dim2'], coords={ - 'dim2': ['a', 'b', 'c'] - }) + ind = DataArray([0.0, 0.5, 1.0], dims=['dim2'], + coords={'dim2': ['a', 'b', 'c']}) actual = data.sel(dim2=ind) expected = data.isel(dim2=[0, 1, 2]) assert_equal(actual, expected) # with conflicted coordinate (silently ignored) - ind = DataArray( - [0.0, 0.5, 1.0], - dims=['new_dim'], - coords={ - 'new_dim': ['a', 'b', 'c'], - 'dim2': 3 - }) + ind = DataArray([0.0, 0.5, 1.0], dims=['new_dim'], + coords={'new_dim': ['a', 'b', 'c'], + 'dim2': 3}) actual = data.sel(dim2=ind) assert_equal(actual['new_dim'].drop('dim2'), ind['new_dim'].drop('dim2')) expected = data.isel(dim2=[0, 1, 2]) expected['dim2'] = (('new_dim'), expected['dim2'].values) - assert_equal(actual['dim2'].drop('new_dim'), expected['dim2']) + assert_equal(actual['dim2'].drop('new_dim'), + expected['dim2']) assert actual['var1'].dims == ('dim1', 'new_dim') # with non-dimensional coordinate - ind = DataArray( - [0.0, 0.5, 1.0], - dims=['dim2'], - coords={ - 'dim2': ['a', 'b', 'c'], - 'numbers': ('dim2', [0, 1, 2]), - 'new_dim': ('dim2', [1.1, 1.2, 1.3]) - }) + ind = DataArray([0.0, 0.5, 1.0], dims=['dim2'], + coords={'dim2': ['a', 'b', 'c'], + 'numbers': ('dim2', [0, 1, 2]), + 'new_dim': ('dim2', [1.1, 1.2, 1.3])}) actual = data.sel(dim2=ind) expected = data.isel(dim2=[0, 1, 2]) assert_equal(actual.drop('new_dim'), expected) assert np.allclose(actual['new_dim'].values, ind['new_dim'].values) def test_sel_dataarray_mindex(self): - midx = pd.MultiIndex.from_product( - [list('abc'), [0, 1]], names=('one', 'two')) - mds = xr.Dataset( - { - 'var': (('x', 'y'), np.random.rand(6, 3)) - }, - coords={ - 'x': midx, - 'y': range(3) - }) + midx = pd.MultiIndex.from_product([list('abc'), [0, 1]], + names=('one', 'two')) + mds = xr.Dataset({'var': (('x', 'y'), np.random.rand(6, 3))}, + coords={'x': midx, 'y': range(3)}) actual_isel = mds.isel(x=xr.DataArray(np.arange(3), dims='x')) actual_sel = mds.sel(x=DataArray(mds.indexes['x'][:3], dims='x')) @@ -1289,15 +1184,10 @@ def test_sel_dataarray_mindex(self): assert_identical(actual_isel, actual_sel) # with coordinate - actual_isel = mds.isel( - x=xr.DataArray(np.arange(3), dims='z', coords={ - 'z': [0, 1, 2] - })) - actual_sel = mds.sel( - x=xr.DataArray( - mds.indexes['x'][:3], dims='z', coords={ - 'z': [0, 1, 2] - })) + actual_isel = mds.isel(x=xr.DataArray(np.arange(3), dims='z', + coords={'z': [0, 1, 2]})) + actual_sel = mds.sel(x=xr.DataArray(mds.indexes['x'][:3], dims='z', + coords={'z': [0, 1, 2]})) assert actual_isel['x'].dims == ('z', ) assert actual_sel['x'].dims == ('z', ) assert_identical(actual_isel, actual_sel) @@ -1309,10 +1199,8 @@ def test_sel_dataarray_mindex(self): with raises_regex(ValueError, 'Vectorized selection is ' 'not available along MultiIndex variable:' ' x'): - mds.sel( - x=xr.DataArray( - [np.array(midx[:2]), - np.array(midx[-2:])], dims=['a', 'b'])) + mds.sel(x=xr.DataArray([np.array(midx[:2]), np.array(midx[-2:])], + dims=['a', 'b'])) def test_sel_drop(self): data = Dataset({'foo': ('x', [1, 2, 3])}, {'x': [0, 1, 2]}) @@ -1345,8 +1233,8 @@ def test_isel_points(self): pdim1 = [1, 2, 3] pdim2 = [4, 5, 1] pdim3 = [1, 2, 3] - actual = data.isel_points( - dim1=pdim1, dim2=pdim2, dim3=pdim3, dim='test_coord') + actual = data.isel_points(dim1=pdim1, dim2=pdim2, dim3=pdim3, + dim='test_coord') assert 'test_coord' in actual.dims assert actual.coords['test_coord'].shape == (len(pdim1), ) @@ -1357,20 +1245,22 @@ def test_isel_points(self): np.testing.assert_array_equal(data['dim2'][pdim2], actual['dim2']) # test that the order of the indexers doesn't matter - assert_identical( - data.isel_points(dim1=pdim1, dim2=pdim2), - data.isel_points(dim2=pdim2, dim1=pdim1)) + assert_identical(data.isel_points(dim1=pdim1, dim2=pdim2), + data.isel_points(dim2=pdim2, dim1=pdim1)) # make sure we're raising errors in the right places - with raises_regex(ValueError, 'All indexers must be the same length'): + with raises_regex(ValueError, + 'All indexers must be the same length'): data.isel_points(dim1=[1, 2], dim2=[1, 2, 3]) - with raises_regex(ValueError, 'dimension bad_key does not exist'): + with raises_regex(ValueError, + 'dimension bad_key does not exist'): data.isel_points(bad_key=[1, 2]) with raises_regex(TypeError, 'Indexers must be integers'): data.isel_points(dim1=[1.5, 2.2]) with raises_regex(TypeError, 'Indexers must be integers'): data.isel_points(dim1=[1, 2, 3], dim2=slice(3)) - with raises_regex(ValueError, 'Indexers must be 1 dimensional'): + with raises_regex(ValueError, + 'Indexers must be 1 dimensional'): data.isel_points(dim1=1, dim2=2) with raises_regex(ValueError, 'Existing dimension names are not valid'): @@ -1387,34 +1277,32 @@ def test_isel_points(self): stations['dim1s'] = ('station', [1, 2, 3]) stations['dim2s'] = ('station', [4, 5, 1]) - actual = data.isel_points( - dim1=stations['dim1s'], - dim2=stations['dim2s'], - dim=stations['station']) + actual = data.isel_points(dim1=stations['dim1s'], + dim2=stations['dim2s'], + dim=stations['station']) assert 'station' in actual.coords assert 'station' in actual.dims - assert_identical(actual['station'].drop(['dim2']), stations['station']) + assert_identical(actual['station'].drop(['dim2']), + stations['station']) # make sure we get the default 'points' coordinate when passed a list - actual = data.isel_points( - dim1=stations['dim1s'], - dim2=stations['dim2s'], - dim=['A', 'B', 'C']) + actual = data.isel_points(dim1=stations['dim1s'], + dim2=stations['dim2s'], + dim=['A', 'B', 'C']) assert 'points' in actual.coords assert actual.coords['points'].values.tolist() == ['A', 'B', 'C'] # test index - actual = data.isel_points( - dim1=stations['dim1s'].values, - dim2=stations['dim2s'].values, - dim=pd.Index(['A', 'B', 'C'], name='letters')) + actual = data.isel_points(dim1=stations['dim1s'].values, + dim2=stations['dim2s'].values, + dim=pd.Index(['A', 'B', 'C'], + name='letters')) assert 'letters' in actual.coords # can pass a numpy array - data.isel_points( - dim1=stations['dim1s'], - dim2=stations['dim2s'], - dim=np.array([4, 5, 6])) + data.isel_points(dim1=stations['dim1s'], + dim2=stations['dim2s'], + dim=np.array([4, 5, 6])) def test_sel_points(self): data = create_test_data() @@ -1425,13 +1313,10 @@ def test_sel_points(self): pdim1 = [1, 2, 3] pdim2 = [4, 5, 1] pdim3 = [1, 2, 3] - expected = data.isel_points( - dim1=pdim1, dim2=pdim2, dim3=pdim3, dim='test_coord') - actual = data.sel_points( - dim1=data.dim1[pdim1], - dim2=data.dim2[pdim2], - dim3=data.dim3[pdim3], - dim='test_coord') + expected = data.isel_points(dim1=pdim1, dim2=pdim2, dim3=pdim3, + dim='test_coord') + actual = data.sel_points(dim1=data.dim1[pdim1], dim2=data.dim2[pdim2], + dim3=data.dim3[pdim3], dim='test_coord') assert_identical(expected, actual) data = Dataset({'foo': (('x', 'y'), np.arange(9).reshape(3, 3))}) @@ -1440,12 +1325,10 @@ def test_sel_points(self): assert_identical(expected, actual) data.coords.update({'x': [0, 1, 2], 'y': [0, 1, 2]}) - expected.coords.update({ - 'x': ('points', [0, 1, 2]), - 'y': ('points', [0, 1, 2]) - }) - actual = data.sel_points( - x=[0.1, 1.1, 2.5], y=[0, 1.2, 2.0], method='pad') + expected.coords.update({'x': ('points', [0, 1, 2]), + 'y': ('points', [0, 1, 2])}) + actual = data.sel_points(x=[0.1, 1.1, 2.5], y=[0, 1.2, 2.0], + method='pad') assert_identical(expected, actual) with pytest.raises(KeyError): @@ -1460,95 +1343,58 @@ def test_sel_fancy(self): pdim1 = [1, 2, 3] pdim2 = [4, 5, 1] pdim3 = [1, 2, 3] - expected = data.isel( - dim1=Variable(('test_coord', ), pdim1), - dim2=Variable(('test_coord', ), pdim2), - dim3=Variable(('test_coord'), pdim3)) - actual = data.sel( - dim1=Variable(('test_coord', ), data.dim1[pdim1]), - dim2=Variable(('test_coord', ), data.dim2[pdim2]), - dim3=Variable(('test_coord', ), data.dim3[pdim3])) + expected = data.isel(dim1=Variable(('test_coord', ), pdim1), + dim2=Variable(('test_coord', ), pdim2), + dim3=Variable(('test_coord'), pdim3)) + actual = data.sel(dim1=Variable(('test_coord', ), data.dim1[pdim1]), + dim2=Variable(('test_coord', ), data.dim2[pdim2]), + dim3=Variable(('test_coord', ), data.dim3[pdim3])) assert_identical(expected, actual) # DataArray Indexer - idx_t = DataArray( - data['time'][[3, 2, 1]].values, - dims=['a'], - coords={ - 'a': ['a', 'b', 'c'] - }) - idx_2 = DataArray( - data['dim2'][[3, 2, 1]].values, - dims=['a'], - coords={ - 'a': ['a', 'b', 'c'] - }) - idx_3 = DataArray( - data['dim3'][[3, 2, 1]].values, - dims=['a'], - coords={ - 'a': ['a', 'b', 'c'] - }) + idx_t = DataArray(data['time'][[3, 2, 1]].values, dims=['a'], + coords={'a': ['a', 'b', 'c']}) + idx_2 = DataArray(data['dim2'][[3, 2, 1]].values, dims=['a'], + coords={'a': ['a', 'b', 'c']}) + idx_3 = DataArray(data['dim3'][[3, 2, 1]].values, dims=['a'], + coords={'a': ['a', 'b', 'c']}) actual = data.sel(time=idx_t, dim2=idx_2, dim3=idx_3) - expected = data.isel( - time=Variable(('a', ), [3, 2, 1]), - dim2=Variable(('a', ), [3, 2, 1]), - dim3=Variable(('a', ), [3, 2, 1])) + expected = data.isel(time=Variable(('a', ), [3, 2, 1]), + dim2=Variable(('a', ), [3, 2, 1]), + dim3=Variable(('a', ), [3, 2, 1])) expected = expected.assign_coords(a=idx_t['a']) assert_identical(expected, actual) - idx_t = DataArray( - data['time'][[3, 2, 1]].values, - dims=['a'], - coords={ - 'a': ['a', 'b', 'c'] - }) - idx_2 = DataArray( - data['dim2'][[2, 1, 3]].values, - dims=['b'], - coords={ - 'b': [0, 1, 2] - }) - idx_3 = DataArray( - data['dim3'][[1, 2, 1]].values, - dims=['c'], - coords={ - 'c': [0.0, 1.1, 2.2] - }) + idx_t = DataArray(data['time'][[3, 2, 1]].values, dims=['a'], + coords={'a': ['a', 'b', 'c']}) + idx_2 = DataArray(data['dim2'][[2, 1, 3]].values, dims=['b'], + coords={'b': [0, 1, 2]}) + idx_3 = DataArray(data['dim3'][[1, 2, 1]].values, dims=['c'], + coords={'c': [0.0, 1.1, 2.2]}) actual = data.sel(time=idx_t, dim2=idx_2, dim3=idx_3) - expected = data.isel( - time=Variable(('a', ), [3, 2, 1]), - dim2=Variable(('b', ), [2, 1, 3]), - dim3=Variable(('c', ), [1, 2, 1])) - expected = expected.assign_coords( - a=idx_t['a'], b=idx_2['b'], c=idx_3['c']) + expected = data.isel(time=Variable(('a', ), [3, 2, 1]), + dim2=Variable(('b', ), [2, 1, 3]), + dim3=Variable(('c', ), [1, 2, 1])) + expected = expected.assign_coords(a=idx_t['a'], b=idx_2['b'], + c=idx_3['c']) assert_identical(expected, actual) # test from sel_points data = Dataset({'foo': (('x', 'y'), np.arange(9).reshape(3, 3))}) data.coords.update({'x': [0, 1, 2], 'y': [0, 1, 2]}) - expected = Dataset( - { - 'foo': ('points', [0, 4, 8]) - }, - coords={ - 'x': Variable(('points', ), [0, 1, 2]), - 'y': Variable(('points', ), [0, 1, 2]) - }) - actual = data.sel( - x=Variable(('points', ), [0, 1, 2]), - y=Variable(('points', ), [0, 1, 2])) + expected = Dataset({'foo': ('points', [0, 4, 8])}, + coords={'x': Variable(('points', ), [0, 1, 2]), + 'y': Variable(('points', ), [0, 1, 2])}) + actual = data.sel(x=Variable(('points', ), [0, 1, 2]), + y=Variable(('points', ), [0, 1, 2])) assert_identical(expected, actual) - expected.coords.update({ - 'x': ('points', [0, 1, 2]), - 'y': ('points', [0, 1, 2]) - }) - actual = data.sel( - x=Variable(('points', ), [0.1, 1.1, 2.5]), - y=Variable(('points', ), [0, 1.2, 2.0]), - method='pad') + expected.coords.update({'x': ('points', [0, 1, 2]), + 'y': ('points', [0, 1, 2])}) + actual = data.sel(x=Variable(('points', ), [0.1, 1.1, 2.5]), + y=Variable(('points', ), [0, 1.2, 2.0]), + method='pad') assert_identical(expected, actual) idx_x = DataArray([0, 1, 2], dims=['a'], coords={'a': ['a', 'b', 'c']}) @@ -1601,16 +1447,12 @@ def test_loc(self): data.loc[dict(dim3='a')] = 0 def test_selection_multiindex(self): - mindex = pd.MultiIndex.from_product( - [['a', 'b'], [1, 2], [-1, -2]], names=('one', 'two', 'three')) - mdata = Dataset( - data_vars={'var': ('x', range(8))}, coords={ - 'x': mindex - }) - - def test_sel(lab_indexer, - pos_indexer, - replaced_idx=False, + mindex = pd.MultiIndex.from_product([['a', 'b'], [1, 2], [-1, -2]], + names=('one', 'two', 'three')) + mdata = Dataset(data_vars={'var': ('x', range(8))}, + coords={'x': mindex}) + + def test_sel(lab_indexer, pos_indexer, replaced_idx=False, renamed_dim=None): ds = mdata.sel(x=lab_indexer) expected_ds = mdata.isel(x=pos_indexer) @@ -1627,41 +1469,27 @@ def test_sel(lab_indexer, test_sel(('a', 1, -1), 0) test_sel(('b', 2, -2), -1) test_sel(('a', 1), [0, 1], replaced_idx=True, renamed_dim='three') - test_sel(('a', ), range(4), replaced_idx=True) + test_sel(('a',), range(4), replaced_idx=True) test_sel('a', range(4), replaced_idx=True) test_sel([('a', 1, -1), ('b', 2, -2)], [0, 7]) test_sel(slice('a', 'b'), range(8)) test_sel(slice(('a', 1), ('b', 1)), range(6)) test_sel({'one': 'a', 'two': 1, 'three': -1}, 0) - test_sel( - { - 'one': 'a', - 'two': 1 - }, [0, 1], - replaced_idx=True, - renamed_dim='three') + test_sel({'one': 'a', 'two': 1}, [0, 1], replaced_idx=True, + renamed_dim='three') test_sel({'one': 'a'}, range(4), replaced_idx=True) - assert_identical( - mdata.loc[{ - 'x': { - 'one': 'a' - } - }], mdata.sel(x={ - 'one': 'a' - })) - assert_identical(mdata.loc[{'x': 'a'}], mdata.sel(x='a')) - assert_identical(mdata.loc[{'x': ('a', 1)}], mdata.sel(x=('a', 1))) - assert_identical( - mdata.loc[{ - 'x': ('a', 1, -1) - }], mdata.sel(x=('a', 1, -1))) + assert_identical(mdata.loc[{'x': {'one': 'a'}}], + mdata.sel(x={'one': 'a'})) + assert_identical(mdata.loc[{'x': 'a'}], + mdata.sel(x='a')) + assert_identical(mdata.loc[{'x': ('a', 1)}], + mdata.sel(x=('a', 1))) + assert_identical(mdata.loc[{'x': ('a', 1, -1)}], + mdata.sel(x=('a', 1, -1))) - assert_identical( - mdata.sel(x={ - 'one': 'a', - 'two': 1 - }), mdata.sel(one='a', two=1)) + assert_identical(mdata.sel(x={'one': 'a', 'two': 1}), + mdata.sel(one='a', two=1)) def test_reindex_like(self): data = create_test_data() @@ -1697,8 +1525,8 @@ def test_reindex(self): actual = data.reindex(dim1=data['dim1'].to_index()) assert_identical(actual, expected) - with raises_regex(ValueError, - 'cannot reindex or align along dimension'): + with raises_regex( + ValueError, 'cannot reindex or align along dimension'): data.reindex(dim1=data['dim1'][:5]) expected = data.isel(dim2=slice(5)) @@ -1724,29 +1552,18 @@ def test_reindex(self): assert_identical(actual, expected) # regression test for #279 - expected = Dataset({ - 'x': ('time', np.random.randn(5)) - }, { - 'time': range(5) - }) + expected = Dataset({'x': ('time', np.random.randn(5))}, + {'time': range(5)}) time2 = DataArray(np.arange(5), dims="time2") with pytest.warns(FutureWarning): actual = expected.reindex(time=time2) assert_identical(actual, expected) # another regression test - ds = Dataset({ - 'foo': (['x', 'y'], np.zeros((3, 4))) - }, { - 'x': range(3), - 'y': range(4) - }) - expected = Dataset({ - 'foo': (['x', 'y'], np.zeros((3, 2))) - }, { - 'x': [0, 1, 3], - 'y': [0, 1] - }) + ds = Dataset({'foo': (['x', 'y'], np.zeros((3, 4)))}, + {'x': range(3), 'y': range(4)}) + expected = Dataset({'foo': (['x', 'y'], np.zeros((3, 2)))}, + {'x': [0, 1, 3], 'y': [0, 1]}) expected['foo'][-1] = np.nan actual = ds.reindex(x=[0, 1, 3], y=[0, 1]) assert_identical(expected, actual) @@ -1758,8 +1575,8 @@ def test_reindex_warning(self): # DataArray with different dimension raises Future warning ind = xr.DataArray([0.0, 1.0], dims=['new_dim'], name='ind') data.reindex(dim2=ind) - assert any( - ["Indexer has dimensions " in str(w.message) for w in ws]) + assert any(["Indexer has dimensions " in + str(w.message) for w in ws]) # Should not warn ind = xr.DataArray([0.0, 1.0], dims=['dim2'], name='ind') @@ -1813,8 +1630,8 @@ def test_align(self): self.assertArrayEqual(left2['dim3'], union) assert_equal(left2['dim3'].variable, right2['dim3'].variable) - assert_identical( - left2.sel(dim3=intersection), right2.sel(dim3=intersection)) + assert_identical(left2.sel(dim3=intersection), + right2.sel(dim3=intersection)) assert np.isnan(left2['var3'][-2:]).all() assert np.isnan(right2['var3'][:2]).all() @@ -1822,16 +1639,16 @@ def test_align(self): assert_equal(left2['dim3'].variable, right2['dim3'].variable) assert_equal(left2['dim3'].variable, left['dim3'].variable) - assert_identical( - left2.sel(dim3=intersection), right2.sel(dim3=intersection)) + assert_identical(left2.sel(dim3=intersection), + right2.sel(dim3=intersection)) assert np.isnan(right2['var3'][:2]).all() left2, right2 = align(left, right, join='right') assert_equal(left2['dim3'].variable, right2['dim3'].variable) assert_equal(left2['dim3'].variable, right['dim3'].variable) - assert_identical( - left2.sel(dim3=intersection), right2.sel(dim3=intersection)) + assert_identical(left2.sel(dim3=intersection), + right2.sel(dim3=intersection)) assert np.isnan(left2['var3'][-2:]).all() @@ -1852,48 +1669,20 @@ def test_align_exact(self): xr.align(left, right, join='exact') def test_align_exclude(self): - x = Dataset({ - 'foo': - DataArray( - [[1, 2], [3, 4]], - dims=['x', 'y'], - coords={ - 'x': [1, 2], - 'y': [3, 4] - }) - }) - y = Dataset({ - 'bar': - DataArray( - [[1, 2], [3, 4]], - dims=['x', 'y'], - coords={ - 'x': [1, 3], - 'y': [5, 6] - }) - }) + x = Dataset({'foo': DataArray([[1, 2], [3, 4]], dims=['x', 'y'], + coords={'x': [1, 2], 'y': [3, 4]})}) + y = Dataset({'bar': DataArray([[1, 2], [3, 4]], dims=['x', 'y'], + coords={'x': [1, 3], 'y': [5, 6]})}) x2, y2 = align(x, y, exclude=['y'], join='outer') - expected_x2 = Dataset({ - 'foo': - DataArray( - [[1, 2], [3, 4], [np.nan, np.nan]], - dims=['x', 'y'], - coords={ - 'x': [1, 2, 3], - 'y': [3, 4] - }) - }) - expected_y2 = Dataset({ - 'bar': - DataArray( - [[1, 2], [np.nan, np.nan], [3, 4]], - dims=['x', 'y'], - coords={ - 'x': [1, 2, 3], - 'y': [5, 6] - }) - }) + expected_x2 = Dataset( + {'foo': DataArray([[1, 2], [3, 4], [np.nan, np.nan]], + dims=['x', 'y'], + coords={'x': [1, 2, 3], 'y': [3, 4]})}) + expected_y2 = Dataset( + {'bar': DataArray([[1, 2], [np.nan, np.nan], [3, 4]], + dims=['x', 'y'], + coords={'x': [1, 2, 3], 'y': [5, 6]})}) assert_identical(expected_x2, x2) assert_identical(expected_y2, y2) @@ -1901,10 +1690,8 @@ def test_align_nocopy(self): x = Dataset({'foo': DataArray([1, 2, 3], coords=[('x', [1, 2, 3])])}) y = Dataset({'foo': DataArray([1, 2], coords=[('x', [1, 2])])}) expected_x2 = x - expected_y2 = Dataset({ - 'foo': - DataArray([1, 2, np.nan], coords=[('x', [1, 2, 3])]) - }) + expected_y2 = Dataset({'foo': DataArray([1, 2, np.nan], + coords=[('x', [1, 2, 3])])}) x2, y2 = align(x, y, copy=False, join='outer') assert_identical(expected_x2, x2) @@ -1918,17 +1705,11 @@ def test_align_nocopy(self): assert_identical(expected_y2, y2) def test_align_indexes(self): - x = Dataset({ - 'foo': - DataArray([1, 2, 3], dims='x', coords=[('x', [1, 2, 3])]) - }) + x = Dataset({'foo': DataArray([1, 2, 3], dims='x', + coords=[('x', [1, 2, 3])])}) x2, = align(x, indexes={'x': [2, 3, 1]}) - expected_x2 = Dataset({ - 'foo': - DataArray([2, 3, 1], dims='x', coords={ - 'x': [2, 3, 1] - }) - }) + expected_x2 = Dataset({'foo': DataArray([2, 3, 1], dims='x', + coords={'x': [2, 3, 1]})}) assert_identical(expected_x2, x2) @@ -1942,20 +1723,12 @@ def test_align_non_unique(self): align(x, y) def test_broadcast(self): - ds = Dataset({ - 'foo': 0, - 'bar': ('x', [1]), - 'baz': ('y', [2, 3]) - }, { - 'c': ('x', [4]) - }) - expected = Dataset({ - 'foo': (('x', 'y'), [[0, 0]]), - 'bar': (('x', 'y'), [[1, 1]]), - 'baz': (('x', 'y'), [[2, 3]]) - }, { - 'c': ('x', [4]) - }) + ds = Dataset({'foo': 0, 'bar': ('x', [1]), 'baz': ('y', [2, 3])}, + {'c': ('x', [4])}) + expected = Dataset({'foo': (('x', 'y'), [[0, 0]]), + 'bar': (('x', 'y'), [[1, 1]]), + 'baz': (('x', 'y'), [[2, 3]])}, + {'c': ('x', [4])}) actual, = broadcast(ds) assert_identical(expected, actual) @@ -1990,93 +1763,43 @@ def test_broadcast_nocopy(self): def test_broadcast_exclude(self): x = Dataset({ - 'foo': - DataArray( - [[1, 2], [3, 4]], - dims=['x', 'y'], - coords={ - 'x': [1, 2], - 'y': [3, 4] - }), - 'bar': - DataArray(5), + 'foo': DataArray([[1, 2], [3, 4]], dims=['x', 'y'], + coords={'x': [1, 2], 'y': [3, 4]}), + 'bar': DataArray(5), }) y = Dataset({ - 'foo': - DataArray( - [[1, 2]], dims=['z', 'y'], coords={ - 'z': [1], - 'y': [5, 6] - }), + 'foo': DataArray([[1, 2]], dims=['z', 'y'], + coords={'z': [1], 'y': [5, 6]}), }) x2, y2 = broadcast(x, y, exclude=['y']) expected_x2 = Dataset({ - 'foo': - DataArray( - [[[1, 2]], [[3, 4]]], - dims=['x', 'z', 'y'], - coords={ - 'z': [1], - 'x': [1, 2], - 'y': [3, 4] - }), - 'bar': - DataArray( - [[5], [5]], dims=['x', 'z'], coords={ - 'x': [1, 2], - 'z': [1] - }), + 'foo': DataArray([[[1, 2]], [[3, 4]]], dims=['x', 'z', 'y'], + coords={'z': [1], 'x': [1, 2], 'y': [3, 4]}), + 'bar': DataArray([[5], [5]], dims=['x', 'z'], + coords={'x': [1, 2], 'z': [1]}), }) expected_y2 = Dataset({ - 'foo': - DataArray( - [[[1, 2]], [[1, 2]]], - dims=['x', 'z', 'y'], - coords={ - 'z': [1], - 'x': [1, 2], - 'y': [5, 6] - }), + 'foo': DataArray([[[1, 2]], [[1, 2]]], dims=['x', 'z', 'y'], + coords={'z': [1], 'x': [1, 2], 'y': [5, 6]}), }) assert_identical(expected_x2, x2) assert_identical(expected_y2, y2) def test_broadcast_misaligned(self): - x = Dataset({ - 'foo': DataArray([1, 2, 3], coords=[('x', [-1, -2, -3])]) - }) - y = Dataset({ - 'bar': - DataArray( - [[1, 2], [3, 4]], - dims=['y', 'x'], - coords={ - 'y': [1, 2], - 'x': [10, -3] - }) - }) + x = Dataset({'foo': DataArray([1, 2, 3], + coords=[('x', [-1, -2, -3])])}) + y = Dataset({'bar': DataArray([[1, 2], [3, 4]], dims=['y', 'x'], + coords={'y': [1, 2], 'x': [10, -3]})}) x2, y2 = broadcast(x, y) - expected_x2 = Dataset({ - 'foo': - DataArray( - [[3, 3], [2, 2], [1, 1], [np.nan, np.nan]], - dims=['x', 'y'], - coords={ - 'y': [1, 2], - 'x': [-3, -2, -1, 10] - }) - }) - expected_y2 = Dataset({ - 'bar': - DataArray( + expected_x2 = Dataset( + {'foo': DataArray([[3, 3], [2, 2], [1, 1], [np.nan, np.nan]], + dims=['x', 'y'], + coords={'y': [1, 2], 'x': [-3, -2, -1, 10]})}) + expected_y2 = Dataset( + {'bar': DataArray( [[2, 4], [np.nan, np.nan], [np.nan, np.nan], [1, 3]], - dims=['x', 'y'], - coords={ - 'y': [1, 2], - 'x': [-3, -2, -1, 10] - }) - }) + dims=['x', 'y'], coords={'y': [1, 2], 'x': [-3, -2, -1, 10]})}) assert_identical(expected_x2, x2) assert_identical(expected_y2, y2) @@ -2098,8 +1821,8 @@ def test_drop_variables(self): assert_identical(data, data.drop([])) - expected = Dataset( - dict((k, data[k]) for k in data.variables if k != 'time')) + expected = Dataset(dict((k, data[k]) for k in data.variables + if k != 'time')) actual = data.drop('time') assert_identical(expected, actual) actual = data.drop(['time']) @@ -2109,10 +1832,8 @@ def test_drop_variables(self): data.drop('not_found_here') def test_drop_index_labels(self): - data = Dataset({ - 'A': (['x', 'y'], np.random.randn(2, 3)), - 'x': ['a', 'b'] - }) + data = Dataset({'A': (['x', 'y'], np.random.randn(2, 3)), + 'x': ['a', 'b']}) actual = data.drop(['a'], 'x') expected = data.isel(x=[1]) @@ -2126,7 +1847,8 @@ def test_drop_index_labels(self): # not contained in axis data.drop(['c'], dim='x') - with raises_regex(ValueError, 'does not have coordinate labels'): + with raises_regex( + ValueError, 'does not have coordinate labels'): data.drop(1, 'y') def test_copy(self): @@ -2166,9 +1888,8 @@ def test_rename(self): if name in dims: dims[dims.index(name)] = newname - self.assertVariableEqual( - Variable(dims, v.values, v.attrs), - renamed[k].variable.to_base_variable()) + self.assertVariableEqual(Variable(dims, v.values, v.attrs), + renamed[k].variable.to_base_variable()) assert v.encoding == renamed[k].encoding assert type(v) == type(renamed.variables[k]) # noqa: E721 @@ -2218,12 +1939,8 @@ def test_rename_inplace(self): def test_swap_dims(self): original = Dataset({'x': [1, 2, 3], 'y': ('x', list('abc')), 'z': 42}) - expected = Dataset({ - 'z': 42 - }, { - 'x': ('y', [1, 2, 3]), - 'y': list('abc') - }) + expected = Dataset({'z': 42}, + {'x': ('y', [1, 2, 3]), 'y': list('abc')}) actual = original.swap_dims({'x': 'y'}) assert_identical(expected, actual) assert isinstance(actual.variables['y'], IndexVariable) @@ -2242,20 +1959,13 @@ def test_swap_dims(self): original.swap_dims({'x': 'z'}) def test_expand_dims_error(self): - original = Dataset( - { - 'x': ('a', np.random.randn(3)), - 'y': (['b', 'a'], np.random.randn(4, 3)), - 'z': ('a', np.random.randn(3)) - }, - coords={ - 'a': np.linspace(0, 1, 3), - 'b': np.linspace(0, 1, 4), - 'c': np.linspace(0, 1, 5) - }, - attrs={ - 'key': 'entry' - }) + original = Dataset({'x': ('a', np.random.randn(3)), + 'y': (['b', 'a'], np.random.randn(4, 3)), + 'z': ('a', np.random.randn(3))}, + coords={'a': np.linspace(0, 1, 3), + 'b': np.linspace(0, 1, 4), + 'c': np.linspace(0, 1, 5)}, + attrs={'key': 'entry'}) with raises_regex(ValueError, 'already exists'): original.expand_dims(dim=['x']) @@ -2267,34 +1977,20 @@ def test_expand_dims_error(self): original.expand_dims(dim=['z']) def test_expand_dims(self): - original = Dataset( - { - 'x': ('a', np.random.randn(3)), - 'y': (['b', 'a'], np.random.randn(4, 3)) - }, - coords={ - 'a': np.linspace(0, 1, 3), - 'b': np.linspace(0, 1, 4), - 'c': np.linspace(0, 1, 5) - }, - attrs={ - 'key': 'entry' - }) + original = Dataset({'x': ('a', np.random.randn(3)), + 'y': (['b', 'a'], np.random.randn(4, 3))}, + coords={'a': np.linspace(0, 1, 3), + 'b': np.linspace(0, 1, 4), + 'c': np.linspace(0, 1, 5)}, + attrs={'key': 'entry'}) actual = original.expand_dims(['z'], [1]) - expected = Dataset( - { - 'x': original['x'].expand_dims('z', 1), - 'y': original['y'].expand_dims('z', 1) - }, - coords={ - 'a': np.linspace(0, 1, 3), - 'b': np.linspace(0, 1, 4), - 'c': np.linspace(0, 1, 5) - }, - attrs={ - 'key': 'entry' - }) + expected = Dataset({'x': original['x'].expand_dims('z', 1), + 'y': original['y'].expand_dims('z', 1)}, + coords={'a': np.linspace(0, 1, 3), + 'b': np.linspace(0, 1, 4), + 'c': np.linspace(0, 1, 5)}, + attrs={'key': 'entry'}) assert_identical(expected, actual) # make sure squeeze restores the original data set. roundtripped = actual.squeeze('z') @@ -2302,19 +1998,12 @@ def test_expand_dims(self): # another test with a negative axis actual = original.expand_dims(['z'], [-1]) - expected = Dataset( - { - 'x': original['x'].expand_dims('z', -1), - 'y': original['y'].expand_dims('z', -1) - }, - coords={ - 'a': np.linspace(0, 1, 3), - 'b': np.linspace(0, 1, 4), - 'c': np.linspace(0, 1, 5) - }, - attrs={ - 'key': 'entry' - }) + expected = Dataset({'x': original['x'].expand_dims('z', -1), + 'y': original['y'].expand_dims('z', -1)}, + coords={'a': np.linspace(0, 1, 3), + 'b': np.linspace(0, 1, 4), + 'c': np.linspace(0, 1, 5)}, + attrs={'key': 'entry'}) assert_identical(expected, actual) # make sure squeeze restores the original data set. roundtripped = actual.squeeze('z') @@ -2369,41 +2058,33 @@ def test_reorder_levels(self): ds.reorder_levels(x=['level_1', 'level_2']) def test_stack(self): - ds = Dataset({ - 'a': ('x', [0, 1]), - 'b': (('x', 'y'), [[0, 1], [2, 3]]), - 'y': ['a', 'b'] - }) - - exp_index = pd.MultiIndex.from_product( - [[0, 1], ['a', 'b']], names=['x', 'y']) - expected = Dataset({ - 'a': ('z', [0, 0, 1, 1]), - 'b': ('z', [0, 1, 2, 3]), - 'z': exp_index - }) + ds = Dataset({'a': ('x', [0, 1]), + 'b': (('x', 'y'), [[0, 1], [2, 3]]), + 'y': ['a', 'b']}) + + exp_index = pd.MultiIndex.from_product([[0, 1], ['a', 'b']], + names=['x', 'y']) + expected = Dataset({'a': ('z', [0, 0, 1, 1]), + 'b': ('z', [0, 1, 2, 3]), + 'z': exp_index}) actual = ds.stack(z=['x', 'y']) assert_identical(expected, actual) - exp_index = pd.MultiIndex.from_product( - [['a', 'b'], [0, 1]], names=['y', 'x']) - expected = Dataset({ - 'a': ('z', [0, 1, 0, 1]), - 'b': ('z', [0, 2, 1, 3]), - 'z': exp_index - }) + exp_index = pd.MultiIndex.from_product([['a', 'b'], [0, 1]], + names=['y', 'x']) + expected = Dataset({'a': ('z', [0, 1, 0, 1]), + 'b': ('z', [0, 2, 1, 3]), + 'z': exp_index}) actual = ds.stack(z=['y', 'x']) assert_identical(expected, actual) def test_unstack(self): - index = pd.MultiIndex.from_product( - [[0, 1], ['a', 'b']], names=['x', 'y']) + index = pd.MultiIndex.from_product([[0, 1], ['a', 'b']], + names=['x', 'y']) ds = Dataset({'b': ('z', [0, 1, 2, 3]), 'z': index}) - expected = Dataset({ - 'b': (('x', 'y'), [[0, 1], [2, 3]]), - 'x': [0, 1], - 'y': ['a', 'b'] - }) + expected = Dataset({'b': (('x', 'y'), [[0, 1], [2, 3]]), + 'x': [0, 1], + 'y': ['a', 'b']}) actual = ds.unstack('z') assert_identical(actual, expected) @@ -2415,12 +2096,10 @@ def test_unstack_errors(self): ds.unstack('x') def test_stack_unstack(self): - ds = Dataset({ - 'a': ('x', [0, 1]), - 'b': (('x', 'y'), [[0, 1], [2, 3]]), - 'x': [0, 1], - 'y': ['a', 'b'] - }) + ds = Dataset({'a': ('x', [0, 1]), + 'b': (('x', 'y'), [[0, 1], [2, 3]]), + 'x': [0, 1], + 'y': ['a', 'b']}) actual = ds.stack(z=['x', 'y']).unstack('z') assert actual.broadcast_equals(ds) @@ -2453,12 +2132,8 @@ def test_update(self): def test_update_auto_align(self): ds = Dataset({'x': ('t', [3, 4])}, {'t': [0, 1]}) - expected = Dataset({ - 'x': ('t', [3, 4]), - 'y': ('t', [np.nan, 5]) - }, { - 't': [0, 1] - }) + expected = Dataset({'x': ('t', [3, 4]), 'y': ('t', [np.nan, 5])}, + {'t': [0, 1]}) actual = ds.copy() other = {'y': ('t', [5]), 't': [1]} with raises_regex(ValueError, 'conflicting sizes'): @@ -2469,12 +2144,8 @@ def test_update_auto_align(self): actual = ds.copy() other = Dataset({'y': ('t', [5]), 't': [100]}) actual.update(other) - expected = Dataset({ - 'x': ('t', [3, 4]), - 'y': ('t', [np.nan] * 2) - }, { - 't': [0, 1] - }) + expected = Dataset({'x': ('t', [3, 4]), 'y': ('t', [np.nan] * 2)}, + {'t': [0, 1]}) assert_identical(expected, actual) def test_getitem(self): @@ -2491,12 +2162,10 @@ def test_getitem(self): assert_equal(expected, actual) actual = data['numbers'] - expected = DataArray( - data['numbers'].variable, - {'dim3': data['dim3'], - 'numbers': data['numbers']}, - dims='dim3', - name='numbers') + expected = DataArray(data['numbers'].variable, + {'dim3': data['dim3'], + 'numbers': data['numbers']}, + dims='dim3', name='numbers') assert_identical(expected, actual) actual = data[dict(dim1=0)] @@ -2526,19 +2195,16 @@ def test_virtual_variables_default_coords(self): def test_virtual_variables_time(self): # access virtual variables data = create_test_data() - expected = DataArray( - 1 + np.arange(20), - coords=[data['time']], - dims='time', - name='dayofyear') + expected = DataArray(1 + np.arange(20), coords=[data['time']], + dims='time', name='dayofyear') assert_array_equal(data['time.month'].values, data.variables['time'].to_index().month) assert_array_equal(data['time.season'].values, 'DJF') # test virtual variable math assert_array_equal(data['time.dayofyear'] + 1, 2 + np.arange(20)) - assert_array_equal( - np.sin(data['time.dayofyear']), np.sin(1 + np.arange(20))) + assert_array_equal(np.sin(data['time.dayofyear']), + np.sin(1 + np.arange(20))) # ensure they become coordinates expected = Dataset({}, {'dayofyear': data['time.dayofyear']}) actual = data[['time.dayofyear']] @@ -2558,24 +2224,17 @@ def test_virtual_variable_same_name(self): def test_virtual_variable_multiindex(self): # access multi-index levels as virtual variables data = create_test_multiindex() - expected = DataArray( - ['a', 'a', 'b', 'b'], - name='level_1', - coords=[data['x'].to_index()], - dims='x') + expected = DataArray(['a', 'a', 'b', 'b'], name='level_1', + coords=[data['x'].to_index()], dims='x') assert_identical(expected, data['level_1']) # combine multi-index level and datetime dr_index = pd.date_range('1/1/2011', periods=4, freq='H') - mindex = pd.MultiIndex.from_arrays( - [['a', 'a', 'b', 'b'], dr_index], - names=('level_str', 'level_date')) + mindex = pd.MultiIndex.from_arrays([['a', 'a', 'b', 'b'], dr_index], + names=('level_str', 'level_date')) data = Dataset({}, {'x': mindex}) - expected = DataArray( - mindex.get_level_values('level_date').hour, - name='hour', - coords=[mindex], - dims='x') + expected = DataArray(mindex.get_level_values('level_date').hour, + name='hour', coords=[mindex], dims='x') assert_identical(expected, data['level_date.hour']) # attribute style access @@ -2590,8 +2249,8 @@ def test_slice_virtual_variable(self): data = create_test_data() self.assertVariableEqual(data['time.dayofyear'][:10].variable, Variable(['time'], 1 + np.arange(10))) - self.assertVariableEqual(data['time.dayofyear'][0].variable, - Variable([], 1)) + self.assertVariableEqual( + data['time.dayofyear'][0].variable, Variable([], 1)) def test_setitem(self): # assign a variable @@ -2607,7 +2266,8 @@ def test_setitem(self): data2['B'] = dv assert_identical(data1, data2) # can't assign an ND array without dimensions - with raises_regex(ValueError, 'without explicit dimension names'): + with raises_regex(ValueError, + 'without explicit dimension names'): data2['C'] = var.values.reshape(2, 4) # but can assign a 1D array data1['C'] = var.values @@ -2665,12 +2325,9 @@ def test_setitem_auto_align(self): def test_setitem_align_new_indexes(self): ds = Dataset({'foo': ('x', [1, 2, 3])}, {'x': [0, 1, 2]}) ds['bar'] = DataArray([2, 3, 4], [('x', [1, 2, 3])]) - expected = Dataset({ - 'foo': ('x', [1, 2, 3]), - 'bar': ('x', [np.nan, 2, 3]) - }, { - 'x': [0, 1, 2] - }) + expected = Dataset({'foo': ('x', [1, 2, 3]), + 'bar': ('x', [np.nan, 2, 3])}, + {'x': [0, 1, 2]}) assert_identical(ds, expected) def test_assign(self): @@ -2722,12 +2379,8 @@ def test_assign_multiindex_level(self): def test_setitem_original_non_unique_index(self): # regression test for GH943 - original = Dataset( - { - 'data': ('x', np.arange(5)) - }, coords={ - 'x': [0, 1, 2, 0, 1] - }) + original = Dataset({'data': ('x', np.arange(5))}, + coords={'x': [0, 1, 2, 0, 1]}) expected = Dataset({'data': ('x', np.arange(5))}, {'x': range(5)}) actual = original.copy() @@ -2746,10 +2399,8 @@ def test_setitem_both_non_unique_index(self): # regression test for GH956 names = ['joaquin', 'manolo', 'joaquin'] values = np.random.randint(0, 256, (3, 4, 4)) - array = DataArray( - values, - dims=['name', 'row', 'column'], - coords=[names, range(4), range(4)]) + array = DataArray(values, dims=['name', 'row', 'column'], + coords=[names, range(4), range(4)]) expected = Dataset({'first': array, 'second': array}) actual = array.rename('first').to_dataset() actual['second'] = array @@ -2774,13 +2425,10 @@ def test_delitem(self): 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.dims)] if args else [] - - expected = Dataset( - dict((k, v.squeeze(*get_args(v))) - for k, v in iteritems(data.variables))) + expected = Dataset(dict((k, v.squeeze(*get_args(v))) + for k, v in iteritems(data.variables))) expected.set_coords(data.coords, inplace=True) assert_identical(expected, data.squeeze(*args)) # invalid squeeze @@ -2806,23 +2454,21 @@ def test_squeeze_drop(self): selected = data.squeeze(dim='y', drop=True) assert_identical(expected, selected) - data = Dataset({'foo': (('x', ), [])}, {'x': []}) + data = Dataset({'foo': (('x',), [])}, {'x': []}) selected = data.squeeze(drop=True) assert_identical(data, selected) def test_groupby(self): - data = Dataset({ - 'z': (['x', 'y'], np.random.randn(3, 5)) - }, { - 'x': ('x', list('abc')), - 'c': ('x', [0, 1, 0]), - 'y': range(5) - }) + data = Dataset({'z': (['x', 'y'], np.random.randn(3, 5))}, + {'x': ('x', list('abc')), + 'c': ('x', [0, 1, 0]), + 'y': range(5)}) groupby = data.groupby('x') assert len(groupby) == 3 expected_groups = {'a': 0, 'b': 1, 'c': 2} assert groupby.groups == expected_groups - expected_items = [('a', data.isel(x=0)), ('b', data.isel(x=1)), + expected_items = [('a', data.isel(x=0)), + ('b', data.isel(x=1)), ('c', data.isel(x=2))] for actual, expected in zip(groupby, expected_items): assert actual[0] == expected[0] @@ -2864,12 +2510,10 @@ def test_groupby_errors(self): data.groupby(data.coords['dim1'].to_index()) def test_groupby_reduce(self): - data = Dataset({ - 'xy': (['x', 'y'], np.random.randn(3, 4)), - 'xonly': ('x', np.random.randn(3)), - 'yonly': ('y', np.random.randn(4)), - 'letters': ('y', ['a', 'a', 'b', 'b']) - }) + data = Dataset({'xy': (['x', 'y'], np.random.randn(3, 4)), + 'xonly': ('x', np.random.randn(3)), + 'yonly': ('y', np.random.randn(4)), + 'letters': ('y', ['a', 'a', 'b', 'b'])}) expected = data.mean('y') expected['yonly'] = expected['yonly'].variable.set_dims({'x': 3}) @@ -2880,15 +2524,10 @@ def test_groupby_reduce(self): self.assertDatasetAllClose(expected, actual) letters = data['letters'] - expected = Dataset({ - 'xy': - data['xy'].groupby(letters).mean(), - 'xonly': (data['xonly'].mean().variable.set_dims({ - 'letters': 2 - })), - 'yonly': - data['yonly'].groupby(letters).mean() - }) + expected = Dataset({'xy': data['xy'].groupby(letters).mean(), + 'xonly': (data['xonly'].mean().variable + .set_dims({'letters': 2})), + 'yonly': data['yonly'].groupby(letters).mean()}) actual = data.groupby('letters').mean() self.assertDatasetAllClose(expected, actual) @@ -2918,8 +2557,8 @@ def reorder_dims(x): grouped = ds.groupby('numbers') zeros = DataArray([0, 0, 0, 0], [('numbers', range(4))]) - expected = ((ds + Variable('dim3', np.zeros(10))).transpose( - 'dim3', 'dim1', 'dim2', 'time')) + expected = ((ds + Variable('dim3', np.zeros(10))) + .transpose('dim3', 'dim1', 'dim2', 'time')) actual = grouped + zeros assert_equal(expected, actual) @@ -2937,31 +2576,24 @@ def reorder_dims(x): with raises_regex(TypeError, 'in-place operations'): ds += grouped - ds = Dataset({ - 'x': ('time', np.arange(100)), - 'time': pd.date_range('2000-01-01', periods=100) - }) + ds = Dataset({'x': ('time', np.arange(100)), + 'time': pd.date_range('2000-01-01', periods=100)}) with raises_regex(ValueError, 'incompat.* grouped binary'): ds + ds.groupby('time.month') def test_groupby_math_virtual(self): - ds = Dataset({ - 'x': ('t', [1, 2, 3]) - }, { - 't': pd.date_range('20100101', periods=3) - }) + ds = Dataset({'x': ('t', [1, 2, 3])}, + {'t': pd.date_range('20100101', periods=3)}) grouped = ds.groupby('t.day') actual = grouped - grouped.mean() - expected = Dataset({'x': ('t', [0, 0, 0])}, ds[['t', 't.day']]) + expected = Dataset({'x': ('t', [0, 0, 0])}, + ds[['t', 't.day']]) assert_identical(actual, expected) def test_groupby_nan(self): # nan should be excluded from groupby - ds = Dataset({ - 'foo': ('x', [1, 2, 3, 4]) - }, { - 'bar': ('x', [1, 1, 2, np.nan]) - }) + ds = Dataset({'foo': ('x', [1, 2, 3, 4])}, + {'bar': ('x', [1, 1, 2, np.nan])}) actual = ds.groupby('bar').mean() expected = Dataset({'foo': ('bar', [1.5, 3]), 'bar': [1, 2]}) assert_identical(actual, expected) @@ -2983,13 +2615,9 @@ def test_groupby_order(self): def test_resample_and_first(self): times = pd.date_range('2000-01-01', freq='6H', periods=10) - ds = Dataset({ - 'foo': (['time', 'x', 'y'], np.random.randn(10, 5, 3)), - 'bar': ('time', np.random.randn(10), { - 'meta': 'data' - }), - 'time': times - }) + ds = Dataset({'foo': (['time', 'x', 'y'], np.random.randn(10, 5, 3)), + 'bar': ('time', np.random.randn(10), {'meta': 'data'}), + 'time': times}) actual = ds.resample(time='1D').first(keep_attrs=True) expected = ds.isel(time=[0, 4, 8]) @@ -2999,30 +2627,19 @@ def test_resample_and_first(self): expected_time = pd.date_range('2000-01-01', freq='3H', periods=19) expected = ds.reindex(time=expected_time) actual = ds.resample(time='3H') - for how in [ - 'mean', - 'sum', - 'first', - 'last', - ]: + for how in ['mean', 'sum', 'first', 'last', ]: method = getattr(actual, how) result = method() assert_equal(expected, result) - for method in [ - np.mean, - ]: + for method in [np.mean, ]: result = actual.reduce(method) assert_equal(expected, result) def test_resample_by_mean_with_keep_attrs(self): times = pd.date_range('2000-01-01', freq='6H', periods=10) - ds = Dataset({ - 'foo': (['time', 'x', 'y'], np.random.randn(10, 5, 3)), - 'bar': ('time', np.random.randn(10), { - 'meta': 'data' - }), - 'time': times - }) + ds = Dataset({'foo': (['time', 'x', 'y'], np.random.randn(10, 5, 3)), + 'bar': ('time', np.random.randn(10), {'meta': 'data'}), + 'time': times}) ds.attrs['dsmeta'] = 'dsdata' resampled_ds = ds.resample(time='1D').mean(keep_attrs=True) @@ -3036,13 +2653,9 @@ def test_resample_by_mean_with_keep_attrs(self): def test_resample_by_mean_discarding_attrs(self): times = pd.date_range('2000-01-01', freq='6H', periods=10) - ds = Dataset({ - 'foo': (['time', 'x', 'y'], np.random.randn(10, 5, 3)), - 'bar': ('time', np.random.randn(10), { - 'meta': 'data' - }), - 'time': times - }) + ds = Dataset({'foo': (['time', 'x', 'y'], np.random.randn(10, 5, 3)), + 'bar': ('time', np.random.randn(10), {'meta': 'data'}), + 'time': times}) ds.attrs['dsmeta'] = 'dsdata' resampled_ds = ds.resample(time='1D').mean(keep_attrs=False) @@ -3052,13 +2665,9 @@ def test_resample_by_mean_discarding_attrs(self): def test_resample_by_last_discarding_attrs(self): times = pd.date_range('2000-01-01', freq='6H', periods=10) - ds = Dataset({ - 'foo': (['time', 'x', 'y'], np.random.randn(10, 5, 3)), - 'bar': ('time', np.random.randn(10), { - 'meta': 'data' - }), - 'time': times - }) + ds = Dataset({'foo': (['time', 'x', 'y'], np.random.randn(10, 5, 3)), + 'bar': ('time', np.random.randn(10), {'meta': 'data'}), + 'time': times}) ds.attrs['dsmeta'] = 'dsdata' resampled_ds = ds.resample(time='1D').last(keep_attrs=False) @@ -3074,13 +2683,14 @@ def test_resample_drop_nondim_coords(self): data = np.tile(np.arange(5), (6, 3, 1)) xx, yy = np.meshgrid(xs * 5, ys * 2.5) tt = np.arange(len(times), dtype=int) - array = DataArray(data, {'time': times, - 'x': xs, - 'y': ys}, ('x', 'y', 'time')) + array = DataArray(data, + {'time': times, 'x': xs, 'y': ys}, + ('x', 'y', 'time')) xcoord = DataArray(xx.T, {'x': xs, 'y': ys}, ('x', 'y')) ycoord = DataArray(yy.T, {'x': xs, 'y': ys}, ('x', 'y')) tcoord = DataArray(tt, {'time': times}, ('time', )) - ds = Dataset({'data': array, 'xc': xcoord, 'yc': ycoord, 'tc': tcoord}) + ds = Dataset({'data': array, 'xc': xcoord, + 'yc': ycoord, 'tc': tcoord}) ds = ds.set_coords(['xc', 'yc', 'tc']) # Re-sample @@ -3098,13 +2708,9 @@ def test_resample_drop_nondim_coords(self): def test_resample_old_vs_new_api(self): times = pd.date_range('2000-01-01', freq='6H', periods=10) - ds = Dataset({ - 'foo': (['time', 'x', 'y'], np.random.randn(10, 5, 3)), - 'bar': ('time', np.random.randn(10), { - 'meta': 'data' - }), - 'time': times - }) + ds = Dataset({'foo': (['time', 'x', 'y'], np.random.randn(10, 5, 3)), + 'bar': ('time', np.random.randn(10), {'meta': 'data'}), + 'time': times}) ds.attrs['dsmeta'] = 'dsdata' for method in ['mean', 'sum', 'count', 'first', 'last']: @@ -3117,12 +2723,8 @@ def test_resample_old_vs_new_api(self): assert_identical(new_api, old_api) def test_to_array(self): - ds = Dataset( - OrderedDict([('a', 1), ('b', ('x', [1, 2, 3]))]), - coords={'c': 42}, - attrs={ - 'Conventions': 'None' - }) + ds = Dataset(OrderedDict([('a', 1), ('b', ('x', [1, 2, 3]))]), + coords={'c': 42}, attrs={'Conventions': 'None'}) data = [[1, 1, 1], [1, 2, 3]] coords = {'c': 42, 'variable': ['a', 'b']} dims = ('variable', 'x') @@ -3138,12 +2740,11 @@ 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')) + 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) @@ -3167,8 +2768,8 @@ def test_to_and_from_dataframe(self): assert expected.equals(actual) # check roundtrip - assert_identical( - ds.assign_coords(x=[0, 1]), Dataset.from_dataframe(actual)) + assert_identical(ds.assign_coords(x=[0, 1]), + Dataset.from_dataframe(actual)) # check pathological cases df = pd.DataFrame([1]) @@ -3184,30 +2785,23 @@ def test_to_and_from_dataframe(self): # GH697 df = pd.DataFrame({'A': []}) actual = Dataset.from_dataframe(df) - expected = Dataset({ - 'A': DataArray([], dims=('index', )) - }, { - 'index': [] - }) + expected = Dataset({'A': DataArray([], dims=('index',))}, + {'index': []}) assert_identical(expected, actual) # regression test for GH278 # use int64 to ensure consistent results for the pandas .equals method # on windows (which requires the same dtype) - ds = Dataset({ - 'x': pd.Index(['bar']), - 'a': ('y', np.array([1], 'int64')) - }).isel(x=0) + ds = Dataset({'x': pd.Index(['bar']), + 'a': ('y', np.array([1], 'int64'))}).isel(x=0) # use .loc to ensure consistent results on Python 3 actual = ds.to_dataframe().loc[:, ['a', 'x']] - expected = pd.DataFrame( - [[1, 'bar']], index=pd.Index([0], name='y'), columns=['a', 'x']) + expected = pd.DataFrame([[1, 'bar']], index=pd.Index([0], name='y'), + columns=['a', 'x']) assert expected.equals(actual), (expected, actual) - ds = Dataset({ - 'x': np.array([0], 'int64'), - 'y': np.array([1], 'int64') - }) + ds = Dataset({'x': np.array([0], 'int64'), + 'y': np.array([1], 'int64')}) actual = ds.to_dataframe() idx = pd.MultiIndex.from_arrays([[0], [1]], names=['x', 'y']) expected = pd.DataFrame([[]], index=idx) @@ -3222,25 +2816,18 @@ def test_from_dataframe_non_unique_columns(self): def test_convert_dataframe_with_many_types_and_multiindex(self): # regression test for GH737 - df = pd.DataFrame({ - 'a': - list('abc'), - 'b': - list(range(1, 4)), - 'c': - np.arange(3, 6).astype('u1'), - 'd': - np.arange(4.0, 7.0, dtype='float64'), - 'e': [True, False, True], - 'f': - pd.Categorical(list('abc')), - 'g': - pd.date_range('20130101', periods=3), - 'h': - pd.date_range('20130101', periods=3, tz='US/Eastern') - }) - df.index = pd.MultiIndex.from_product( - [['a'], range(3)], names=['one', 'two']) + df = pd.DataFrame({'a': list('abc'), + 'b': list(range(1, 4)), + 'c': np.arange(3, 6).astype('u1'), + 'd': np.arange(4.0, 7.0, dtype='float64'), + 'e': [True, False, True], + 'f': pd.Categorical(list('abc')), + 'g': pd.date_range('20130101', periods=3), + 'h': pd.date_range('20130101', + periods=3, + tz='US/Eastern')}) + df.index = pd.MultiIndex.from_product([['a'], range(3)], + names=['one', 'two']) roundtripped = Dataset.from_dataframe(df).to_dataframe() # we can't do perfectly, but we should be at least as faithful as # np.asarray @@ -3258,33 +2845,20 @@ def test_to_and_from_dict(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 = { - 'coords': { - 't': { - 'dims': ('t', ), - 'data': t, - 'attrs': {} - } - }, - 'attrs': {}, - 'dims': { - 't': 10 - }, - 'data_vars': { - 'a': { - 'dims': ('t', ), - 'data': x.tolist(), - 'attrs': {} - }, - 'b': { - 'dims': ('t', ), - 'data': y.tolist(), - 'attrs': {} - } - } - } + ds = Dataset(OrderedDict([('a', ('t', x)), + ('b', ('t', y)), + ('t', ('t', t))])) + expected = {'coords': {'t': {'dims': ('t',), + 'data': t, + 'attrs': {}}}, + 'attrs': {}, + 'dims': {'t': 10}, + 'data_vars': {'a': {'dims': ('t',), + 'data': x.tolist(), + 'attrs': {}}, + 'b': {'dims': ('t',), + 'data': y.tolist(), + 'attrs': {}}}} actual = ds.to_dict() @@ -3304,58 +2878,22 @@ def test_to_and_from_dict(self): # this one has no attrs field, the dims are strings, and x, y are # np.arrays - d = { - 'coords': { - 't': { - 'dims': 't', - 'data': t - } - }, - 'dims': 't', - 'data_vars': { - 'a': { - 'dims': 't', - 'data': x - }, - 'b': { - 'dims': 't', - 'data': y - } - } - } + d = {'coords': {'t': {'dims': 't', 'data': t}}, + 'dims': 't', + 'data_vars': {'a': {'dims': 't', 'data': x}, + 'b': {'dims': 't', 'data': y}}} assert_identical(ds, Dataset.from_dict(d)) # this is kind of a flattened version with no coords, or data_vars - d = { - 'a': { - 'dims': 't', - 'data': x - }, - 't': { - 'data': t, - 'dims': 't' - }, - 'b': { - 'dims': 't', - 'data': y - } - } + d = {'a': {'dims': 't', 'data': x}, + 't': {'data': t, 'dims': 't'}, + 'b': {'dims': 't', 'data': y}} assert_identical(ds, Dataset.from_dict(d)) # this one is missing some necessary information - d = { - 'a': { - 'data': x - }, - 't': { - 'data': t, - 'dims': 't' - }, - 'b': { - 'dims': 't', - 'data': y - } - } + d = {'a': {'data': x}, + 't': {'data': t, 'dims': 't'}, + 'b': {'dims': 't', 'data': y}} with raises_regex(ValueError, "cannot convert dict " "without the key 'dims'"): Dataset.from_dict(d) @@ -3365,9 +2903,10 @@ def test_to_and_from_dict_with_time_dim(self): y = np.random.randn(10, 3) t = pd.date_range('20130101', periods=10) lat = [77.7, 83.2, 76] - ds = Dataset( - OrderedDict([('a', (['t', 'lat'], x)), ('b', (['t', 'lat'], y)), - ('t', ('t', t)), ('lat', ('lat', lat))])) + ds = Dataset(OrderedDict([('a', (['t', 'lat'], x)), + ('b', (['t', 'lat'], y)), + ('t', ('t', t)), + ('lat', ('lat', lat))])) roundtripped = Dataset.from_dict(ds.to_dict()) assert_identical(ds, roundtripped) @@ -3379,9 +2918,10 @@ def test_to_and_from_dict_with_nan_nat(self): t[2] = np.nan lat = [77.7, 83.2, 76] - ds = Dataset( - OrderedDict([('a', (['t', 'lat'], x)), ('b', (['t', 'lat'], y)), - ('t', ('t', t)), ('lat', ('lat', lat))])) + ds = Dataset(OrderedDict([('a', (['t', 'lat'], x)), + ('b', (['t', 'lat'], y)), + ('t', ('t', t)), + ('lat', ('lat', lat))])) roundtripped = Dataset.from_dict(ds.to_dict()) assert_identical(ds, roundtripped) @@ -3390,19 +2930,15 @@ def test_to_dict_with_numpy_attrs(self): x = np.random.randn(10) y = np.random.randn(10) t = list('abcdefghij') - attrs = { - 'created': np.float64(1998), - 'coords': np.array([37, -110.1, 100]), - 'maintainer': 'bar' - } - ds = Dataset( - OrderedDict([('a', ('t', x, attrs)), ('b', ('t', y, attrs)), - ('t', ('t', t))])) - expected_attrs = { - 'created': np.asscalar(attrs['created']), - 'coords': attrs['coords'].tolist(), - 'maintainer': 'bar' - } + attrs = {'created': np.float64(1998), + 'coords': np.array([37, -110.1, 100]), + 'maintainer': 'bar'} + ds = Dataset(OrderedDict([('a', ('t', x, attrs)), + ('b', ('t', y, attrs)), + ('t', ('t', t))])) + expected_attrs = {'created': np.asscalar(attrs['created']), + 'coords': attrs['coords'].tolist(), + 'maintainer': 'bar'} actual = ds.to_dict() # check that they are identical @@ -3491,7 +3027,8 @@ def test_dropna(self): ds.dropna('a', how=None) def test_fillna(self): - ds = Dataset({'a': ('x', [np.nan, 1, np.nan, 3])}, {'x': [0, 1, 2, 3]}) + ds = Dataset({'a': ('x', [np.nan, 1, np.nan, 3])}, + {'x': [0, 1, 2, 3]}) # fill with -1 actual = ds.fillna(-1) @@ -3526,12 +3063,8 @@ def test_fillna(self): # okay to only include some data variables ds['b'] = np.nan actual = ds.fillna({'a': -1}) - expected = Dataset({ - 'a': ('x', [-1, 1, -1, 3]), - 'b': np.nan - }, { - 'x': [0, 1, 2, 3] - }) + expected = Dataset({'a': ('x', [-1, 1, -1, 3]), 'b': np.nan}, + {'x': [0, 1, 2, 3]}) assert_identical(expected, actual) # but new data variables is not okay @@ -3669,21 +3202,17 @@ def test_where_drop(self): ds.where(np.arange(5) > 1, drop=True) # 1d with odd coordinates - array = DataArray( - np.array([2, 7, 1, 8, 3]), - coords=[np.array([3, 1, 4, 5, 9])], - dims=['x']) - expected = DataArray( - np.array([7, 8, 3]), coords=[np.array([1, 5, 9])], dims=['x']) + array = DataArray(np.array([2, 7, 1, 8, 3]), + coords=[np.array([3, 1, 4, 5, 9])], dims=['x']) + expected = DataArray(np.array([7, 8, 3]), coords=[np.array([1, 5, 9])], + dims=['x']) actual = array.where(array > 2, drop=True) assert_identical(expected, actual) # 1d multiple variables ds = Dataset({'a': (('x'), [0, 1, 2, 3]), 'b': (('x'), [4, 5, 6, 7])}) - expected = Dataset({ - 'a': (('x'), [np.nan, 1, 2, 3]), - 'b': (('x'), [4, 5, 6, np.nan]) - }) + expected = Dataset({'a': (('x'), [np.nan, 1, 2, 3]), + 'b': (('x'), [4, 5, 6, np.nan])}) actual = ds.where((ds > 0) & (ds < 7), drop=True) assert_identical(expected, actual) @@ -3694,44 +3223,28 @@ def test_where_drop(self): assert_identical(expected, actual) # 2d with odd coordinates - ds = Dataset( - { - 'a': (('x', 'y'), [[0, 1], [2, 3]]) - }, - coords={ - 'x': [4, 3], - 'y': [1, 2], - 'z': (['x', 'y'], [[np.e, np.pi], [np.pi * np.e, np.pi * 3]]) - }) - expected = Dataset( - { - 'a': (('x', 'y'), [[3]]) - }, - coords={ - 'x': [3], - 'y': [2], - 'z': (['x', 'y'], [[np.pi * 3]]) - }) + ds = Dataset({'a': (('x', 'y'), [[0, 1], [2, 3]])}, coords={ + 'x': [4, 3], 'y': [1, 2], + 'z': (['x', 'y'], [[np.e, np.pi], [np.pi * np.e, np.pi * 3]])}) + expected = Dataset({'a': (('x', 'y'), [[3]])}, + coords={'x': [3], 'y': [2], + 'z': (['x', 'y'], [[np.pi * 3]])}) actual = ds.where(ds > 2, drop=True) assert_identical(expected, actual) # 2d multiple variables - ds = Dataset({ - 'a': (('x', 'y'), [[0, 1], [2, 3]]), - 'b': (('x', 'y'), [[4, 5], [6, 7]]) - }) - expected = Dataset({ - 'a': (('x', 'y'), [[np.nan, 1], [2, 3]]), - 'b': (('x', 'y'), [[4, 5], [6, 7]]) - }) + ds = Dataset({'a': (('x', 'y'), [[0, 1], [2, 3]]), + 'b': (('x', 'y'), [[4, 5], [6, 7]])}) + expected = Dataset({'a': (('x', 'y'), [[np.nan, 1], [2, 3]]), + 'b': (('x', 'y'), [[4, 5], [6, 7]])}) actual = ds.where(ds > 0, drop=True) assert_identical(expected, actual) def test_where_drop_empty(self): # regression test for GH1341 - array = DataArray( - np.random.rand(100, 10), dims=['nCells', 'nVertLevels']) - mask = DataArray(np.zeros((100, ), dtype='bool'), dims='nCells') + array = DataArray(np.random.rand(100, 10), + dims=['nCells', 'nVertLevels']) + mask = DataArray(np.zeros((100,), dtype='bool'), dims='nCells') actual = array.where(mask, drop=True) expected = DataArray(np.zeros((0, 10)), dims=['nCells', 'nVertLevels']) assert_identical(expected, actual) @@ -3748,16 +3261,17 @@ def test_reduce(self): assert len(data.mean().coords) == 0 actual = data.max() - expected = Dataset( - dict((k, v.max()) for k, v in iteritems(data.data_vars))) + expected = Dataset(dict((k, v.max()) + for k, v in iteritems(data.data_vars))) assert_equal(expected, actual) - assert_equal(data.min(dim=['dim1']), data.min(dim='dim1')) + assert_equal(data.min(dim=['dim1']), + data.min(dim='dim1')) - for reduct, expected in [('dim2', ['dim1', 'dim3', 'time']), ([ - 'dim2', 'time' - ], ['dim1', 'dim3']), (('dim2', 'time'), ['dim1', 'dim3']), - ((), ['dim1', 'dim2', 'dim3', 'time'])]: + for reduct, expected in [('dim2', ['dim1', 'dim3', 'time']), + (['dim2', 'time'], ['dim1', 'dim3']), + (('dim2', 'time'), ['dim1', 'dim3']), + ((), ['dim1', 'dim2', 'dim3', 'time'])]: actual = data.min(dim=reduct).dims print(reduct, actual, expected) self.assertItemsEqual(actual, expected) @@ -3765,15 +3279,12 @@ def test_reduce(self): assert_equal(data.mean(dim=[]), data) # uint support - data = xr.Dataset({ - 'a': (('x', 'y'), np.arange(6).reshape(3, 2).astype('uint')), - 'b': (('x', ), np.array([0.1, 0.2, np.nan])) - }) + data = xr.Dataset({'a': (('x', 'y'), + np.arange(6).reshape(3, 2).astype('uint')), + 'b': (('x', ), np.array([0.1, 0.2, np.nan]))}) actual = data.mean('x', skipna=True) - expected = xr.Dataset({ - 'a': data['a'].mean('x'), - 'b': data['b'].mean('x', skipna=True) - }) + expected = xr.Dataset({'a': data['a'].mean('x'), + 'b': data['b'].mean('x', skipna=True)}) assert_identical(actual, expected) def test_reduce_bad_dim(self): @@ -3794,11 +3305,12 @@ def test_reduce_cumsum_test_dims(self): getattr(data, cumfunc)(dim='bad_dim') # ensure dimensions are correct - for reduct, expected in [('dim1', [ - 'dim1', 'dim2', 'dim3', 'time' - ]), ('dim2', ['dim1', 'dim2', 'dim3', - 'time']), ('dim3', ['dim1', 'dim2', 'dim3', 'time']), - ('time', ['dim1', 'dim2', 'dim3'])]: + for reduct, expected in [ + ('dim1', ['dim1', 'dim2', 'dim3', 'time']), + ('dim2', ['dim1', 'dim2', 'dim3', 'time']), + ('dim3', ['dim1', 'dim2', 'dim3', 'time']), + ('time', ['dim1', 'dim2', 'dim3']) + ]: actual = getattr(data, cumfunc)(dim=reduct).dims print(reduct, actual, expected) self.assertItemsEqual(actual, expected) @@ -3814,7 +3326,8 @@ def test_reduce_non_numeric(self): assert 'var4' not in data1.mean() assert_equal(data1.mean(), data2.mean()) - assert_equal(data1.mean(dim='dim1'), data2.mean(dim='dim1')) + assert_equal(data1.mean(dim='dim1'), + data2.mean(dim='dim1')) def test_reduce_strings(self): expected = Dataset({'x': 'a'}) @@ -3895,6 +3408,7 @@ def test_reduce_scalars(self): assert_identical(expected, actual) def test_reduce_only_one_axis(self): + def mean_only_one_axis(x, axis): if not isinstance(axis, integer_types): raise TypeError('non-integer axis') @@ -3921,8 +3435,8 @@ def test_quantile(self): assert 'quantile' in ds_quantile for var, dar in ds.data_vars.items(): assert var in ds_quantile - assert_identical(ds_quantile[var], dar.quantile( - q, dim=dim)) + assert_identical( + ds_quantile[var], dar.quantile(q, dim=dim)) dim = ['dim1', 'dim2'] ds_quantile = ds.quantile(q, dim=dim) assert 'dim3' in ds_quantile.dims @@ -3961,8 +3475,8 @@ def test_apply(self): actual = data.apply(lambda x: x.mean(keep_attrs=True), keep_attrs=True) assert_identical(expected, actual) - assert_identical( - data.apply(lambda x: x, keep_attrs=True), data.drop('time')) + assert_identical(data.apply(lambda x: x, keep_attrs=True), + data.drop('time')) def scale(x, multiple=1): return multiple * x @@ -3976,10 +3490,11 @@ def scale(x, multiple=1): assert_equal(expected, actual) def make_example_math_dataset(self): - variables = OrderedDict([('bar', ('x', np.arange(100, 400, 100))), - ('foo', (('x', 'y'), - 1.0 * np.arange(12).reshape(3, 4)))]) - coords = {'abc': ('x', ['a', 'b', 'c']), 'y': 10 * np.arange(4)} + variables = OrderedDict( + [('bar', ('x', np.arange(100, 400, 100))), + ('foo', (('x', 'y'), 1.0 * np.arange(12).reshape(3, 4)))]) + coords = {'abc': ('x', ['a', 'b', 'c']), + 'y': 10 * np.arange(4)} ds = Dataset(variables, coords) ds['foo'][0, 0] = np.nan return ds @@ -4003,10 +3518,9 @@ def test_unary_ops(self): assert_identical(ds.apply(abs), abs(ds)) assert_identical(ds.apply(lambda x: x + 4), ds + 4) - for func in [ - lambda x: x.isnull(), lambda x: x.round(), - lambda x: x.astype(int) - ]: + for func in [lambda x: x.isnull(), + lambda x: x.round(), + lambda x: x.astype(int)]: assert_identical(ds.apply(func), func(ds)) assert_identical(ds.isnull(), ~ds.notnull()) @@ -4114,10 +3628,8 @@ def test_dataset_math_errors(self): assert_identical(actual, ds) def test_dataset_transpose(self): - ds = Dataset({ - 'a': (('x', 'y'), np.random.randn(3, 4)), - 'b': (('y', 'x'), np.random.randn(4, 3)) - }) + ds = Dataset({'a': (('x', 'y'), np.random.randn(3, 4)), + 'b': (('y', 'x'), np.random.randn(4, 3))}) actual = ds.transpose() expected = ds.apply(lambda x: x.transpose()) @@ -4178,12 +3690,12 @@ def test_dataset_diff_n1(self): ds = create_test_data(seed=1) actual = ds.diff('dim2') expected = dict() - expected['var1'] = DataArray( - np.diff(ds['var1'].values, axis=1), - {'dim2': ds['dim2'].values[1:]}, ['dim1', 'dim2']) - expected['var2'] = DataArray( - np.diff(ds['var2'].values, axis=1), - {'dim2': ds['dim2'].values[1:]}, ['dim1', 'dim2']) + expected['var1'] = DataArray(np.diff(ds['var1'].values, axis=1), + {'dim2': ds['dim2'].values[1:]}, + ['dim1', 'dim2']) + expected['var2'] = DataArray(np.diff(ds['var2'].values, axis=1), + {'dim2': ds['dim2'].values[1:]}, + ['dim1', 'dim2']) expected['var3'] = ds['var3'] expected = Dataset(expected, coords={'time': ds['time'].values}) expected.coords['numbers'] = ('dim3', ds['numbers'].values) @@ -4193,12 +3705,12 @@ def test_dataset_diff_n2(self): ds = create_test_data(seed=1) actual = ds.diff('dim2', n=2) expected = dict() - expected['var1'] = DataArray( - np.diff(ds['var1'].values, axis=1, n=2), - {'dim2': ds['dim2'].values[2:]}, ['dim1', 'dim2']) - expected['var2'] = DataArray( - np.diff(ds['var2'].values, axis=1, n=2), - {'dim2': ds['dim2'].values[2:]}, ['dim1', 'dim2']) + expected['var1'] = DataArray(np.diff(ds['var1'].values, axis=1, n=2), + {'dim2': ds['dim2'].values[2:]}, + ['dim1', 'dim2']) + expected['var2'] = DataArray(np.diff(ds['var2'].values, axis=1, n=2), + {'dim2': ds['dim2'].values[2:]}, + ['dim1', 'dim2']) expected['var3'] = ds['var3'] expected = Dataset(expected, coords={'time': ds['time'].values}) expected.coords['numbers'] = ('dim3', ds['numbers'].values) @@ -4261,15 +3773,10 @@ def test_filter_by_attrs(self): precip = dict(standard_name='convective_precipitation_flux') temp0 = dict(standard_name='air_potential_temperature', height='0 m') temp10 = dict(standard_name='air_potential_temperature', height='10 m') - ds = Dataset( - { - 'temperature_0': (['t'], [0], temp0), - 'temperature_10': (['t'], [0], temp10), - 'precipitation': (['t'], [0], precip) - }, - coords={ - 'time': (['t'], [0], dict(axis='T')) - }) + ds = Dataset({'temperature_0': (['t'], [0], temp0), + 'temperature_10': (['t'], [0], temp10), + 'precipitation': (['t'], [0], precip)}, + coords={'time': (['t'], [0], dict(axis='T'))}) # Test return empty Dataset. ds.filter_by_attrs(standard_name='invalid_standard_name') @@ -4279,8 +3786,8 @@ def test_filter_by_attrs(self): # Test return one DataArray. new_ds = ds.filter_by_attrs( standard_name='convective_precipitation_flux') - assert (new_ds['precipitation'].standard_name == - 'convective_precipitation_flux') + assert (new_ds['precipitation'].standard_name + == 'convective_precipitation_flux') assert_equal(new_ds['precipitation'], ds['precipitation']) @@ -4335,18 +3842,10 @@ def test_binary_op_join_setting(self): def test_full_like(self): # For more thorough tests, see test_variable.py # Note: testing data_vars with mismatched dtypes - ds = Dataset( - { - 'd1': - DataArray([1, 2, 3], dims=['x'], coords={ - 'x': [10, 20, 30] - }), - 'd2': - DataArray([1.1, 2.2, 3.3], dims=['y']) - }, - attrs={ - 'foo': 'bar' - }) + ds = Dataset({ + 'd1': DataArray([1, 2, 3], dims=['x'], coords={'x': [10, 20, 30]}), + 'd2': DataArray([1.1, 2.2, 3.3], dims=['y']) + }, attrs={'foo': 'bar'}) actual = full_like(ds, 2) expect = ds.copy(deep=True) @@ -4370,14 +3869,9 @@ def test_combine_first(self): dsx1 = DataArray([1, 1], [('x', ['b', 'c'])]).to_dataset(name='dsx1') actual = dsx0.combine_first(dsx1) - expected = Dataset( - { - 'dsx0': ('x', [0, 0, np.nan]), - 'dsx1': ('x', [np.nan, 1, 1]) - }, - coords={ - 'x': ['a', 'b', 'c'] - }) + expected = Dataset({'dsx0': ('x', [0, 0, np.nan]), + 'dsx1': ('x', [np.nan, 1, 1])}, + coords={'x': ['a', 'b', 'c']}) assert_equal(actual, expected) assert_equal(actual, xr.merge([dsx0, dsx1])) @@ -4389,29 +3883,23 @@ def test_combine_first(self): assert_equal(actual, expected) def test_sortby(self): - ds = Dataset({ - 'A': - DataArray([[1, 2], [3, 4], [5, 6]], [('x', ['c', 'b', 'a']), - ('y', [1, 0])]), - 'B': - DataArray([[5, 6], [7, 8], [9, 10]], dims=['x', 'y']) - }) - - sorted1d = Dataset({ - 'A': - DataArray([[5, 6], [3, 4], [1, 2]], [('x', ['a', 'b', 'c']), - ('y', [1, 0])]), - 'B': - DataArray([[9, 10], [7, 8], [5, 6]], dims=['x', 'y']) - }) - - sorted2d = Dataset({ - 'A': - DataArray([[6, 5], [4, 3], [2, 1]], [('x', ['a', 'b', 'c']), - ('y', [0, 1])]), - 'B': - DataArray([[10, 9], [8, 7], [6, 5]], dims=['x', 'y']) - }) + ds = Dataset({'A': DataArray([[1, 2], [3, 4], [5, 6]], + [('x', ['c', 'b', 'a']), + ('y', [1, 0])]), + 'B': DataArray([[5, 6], [7, 8], [9, 10]], + dims=['x', 'y'])}) + + sorted1d = Dataset({'A': DataArray([[5, 6], [3, 4], [1, 2]], + [('x', ['a', 'b', 'c']), + ('y', [1, 0])]), + 'B': DataArray([[9, 10], [7, 8], [5, 6]], + dims=['x', 'y'])}) + + sorted2d = Dataset({'A': DataArray([[6, 5], [4, 3], [2, 1]], + [('x', ['a', 'b', 'c']), + ('y', [0, 1])]), + 'B': DataArray([[10, 9], [8, 7], [6, 5]], + dims=['x', 'y'])}) expected = sorted1d dax = DataArray([100, 99, 98], [('x', ['c', 'b', 'a'])]) @@ -4459,23 +3947,18 @@ def test_sortby(self): # test pandas.MultiIndex indices = (('b', 1), ('b', 0), ('a', 1), ('a', 0)) midx = pd.MultiIndex.from_tuples(indices, names=['one', 'two']) - ds_midx = Dataset({ - 'A': - DataArray([[1, 2], [3, 4], [5, 6], [7, 8]], [('x', midx), - ('y', [1, 0])]), - 'B': - DataArray([[5, 6], [7, 8], [9, 10], [11, 12]], dims=['x', 'y']) - }) + ds_midx = Dataset({'A': DataArray([[1, 2], [3, 4], [5, 6], [7, 8]], + [('x', midx), ('y', [1, 0])]), + 'B': DataArray([[5, 6], [7, 8], [9, 10], [11, 12]], + dims=['x', 'y'])}) actual = ds_midx.sortby('x') - midx_reversed = pd.MultiIndex.from_tuples( - tuple(reversed(indices)), names=['one', 'two']) - expected = Dataset({ - 'A': - DataArray([[7, 8], [5, 6], [3, 4], [1, 2]], [('x', midx_reversed), - ('y', [1, 0])]), - 'B': - DataArray([[11, 12], [9, 10], [7, 8], [5, 6]], dims=['x', 'y']) - }) + midx_reversed = pd.MultiIndex.from_tuples(tuple(reversed(indices)), + names=['one', 'two']) + expected = Dataset({'A': DataArray([[7, 8], [5, 6], [3, 4], [1, 2]], + [('x', midx_reversed), + ('y', [1, 0])]), + 'B': DataArray([[11, 12], [9, 10], [7, 8], [5, 6]], + dims=['x', 'y'])}) assert_equal(actual, expected) # multi-dim sort by coordinate objects @@ -4489,10 +3972,8 @@ def test_sortby(self): def test_attribute_access(self): ds = create_test_data(seed=1) - for key in [ - 'var1', 'var2', 'var3', 'time', 'dim1', 'dim2', 'dim3', - 'numbers' - ]: + for key in ['var1', 'var2', 'var3', 'time', 'dim1', + 'dim2', 'dim3', 'numbers']: assert_equal(ds[key], getattr(ds, key)) assert key in dir(ds) @@ -4506,9 +3987,8 @@ def test_attribute_access(self): def test_ipython_key_completion(self): ds = create_test_data(seed=1) actual = ds._ipython_key_completions_() - expected = [ - 'var1', 'var2', 'var3', 'time', 'dim1', 'dim2', 'dim3', 'numbers' - ] + expected = ['var1', 'var2', 'var3', 'time', 'dim1', + 'dim2', 'dim3', 'numbers'] for item in actual: ds[item] # should not raise assert sorted(actual) == sorted(expected) @@ -4523,10 +4003,8 @@ def test_ipython_key_completion(self): # MultiIndex ds_midx = ds.stack(dim12=['dim1', 'dim2']) actual = ds_midx._ipython_key_completions_() - expected = [ - 'var1', 'var2', 'var3', 'time', 'dim1', 'dim2', 'dim3', 'numbers', - 'dim12' - ] + expected = ['var1', 'var2', 'var3', 'time', 'dim1', + 'dim2', 'dim3', 'numbers', 'dim12'] for item in actual: ds_midx[item] # should not raise assert sorted(actual) == sorted(expected) @@ -4551,7 +4029,6 @@ def test_ipython_key_completion(self): ds.data_vars[item] # should not raise assert sorted(actual) == sorted(expected) - # Py.test tests @@ -4562,7 +4039,8 @@ def data_set(seed=None): def test_dir_expected_attrs(data_set): - some_expected_attrs = {'pipe', 'mean', 'isnull', 'var1', 'dim2', 'numbers'} + some_expected_attrs = {'pipe', 'mean', 'isnull', 'var1', + 'dim2', 'numbers'} result = dir(data_set) assert set(result) >= some_expected_attrs @@ -4583,27 +4061,21 @@ def test_dir_unicode(data_set): @pytest.fixture(params=[1]) def ds(request): if request.param == 1: - return Dataset({ - 'z1': (['y', 'x'], np.random.randn(2, 8)), - 'z2': (['time', 'y'], np.random.randn(10, 2)) - }, { - 'x': ('x', np.linspace(0, 1.0, 8)), - 'time': ('time', np.linspace(0, 1.0, 10)), - 'c': ('y', ['a', 'b']), - 'y': range(2) - }) + return Dataset({'z1': (['y', 'x'], np.random.randn(2, 8)), + 'z2': (['time', 'y'], np.random.randn(10, 2))}, + {'x': ('x', np.linspace(0, 1.0, 8)), + 'time': ('time', np.linspace(0, 1.0, 10)), + 'c': ('y', ['a', 'b']), + 'y': range(2)}) if request.param == 2: - return Dataset({ - 'z1': (['time', 'y'], np.random.randn(10, 2)), - 'z2': (['time'], np.random.randn(10)), - 'z3': (['x', 'time'], np.random.randn(8, 10)) - }, { - 'x': ('x', np.linspace(0, 1.0, 8)), - 'time': ('time', np.linspace(0, 1.0, 10)), - 'c': ('y', ['a', 'b']), - 'y': range(2) - }) + return Dataset({'z1': (['time', 'y'], np.random.randn(10, 2)), + 'z2': (['time'], np.random.randn(10)), + 'z3': (['x', 'time'], np.random.randn(8, 10))}, + {'x': ('x', np.linspace(0, 1.0, 8)), + 'time': ('time', np.linspace(0, 1.0, 10)), + 'c': ('y', ['a', 'b']), + 'y': range(2)}) def test_rolling_properties(ds): @@ -4638,8 +4110,8 @@ def test_rolling_wrapped_bottleneck(ds, name, center, min_periods, key): if key is 'z1': # z1 does not depend on 'Time' axis. Stored as it is. expected = ds[key] elif key is 'z2': - expected = getattr(bn, func_name)( - ds[key].values, window=7, axis=0, min_count=min_periods) + expected = getattr(bn, func_name)(ds[key].values, window=7, axis=0, + min_count=min_periods) assert_array_equal(actual[key].values, expected) # Test center @@ -4652,25 +4124,23 @@ def test_rolling_wrapped_bottleneck(ds, name, center, min_periods, key): @pytest.mark.parametrize('min_periods', (None, 1, 2, 3)) @pytest.mark.parametrize('window', (1, 2, 3, 4)) def test_rolling_pandas_compat(center, window, min_periods): - df = pd.DataFrame({ - 'x': np.random.randn(20), - 'y': np.random.randn(20), - 'time': np.linspace(0, 1, 20) - }) + df = pd.DataFrame({'x': np.random.randn(20), 'y': np.random.randn(20), + 'time': np.linspace(0, 1, 20)}) ds = Dataset.from_dataframe(df) if min_periods is not None and window < min_periods: min_periods = window - df_rolling = df.rolling( - window, center=center, min_periods=min_periods).mean() - ds_rolling = ds.rolling( - index=window, center=center, min_periods=min_periods).mean() + df_rolling = df.rolling(window, center=center, + min_periods=min_periods).mean() + ds_rolling = ds.rolling(index=window, center=center, + min_periods=min_periods).mean() # pandas does some fancy stuff in the last position, # we're not going to do that yet! np.testing.assert_allclose(df_rolling['x'].values[:-1], ds_rolling['x'].values[:-1]) - np.testing.assert_allclose(df_rolling.index, ds_rolling['index']) + np.testing.assert_allclose(df_rolling.index, + ds_rolling['index']) @pytest.mark.slow @@ -4688,8 +4158,8 @@ def test_rolling_reduce(ds, center, min_periods, window, name): if name == 'std' and window == 1: pytest.skip('std with window == 1 is unstable in bottleneck') - rolling_obj = ds.rolling( - time=window, center=center, min_periods=min_periods) + rolling_obj = ds.rolling(time=window, center=center, + min_periods=min_periods) # add nan prefix to numpy methods to get similar behavior as bottleneck actual = rolling_obj.reduce(getattr(np, 'nan%s' % name)) From a78169b8452583dee858f4d80942611dd5ba12c3 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 19:31:43 -0500 Subject: [PATCH 31/41] test_ufuncs normal again --- xarray/tests/test_ufuncs.py | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/xarray/tests/test_ufuncs.py b/xarray/tests/test_ufuncs.py index 1b13b1d66cb..a42819605fa 100644 --- a/xarray/tests/test_ufuncs.py +++ b/xarray/tests/test_ufuncs.py @@ -8,7 +8,8 @@ import xarray.ufuncs as xu import xarray as xr -from . import (TestCase, raises_regex, assert_identical, assert_array_equal) +from . import ( + TestCase, raises_regex, assert_identical, assert_array_equal) class TestOps(TestCase): @@ -20,28 +21,20 @@ def assert_identical(self, a, b): assert_array_equal(a, b) def test_unary(self): - args = [ - 0, - np.zeros(2), - xr.Variable(['x'], [0, 0]), - xr.DataArray([0, 0], dims='x'), - xr.Dataset({ - 'y': ('x', [0, 0]) - }) - ] + args = [0, + np.zeros(2), + xr.Variable(['x'], [0, 0]), + xr.DataArray([0, 0], dims='x'), + xr.Dataset({'y': ('x', [0, 0])})] for a in args: self.assert_identical(a + 1, xu.cos(a)) def test_binary(self): - args = [ - 0, - np.zeros(2), - xr.Variable(['x'], [0, 0]), - xr.DataArray([0, 0], dims='x'), - xr.Dataset({ - 'y': ('x', [0, 0]) - }) - ] + args = [0, + np.zeros(2), + xr.Variable(['x'], [0, 0]), + xr.DataArray([0, 0], dims='x'), + xr.Dataset({'y': ('x', [0, 0])})] for n, t1 in enumerate(args): for t2 in args[n:]: self.assert_identical(t2 + 1, xu.maximum(t1, t2 + 1)) From b1a6766efc4f145cb59e2941391ce22846ed5964 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 19:37:04 -0500 Subject: [PATCH 32/41] final lint normalize --- xarray/tests/test_variable.py | 480 +++++++++++++++------------------- 1 file changed, 218 insertions(+), 262 deletions(-) diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index 295ec6e86c3..f49a5d7b501 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -15,16 +15,18 @@ from xarray import Variable, IndexVariable, Coordinate, Dataset from xarray.core import indexing from xarray.core.variable import as_variable, as_compatible_data -from xarray.core.indexing import ( - PandasIndexAdapter, LazilyIndexedArray, BasicIndexer, OuterIndexer, - VectorizedIndexer, NumpyIndexingAdapter, CopyOnWriteArray, - MemoryCachedArray, DaskIndexingAdapter) +from xarray.core.indexing import (PandasIndexAdapter, LazilyIndexedArray, + BasicIndexer, OuterIndexer, + VectorizedIndexer, NumpyIndexingAdapter, + CopyOnWriteArray, MemoryCachedArray, + DaskIndexingAdapter) from xarray.core.pycompat import PY3, OrderedDict from xarray.core.common import full_like, zeros_like, ones_like from xarray.core.utils import NDArrayMixin -from . import (TestCase, source_ndarray, requires_dask, raises_regex, - assert_identical, assert_array_equal) +from . import ( + TestCase, source_ndarray, requires_dask, raises_regex, assert_identical, + assert_array_equal) from xarray.tests import requires_bottleneck @@ -33,10 +35,10 @@ class VariableSubclassTestCases(object): def test_properties(self): data = 0.5 * np.arange(10) v = self.cls(['time'], data, {'foo': 'bar'}) - assert v.dims == ('time', ) + assert v.dims == ('time',) assert_array_equal(v.values, data) assert v.dtype == float - assert v.shape == (10, ) + assert v.shape == (10,) assert v.size == 10 assert v.sizes == {'time': 10} assert v.nbytes == 80 @@ -108,29 +110,25 @@ def test_getitem_1d_fancy(self): def test_getitem_with_mask(self): v = self.cls(['x'], [0, 1, 2]) assert_identical(v._getitem_with_mask(-1), Variable((), np.nan)) - assert_identical( - v._getitem_with_mask([0, -1, 1]), self.cls(['x'], [0, np.nan, 1])) - assert_identical( - v._getitem_with_mask(slice(2)), self.cls(['x'], [0, 1])) - assert_identical( - v._getitem_with_mask([0, -1, 1], fill_value=-99), - self.cls(['x'], [0, -99, 1])) + assert_identical(v._getitem_with_mask([0, -1, 1]), + self.cls(['x'], [0, np.nan, 1])) + assert_identical(v._getitem_with_mask(slice(2)), + self.cls(['x'], [0, 1])) + assert_identical(v._getitem_with_mask([0, -1, 1], fill_value=-99), + self.cls(['x'], [0, -99, 1])) def test_getitem_with_mask_size_zero(self): v = self.cls(['x'], []) assert_identical(v._getitem_with_mask(-1), Variable((), np.nan)) - assert_identical( - v._getitem_with_mask([-1, -1, -1]), - self.cls(['x'], [np.nan, np.nan, np.nan])) + assert_identical(v._getitem_with_mask([-1, -1, -1]), + self.cls(['x'], [np.nan, np.nan, np.nan])) def test_getitem_with_mask_nd_indexer(self): v = self.cls(['x'], [0, 1, 2]) indexer = Variable(('x', 'y'), [[0, -1], [-1, 2]]) assert_identical(v._getitem_with_mask(indexer, fill_value=-1), indexer) - def _assertIndexedLikeNDArray(self, - variable, - expected_value0, + def _assertIndexedLikeNDArray(self, variable, expected_value0, expected_dtype=None): """Given a 1-dimensional variable, verify that the variable is indexed like a numpy.ndarray. @@ -154,12 +152,14 @@ def _assertIndexedLikeNDArray(self, assert variable[0].values.dtype == expected_dtype def test_index_0d_int(self): - for value, dtype in [(0, np.int_), (np.int32(0), np.int32)]: + for value, dtype in [(0, np.int_), + (np.int32(0), np.int32)]: x = self.cls(['x'], [value]) self._assertIndexedLikeNDArray(x, value, dtype) def test_index_0d_float(self): - for value, dtype in [(0.5, np.float_), (np.float32(0.5), np.float32)]: + for value, dtype in [(0.5, np.float_), + (np.float32(0.5), np.float32)]: x = self.cls(['x'], [value]) self._assertIndexedLikeNDArray(x, value, dtype) @@ -184,12 +184,12 @@ def test_index_0d_timedelta64(self): td = timedelta(hours=1) x = self.cls(['x'], [np.timedelta64(td)]) - self._assertIndexedLikeNDArray(x, np.timedelta64(td), - 'timedelta64[ns]') + self._assertIndexedLikeNDArray( + x, np.timedelta64(td), 'timedelta64[ns]') x = self.cls(['x'], pd.to_timedelta([td])) - self._assertIndexedLikeNDArray(x, np.timedelta64(td), - 'timedelta64[ns]') + self._assertIndexedLikeNDArray( + x, np.timedelta64(td), 'timedelta64[ns]') def test_index_0d_not_a_time(self): d = np.datetime64('NaT', 'ns') @@ -197,6 +197,7 @@ def test_index_0d_not_a_time(self): self._assertIndexedLikeNDArray(x, d) def test_index_0d_object(self): + class HashableItemWrapper(object): def __init__(self, item): self.item = item @@ -215,7 +216,7 @@ def __repr__(self): self._assertIndexedLikeNDArray(x, item, expected_dtype=False) def test_0d_object_array_with_list(self): - listarray = np.empty((1, ), dtype=object) + listarray = np.empty((1,), dtype=object) listarray[0] = [1, 2, 3] x = self.cls('x', listarray) assert_array_equal(x.data, listarray) @@ -225,10 +226,8 @@ def test_0d_object_array_with_list(self): def test_index_and_concat_datetime(self): # regression test for #125 date_range = pd.date_range('2011-09-01', periods=10) - for dates in [ - date_range, date_range.values, - date_range.to_pydatetime() - ]: + for dates in [date_range, date_range.values, + date_range.to_pydatetime()]: expected = self.cls('t', dates) for times in [[expected[i] for i in range(10)], [expected[i:(i + 1)] for i in range(10)], @@ -285,8 +284,8 @@ def test_pandas_data(self): assert v[0].values == v.values[0] def test_pandas_period_index(self): - v = self.cls(['x'], pd.period_range( - start='2000', periods=20, freq='B')) + v = self.cls(['x'], pd.period_range(start='2000', periods=20, + freq='B')) v = v.load() # for dask-based Variable assert v[0] == pd.Period('2000', freq='B') assert "Period('2000-01-03', 'B')" in repr(v) @@ -350,23 +349,20 @@ def test_array_interface(self): assert_array_equal(v.astype(float), x.astype(float)) # think this is a break, that argsort changes the type assert_identical(v.argsort(), v.to_base_variable()) - assert_identical( - v.clip(2, 3), - self.cls('x', x.clip(2, 3)).to_base_variable()) + assert_identical(v.clip(2, 3), + self.cls('x', x.clip(2, 3)).to_base_variable()) # test ufuncs - assert_identical( - np.sin(v), - self.cls(['x'], np.sin(x)).to_base_variable()) + assert_identical(np.sin(v), + self.cls(['x'], np.sin(x)).to_base_variable()) assert isinstance(np.sin(v), Variable) assert not isinstance(np.sin(v), IndexVariable) def example_1d_objects(self): - for data in [ - range(3), 0.5 * np.arange(3), - 0.5 * np.arange(3, dtype=np.float32), - pd.date_range('2000-01-01', periods=3), - np.array(['a', 'b', 'c'], dtype=object) - ]: + for data in [range(3), + 0.5 * np.arange(3), + 0.5 * np.arange(3, dtype=np.float32), + pd.date_range('2000-01-01', periods=3), + np.array(['a', 'b', 'c'], dtype=object)]: yield (self.cls('x', data), data) def test___array__(self): @@ -401,16 +397,13 @@ def test_eq_all_dtypes(self): def test_encoding_preserved(self): expected = self.cls('x', range(3), {'foo': 1}, {'bar': 2}) - for actual in [ - expected.T, expected[...], - expected.squeeze(), - expected.isel(x=slice(None)), - expected.set_dims({ - 'x': 3 - }), - expected.copy(deep=True), - expected.copy(deep=False) - ]: + for actual in [expected.T, + expected[...], + expected.squeeze(), + expected.isel(x=slice(None)), + expected.set_dims({'x': 3}), + expected.copy(deep=True), + expected.copy(deep=False)]: assert_identical(expected.to_base_variable(), actual.to_base_variable()) @@ -421,22 +414,18 @@ def test_concat(self): y = np.arange(5, 10) v = self.cls(['a'], x) w = self.cls(['a'], y) - assert_identical( - Variable(['b', 'a'], np.array([x, y])), Variable.concat([v, w], - 'b')) - assert_identical( - Variable(['b', 'a'], np.array([x, y])), Variable.concat((v, w), - 'b')) - assert_identical( - Variable(['b', 'a'], np.array([x, y])), Variable.concat((v, w), - 'b')) + assert_identical(Variable(['b', 'a'], np.array([x, y])), + Variable.concat([v, w], 'b')) + assert_identical(Variable(['b', 'a'], np.array([x, y])), + Variable.concat((v, w), 'b')) + assert_identical(Variable(['b', 'a'], np.array([x, y])), + Variable.concat((v, w), 'b')) with raises_regex(ValueError, 'inconsistent dimensions'): Variable.concat([v, Variable(['c'], y)], 'b') # test indexers actual = Variable.concat( [v, w], - positions=[np.arange(0, 10, 2), - np.arange(1, 10, 2)], + positions=[np.arange(0, 10, 2), np.arange(1, 10, 2)], dim='a') expected = Variable('a', np.array([x, y]).ravel(order='F')) assert_identical(expected, actual) @@ -454,9 +443,8 @@ def test_concat_attrs(self): # different or conflicting attributes should be removed v = self.cls('a', np.arange(5), {'foo': 'bar'}) w = self.cls('a', np.ones(5)) - expected = self.cls('a', - np.concatenate([np.arange(5), - np.ones(5)])).to_base_variable() + expected = self.cls( + 'a', np.concatenate([np.arange(5), np.ones(5)])).to_base_variable() assert_identical(expected, Variable.concat([v, w], 'a')) w.attrs['foo'] = 2 assert_identical(expected, Variable.concat([v, w], 'a')) @@ -470,8 +458,8 @@ def test_concat_fixed_len_str(self): x = self.cls('animal', np.array(['horse'], dtype=kind)) y = self.cls('animal', np.array(['aardvark'], dtype=kind)) actual = Variable.concat([x, y], 'animal') - expected = Variable('animal', - np.array(['horse', 'aardvark'], dtype=kind)) + expected = Variable( + 'animal', np.array(['horse', 'aardvark'], dtype=kind)) self.assertVariableEqual(expected, actual) def test_concat_number_strings(self): @@ -501,8 +489,8 @@ def test_copy(self): assert_identical(v, copy(v)) def test_copy_index(self): - midx = pd.MultiIndex.from_product( - [['a', 'b'], [1, 2], [-1, -2]], names=('one', 'two', 'three')) + midx = pd.MultiIndex.from_product([['a', 'b'], [1, 2], [-1, -2]], + names=('one', 'two', 'three')) v = self.cls('x', midx) for deep in [True, False]: w = v.copy(deep=deep) @@ -518,8 +506,8 @@ def test_real_and_imag(self): expected_im = self.cls('x', -np.arange(3), {'foo': 'bar'}) assert_identical(v.imag, expected_im) - expected_abs = self.cls('x', np.sqrt( - 2 * np.arange(3) ** 2)).to_base_variable() + expected_abs = self.cls( + 'x', np.sqrt(2 * np.arange(3) ** 2)).to_base_variable() self.assertVariableAllClose(abs(v), expected_abs) def test_aggregate_complex(self): @@ -535,11 +523,9 @@ def test_pandas_cateogrical_dtype(self): assert v.dtype == 'int64' def test_pandas_datetime64_with_tz(self): - data = pd.date_range( - start='2000-01-01', - tz=pytz.timezone('America/New_York'), - periods=10, - freq='1h') + data = pd.date_range(start='2000-01-01', + tz=pytz.timezone('America/New_York'), + periods=10, freq='1h') v = self.cls('x', data) print(v) # should not error if 'America/New_York' in str(data.dtype): @@ -662,7 +648,7 @@ def test_getitem_fancy(self): # along diagonal ind = Variable(['a'], [0, 1]) v_new = v[ind, ind] - assert v_new.dims == ('a', ) + assert v_new.dims == ('a',) assert_array_equal(v_new, v_data[[0, 1], [0, 1]]) # with integer @@ -691,7 +677,7 @@ def test_getitem_fancy(self): # slice matches explicit dimension ind = Variable(['y'], [0, 1]) v_new = v[ind, :2] - assert v_new.dims == ('y', ) + assert v_new.dims == ('y',) assert_array_equal(v_new, v_data[[0, 1], [0, 1]]) # with multiple slices @@ -773,9 +759,9 @@ def test_numpy_same_methods(self): def test_datetime64_conversion_scalar(self): expected = np.datetime64('2000-01-01', 'ns') for values in [ - np.datetime64('2000-01-01'), - pd.Timestamp('2000-01-01T00'), - datetime(2000, 1, 1), + np.datetime64('2000-01-01'), + pd.Timestamp('2000-01-01T00'), + datetime(2000, 1, 1), ]: v = Variable([], values) assert v.dtype == np.dtype('datetime64[ns]') @@ -785,9 +771,9 @@ def test_datetime64_conversion_scalar(self): def test_timedelta64_conversion_scalar(self): expected = np.timedelta64(24 * 60 * 60 * 10 ** 9, 'ns') for values in [ - np.timedelta64(1, 'D'), - pd.Timedelta('1 day'), - timedelta(days=1), + np.timedelta64(1, 'D'), + pd.Timedelta('1 day'), + timedelta(days=1), ]: v = Variable([], values) assert v.dtype == np.dtype('timedelta64[ns]') @@ -817,16 +803,10 @@ def test_0d_timedelta(self): def test_equals_and_identical(self): d = np.random.rand(10, 3) d[0, 0] = np.nan - v1 = Variable( - ('dim1', 'dim2'), data=d, attrs={ - 'att1': 3, - 'att2': [1, 2, 3] - }) - v2 = Variable( - ('dim1', 'dim2'), data=d, attrs={ - 'att1': 3, - 'att2': [1, 2, 3] - }) + v1 = Variable(('dim1', 'dim2'), data=d, + attrs={'att1': 3, 'att2': [1, 2, 3]}) + v2 = Variable(('dim1', 'dim2'), data=d, + attrs={'att1': 3, 'att2': [1, 2, 3]}) assert v1.equals(v2) assert v1.identical(v2) @@ -887,10 +867,8 @@ def test_no_conflicts(self): def test_as_variable(self): data = np.arange(10) expected = Variable('x', data) - expected_extra = Variable( - 'x', data, attrs={'myattr': 'val'}, encoding={ - 'scale_factor': 1 - }) + expected_extra = Variable('x', data, attrs={'myattr': 'val'}, + encoding={'scale_factor': 1}) assert_identical(expected, as_variable(expected)) @@ -921,7 +899,8 @@ def test_as_variable(self): with raises_regex(TypeError, 'tuples to convert'): as_variable(tuple(data)) - with raises_regex(TypeError, 'without an explicit list of dimensions'): + with raises_regex( + TypeError, 'without an explicit list of dimensions'): as_variable(data) actual = as_variable(data, name='x') @@ -933,9 +912,11 @@ def test_as_variable(self): data = np.arange(9).reshape((3, 3)) expected = Variable(('x', 'y'), data) - with raises_regex(ValueError, 'without explicit dimension names'): + with raises_regex( + ValueError, 'without explicit dimension names'): as_variable(data, name='x') - with raises_regex(ValueError, 'has more than 1-dimension'): + with raises_regex( + ValueError, 'has more than 1-dimension'): as_variable(expected, name='x') def test_repr(self): @@ -1034,7 +1015,8 @@ def test_items(self): assert_identical(v, v[...]) assert_identical(Variable(['y'], data[0]), v[0]) assert_identical(Variable(['x'], data[:, 0]), v[:, 0]) - assert_identical(Variable(['x', 'y'], data[:3, :2]), v[:3, :2]) + assert_identical(Variable(['x', 'y'], data[:3, :2]), + v[:3, :2]) # test array indexing x = Variable(['x'], np.arange(10)) y = Variable(['y'], np.arange(11)) @@ -1083,9 +1065,8 @@ def test_getitem_basic(self): def test_getitem_with_mask_2d_input(self): v = Variable(('x', 'y'), [[0, 1, 2], [3, 4, 5]]) - assert_identical( - v._getitem_with_mask(([-1, 0], [1, -1])), - Variable(('x', 'y'), [[np.nan, np.nan], [1, np.nan]])) + assert_identical(v._getitem_with_mask(([-1, 0], [1, -1])), + Variable(('x', 'y'), [[np.nan, np.nan], [1, np.nan]])) assert_identical(v._getitem_with_mask((slice(2), [0, 1, 2])), v) def test_isel(self): @@ -1187,12 +1168,12 @@ def test_transpose(self): def test_transpose_0d(self): for value in [ - 3.5, - ('a', 1), - np.datetime64('2000-01-01'), - np.timedelta64(1, 'h'), - None, - object(), + 3.5, + ('a', 1), + np.datetime64('2000-01-01'), + np.timedelta64(1, 'h'), + None, + object(), ]: variable = Variable([], value) actual = variable.transpose() @@ -1215,7 +1196,7 @@ def test_squeeze(self): def test_get_axis_num(self): v = Variable(['x', 'y', 'z'], np.random.randn(2, 3, 4)) assert v.get_axis_num('x') == 0 - assert v.get_axis_num(['x']) == (0, ) + assert v.get_axis_num(['x']) == (0,) assert v.get_axis_num(['x', 'y']) == (0, 1) assert v.get_axis_num(['z', 'y', 'x']) == (2, 1, 0) with raises_regex(ValueError, 'not found in array dim'): @@ -1244,8 +1225,8 @@ def test_set_dims(self): def test_set_dims_object_dtype(self): v = Variable([], ('a', 1)) - actual = v.set_dims(('x', ), (3, )) - exp_values = np.empty((3, ), dtype=object) + actual = v.set_dims(('x',), (3,)) + exp_values = np.empty((3,), dtype=object) for i in range(3): exp_values[i] = ('a', 1) expected = Variable(['x'], exp_values) @@ -1257,14 +1238,14 @@ def test_stack(self): expected = Variable('z', [0, 1, 2, 3], v.attrs) assert_identical(actual, expected) - actual = v.stack(z=('x', )) + actual = v.stack(z=('x',)) expected = Variable(('y', 'z'), v.data.T, v.attrs) assert_identical(actual, expected) - actual = v.stack(z=(), ) + actual = v.stack(z=(),) assert_identical(actual, v) - actual = v.stack(X=('x', ), Y=('y', )).transpose('X', 'Y') + actual = v.stack(X=('x',), Y=('y',)).transpose('X', 'Y') expected = Variable(('X', 'Y'), v.data, v.attrs) assert_identical(actual, expected) @@ -1272,9 +1253,9 @@ def test_stack_errors(self): v = Variable(['x', 'y'], [[0, 1], [2, 3]], {'foo': 'bar'}) with raises_regex(ValueError, 'invalid existing dim'): - v.stack(z=('x1', )) + v.stack(z=('x1',)) with raises_regex(ValueError, 'cannot create a new dim'): - v.stack(x=('x', )) + v.stack(x=('x',)) def test_unstack(self): v = Variable('z', [0, 1, 2, 3], {'foo': 'bar'}) @@ -1295,7 +1276,7 @@ def test_unstack_errors(self): with raises_regex(ValueError, 'invalid existing dim'): v.unstack(foo={'x': 4}) with raises_regex(ValueError, 'cannot create a new dim'): - v.stack(z=('z', )) + v.stack(z=('z',)) with raises_regex(ValueError, 'the product of the new dim'): v.unstack(z={'x': 5}) @@ -1319,27 +1300,30 @@ def test_broadcasting_math(self): x = np.random.randn(2, 3) v = Variable(['a', 'b'], x) # 1d to 2d broadcasting - assert_identical(v * v, - Variable(['a', 'b'], np.einsum('ab,ab->ab', x, x))) - assert_identical(v * v[0], - Variable(['a', 'b'], np.einsum('ab,b->ab', x, x[0]))) - assert_identical(v[0] * v, - Variable(['b', 'a'], np.einsum('b,ab->ba', x[0], x))) - assert_identical(v[0] * v[:, 0], - Variable(['b', 'a'], - np.einsum('b,a->ba', x[0], x[:, 0]))) + assert_identical( + v * v, + Variable(['a', 'b'], np.einsum('ab,ab->ab', x, x))) + assert_identical( + v * v[0], + Variable(['a', 'b'], np.einsum('ab,b->ab', x, x[0]))) + assert_identical( + v[0] * v, + Variable(['b', 'a'], np.einsum('b,ab->ba', x[0], x))) + assert_identical( + v[0] * v[:, 0], + Variable(['b', 'a'], np.einsum('b,a->ba', x[0], x[:, 0]))) # higher dim broadcasting y = np.random.randn(3, 4, 5) w = Variable(['b', 'c', 'd'], y) - assert_identical(v * w, - Variable(['a', 'b', 'c', 'd'], - np.einsum('ab,bcd->abcd', x, y))) - assert_identical(w * v, - Variable(['b', 'c', 'd', 'a'], - np.einsum('bcd,ab->bcda', y, x))) - assert_identical(v * w[0], - Variable(['a', 'b', 'c', 'd'], - np.einsum('ab,cd->abcd', x, y[0]))) + assert_identical( + v * w, Variable(['a', 'b', 'c', 'd'], + np.einsum('ab,bcd->abcd', x, y))) + assert_identical( + w * v, Variable(['b', 'c', 'd', 'a'], + np.einsum('bcd,ab->bcda', y, x))) + assert_identical( + v * w[0], Variable(['a', 'b', 'c', 'd'], + np.einsum('ab,cd->abcd', x, y[0]))) def test_broadcasting_failures(self): a = Variable(['x'], np.arange(10)) @@ -1365,13 +1349,14 @@ def test_inplace_math(self): def test_reduce(self): v = Variable(['x', 'y'], self.d, {'ignored': 'attributes'}) - assert_identical( - v.reduce(np.std, 'x'), Variable(['y'], self.d.std(axis=0))) - assert_identical(v.reduce(np.std, axis=0), v.reduce(np.std, dim='x')) - assert_identical( - v.reduce(np.std, ['y', 'x']), Variable( - [], self.d.std(axis=(0, 1)))) - assert_identical(v.reduce(np.std), Variable([], self.d.std())) + assert_identical(v.reduce(np.std, 'x'), + Variable(['y'], self.d.std(axis=0))) + assert_identical(v.reduce(np.std, axis=0), + v.reduce(np.std, dim='x')) + assert_identical(v.reduce(np.std, ['y', 'x']), + Variable([], self.d.std(axis=(0, 1)))) + assert_identical(v.reduce(np.std), + Variable([], self.d.std())) assert_identical( v.reduce(np.mean, 'x').reduce(np.std, 'y'), Variable([], self.d.mean(axis=0).std())) @@ -1380,9 +1365,8 @@ def test_reduce(self): with raises_regex(ValueError, 'cannot supply both'): v.mean(dim='x', axis=0) - @pytest.mark.skipif( - LooseVersion(np.__version__) < LooseVersion('1.10.0'), - reason='requires numpy version 1.10.0 or later') + @pytest.mark.skipif(LooseVersion(np.__version__) < LooseVersion('1.10.0'), + reason='requires numpy version 1.10.0 or later') def test_quantile(self): v = Variable(['x', 'y'], self.d) for q in [0.25, [0.50], [0.25, 0.75]]: @@ -1390,8 +1374,8 @@ def test_quantile(self): [None, 'x', ['x'], ['x', 'y']]): actual = v.quantile(q, dim=dim) - expected = np.nanpercentile( - self.d, np.array(q) * 100, axis=axis) + expected = np.nanpercentile(self.d, np.array(q) * 100, + axis=axis) np.testing.assert_allclose(actual.values, expected) @requires_dask @@ -1449,10 +1433,10 @@ def test_reduce_funcs(self): assert_identical(np.mean(v), Variable([], 2)) assert_identical(v.prod(), Variable([], 6)) - assert_identical( - v.cumsum(axis=0), Variable('x', np.array([1, 1, 3, 6]))) - assert_identical( - v.cumprod(axis=0), Variable('x', np.array([1, 1, 2, 6]))) + assert_identical(v.cumsum(axis=0), + Variable('x', np.array([1, 1, 3, 6]))) + assert_identical(v.cumprod(axis=0), + Variable('x', np.array([1, 1, 2, 6]))) assert_identical(v.var(), Variable([], 2.0 / 3)) if LooseVersion(np.__version__) < '1.9': @@ -1468,7 +1452,8 @@ def test_reduce_funcs(self): v = Variable('t', pd.date_range('2000-01-01', periods=3)) with pytest.raises(NotImplementedError): v.max(skipna=True) - assert_identical(v.max(), Variable([], pd.Timestamp('2000-01-03'))) + assert_identical( + v.max(), Variable([], pd.Timestamp('2000-01-03'))) def test_reduce_keep_attrs(self): _attrs = {'units': 'test', 'long_name': 'testing'} @@ -1530,43 +1515,36 @@ def assert_assigned_2d(array, key_x, key_y, values): assert_array_equal(expected, v) # 1d vectorized indexing - assert_assigned_2d( - np.random.randn(4, 3), - key_x=Variable(['a'], [0, 1]), - key_y=Variable(['a'], [0, 1]), - values=0) - assert_assigned_2d( - np.random.randn(4, 3), - key_x=Variable(['a'], [0, 1]), - key_y=Variable(['a'], [0, 1]), - values=Variable((), 0)) - assert_assigned_2d( - np.random.randn(4, 3), - key_x=Variable(['a'], [0, 1]), - key_y=Variable(['a'], [0, 1]), - values=Variable(('a'), [3, 2])) - assert_assigned_2d( - np.random.randn(4, 3), - key_x=slice(None), - key_y=Variable(['a'], [0, 1]), - values=Variable(('a'), [3, 2])) + assert_assigned_2d(np.random.randn(4, 3), + key_x=Variable(['a'], [0, 1]), + key_y=Variable(['a'], [0, 1]), + values=0) + assert_assigned_2d(np.random.randn(4, 3), + key_x=Variable(['a'], [0, 1]), + key_y=Variable(['a'], [0, 1]), + values=Variable((), 0)) + assert_assigned_2d(np.random.randn(4, 3), + key_x=Variable(['a'], [0, 1]), + key_y=Variable(['a'], [0, 1]), + values=Variable(('a'), [3, 2])) + assert_assigned_2d(np.random.randn(4, 3), + key_x=slice(None), + key_y=Variable(['a'], [0, 1]), + values=Variable(('a'), [3, 2])) # 2d-vectorized indexing - assert_assigned_2d( - np.random.randn(4, 3), - key_x=Variable(['a', 'b'], [[0, 1]]), - key_y=Variable(['a', 'b'], [[1, 0]]), - values=0) - assert_assigned_2d( - np.random.randn(4, 3), - key_x=Variable(['a', 'b'], [[0, 1]]), - key_y=Variable(['a', 'b'], [[1, 0]]), - values=[0]) - assert_assigned_2d( - np.random.randn(5, 4), - key_x=Variable(['a', 'b'], [[0, 1], [2, 3]]), - key_y=Variable(['a', 'b'], [[1, 0], [3, 3]]), - values=[2, 3]) + assert_assigned_2d(np.random.randn(4, 3), + key_x=Variable(['a', 'b'], [[0, 1]]), + key_y=Variable(['a', 'b'], [[1, 0]]), + values=0) + assert_assigned_2d(np.random.randn(4, 3), + key_x=Variable(['a', 'b'], [[0, 1]]), + key_y=Variable(['a', 'b'], [[1, 0]]), + values=[0]) + assert_assigned_2d(np.random.randn(5, 4), + key_x=Variable(['a', 'b'], [[0, 1], [2, 3]]), + key_y=Variable(['a', 'b'], [[1, 0], [3, 3]]), + values=[2, 3]) # vindex with slice v = Variable(['x', 'y', 'z'], np.ones((4, 3, 2))) @@ -1644,9 +1622,8 @@ def test_getitem_with_mask_nd_indexer(self): import dask.array as da v = Variable(['x'], da.arange(3, chunks=3)) indexer = Variable(('x', 'y'), [[0, -1], [-1, 2]]) - assert_identical( - v._getitem_with_mask(indexer, fill_value=-1), - self.cls(('x', 'y'), [[0, -1], [-1, 2]])) + assert_identical(v._getitem_with_mask(indexer, fill_value=-1), + self.cls(('x', 'y'), [[0, -1], [-1, 2]])) class TestIndexVariable(TestCase, VariableSubclassTestCases): @@ -1684,16 +1661,16 @@ def test_name(self): coord.name = 'y' def test_level_names(self): - midx = pd.MultiIndex.from_product( - [['a', 'b'], [1, 2]], names=['level_1', 'level_2']) + midx = pd.MultiIndex.from_product([['a', 'b'], [1, 2]], + names=['level_1', 'level_2']) x = IndexVariable('x', midx) assert x.level_names == midx.names assert IndexVariable('y', [10.0]).level_names is None def test_get_level_variable(self): - midx = pd.MultiIndex.from_product( - [['a', 'b'], [1, 2]], names=['level_1', 'level_2']) + midx = pd.MultiIndex.from_product([['a', 'b'], [1, 2]], + names=['level_1', 'level_2']) x = IndexVariable('x', midx) level_1 = IndexVariable('x', midx.get_level_values('level_1')) assert_identical(x.get_level_variable('level_1'), level_1) @@ -1703,10 +1680,8 @@ def test_get_level_variable(self): def test_concat_periods(self): periods = pd.period_range('2000-01-01', periods=10) - coords = [ - IndexVariable('t', periods[:5]), - IndexVariable('t', periods[5:]) - ] + coords = [IndexVariable('t', periods[:5]), + IndexVariable('t', periods[5:])] expected = IndexVariable('t', periods) actual = IndexVariable.concat(coords, dim='t') assert actual.identical(expected) @@ -1753,11 +1728,9 @@ class TestAsCompatibleData(TestCase): def test_unchanged_types(self): types = (np.asarray, PandasIndexAdapter, indexing.LazilyIndexedArray) for t in types: - for data in [ - np.arange(3), - pd.date_range('2000-01-01', periods=3), - pd.date_range('2000-01-01', periods=3).values - ]: + for data in [np.arange(3), + pd.date_range('2000-01-01', periods=3), + pd.date_range('2000-01-01', periods=3).values]: x = t(data) assert source_ndarray(x) is \ source_ndarray(as_compatible_data(x)) @@ -1811,12 +1784,8 @@ def test_datetime(self): def test_full_like(self): # For more thorough tests, see test_variable.py - orig = Variable( - dims=('x', 'y'), - data=[[1.5, 2.0], [3.1, 4.3]], - attrs={ - 'foo': 'bar' - }) + orig = Variable(dims=('x', 'y'), data=[[1.5, 2.0], [3.1, 4.3]], + attrs={'foo': 'bar'}) expect = orig.copy(deep=True) expect.values = [[2.0, 2.0], [2.0, 2.0]] @@ -1829,12 +1798,8 @@ def test_full_like(self): @requires_dask def test_full_like_dask(self): - orig = Variable( - dims=('x', 'y'), - data=[[1.5, 2.0], [3.1, 4.3]], - attrs={ - 'foo': 'bar' - }).chunk(((1, 1), (2, ))) + orig = Variable(dims=('x', 'y'), data=[[1.5, 2.0], [3.1, 4.3]], + attrs={'foo': 'bar'}).chunk(((1, 1), (2,))) def check(actual, expect_dtype, expect_values): assert actual.dtype == expect_dtype @@ -1844,11 +1809,11 @@ def check(actual, expect_dtype, expect_values): assert actual.chunks == orig.chunks assert_array_equal(actual.values, expect_values) - check(full_like(orig, 2), orig.dtype, np.full_like(orig.values, 2)) + check(full_like(orig, 2), + orig.dtype, np.full_like(orig.values, 2)) # override dtype - check( - full_like(orig, True, dtype=bool), bool, - np.full_like(orig.values, True, dtype=bool)) + check(full_like(orig, True, dtype=bool), + bool, np.full_like(orig.values, True, dtype=bool)) # Check that there's no array stored inside dask # (e.g. we didn't create a numpy array and then we chunked it!) @@ -1861,26 +1826,20 @@ def check(actual, expect_dtype, expect_values): assert not isinstance(v, np.ndarray) def test_zeros_like(self): - orig = Variable( - dims=('x', 'y'), - data=[[1.5, 2.0], [3.1, 4.3]], - attrs={ - 'foo': 'bar' - }) - assert_identical(zeros_like(orig), full_like(orig, 0)) - assert_identical( - zeros_like(orig, dtype=int), full_like(orig, 0, dtype=int)) + orig = Variable(dims=('x', 'y'), data=[[1.5, 2.0], [3.1, 4.3]], + attrs={'foo': 'bar'}) + assert_identical(zeros_like(orig), + full_like(orig, 0)) + assert_identical(zeros_like(orig, dtype=int), + full_like(orig, 0, dtype=int)) def test_ones_like(self): - orig = Variable( - dims=('x', 'y'), - data=[[1.5, 2.0], [3.1, 4.3]], - attrs={ - 'foo': 'bar' - }) - assert_identical(ones_like(orig), full_like(orig, 1)) - assert_identical( - ones_like(orig, dtype=int), full_like(orig, 1, dtype=int)) + orig = Variable(dims=('x', 'y'), data=[[1.5, 2.0], [3.1, 4.3]], + attrs={'foo': 'bar'}) + assert_identical(ones_like(orig), + full_like(orig, 1)) + assert_identical(ones_like(orig, dtype=int), + full_like(orig, 1, dtype=int)) def test_unsupported_type(self): # Non indexable type @@ -1913,8 +1872,8 @@ def setUp(self): self.d = np.random.random((10, 3)).astype(np.float64) def check_orthogonal_indexing(self, v): - assert np.allclose( - v.isel(x=[8, 3], y=[2, 1]), self.d[[8, 3]][:, [2, 1]]) + assert np.allclose(v.isel(x=[8, 3], y=[2, 1]), + self.d[[8, 3]][:, [2, 1]]) def check_vectorized_indexing(self, v): ind_x = Variable('z', [0, 2]) @@ -1927,9 +1886,8 @@ def test_NumpyIndexingAdapter(self): self.check_vectorized_indexing(v) # could not doubly wrapping with raises_regex(TypeError, 'NumpyIndexingAdapter only wraps '): - v = Variable( - dims=('x', 'y'), - data=NumpyIndexingAdapter(NumpyIndexingAdapter(self.d))) + v = Variable(dims=('x', 'y'), data=NumpyIndexingAdapter( + NumpyIndexingAdapter(self.d))) def test_LazilyIndexedArray(self): v = Variable(dims=('x', 'y'), data=LazilyIndexedArray(self.d)) @@ -1937,14 +1895,12 @@ def test_LazilyIndexedArray(self): with raises_regex(NotImplementedError, 'Vectorized indexing for '): self.check_vectorized_indexing(v) # doubly wrapping - v = Variable( - dims=('x', 'y'), - data=LazilyIndexedArray(LazilyIndexedArray(self.d))) + v = Variable(dims=('x', 'y'), + data=LazilyIndexedArray(LazilyIndexedArray(self.d))) self.check_orthogonal_indexing(v) # hierarchical wrapping - v = Variable( - dims=('x', 'y'), - data=LazilyIndexedArray(NumpyIndexingAdapter(self.d))) + v = Variable(dims=('x', 'y'), + data=LazilyIndexedArray(NumpyIndexingAdapter(self.d))) self.check_orthogonal_indexing(v) def test_CopyOnWriteArray(self): @@ -1952,8 +1908,8 @@ def test_CopyOnWriteArray(self): self.check_orthogonal_indexing(v) self.check_vectorized_indexing(v) # doubly wrapping - v = Variable( - dims=('x', 'y'), data=CopyOnWriteArray(LazilyIndexedArray(self.d))) + v = Variable(dims=('x', 'y'), + data=CopyOnWriteArray(LazilyIndexedArray(self.d))) self.check_orthogonal_indexing(v) with raises_regex(NotImplementedError, 'Vectorized indexing for '): self.check_vectorized_indexing(v) @@ -1963,8 +1919,8 @@ def test_MemoryCachedArray(self): self.check_orthogonal_indexing(v) self.check_vectorized_indexing(v) # doubly wrapping - v = Variable( - dims=('x', 'y'), data=CopyOnWriteArray(MemoryCachedArray(self.d))) + v = Variable(dims=('x', 'y'), + data=CopyOnWriteArray(MemoryCachedArray(self.d))) self.check_orthogonal_indexing(v) self.check_vectorized_indexing(v) @@ -1976,7 +1932,7 @@ def test_DaskIndexingAdapter(self): self.check_orthogonal_indexing(v) self.check_vectorized_indexing(v) # doubly wrapping - v = Variable( - dims=('x', 'y'), data=CopyOnWriteArray(DaskIndexingAdapter(da))) + v = Variable(dims=('x', 'y'), + data=CopyOnWriteArray(DaskIndexingAdapter(da))) self.check_orthogonal_indexing(v) self.check_vectorized_indexing(v) From 1ba00ee7fb740519038dcfd01e5690af5b813cee Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 19:47:36 -0500 Subject: [PATCH 33/41] a few left overs --- xarray/tests/test_backends.py | 30 +++++++++++++++--------------- xarray/tests/test_dask.py | 4 ++-- xarray/tests/test_dataarray.py | 22 +++++++++++----------- xarray/tests/test_dataset.py | 8 ++++---- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 5404c0e8a81..3b1348cef8b 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -182,7 +182,7 @@ def test_write_store(self): # we need to cf decode the store because it has time and # non-dimension coordinates with xr.decode_cf(store) as actual: - self.assertDatasetAllClose(expected, actual) + assert_allclose(expected, actual) def check_dtypes_roundtripped(self, expected, actual): for k in expected.variables: @@ -534,58 +534,58 @@ def test_unsigned_roundtrip_mask_and_scale(self): for k in decoded.variables: self.assertEqual(decoded.variables[k].dtype, actual.variables[k].dtype) - self.assertDatasetAllClose(decoded, actual, decode_bytes=False) + assert_allclose(decoded, actual, decode_bytes=False) with self.roundtrip(decoded, open_kwargs=dict(decode_cf=False)) as actual: for k in encoded.variables: self.assertEqual(encoded.variables[k].dtype, actual.variables[k].dtype) - self.assertDatasetAllClose(encoded, actual, decode_bytes=False) + assert_allclose(encoded, actual, decode_bytes=False) with self.roundtrip(encoded, open_kwargs=dict(decode_cf=False)) as actual: for k in encoded.variables: self.assertEqual(encoded.variables[k].dtype, actual.variables[k].dtype) - self.assertDatasetAllClose(encoded, actual, decode_bytes=False) + assert_allclose(encoded, actual, decode_bytes=False) # make sure roundtrip encoding didn't change the # original dataset. - self.assertDatasetAllClose( + assert_allclose( encoded, create_encoded_unsigned_masked_scaled_data()) with self.roundtrip(encoded) as actual: for k in decoded.variables: self.assertEqual(decoded.variables[k].dtype, actual.variables[k].dtype) - self.assertDatasetAllClose(decoded, actual, decode_bytes=False) + assert_allclose(decoded, actual, decode_bytes=False) with self.roundtrip(encoded, open_kwargs=dict(decode_cf=False)) as actual: for k in encoded.variables: self.assertEqual(encoded.variables[k].dtype, actual.variables[k].dtype) - self.assertDatasetAllClose(encoded, actual, decode_bytes=False) + assert_allclose(encoded, actual, decode_bytes=False) def test_roundtrip_mask_and_scale(self): decoded = create_masked_and_scaled_data() encoded = create_encoded_masked_and_scaled_data() with self.roundtrip(decoded) as actual: - self.assertDatasetAllClose(decoded, actual, decode_bytes=False) + assert_allclose(decoded, actual, decode_bytes=False) with self.roundtrip(decoded, open_kwargs=dict(decode_cf=False)) as actual: # TODO: this assumes that all roundtrips will first # encode. Is that something we want to test for? - self.assertDatasetAllClose(encoded, actual, decode_bytes=False) + assert_allclose(encoded, actual, decode_bytes=False) with self.roundtrip(encoded, open_kwargs=dict(decode_cf=False)) as actual: - self.assertDatasetAllClose(encoded, actual, decode_bytes=False) + assert_allclose(encoded, actual, decode_bytes=False) # make sure roundtrip encoding didn't change the # original dataset. - self.assertDatasetAllClose(encoded, + assert_allclose(encoded, create_encoded_masked_and_scaled_data(), decode_bytes=False) with self.roundtrip(encoded) as actual: - self.assertDatasetAllClose(decoded, actual, decode_bytes=False) + assert_allclose(decoded, actual, decode_bytes=False) with self.roundtrip(encoded, open_kwargs=dict(decode_cf=False)) as actual: - self.assertDatasetAllClose(encoded, actual, decode_bytes=False) + assert_allclose(encoded, actual, decode_bytes=False) def test_coordinates_encoding(self): def equals_latlon(obj): @@ -1931,7 +1931,7 @@ def test_open_and_do_math(self): original.to_netcdf(tmp) with open_mfdataset(tmp, autoclose=self.autoclose) as ds: actual = 1.0 * ds - self.assertDatasetAllClose(original, actual, + assert_allclose(original, actual, decode_bytes=False) def test_open_mfdataset_concat_dim_none(self): @@ -1993,7 +1993,7 @@ def test_dataarray_compute(self): computed = actual.compute() self.assertFalse(actual._in_memory) self.assertTrue(computed._in_memory) - self.assertDataArrayAllClose(actual, computed, decode_bytes=False) + assert_allclose(actual, computed, decode_bytes=False) def test_vectorized_indexing(self): self._test_vectorized_indexing(vindex_support=True) diff --git a/xarray/tests/test_dask.py b/xarray/tests/test_dask.py index 4d614e9be38..a0173b53a49 100644 --- a/xarray/tests/test_dask.py +++ b/xarray/tests/test_dask.py @@ -16,7 +16,7 @@ from xarray.core.pycompat import suppress, OrderedDict from . import ( TestCase, assert_frame_equal, raises_regex, assert_equal, assert_identical, - assert_array_equal) + assert_array_equal, assert_allclose) from xarray.tests import mock @@ -244,7 +244,7 @@ def assertLazyAndIdentical(self, expected, actual): self.assertLazyAnd(expected, actual, assert_identical) def assertLazyAndAllClose(self, expected, actual): - self.assertLazyAnd(expected, actual, self.assertDataArrayAllClose) + self.assertLazyAnd(expected, actual, assert_allclose) def assertLazyAndEqual(self, expected, actual): self.assertLazyAnd(expected, actual, assert_equal) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index b00276b3cff..ffcf298fb21 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -1902,25 +1902,25 @@ def test_groupby_sum(self): 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()) + assert_allclose(expected_sum_all, grouped.reduce(np.sum)) + assert_allclose(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) + assert_allclose(expected, actual) actual = array['y'].groupby('abc').sum() - self.assertDataArrayAllClose(expected, actual) + assert_allclose(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), 'abc': Variable(['abc'], np.array(['a', 'b', 'c']))})['foo'] - self.assertDataArrayAllClose(expected_sum_axis1, + assert_allclose(expected_sum_axis1, grouped.reduce(np.sum, 'y')) - self.assertDataArrayAllClose(expected_sum_axis1, grouped.sum('y')) + assert_allclose(expected_sum_axis1, grouped.sum('y')) def test_groupby_count(self): array = DataArray( @@ -1959,7 +1959,7 @@ def center(x): center(self.x[:, 10:])]) expected_ds['foo'] = (['x', 'y'], exp_data) expected_centered = expected_ds['foo'] - self.assertDataArrayAllClose(expected_centered, grouped.apply(center)) + assert_allclose(expected_centered, grouped.apply(center)) def test_groupby_apply_ndarray(self): # regression test for #326 @@ -2005,7 +2005,7 @@ def test_groupby_math(self): expected_agg = (grouped.mean() - np.arange(3)).rename(None) actual = grouped - DataArray(range(3), [('abc', ['a', 'b', 'c'])]) actual_agg = actual.groupby('abc').mean() - self.assertDataArrayAllClose(expected_agg, actual_agg) + assert_allclose(expected_agg, actual_agg) with raises_regex(TypeError, 'only support binary ops'): grouped + 1 @@ -2389,7 +2389,7 @@ def test_upsample_interpolate(self): # Use AllClose because there are some small differences in how # we upsample timeseries versus the integer indexing as I've # done here due to floating point arithmetic - self.assertDataArrayAllClose(expected, actual, rtol=1e-16) + assert_allclose(expected, actual, rtol=1e-16) @requires_scipy def test_upsample_interpolate_regression_1605(self): @@ -3252,10 +3252,10 @@ def test_rank(self): assert_equal(ar.rank('dim_1'), expect_1) # int x = DataArray([3, 2, 1]) - self.assertDataArrayEqual(x.rank('dim_0'), x) + assert_equal(x.rank('dim_0'), x) # str y = DataArray(['c', 'b', 'a']) - self.assertDataArrayEqual(y.rank('dim_0'), x) + assert_equal(y.rank('dim_0'), x) x = DataArray([3.0, 1.0, np.nan, 2.0, 4.0], dims=('z',)) y = DataArray([0.75, 0.25, np.nan, 0.5, 1.0], dims=('z',)) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 3f1aa8436e6..09f1c7d9357 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -28,7 +28,7 @@ from xarray.core.common import full_like from . import (TestCase, raises_regex, InaccessibleArray, UnexpectedDataAccess, - requires_dask, source_ndarray) + requires_dask, source_ndarray, assert_allclose) from xarray.tests import (assert_equal, assert_allclose, assert_identical, assert_array_equal, requires_bottleneck, @@ -2518,10 +2518,10 @@ def test_groupby_reduce(self): expected = data.mean('y') expected['yonly'] = expected['yonly'].variable.set_dims({'x': 3}) actual = data.groupby('x').mean() - self.assertDatasetAllClose(expected, actual) + assert_allclose(expected, actual) actual = data.groupby('x').mean('y') - self.assertDatasetAllClose(expected, actual) + assert_allclose(expected, actual) letters = data['letters'] expected = Dataset({'xy': data['xy'].groupby(letters).mean(), @@ -2529,7 +2529,7 @@ def test_groupby_reduce(self): .set_dims({'letters': 2})), 'yonly': data['yonly'].groupby(letters).mean()}) actual = data.groupby('letters').mean() - self.assertDatasetAllClose(expected, actual) + assert_allclose(expected, actual) def test_groupby_math(self): def reorder_dims(x): From aa340ff0f87350f8fd329db97fad3d64d376d2f1 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 19:50:52 -0500 Subject: [PATCH 34/41] xarray-specific functions changed in backends --- xarray/tests/test_backends.py | 178 +++++++++++++++++----------------- 1 file changed, 89 insertions(+), 89 deletions(-) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 3b1348cef8b..a546ec84149 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -29,7 +29,7 @@ requires_scipy_or_netCDF4, requires_dask, requires_h5netcdf, requires_pynio, requires_pathlib, requires_zarr, requires_rasterio, has_netCDF4, has_scipy, assert_allclose, - flaky, network, assert_identical, raises_regex) + flaky, network, assert_identical, raises_regex, assert_equal) from .test_dataset import create_test_data @@ -173,7 +173,7 @@ def test_zero_dimensional_variable(self): expected['bytes_var'] = ([], b'foobar') expected['string_var'] = ([], u'foobar') with self.roundtrip(expected) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_write_store(self): expected = create_test_data() @@ -201,7 +201,7 @@ def test_roundtrip_test_data(self): expected = create_test_data() with self.roundtrip(expected) as actual: self.check_dtypes_roundtripped(expected, actual) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_load(self): expected = create_test_data() @@ -218,7 +218,7 @@ def assert_loads(vars=None): for k, v in actual.variables.items(): if k in vars: self.assertTrue(v._in_memory) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) with pytest.raises(AssertionError): # make sure the contextmanager works! @@ -234,7 +234,7 @@ def assert_loads(vars=None): # verify we can read data even after closing the file with self.roundtrip(expected) as ds: actual = ds.load() - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_dataset_compute(self): expected = create_test_data() @@ -252,8 +252,8 @@ def test_dataset_compute(self): for v in computed.variables.values(): self.assertTrue(v._in_memory) - self.assertDatasetIdentical(expected, actual) - self.assertDatasetIdentical(expected, computed) + assert_identical(expected, actual) + assert_identical(expected, computed) def test_pickle(self): expected = Dataset({'foo': ('x', [42])}) @@ -263,14 +263,14 @@ def test_pickle(self): # windows doesn't like opening the same file twice roundtripped.close() unpickled_ds = pickle.loads(raw_pickle) - self.assertDatasetIdentical(expected, unpickled_ds) + assert_identical(expected, unpickled_ds) def test_pickle_dataarray(self): expected = Dataset({'foo': ('x', [42])}) with self.roundtrip( expected, allow_cleanup_failure=ON_WINDOWS) as roundtripped: unpickled_array = pickle.loads(pickle.dumps(roundtripped['foo'])) - self.assertDatasetIdentical(expected['foo'], unpickled_array) + assert_identical(expected['foo'], unpickled_array) def test_dataset_caching(self): expected = Dataset({'foo': ('x', [5, 6, 7])}) @@ -291,7 +291,7 @@ def test_dataset_caching(self): def test_roundtrip_None_variable(self): expected = Dataset({None: (('x', 'y'), [[0, 1], [2, 3]])}) with self.roundtrip(expected) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_roundtrip_object_dtype(self): floats = np.array([0.0, 0.0, 1.0, 2.0, 3.0], dtype=object) @@ -312,7 +312,7 @@ def test_roundtrip_object_dtype(self): expected = original.copy(deep=True) with self.roundtrip(original) as actual: try: - self.assertDatasetIdentical(expected, actual) + assert_identical(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) @@ -322,23 +322,23 @@ def test_roundtrip_object_dtype(self): # https://github.com/pydata/xarray/issues/1647 expected['bytes_nans'][-1] = b'' expected['strings_nans'][-1] = u'' - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_roundtrip_string_data(self): expected = Dataset({'x': ('t', ['ab', 'cdef'])}) with self.roundtrip(expected) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_roundtrip_string_encoded_characters(self): expected = Dataset({'x': ('t', [u'ab', u'cdef'])}) expected['x'].encoding['dtype'] = 'S1' with self.roundtrip(expected) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) self.assertEqual(actual['x'].encoding['_Encoding'], 'utf-8') expected['x'].encoding['_Encoding'] = 'ascii' with self.roundtrip(expected) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) self.assertEqual(actual['x'].encoding['_Encoding'], 'ascii') def test_roundtrip_datetime_data(self): @@ -346,20 +346,20 @@ def test_roundtrip_datetime_data(self): expected = Dataset({'t': ('t', times), 't0': times[0]}) kwds = {'encoding': {'t0': {'units': 'days since 1950-01-01'}}} with self.roundtrip(expected, save_kwargs=kwds) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) assert actual.t0.encoding['units'] == 'days since 1950-01-01' def test_roundtrip_timedelta_data(self): time_deltas = pd.to_timedelta(['1h', '2h', 'NaT']) expected = Dataset({'td': ('td', time_deltas), 'td0': time_deltas[0]}) with self.roundtrip(expected) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_roundtrip_float64_data(self): expected = Dataset({'x': ('y', np.array([1.0, 2.0, np.pi], dtype='float64'))}) with self.roundtrip(expected) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_roundtrip_example_1_netcdf(self): expected = open_example_dataset('example_1.nc') @@ -368,32 +368,32 @@ def test_roundtrip_example_1_netcdf(self): # will depend on the encoding used. For example, # without CF encoding 'actual' will end up with # a dtype attribute. - self.assertDatasetEqual(expected, actual) + assert_equal(expected, actual) 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) + assert_identical(original, actual) def test_roundtrip_global_coordinates(self): original = Dataset({'x': [2, 3], 'y': ('a', [42]), 'z': ('x', [4, 5])}) with self.roundtrip(original) as actual: - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) def test_roundtrip_coordinates_with_space(self): original = Dataset(coords={'x': 0, 'y z': 1}) expected = Dataset({'y z': 1}, {'x': 0}) with pytest.warns(xr.SerializationWarning): with self.roundtrip(original) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_roundtrip_boolean_dtype(self): original = create_boolean_data() self.assertEqual(original['x'].dtype, 'bool') with self.roundtrip(original) as actual: - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) self.assertEqual(actual['x'].dtype, 'bool') def test_orthogonal_indexing(self): @@ -438,7 +438,7 @@ def test_isel_dataarray(self): with self.roundtrip(in_memory) as on_disk: expected = in_memory.isel(dim2=in_memory['dim2'] < 3) actual = on_disk.isel(dim2=on_disk['dim2'] < 3) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def validate_array_type(self, ds): # Make sure that only NumpyIndexingAdapter stores a bare np.ndarray. @@ -510,11 +510,11 @@ def test_roundtrip_bytes_with_fill_value(self): expected = original.copy(deep=True) print(original) with self.roundtrip(original) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) original = Dataset({'x': ('t', values, {}, {'_FillValue': b''})}) with self.roundtrip(original) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_roundtrip_string_with_fill_value_nchar(self): values = np.array([u'ab', u'cdef', np.nan], dtype=object) @@ -525,7 +525,7 @@ def test_roundtrip_string_with_fill_value_nchar(self): # Not supported yet. with pytest.raises(NotImplementedError): with self.roundtrip(original) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_unsigned_roundtrip_mask_and_scale(self): decoded = create_unsigned_masked_scaled_data() @@ -594,7 +594,7 @@ def equals_latlon(obj): 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) + assert_identical(actual, original) with create_tmp_file() as tmp_file: original.to_netcdf(tmp_file) with open_dataset(tmp_file, decode_coords=False) as ds: @@ -607,7 +607,7 @@ def equals_latlon(obj): modified = original.drop(['temp', 'precip']) with self.roundtrip(modified) as actual: - self.assertDatasetIdentical(actual, modified) + assert_identical(actual, modified) with create_tmp_file() as tmp_file: modified.to_netcdf(tmp_file) with open_dataset(tmp_file, decode_coords=False) as ds: @@ -626,7 +626,7 @@ def test_roundtrip_endian(self): # one hold mixed endian data (ds) the other should be # all big endian (actual). assertDatasetIdentical # should still pass though. - self.assertDatasetIdentical(ds, actual) + assert_identical(ds, actual) if type(self) is NetCDF4DataTest: ds['z'].encoding['endian'] = 'big' @@ -672,7 +672,7 @@ def test_encoding_kwarg(self): 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) + assert_identical(actual, ds) def test_default_fill_value(self): # Test default encoding for float: @@ -713,7 +713,7 @@ def test_append_write(self): # regression for GH1215 data = create_test_data() with self.roundtrip_append(data) as actual: - self.assertDatasetIdentical(data, actual) + assert_identical(data, actual) def test_append_overwrite_values(self): # regression for GH1215 @@ -724,7 +724,7 @@ def test_append_overwrite_values(self): data['var9'] = data['var2'] * 3 self.save(data[['var2', 'var9']], tmp_file, mode='a') with self.open(tmp_file) as actual: - self.assertDatasetIdentical(data, actual) + assert_identical(data, actual) def test_vectorized_indexing(self): self._test_vectorized_indexing(vindex_support=False) @@ -821,9 +821,9 @@ def test_write_groups(self): data1.to_netcdf(tmp_file, group='data/1') data2.to_netcdf(tmp_file, group='data/2', mode='a') with open_dataset(tmp_file, group='data/1') as actual1: - self.assertDatasetIdentical(data1, actual1) + assert_identical(data1, actual1) with open_dataset(tmp_file, group='data/2') as actual2: - self.assertDatasetIdentical(data2, actual2) + assert_identical(data2, actual2) def test_roundtrip_string_with_fill_value_vlen(self): values = np.array([u'ab', u'cdef', np.nan], dtype=object) @@ -836,12 +836,12 @@ def test_roundtrip_string_with_fill_value_vlen(self): original = Dataset({'x': ('t', values, {}, {'_FillValue': u'XXX'})}) with pytest.raises(NotImplementedError): with self.roundtrip(original) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) original = Dataset({'x': ('t', values, {}, {'_FillValue': u''})}) with pytest.raises(NotImplementedError): with self.roundtrip(original) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_roundtrip_character_array(self): with create_tmp_file() as tmp_file: @@ -856,15 +856,15 @@ def test_roundtrip_character_array(self): values = np.array(['abc', 'def'], dtype='S') expected = Dataset({'x': ('x', values)}) with open_dataset(tmp_file) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) # regression test for #157 with self.roundtrip(actual) as roundtripped: - self.assertDatasetIdentical(expected, roundtripped) + assert_identical(expected, roundtripped) def test_default_to_char_arrays(self): data = Dataset({'x': np.array(['foo', 'zzzz'], dtype='S')}) with self.roundtrip(data) as actual: - self.assertDatasetIdentical(data, actual) + assert_identical(data, actual) self.assertEqual(actual['x'].dtype, np.dtype('S4')) def test_open_encodings(self): @@ -935,7 +935,7 @@ def test_compression_encoding(self): # regression test for #156 expected = data.isel(dim1=0) with self.roundtrip(expected) as actual: - self.assertDatasetEqual(expected, actual) + assert_equal(expected, actual) def test_encoding_chunksizes_unlimited(self): # regression test for GH1225 @@ -950,7 +950,7 @@ def test_encoding_chunksizes_unlimited(self): 'original_shape': (3,), } with self.roundtrip(ds) as actual: - self.assertDatasetEqual(ds, actual) + assert_equal(ds, actual) def test_mask_and_scale(self): with create_tmp_file() as tmp_file: @@ -974,7 +974,7 @@ def test_mask_and_scale(self): # now check xarray with open_dataset(tmp_file) as ds: expected = create_masked_and_scaled_data() - self.assertDatasetIdentical(expected, ds) + assert_identical(expected, ds) def test_0dimensional_variable(self): # This fix verifies our work-around to this netCDF4-python bug: @@ -986,7 +986,7 @@ def test_0dimensional_variable(self): with open_dataset(tmp_file) as ds: expected = Dataset({'x': ((), 123)}) - self.assertDatasetIdentical(expected, ds) + assert_identical(expected, ds) def test_already_open_dataset(self): with create_tmp_file() as tmp_file: @@ -998,7 +998,7 @@ def test_already_open_dataset(self): with backends.NetCDF4DataStore(nc, autoclose=False) as store: with open_dataset(store) as ds: expected = Dataset({'x': ((), 42)}) - self.assertDatasetIdentical(expected, ds) + assert_identical(expected, ds) def test_variable_len_strings(self): with create_tmp_file() as tmp_file: @@ -1012,7 +1012,7 @@ def test_variable_len_strings(self): expected = Dataset({'x': ('x', values)}) for kwargs in [{}, {'decode_cf': True}]: with open_dataset(tmp_file, **kwargs) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) @requires_netCDF4 @@ -1228,11 +1228,11 @@ def test_write_persistence_modes(self): # overwrite mode with self.roundtrip(original, save_kwargs={'mode': 'w'}) as actual: - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) # don't overwrite mode with self.roundtrip(original, save_kwargs={'mode': 'w-'}) as actual: - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) # make sure overwriting works as expected with self.create_zarr_target() as store: @@ -1240,7 +1240,7 @@ def test_write_persistence_modes(self): # should overwrite with no error original.to_zarr(store, mode='w') actual = xr.open_zarr(store) - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) with pytest.raises(ValueError): original.to_zarr(store, mode='w-') @@ -1264,11 +1264,11 @@ def test_group(self): group = 'some/random/path' with self.roundtrip(original, save_kwargs={'group': group}, open_kwargs={'group': group}) as actual: - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) with pytest.raises(KeyError): with self.roundtrip(original, save_kwargs={'group': group}) as actual: - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) # TODO: implement zarr object encoding and make these tests pass @pytest.mark.xfail(reason="Zarr object encoding not implemented") @@ -1350,7 +1350,7 @@ def test_bytesio_pickle(self): fobj = BytesIO(data.to_netcdf()) with open_dataset(fobj, autoclose=self.autoclose) as ds: unpickled = pickle.loads(pickle.dumps(ds)) - self.assertDatasetIdentical(unpickled, data) + assert_identical(unpickled, data) class ScipyInMemoryDataTestAutocloseTrue(ScipyInMemoryDataTest): @@ -1404,7 +1404,7 @@ def test_array_attrs(self): def test_roundtrip_example_1_netcdf_gz(self): with open_example_dataset('example_1.nc.gz') as expected: with open_example_dataset('example_1.nc') as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_netcdf3_endianness(self): # regression test for GH416 @@ -1517,12 +1517,12 @@ def test_encoding_unlimited_dims(self): with self.roundtrip(ds, save_kwargs=dict(unlimited_dims=['y'])) as actual: self.assertEqual(actual.encoding['unlimited_dims'], set('y')) - self.assertDatasetEqual(ds, actual) + assert_equal(ds, actual) ds.encoding = {'unlimited_dims': ['y']} with self.roundtrip(ds) as actual: self.assertEqual(actual.encoding['unlimited_dims'], set('y')) - self.assertDatasetEqual(ds, actual) + assert_equal(ds, actual) class GenericNetCDFDataTestAutocloseTrue(GenericNetCDFDataTest): @@ -1550,7 +1550,7 @@ def test_array_type_after_indexing(self): def test_complex(self): expected = Dataset({'x': ('y', np.ones(5) + 1j * np.ones(5))}) with self.roundtrip(expected) as actual: - self.assertDatasetEqual(expected, actual) + assert_equal(expected, actual) @pytest.mark.xfail(reason='https://github.com/pydata/xarray/issues/535') def test_cross_engine_read_write_netcdf4(self): @@ -1565,7 +1565,7 @@ def test_cross_engine_read_write_netcdf4(self): data.to_netcdf(tmp_file, engine=write_engine) for read_engine in valid_engines: with open_dataset(tmp_file, engine=read_engine) as actual: - self.assertDatasetIdentical(data, actual) + assert_identical(data, actual) def test_read_byte_attrs_as_unicode(self): with create_tmp_file() as tmp_file: @@ -1573,18 +1573,18 @@ def test_read_byte_attrs_as_unicode(self): nc.foo = b'bar' with open_dataset(tmp_file) as actual: expected = Dataset(attrs={'foo': 'bar'}) - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_encoding_unlimited_dims(self): ds = Dataset({'x': ('y', np.arange(10.0))}) with self.roundtrip(ds, save_kwargs=dict(unlimited_dims=['y'])) as actual: self.assertEqual(actual.encoding['unlimited_dims'], set('y')) - self.assertDatasetEqual(ds, actual) + assert_equal(ds, actual) ds.encoding = {'unlimited_dims': ['y']} with self.roundtrip(ds) as actual: self.assertEqual(actual.encoding['unlimited_dims'], set('y')) - self.assertDatasetEqual(ds, actual) + assert_equal(ds, actual) # tests pending h5netcdf fix @@ -1610,10 +1610,10 @@ def validate_open_mfdataset_autoclose(self, engine, nfiles=10): # check that calculation on opened datasets works properly ds = open_mfdataset(tmpfiles, engine=readengine, autoclose=True) - self.assertAllClose(ds.x.sum().values, + assert_allclose(ds.x.sum().values, (nfiles * (nfiles - 1)) / 2) - self.assertAllClose(ds.foo.sum().values, np.sum(randdata)) - self.assertAllClose(ds.sum().foo.values, np.sum(randdata)) + assert_allclose(ds.foo.sum().values, np.sum(randdata)) + assert_allclose(ds.sum().foo.values, np.sum(randdata)) ds.close() def validate_open_mfdataset_large_num_files(self, engine): @@ -1729,12 +1729,12 @@ def test_open_mfdataset_does_same_as_concat(self): with open_mfdataset(files, data_vars=opt) as ds: kwargs = dict(data_vars=opt, dim='t') ds_expect = xr.concat([ds1, ds2], **kwargs) - self.assertDatasetIdentical(ds, ds_expect) + assert_identical(ds, ds_expect) with open_mfdataset(files, coords=opt) as ds: kwargs = dict(coords=opt, dim='t') ds_expect = xr.concat([ds1, ds2], **kwargs) - self.assertDatasetIdentical(ds, ds_expect) + assert_identical(ds, ds_expect) def test_common_coord_when_datavars_all(self): opt = 'all' @@ -1809,7 +1809,7 @@ def test_roundtrip_datetime_data(self): times = pd.to_datetime(['2000-01-01', '2000-01-02', 'NaT']) expected = Dataset({'t': ('t', times), 't0': times[0]}) with self.roundtrip(expected) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_write_store(self): # Override method in DatasetIOTestCases - not applicable to dask @@ -1833,7 +1833,7 @@ def test_open_mfdataset(self): self.assertIsInstance(actual.foo.variable.data, da.Array) self.assertEqual(actual.foo.variable.data.chunks, ((5, 5),)) - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) with open_mfdataset([tmp1, tmp2], chunks={'x': 3}, autoclose=self.autoclose) as actual: self.assertEqual(actual.foo.variable.data.chunks, @@ -1853,7 +1853,7 @@ def test_open_mfdataset_pathlib(self): original.isel(x=slice(5, 10)).to_netcdf(tmp2) with open_mfdataset([tmp1, tmp2], autoclose=self.autoclose) as actual: - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) def test_attrs_mfdataset(self): original = Dataset({'foo': ('x', np.random.randn(10))}) @@ -1885,7 +1885,7 @@ def preprocess(ds): expected = preprocess(original) with open_mfdataset(tmp, preprocess=preprocess, autoclose=self.autoclose) as actual: - self.assertDatasetIdentical(expected, actual) + assert_identical(expected, actual) def test_save_mfdataset_roundtrip(self): original = Dataset({'foo': ('x', np.random.randn(10))}) @@ -1896,7 +1896,7 @@ def test_save_mfdataset_roundtrip(self): save_mfdataset(datasets, [tmp1, tmp2]) with open_mfdataset([tmp1, tmp2], autoclose=self.autoclose) as actual: - self.assertDatasetIdentical(actual, original) + assert_identical(actual, original) def test_save_mfdataset_invalid(self): ds = Dataset() @@ -1923,7 +1923,7 @@ def test_save_mfdataset_pathlib_roundtrip(self): save_mfdataset(datasets, [tmp1, tmp2]) with open_mfdataset([tmp1, tmp2], autoclose=self.autoclose) as actual: - self.assertDatasetIdentical(actual, original) + assert_identical(actual, original) def test_open_and_do_math(self): original = Dataset({'foo': ('x', np.random.randn(10))}) @@ -1942,7 +1942,7 @@ def test_open_mfdataset_concat_dim_none(self): Dataset({'x': np.nan}).to_netcdf(tmp2) with open_mfdataset([tmp1, tmp2], concat_dim=None, autoclose=self.autoclose) as actual: - self.assertDatasetIdentical(data, actual) + assert_identical(data, actual) def test_open_dataset(self): original = Dataset({'foo': ('x', np.random.randn(10))}) @@ -1951,12 +1951,12 @@ def test_open_dataset(self): with open_dataset(tmp, chunks={'x': 5}) as actual: self.assertIsInstance(actual.foo.variable.data, da.Array) self.assertEqual(actual.foo.variable.data.chunks, ((5, 5),)) - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) with open_dataset(tmp, chunks=5) as actual: - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) with open_dataset(tmp) as actual: self.assertIsInstance(actual.foo.variable.data, np.ndarray) - self.assertDatasetIdentical(original, actual) + assert_identical(original, actual) def test_dask_roundtrip(self): with create_tmp_file() as tmp: @@ -1964,11 +1964,11 @@ def test_dask_roundtrip(self): data.to_netcdf(tmp) chunks = {'dim1': 4, 'dim2': 4, 'dim3': 4, 'time': 10} with open_dataset(tmp, chunks=chunks) as dask_ds: - self.assertDatasetIdentical(data, dask_ds) + assert_identical(data, dask_ds) with create_tmp_file() as tmp2: dask_ds.to_netcdf(tmp2) with open_dataset(tmp2) as on_disk: - self.assertDatasetIdentical(data, on_disk) + assert_identical(data, on_disk) def test_deterministic_names(self): with create_tmp_file() as tmp: @@ -2034,7 +2034,7 @@ def create_datasets(self, **kwargs): def test_cmp_local_file(self): with self.create_datasets() as (actual, expected): - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) # global attributes should be global attributes on the dataset self.assertNotIn('NC_GLOBAL', actual.attrs) @@ -2046,15 +2046,15 @@ def test_cmp_local_file(self): assert actual.attrs.keys() == expected.attrs.keys() with self.create_datasets() as (actual, expected): - self.assertDatasetEqual( + assert_equal( actual.isel(l=2), expected.isel(l=2)) # noqa: E741 with self.create_datasets() as (actual, expected): - self.assertDatasetEqual(actual.isel(i=0, j=-1), + assert_equal(actual.isel(i=0, j=-1), expected.isel(i=0, j=-1)) with self.create_datasets() as (actual, expected): - self.assertDatasetEqual(actual.isel(j=slice(1, 2)), + assert_equal(actual.isel(j=slice(1, 2)), expected.isel(j=slice(1, 2))) def test_compatible_to_netcdf(self): @@ -2064,12 +2064,12 @@ def test_compatible_to_netcdf(self): actual.to_netcdf(tmp_file) actual = open_dataset(tmp_file) actual['bears'] = actual['bears'].astype(str) - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) @requires_dask def test_dask(self): with self.create_datasets(chunks={'j': 2}) as (actual, expected): - self.assertDatasetEqual(actual, expected) + assert_equal(actual, expected) @network @@ -2132,7 +2132,7 @@ def test_weakrefs(self): on_disk = open_dataset(tmp_file, engine='pynio') actual = on_disk.rename({'foo': 'bar', 'x': 'y'}) del on_disk # trigger garbage collection - self.assertDatasetIdentical(actual, expected) + assert_identical(actual, expected) class TestPyNioAutocloseTrue(TestPyNio): @@ -2612,7 +2612,7 @@ def test_dataarray_to_netcdf_no_name(self): original_da.to_netcdf(tmp) with open_dataarray(tmp) as loaded_da: - self.assertDataArrayIdentical(original_da, loaded_da) + assert_identical(original_da, loaded_da) def test_dataarray_to_netcdf_with_name(self): original_da = DataArray(np.arange(12).reshape((3, 4)), @@ -2622,7 +2622,7 @@ def test_dataarray_to_netcdf_with_name(self): original_da.to_netcdf(tmp) with open_dataarray(tmp) as loaded_da: - self.assertDataArrayIdentical(original_da, loaded_da) + assert_identical(original_da, loaded_da) def test_dataarray_to_netcdf_coord_name_clash(self): original_da = DataArray(np.arange(12).reshape((3, 4)), @@ -2633,7 +2633,7 @@ def test_dataarray_to_netcdf_coord_name_clash(self): original_da.to_netcdf(tmp) with open_dataarray(tmp) as loaded_da: - self.assertDataArrayIdentical(original_da, loaded_da) + assert_identical(original_da, loaded_da) def test_open_dataarray_options(self): data = DataArray( @@ -2644,7 +2644,7 @@ def test_open_dataarray_options(self): expected = data.drop('y') with open_dataarray(tmp, drop_variables=['y']) as loaded: - self.assertDataArrayIdentical(expected, loaded) + assert_identical(expected, loaded) def test_dataarray_to_netcdf_return_bytes(self): # regression test for GH1410 @@ -2661,4 +2661,4 @@ def test_dataarray_to_netcdf_no_name_pathlib(self): original_da.to_netcdf(tmp) with open_dataarray(tmp) as loaded_da: - self.assertDataArrayIdentical(original_da, loaded_da) + assert_identical(original_da, loaded_da) From c296f8952231a822b273396786abc6d1f1cc6de7 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 20:07:00 -0500 Subject: [PATCH 35/41] undo self.AllClose --- xarray/tests/test_backends.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index a546ec84149..a2e70a03375 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -1610,10 +1610,10 @@ def validate_open_mfdataset_autoclose(self, engine, nfiles=10): # check that calculation on opened datasets works properly ds = open_mfdataset(tmpfiles, engine=readengine, autoclose=True) - assert_allclose(ds.x.sum().values, + self.assertAllClose(ds.x.sum().values, (nfiles * (nfiles - 1)) / 2) - assert_allclose(ds.foo.sum().values, np.sum(randdata)) - assert_allclose(ds.sum().foo.values, np.sum(randdata)) + self.assertAllClose(ds.foo.sum().values, np.sum(randdata)) + self.assertAllClose(ds.sum().foo.values, np.sum(randdata)) ds.close() def validate_open_mfdataset_large_num_files(self, engine): From d2e3d160418e026e15dad3f639461aac45723184 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 20:09:13 -0500 Subject: [PATCH 36/41] remove some old xarray funcs --- xarray/tests/__init__.py | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/xarray/tests/__init__.py b/xarray/tests/__init__.py index 27043f14f9d..95b0c40c161 100644 --- a/xarray/tests/__init__.py +++ b/xarray/tests/__init__.py @@ -156,38 +156,10 @@ def assertAllClose(self, a1, a2, rtol=1e-05, atol=1e-8): __tracebackhide__ = True # noqa: F841 assert allclose_or_equiv(a1, a2, rtol=rtol, atol=atol) - def assertDatasetEqual(self, d1, d2): - __tracebackhide__ = True # noqa: F841 - assert_equal(d1, d2) - - def assertDatasetIdentical(self, d1, d2): - __tracebackhide__ = True # noqa: F841 - assert_identical(d1, d2) - - def assertDatasetAllClose(self, d1, d2, rtol=1e-05, atol=1e-08, - decode_bytes=True): - __tracebackhide__ = True # noqa: F841 - assert_allclose(d1, d2, rtol=rtol, atol=atol, - decode_bytes=decode_bytes) - def assertCoordinatesEqual(self, d1, d2): __tracebackhide__ = True # noqa: F841 assert_equal(d1, d2) - def assertDataArrayEqual(self, ar1, ar2): - __tracebackhide__ = True # noqa: F841 - assert_equal(ar1, ar2) - - def assertDataArrayIdentical(self, ar1, ar2): - __tracebackhide__ = True # noqa: F841 - assert_identical(ar1, ar2) - - def assertDataArrayAllClose(self, ar1, ar2, rtol=1e-05, atol=1e-08, - decode_bytes=True): - __tracebackhide__ = True # noqa: F841 - assert_allclose(ar1, ar2, rtol=rtol, atol=atol, - decode_bytes=decode_bytes) - @contextmanager def raises_regex(error, pattern): From ab59d048a30a0ad6e6cd55b6dd5a517aef69e3eb Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 20:13:39 -0500 Subject: [PATCH 37/41] assert_array_equal complete --- xarray/tests/__init__.py | 4 ---- xarray/tests/test_backends.py | 7 ++++--- xarray/tests/test_dataarray.py | 4 ++-- xarray/tests/test_dataset.py | 7 ++++--- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/xarray/tests/__init__.py b/xarray/tests/__init__.py index 95b0c40c161..c02a8bc7ff8 100644 --- a/xarray/tests/__init__.py +++ b/xarray/tests/__init__.py @@ -144,10 +144,6 @@ def assertVariableNotEqual(self, v1, v2): __tracebackhide__ = True # noqa: F841 assert not v1.equals(v2) - def assertArrayEqual(self, a1, a2): - __tracebackhide__ = True # noqa: F841 - assert_array_equal(a1, a2) - def assertEqual(self, a1, a2): __tracebackhide__ = True # noqa: F841 assert a1 == a2 or (a1 != a1 and a2 != a2) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index a2e70a03375..273efb31ad4 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -29,7 +29,8 @@ requires_scipy_or_netCDF4, requires_dask, requires_h5netcdf, requires_pynio, requires_pathlib, requires_zarr, requires_rasterio, has_netCDF4, has_scipy, assert_allclose, - flaky, network, assert_identical, raises_regex, assert_equal) + flaky, network, assert_identical, raises_regex, assert_equal, + assert_array_equal) from .test_dataset import create_test_data @@ -918,7 +919,7 @@ def test_dump_and_open_encodings(self): with nc4.Dataset(tmp_file2, 'r') as ds: self.assertEqual( ds.variables['time'].getncattr('units'), units) - self.assertArrayEqual( + assert_array_equal( ds.variables['time'], np.arange(10) + 4) def test_compression_encoding(self): @@ -969,7 +970,7 @@ def test_mask_and_scale(self): expected = np.ma.array([-1, -1, 10, 10.1, 10.2], mask=[True, True, False, False, False]) actual = nc.variables['x'][:] - self.assertArrayEqual(expected, actual) + assert_array_equal(expected, actual) # now check xarray with open_dataset(tmp_file) as ds: diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index ffcf298fb21..6f01ad20cfa 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -2884,7 +2884,7 @@ def test_to_and_from_iris(self): for coord, orginal_key in zip((actual.coords()), original.coords): original_coord = original.coords[orginal_key] assert coord.var_name == original_coord.name - self.assertArrayEqual( + assert_array_equal( coord.points, CFDatetimeCoder().encode(original_coord).values) assert (actual.coord_dims(coord) == original.get_axis_num( @@ -2956,7 +2956,7 @@ def test_to_and_from_iris_dask(self): for coord, orginal_key in zip((actual.coords()), original.coords): original_coord = original.coords[orginal_key] assert coord.var_name == original_coord.name - self.assertArrayEqual( + assert_array_equal( coord.points, CFDatetimeCoder().encode(original_coord).values) assert (actual.coord_dims(coord) == original.get_axis_num( diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 09f1c7d9357..2d2879a852c 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -28,7 +28,8 @@ from xarray.core.common import full_like from . import (TestCase, raises_regex, InaccessibleArray, UnexpectedDataAccess, - requires_dask, source_ndarray, assert_allclose) + requires_dask, source_ndarray, assert_allclose, + assert_array_equal) from xarray.tests import (assert_equal, assert_allclose, assert_identical, assert_array_equal, requires_bottleneck, @@ -1627,7 +1628,7 @@ def test_align(self): left2, right2 = align(left, right, join='outer') - self.assertArrayEqual(left2['dim3'], union) + assert_array_equal(left2['dim3'], union) assert_equal(left2['dim3'].variable, right2['dim3'].variable) assert_identical(left2.sel(dim3=intersection), @@ -2243,7 +2244,7 @@ def test_virtual_variable_multiindex(self): def test_time_season(self): ds = Dataset({'t': pd.date_range('2000-01-01', periods=12, freq='M')}) seas = ['DJF'] * 2 + ['MAM'] * 3 + ['JJA'] * 3 + ['SON'] * 3 + ['DJF'] - self.assertArrayEqual(seas, ds['t.season']) + assert_array_equal(seas, ds['t.season']) def test_slice_virtual_variable(self): data = create_test_data() From b2cf4e86596288e3513b83daf375ffae93f27d03 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 20:24:34 -0500 Subject: [PATCH 38/41] remove assertVariable* --- xarray/tests/__init__.py | 21 +++-------------- xarray/tests/test_backends.py | 17 +++++++------- xarray/tests/test_dask.py | 2 +- xarray/tests/test_dataarray.py | 10 ++++---- xarray/tests/test_dataset.py | 43 ++++++++++++++++------------------ xarray/tests/test_variable.py | 13 +++++----- 6 files changed, 44 insertions(+), 62 deletions(-) diff --git a/xarray/tests/__init__.py b/xarray/tests/__init__.py index c02a8bc7ff8..f373d201b03 100644 --- a/xarray/tests/__init__.py +++ b/xarray/tests/__init__.py @@ -8,14 +8,15 @@ import importlib import numpy as np -from numpy.testing import assert_array_equal +from numpy.testing import assert_array_equal # noqa: F401 from xarray.core.duck_array_ops import allclose_or_equiv import pytest from xarray.core import utils from xarray.core.pycompat import PY3 from xarray.core.indexing import ExplicitlyIndexed -from xarray.testing import assert_equal, assert_identical, assert_allclose +from xarray.testing import (assert_equal, assert_identical, # noqa: F401 + assert_allclose) from xarray.plot.utils import import_seaborn try: @@ -128,18 +129,6 @@ def assertWarns(self, message): assert len(w) > 0 assert any(message in str(wi.message) for wi in w) - def assertVariableEqual(self, v1, v2): - __tracebackhide__ = True # noqa: F841 - assert_equal(v1, v2) - - def assertVariableIdentical(self, v1, v2): - __tracebackhide__ = True # noqa: F841 - assert_identical(v1, v2) - - def assertVariableAllClose(self, v1, v2, rtol=1e-05, atol=1e-08): - __tracebackhide__ = True # noqa: F841 - assert_allclose(v1, v2, rtol=rtol, atol=atol) - def assertVariableNotEqual(self, v1, v2): __tracebackhide__ = True # noqa: F841 assert not v1.equals(v2) @@ -152,10 +141,6 @@ def assertAllClose(self, a1, a2, rtol=1e-05, atol=1e-8): __tracebackhide__ = True # noqa: F841 assert allclose_or_equiv(a1, a2, rtol=rtol, atol=atol) - def assertCoordinatesEqual(self, d1, d2): - __tracebackhide__ = True # noqa: F841 - assert_equal(d1, d2) - @contextmanager def raises_regex(error, pattern): diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 273efb31ad4..a3256f84bdd 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -580,8 +580,8 @@ def test_roundtrip_mask_and_scale(self): # make sure roundtrip encoding didn't change the # original dataset. assert_allclose(encoded, - create_encoded_masked_and_scaled_data(), - decode_bytes=False) + create_encoded_masked_and_scaled_data(), + decode_bytes=False) with self.roundtrip(encoded) as actual: assert_allclose(decoded, actual, decode_bytes=False) with self.roundtrip(encoded, @@ -786,7 +786,7 @@ def test_open_group(self): # check equivalent ways to specify group for group in 'foo', '/foo', 'foo/', '/foo/': with open_dataset(tmp_file, group=group) as actual: - self.assertVariableEqual(actual['x'], expected['x']) + assert_equal(actual['x'], expected['x']) # check that missing group raises appropriate exception with pytest.raises(IOError): @@ -813,7 +813,7 @@ def test_open_subgroup(self): # check equivalent ways to specify group for group in 'foo/bar', '/foo/bar', 'foo/bar/', '/foo/bar/': with open_dataset(tmp_file, group=group) as actual: - self.assertVariableEqual(actual['x'], expected['x']) + assert_equal(actual['x'], expected['x']) def test_write_groups(self): data1 = create_test_data() @@ -887,7 +887,7 @@ def test_open_encodings(self): expected['time'] = ('time', time, {}, encoding) with open_dataset(tmp_file) as actual: - self.assertVariableEqual(actual['time'], expected['time']) + assert_equal(actual['time'], expected['time']) actual_encoding = dict((k, v) for k, v in iteritems(actual['time'].encoding) if k in expected['time'].encoding) @@ -1932,8 +1932,7 @@ def test_open_and_do_math(self): original.to_netcdf(tmp) with open_mfdataset(tmp, autoclose=self.autoclose) as ds: actual = 1.0 * ds - assert_allclose(original, actual, - decode_bytes=False) + assert_allclose(original, actual, decode_bytes=False) def test_open_mfdataset_concat_dim_none(self): with create_tmp_file() as tmp1: @@ -2052,11 +2051,11 @@ def test_cmp_local_file(self): with self.create_datasets() as (actual, expected): assert_equal(actual.isel(i=0, j=-1), - expected.isel(i=0, j=-1)) + expected.isel(i=0, j=-1)) with self.create_datasets() as (actual, expected): assert_equal(actual.isel(j=slice(1, 2)), - expected.isel(j=slice(1, 2))) + expected.isel(j=slice(1, 2))) def test_compatible_to_netcdf(self): # make sure it can be saved as a netcdf diff --git a/xarray/tests/test_dask.py b/xarray/tests/test_dask.py index a0173b53a49..7833a43a894 100644 --- a/xarray/tests/test_dask.py +++ b/xarray/tests/test_dask.py @@ -53,7 +53,7 @@ def assertLazyAndIdentical(self, expected, actual): self.assertLazyAnd(expected, actual, assert_identical) def assertLazyAndAllClose(self, expected, actual): - self.assertLazyAnd(expected, actual, self.assertVariableAllClose) + self.assertLazyAnd(expected, actual, assert_allclose) def setUp(self): self.values = np.random.RandomState(0).randn(4, 6) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 6f01ad20cfa..09952fa2f35 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -63,13 +63,13 @@ def test_repr_multiindex(self): assert expected == repr(self.mda) def test_properties(self): - self.assertVariableEqual(self.dv.variable, self.v) + assert_equal(self.dv.variable, self.v) assert_array_equal(self.dv.values, self.v.values) for attr in ['dims', 'dtype', 'shape', 'size', 'nbytes', 'ndim', 'attrs']: assert getattr(self.dv, attr) == getattr(self.v, attr) assert len(self.dv) == len(self.v) - self.assertVariableEqual(self.dv.variable, self.v) + assert_equal(self.dv.variable, self.v) assert set(self.dv.coords) == set(self.ds.coords) for k, v in iteritems(self.dv.coords): assert_array_equal(v, self.ds.coords[k]) @@ -440,7 +440,7 @@ def test_getitem(self): for i in [I[:], I[...], I[x.values], I[x.variable], I[x], I[x, y], I[x.values > -1], I[x.variable > -1], I[x > -1], I[x > -1, y > -1]]: - self.assertVariableEqual(self.dv, self.dv[i]) + assert_equal(self.dv, self.dv[i]) for i in [I[0], I[:, 0], I[:3, :2], I[x.values[:3]], I[x.variable[:3]], I[x[:3]], I[x[:3], y[:4]], @@ -1652,7 +1652,7 @@ def test_unstack_pandas_consistency(self): assert_identical(expected, actual) def test_transpose(self): - self.assertVariableEqual(self.dv.variable.transpose(), + assert_equal(self.dv.variable.transpose(), self.dv.transpose().variable) def test_squeeze(self): @@ -1767,7 +1767,7 @@ def test_reduce(self): expected = DataArray([0, 0], {'x': coords['x'], 'c': -999}, 'x') assert_identical(expected, actual) - self.assertVariableEqual(self.dv.reduce(np.mean, 'x').variable, + assert_equal(self.dv.reduce(np.mean, 'x').variable, self.v.reduce(np.mean, 'x')) orig = DataArray([[1, 0, np.nan], [3, 0, 3]], coords, dims=['x', 'y']) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 2d2879a852c..349c72dfe7e 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -28,12 +28,9 @@ from xarray.core.common import full_like from . import (TestCase, raises_regex, InaccessibleArray, UnexpectedDataAccess, - requires_dask, source_ndarray, assert_allclose, - assert_array_equal) - -from xarray.tests import (assert_equal, assert_allclose, assert_identical, - assert_array_equal, requires_bottleneck, - requires_scipy) + requires_dask, source_ndarray, assert_array_equal, assert_equal, + assert_allclose, assert_identical, requires_bottleneck, + requires_scipy) def create_test_data(seed=None): @@ -1809,13 +1806,13 @@ def test_variable_indexing(self): v = data['var1'] d1 = data['dim1'] d2 = data['dim2'] - self.assertVariableEqual(v, v[d1.values]) - self.assertVariableEqual(v, v[d1]) - self.assertVariableEqual(v[:3], v[d1 < 3]) - self.assertVariableEqual(v[:, 3:], v[:, d2 >= 1.5]) - self.assertVariableEqual(v[:3, 3:], v[d1 < 3, d2 >= 1.5]) - self.assertVariableEqual(v[:3, :2], v[range(3), range(2)]) - self.assertVariableEqual(v[:3, :2], v.loc[d1[:3], d2[:2]]) + assert_equal(v, v[d1.values]) + assert_equal(v, v[d1]) + assert_equal(v[:3], v[d1 < 3]) + assert_equal(v[:, 3:], v[:, d2 >= 1.5]) + assert_equal(v[:3, 3:], v[d1 < 3, d2 >= 1.5]) + assert_equal(v[:3, :2], v[range(3), range(2)]) + assert_equal(v[:3, :2], v.loc[d1[:3], d2[:2]]) def test_drop_variables(self): data = create_test_data() @@ -1889,8 +1886,8 @@ def test_rename(self): if name in dims: dims[dims.index(name)] = newname - self.assertVariableEqual(Variable(dims, v.values, v.attrs), - renamed[k].variable.to_base_variable()) + assert_equal(Variable(dims, v.values, v.attrs), + renamed[k].variable.to_base_variable()) assert v.encoding == renamed[k].encoding assert type(v) == type(renamed.variables[k]) # noqa: E721 @@ -2152,7 +2149,7 @@ def test_update_auto_align(self): def test_getitem(self): data = create_test_data() assert isinstance(data['var1'], DataArray) - self.assertVariableEqual(data['var1'].variable, data.variables['var1']) + assert_equal(data['var1'].variable, data.variables['var1']) with pytest.raises(KeyError): data['notfound'] with pytest.raises(KeyError): @@ -2248,9 +2245,9 @@ def test_time_season(self): def test_slice_virtual_variable(self): data = create_test_data() - self.assertVariableEqual(data['time.dayofyear'][:10].variable, - Variable(['time'], 1 + np.arange(10))) - self.assertVariableEqual( + assert_equal(data['time.dayofyear'][:10].variable, + Variable(['time'], 1 + np.arange(10))) + assert_equal( data['time.dayofyear'][0].variable, Variable([], 1)) def test_setitem(self): @@ -2286,7 +2283,7 @@ def test_setitem(self): data1['dim1'] = data1['dim1'][:5] # override an existing value data1['A'] = 3 * data2['A'] - self.assertVariableEqual(data1['A'], 3 * data2['A']) + assert_equal(data1['A'], 3 * data2['A']) with pytest.raises(NotImplementedError): data1[{'x': 0}] = 0 @@ -2497,9 +2494,9 @@ def test_groupby_iter(self): data = create_test_data() for n, (t, sub) in enumerate(list(data.groupby('dim1'))[:3]): assert 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']) + assert_equal(data['var1'][n], sub['var1']) + assert_equal(data['var2'][n], sub['var2']) + assert_equal(data['var3'][:, n], sub['var3']) def test_groupby_errors(self): data = create_test_data() diff --git a/xarray/tests/test_variable.py b/xarray/tests/test_variable.py index f49a5d7b501..5a89627a0f9 100644 --- a/xarray/tests/test_variable.py +++ b/xarray/tests/test_variable.py @@ -1,3 +1,4 @@ + from __future__ import absolute_import from __future__ import division from __future__ import print_function @@ -26,7 +27,7 @@ from . import ( TestCase, source_ndarray, requires_dask, raises_regex, assert_identical, - assert_array_equal) + assert_array_equal, assert_equal, assert_allclose) from xarray.tests import requires_bottleneck @@ -460,7 +461,7 @@ def test_concat_fixed_len_str(self): actual = Variable.concat([x, y], 'animal') expected = Variable( 'animal', np.array(['horse', 'aardvark'], dtype=kind)) - self.assertVariableEqual(expected, actual) + assert_equal(expected, actual) def test_concat_number_strings(self): # regression test for #305 @@ -508,13 +509,13 @@ def test_real_and_imag(self): expected_abs = self.cls( 'x', np.sqrt(2 * np.arange(3) ** 2)).to_base_variable() - self.assertVariableAllClose(abs(v), expected_abs) + assert_allclose(abs(v), expected_abs) def test_aggregate_complex(self): # should skip NaNs v = self.cls('x', [1, 2j, np.nan]) expected = Variable((), 0.5 + 1j) - self.assertVariableAllClose(v.mean(), expected) + assert_allclose(v.mean(), expected) def test_pandas_cateogrical_dtype(self): data = pd.Categorical(np.arange(10, dtype='int64')) @@ -1360,7 +1361,7 @@ def test_reduce(self): assert_identical( v.reduce(np.mean, 'x').reduce(np.std, 'y'), Variable([], self.d.mean(axis=0).std())) - self.assertVariableAllClose(v.mean('x'), v.reduce(np.mean, 'x')) + assert_allclose(v.mean('x'), v.reduce(np.mean, 'x')) with raises_regex(ValueError, 'cannot supply both'): v.mean(dim='x', axis=0) @@ -1413,7 +1414,7 @@ def test_rank(self): # pct v = Variable(['x'], [3.0, 1.0, np.nan, 2.0, 4.0]) v_expect = Variable(['x'], [0.75, 0.25, np.nan, 0.5, 1.0]) - self.assertVariableEqual(v.rank('x', pct=True), v_expect) + assert_equal(v.rank('x', pct=True), v_expect) # invalid dim with raises_regex(ValueError, 'not found'): v.rank('y') From caed1bd633492b791a0f067bb129f3bfd3a7cf3a Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Sun, 14 Jan 2018 20:34:05 -0500 Subject: [PATCH 39/41] more parentheses please --- xarray/tests/test_dataarray.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 09952fa2f35..5524b4c971d 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -1653,7 +1653,7 @@ def test_unstack_pandas_consistency(self): def test_transpose(self): assert_equal(self.dv.variable.transpose(), - self.dv.transpose().variable) + self.dv.transpose().variable) def test_squeeze(self): assert_equal(self.dv.variable.squeeze(), self.dv.squeeze().variable) @@ -1768,7 +1768,7 @@ def test_reduce(self): assert_identical(expected, actual) assert_equal(self.dv.reduce(np.mean, 'x').variable, - self.v.reduce(np.mean, 'x')) + self.v.reduce(np.mean, 'x')) orig = DataArray([[1, 0, np.nan], [3, 0, 3]], coords, dims=['x', 'y']) actual = orig.count() @@ -1918,8 +1918,7 @@ def test_groupby_sum(self): self.x[:, 10:].sum(1), self.x[:, 9:10].sum(1)]).T), 'abc': Variable(['abc'], np.array(['a', 'b', 'c']))})['foo'] - assert_allclose(expected_sum_axis1, - grouped.reduce(np.sum, 'y')) + assert_allclose(expected_sum_axis1, grouped.reduce(np.sum, 'y')) assert_allclose(expected_sum_axis1, grouped.sum('y')) def test_groupby_count(self): @@ -2875,11 +2874,11 @@ def test_to_and_from_iris(self): assert actual.var_name == original.name self.assertItemsEqual([d.var_name for d in actual.dim_coords], original.dims) - assert (actual.cell_methods == - iris.coords.CellMethod(method='mean', - coords=('height',), - intervals=(), - comments=('A cell method',))) + assert (actual.cell_methods == (iris.coords.CellMethod( + method='mean', + coords=('height', ), + intervals=(), + comments=('A cell method', )), )) for coord, orginal_key in zip((actual.coords()), original.coords): original_coord = original.coords[orginal_key] @@ -2947,11 +2946,11 @@ def test_to_and_from_iris_dask(self): assert actual.var_name == original.name self.assertItemsEqual([d.var_name for d in actual.dim_coords], original.dims) - assert (actual.cell_methods == - iris.coords.CellMethod(method='mean', - coords=('height',), - intervals=(), - comments=('A cell method',))) + assert (actual.cell_methods == (iris.coords.CellMethod( + method='mean', + coords=('height', ), + intervals=(), + comments=('A cell method', )), )) for coord, orginal_key in zip((actual.coords()), original.coords): original_coord = original.coords[orginal_key] From 3938aa76df5ee6e9282dc91aca8060627023c375 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Mon, 15 Jan 2018 12:50:04 -0500 Subject: [PATCH 40/41] install pytest through pip in 3.4 CI --- ci/requirements-py34.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/requirements-py34.yml b/ci/requirements-py34.yml index 0b0d4099e3b..ba79e00bb12 100644 --- a/ci/requirements-py34.yml +++ b/ci/requirements-py34.yml @@ -2,9 +2,9 @@ name: test_env dependencies: - python=3.4 - bottleneck - - pytest - flake8 - pandas - pip: - coveralls - pytest-cov + - pytest From ec9d88ca045cc690e8188a7062799c64dd2539c5 Mon Sep 17 00:00:00 2001 From: Maximilian Roos Date: Mon, 15 Jan 2018 13:16:00 -0500 Subject: [PATCH 41/41] flake8 fails on 3.4 only without this? --- xarray/tests/test_dataset.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 349c72dfe7e..09d67613007 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -3784,8 +3784,8 @@ def test_filter_by_attrs(self): # Test return one DataArray. new_ds = ds.filter_by_attrs( standard_name='convective_precipitation_flux') - assert (new_ds['precipitation'].standard_name - == 'convective_precipitation_flux') + assert (new_ds['precipitation'].standard_name == + 'convective_precipitation_flux') assert_equal(new_ds['precipitation'], ds['precipitation'])