@@ -132,10 +132,11 @@ def __init__(self, cf_var):
132132 self .varname = cf_var .name
133133 self .dtype = cf_var .dtype
134134 self .is_chardata = np .issubdtype (self .dtype , np .bytes_ )
135- self .read_encoding = self ._get_encoding (cf_var , writing = False )
136- self .write_encoding = self ._get_encoding (cf_var , writing = True )
137- self .n_chars_dim = cf_var .group ().dimensions [cf_var .dimensions [- 1 ]].size
138- self .string_width = self ._get_string_width (cf_var )
135+ if self .is_chardata :
136+ self .read_encoding = self ._get_encoding (cf_var , writing = False )
137+ self .write_encoding = self ._get_encoding (cf_var , writing = True )
138+ self .n_chars_dim = cf_var .group ().dimensions [cf_var .dimensions [- 1 ]].size
139+ self .string_width = self ._get_string_width (cf_var )
139140
140141 @staticmethod
141142 def _get_encoding (cf_var , writing = False ) -> str :
@@ -199,7 +200,7 @@ def decode_bytes_to_stringarray(self, data: np.ndarray) -> np.ndarray:
199200 return data
200201
201202 def encode_strings_as_bytearray (self , data : np .ndarray ) -> np .ndarray :
202- if data .dtype .kind == "U" :
203+ if self . is_chardata and data .dtype .kind == "U" :
203204 # N.B. it is also possible to pass a byte array (dtype "S1"),
204205 # to be written directly, without processing.
205206 try :
0 commit comments