@@ -753,13 +753,26 @@ def test_encoding_kwarg(self):
753
753
with self .roundtrip (ds , save_kwargs = kwargs ) as actual :
754
754
pass
755
755
756
+ def test_encoding_kwarg_dates (self ):
756
757
ds = Dataset ({'t' : pd .date_range ('2000-01-01' , periods = 3 )})
757
758
units = 'days since 1900-01-01'
758
759
kwargs = dict (encoding = {'t' : {'units' : units }})
759
760
with self .roundtrip (ds , save_kwargs = kwargs ) as actual :
760
761
self .assertEqual (actual .t .encoding ['units' ], units )
761
762
assert_identical (actual , ds )
762
763
764
+ def test_encoding_kwarg_fixed_width_string (self ):
765
+ # regression test for GH2149
766
+ for strings in [
767
+ [b'foo' , b'bar' , b'baz' ],
768
+ [u'foo' , u'bar' , u'baz' ],
769
+ ]:
770
+ ds = Dataset ({'x' : strings })
771
+ kwargs = dict (encoding = {'x' : {'dtype' : 'S1' }})
772
+ with self .roundtrip (ds , save_kwargs = kwargs ) as actual :
773
+ self .assertEqual (actual ['x' ].encoding ['dtype' ], 'S1' )
774
+ assert_identical (actual , ds )
775
+
763
776
def test_default_fill_value (self ):
764
777
# Test default encoding for float:
765
778
ds = Dataset ({'x' : ('y' , np .arange (10.0 ))})
@@ -879,8 +892,8 @@ def create_tmp_files(nfiles, suffix='.nc', allow_cleanup_failure=False):
879
892
yield files
880
893
881
894
882
- @requires_netCDF4
883
895
class BaseNetCDF4Test (CFEncodedDataTest ):
896
+ """Tests for both netCDF4-python and h5netcdf."""
884
897
885
898
engine = 'netcdf4'
886
899
@@ -942,6 +955,18 @@ def test_write_groups(self):
942
955
with self .open (tmp_file , group = 'data/2' ) as actual2 :
943
956
assert_identical (data2 , actual2 )
944
957
958
+ def test_encoding_kwarg_vlen_string (self ):
959
+ for input_strings in [
960
+ [b'foo' , b'bar' , b'baz' ],
961
+ [u'foo' , u'bar' , u'baz' ],
962
+ ]:
963
+ original = Dataset ({'x' : input_strings })
964
+ expected = Dataset ({'x' : [u'foo' , u'bar' , u'baz' ]})
965
+ kwargs = dict (encoding = {'x' : {'dtype' : str }})
966
+ with self .roundtrip (original , save_kwargs = kwargs ) as actual :
967
+ assert actual ['x' ].encoding ['dtype' ] is str
968
+ assert_identical (actual , expected )
969
+
945
970
def test_roundtrip_string_with_fill_value_vlen (self ):
946
971
values = np .array ([u'ab' , u'cdef' , np .nan ], dtype = object )
947
972
expected = Dataset ({'x' : ('t' , values )})
@@ -1054,6 +1079,23 @@ def test_compression_encoding(self):
1054
1079
with self .roundtrip (expected ) as actual :
1055
1080
assert_equal (expected , actual )
1056
1081
1082
+ def test_encoding_kwarg_compression (self ):
1083
+ ds = Dataset ({'x' : np .arange (10.0 )})
1084
+ encoding = dict (dtype = 'f4' , zlib = True , complevel = 9 , fletcher32 = True ,
1085
+ chunksizes = (5 ,), shuffle = True )
1086
+ kwargs = dict (encoding = dict (x = encoding ))
1087
+
1088
+ with self .roundtrip (ds , save_kwargs = kwargs ) as actual :
1089
+ assert_equal (actual , ds )
1090
+ self .assertEqual (actual .x .encoding ['dtype' ], 'f4' )
1091
+ self .assertEqual (actual .x .encoding ['zlib' ], True )
1092
+ self .assertEqual (actual .x .encoding ['complevel' ], 9 )
1093
+ self .assertEqual (actual .x .encoding ['fletcher32' ], True )
1094
+ self .assertEqual (actual .x .encoding ['chunksizes' ], (5 ,))
1095
+ self .assertEqual (actual .x .encoding ['shuffle' ], True )
1096
+
1097
+ self .assertEqual (ds .x .encoding , {})
1098
+
1057
1099
def test_encoding_chunksizes_unlimited (self ):
1058
1100
# regression test for GH1225
1059
1101
ds = Dataset ({'x' : [1 , 2 , 3 ], 'y' : ('x' , [2 , 3 , 4 ])})
@@ -1117,7 +1159,7 @@ def test_already_open_dataset(self):
1117
1159
expected = Dataset ({'x' : ((), 42 )})
1118
1160
assert_identical (expected , ds )
1119
1161
1120
- def test_variable_len_strings (self ):
1162
+ def test_read_variable_len_strings (self ):
1121
1163
with create_tmp_file () as tmp_file :
1122
1164
values = np .array (['foo' , 'bar' , 'baz' ], dtype = object )
1123
1165
@@ -1410,6 +1452,10 @@ def test_group(self):
1410
1452
open_kwargs = {'group' : group }) as actual :
1411
1453
assert_identical (original , actual )
1412
1454
1455
+ def test_encoding_kwarg_fixed_width_string (self ):
1456
+ # not relevant for zarr, since we don't use EncodedStringCoder
1457
+ pass
1458
+
1413
1459
# TODO: someone who understand caching figure out whether chaching
1414
1460
# makes sense for Zarr backend
1415
1461
@pytest .mark .xfail (reason = "Zarr caching not implemented" )
@@ -1579,6 +1625,13 @@ def create_store(self):
1579
1625
tmp_file , mode = 'w' , format = 'NETCDF3_CLASSIC' ) as store :
1580
1626
yield store
1581
1627
1628
+ def test_encoding_kwarg_vlen_string (self ):
1629
+ original = Dataset ({'x' : [u'foo' , u'bar' , u'baz' ]})
1630
+ kwargs = dict (encoding = {'x' : {'dtype' : str }})
1631
+ with raises_regex (ValueError , 'encoding dtype=str for vlen' ):
1632
+ with self .roundtrip (original , save_kwargs = kwargs ):
1633
+ pass
1634
+
1582
1635
1583
1636
class NetCDF3ViaNetCDF4DataTestAutocloseTrue (NetCDF3ViaNetCDF4DataTest ):
1584
1637
autoclose = True
0 commit comments