36
36
from pandas .util ._decorators import Appender
37
37
from pandas .util ._decorators import deprecate_kwarg
38
38
39
- VALID_ENCODINGS = ('ascii' , 'us-ascii' , 'latin-1' , 'latin_1' , 'iso-8859-1' ,
40
- 'iso8859-1' , '8859' , 'cp819' , 'latin' , 'latin1' , 'L1' )
41
-
42
39
_version_error = ("Version of given Stata file is not 104, 105, 108, "
43
40
"111 (Stata 7SE), 113 (Stata 8/9), 114 (Stata 10/11), "
44
41
"115 (Stata 12), 117 (Stata 13), or 118 (Stata 14)" )
169
166
170
167
171
168
@Appender (_read_stata_doc )
169
+ @deprecate_kwarg (old_arg_name = 'encoding' , new_arg_name = None )
172
170
@deprecate_kwarg (old_arg_name = 'index' , new_arg_name = 'index_col' )
173
171
def read_stata (filepath_or_buffer , convert_dates = True ,
174
172
convert_categoricals = True , encoding = None , index_col = None ,
@@ -182,7 +180,7 @@ def read_stata(filepath_or_buffer, convert_dates=True,
182
180
preserve_dtypes = preserve_dtypes ,
183
181
columns = columns ,
184
182
order_categoricals = order_categoricals ,
185
- chunksize = chunksize , encoding = encoding )
183
+ chunksize = chunksize )
186
184
187
185
if iterator or chunksize :
188
186
data = reader
@@ -838,15 +836,8 @@ def get_base_missing_value(cls, dtype):
838
836
839
837
840
838
class StataParser (object ):
841
- _default_encoding = 'latin-1'
842
-
843
- def __init__ (self , encoding ):
844
- if encoding is not None :
845
- if encoding not in VALID_ENCODINGS :
846
- raise ValueError ('Unknown encoding. Only latin-1 and ascii '
847
- 'supported.' )
848
839
849
- self . _encoding = encoding
840
+ def __init__ ( self ):
850
841
851
842
# type code.
852
843
# --------------------
@@ -959,12 +950,13 @@ def __init__(self, encoding):
959
950
class StataReader (StataParser , BaseIterator ):
960
951
__doc__ = _stata_reader_doc
961
952
953
+ @deprecate_kwarg (old_arg_name = 'encoding' , new_arg_name = None )
962
954
@deprecate_kwarg (old_arg_name = 'index' , new_arg_name = 'index_col' )
963
955
def __init__ (self , path_or_buf , convert_dates = True ,
964
956
convert_categoricals = True , index_col = None ,
965
957
convert_missing = False , preserve_dtypes = True ,
966
958
columns = None , order_categoricals = True ,
967
- encoding = 'latin-1' , chunksize = None ):
959
+ encoding = None , chunksize = None ):
968
960
super (StataReader , self ).__init__ (encoding )
969
961
self .col_sizes = ()
970
962
@@ -977,10 +969,6 @@ def __init__(self, path_or_buf, convert_dates=True,
977
969
self ._preserve_dtypes = preserve_dtypes
978
970
self ._columns = columns
979
971
self ._order_categoricals = order_categoricals
980
- if encoding is not None :
981
- if encoding not in VALID_ENCODINGS :
982
- raise ValueError ('Unknown encoding. Only latin-1 and ascii '
983
- 'supported.' )
984
972
self ._encoding = encoding
985
973
self ._chunksize = chunksize
986
974
@@ -998,18 +986,13 @@ def __init__(self, path_or_buf, convert_dates=True,
998
986
path_or_buf = _stringify_path (path_or_buf )
999
987
if isinstance (path_or_buf , str ):
1000
988
path_or_buf , encoding , _ , should_close = get_filepath_or_buffer (
1001
- path_or_buf , encoding = self ._default_encoding
1002
- )
989
+ path_or_buf )
1003
990
1004
991
if isinstance (path_or_buf , (str , text_type , bytes )):
1005
992
self .path_or_buf = open (path_or_buf , 'rb' )
1006
993
else :
1007
994
# Copy to BytesIO, and ensure no encoding
1008
995
contents = path_or_buf .read ()
1009
- try :
1010
- contents = contents .encode (self ._default_encoding )
1011
- except :
1012
- pass
1013
996
self .path_or_buf = BytesIO (contents )
1014
997
1015
998
self ._read_header ()
@@ -1030,6 +1013,15 @@ def close(self):
1030
1013
except IOError :
1031
1014
pass
1032
1015
1016
+ def _set_encoding (self ):
1017
+ """
1018
+ Set string encoding which depends on file version
1019
+ """
1020
+ if self .format_version < 118 :
1021
+ self ._encoding = 'latin-1'
1022
+ else :
1023
+ self ._encoding = 'utf-8'
1024
+
1033
1025
def _read_header (self ):
1034
1026
first_char = self .path_or_buf .read (1 )
1035
1027
if struct .unpack ('c' , first_char )[0 ] == b'<' :
@@ -1049,6 +1041,7 @@ def _read_new_header(self, first_char):
1049
1041
self .format_version = int (self .path_or_buf .read (3 ))
1050
1042
if self .format_version not in [117 , 118 ]:
1051
1043
raise ValueError (_version_error )
1044
+ self ._set_encoding ()
1052
1045
self .path_or_buf .read (21 ) # </release><byteorder>
1053
1046
self .byteorder = self .path_or_buf .read (3 ) == b'MSF' and '>' or '<'
1054
1047
self .path_or_buf .read (15 ) # </byteorder><K>
@@ -1235,6 +1228,7 @@ def _read_old_header(self, first_char):
1235
1228
self .format_version = struct .unpack ('b' , first_char )[0 ]
1236
1229
if self .format_version not in [104 , 105 , 108 , 111 , 113 , 114 , 115 ]:
1237
1230
raise ValueError (_version_error )
1231
+ self ._set_encoding ()
1238
1232
self .byteorder = struct .unpack ('b' , self .path_or_buf .read (1 ))[
1239
1233
0 ] == 0x1 and '>' or '<'
1240
1234
self .filetype = struct .unpack ('b' , self .path_or_buf .read (1 ))[0 ]
@@ -1338,16 +1332,9 @@ def _decode(self, s):
1338
1332
return s .decode ('utf-8' )
1339
1333
1340
1334
def _null_terminate (self , s ):
1341
- if compat .PY3 or self ._encoding is not None :
1342
- # have bytes not strings, so must decode
1343
- s = s .partition (b"\0 " )[0 ]
1344
- return s .decode (self ._encoding or self ._default_encoding )
1345
- else :
1346
- null_byte = "\0 "
1347
- try :
1348
- return s .lstrip (null_byte )[:s .index (null_byte )]
1349
- except :
1350
- return s
1335
+ # have bytes not strings, so must decode
1336
+ s = s .partition (b"\0 " )[0 ]
1337
+ return s .decode (self ._encoding )
1351
1338
1352
1339
def _read_value_labels (self ):
1353
1340
if self ._value_labels_read :
@@ -1433,10 +1420,7 @@ def _read_strls(self):
1433
1420
self .path_or_buf .read (4 ))[0 ]
1434
1421
va = self .path_or_buf .read (length )
1435
1422
if typ == 130 :
1436
- encoding = 'utf-8'
1437
- if self .format_version == 117 :
1438
- encoding = self ._encoding or self ._default_encoding
1439
- va = va [0 :- 1 ].decode (encoding )
1423
+ va = va [0 :- 1 ].decode (self ._encoding )
1440
1424
# Wrap v_o in a string to allow uint64 values as keys on 32bit OS
1441
1425
self .GSO [str (v_o )] = va
1442
1426
0 commit comments