@@ -421,10 +421,6 @@ def _read(filepath_or_buffer: FilePathOrBuffer, kwds):
421
421
kwds ["encoding" ] = encoding
422
422
compression = kwds .get ("compression" , "infer" )
423
423
424
- # TODO: get_filepath_or_buffer could return
425
- # Union[FilePathOrBuffer, s3fs.S3File, gcsfs.GCSFile]
426
- # though mypy handling of conditional imports is difficult.
427
- # See https://github.com/python/mypy/issues/1297
428
424
ioargs = get_filepath_or_buffer (
429
425
filepath_or_buffer , encoding , compression , storage_options = storage_options
430
426
)
@@ -914,7 +910,6 @@ def __init__(self, f, engine=None, **kwds):
914
910
915
911
# miscellanea
916
912
self .engine = engine
917
- self ._engine = None
918
913
self ._currow = 0
919
914
920
915
options = self ._get_options_with_defaults (engine )
@@ -923,14 +918,13 @@ def __init__(self, f, engine=None, **kwds):
923
918
self .nrows = options .pop ("nrows" , None )
924
919
self .squeeze = options .pop ("squeeze" , False )
925
920
926
- # might mutate self.engine
927
- self .engine = self ._check_file_or_buffer (f , engine )
921
+ self ._check_file_or_buffer (f , engine )
928
922
self .options , self .engine = self ._clean_options (options , engine )
929
923
930
924
if "has_index_names" in kwds :
931
925
self .options ["has_index_names" ] = kwds ["has_index_names" ]
932
926
933
- self ._make_engine (self .engine )
927
+ self ._engine = self . _make_engine (self .engine )
934
928
935
929
def close (self ):
936
930
self ._engine .close ()
@@ -987,24 +981,21 @@ def _check_file_or_buffer(self, f, engine):
987
981
msg = "The 'python' engine cannot iterate through this file buffer."
988
982
raise ValueError (msg )
989
983
990
- return engine
991
-
992
984
def _clean_options (self , options , engine ):
993
985
result = options .copy ()
994
986
995
987
engine_specified = self ._engine_specified
996
988
fallback_reason = None
997
989
998
- sep = options ["delimiter" ]
999
- delim_whitespace = options ["delim_whitespace" ]
1000
-
1001
990
# C engine not supported yet
1002
991
if engine == "c" :
1003
992
if options ["skipfooter" ] > 0 :
1004
993
fallback_reason = "the 'c' engine does not support skipfooter"
1005
994
engine = "python"
1006
995
1007
- encoding = sys .getfilesystemencoding () or "utf-8"
996
+ sep = options ["delimiter" ]
997
+ delim_whitespace = options ["delim_whitespace" ]
998
+
1008
999
if sep is None and not delim_whitespace :
1009
1000
if engine == "c" :
1010
1001
fallback_reason = (
@@ -1029,6 +1020,7 @@ def _clean_options(self, options, engine):
1029
1020
result ["delimiter" ] = r"\s+"
1030
1021
elif sep is not None :
1031
1022
encodeable = True
1023
+ encoding = sys .getfilesystemencoding () or "utf-8"
1032
1024
try :
1033
1025
if len (sep .encode (encoding )) > 1 :
1034
1026
encodeable = False
@@ -1161,29 +1153,26 @@ def __next__(self):
1161
1153
raise
1162
1154
1163
1155
def _make_engine (self , engine = "c" ):
1164
- if engine == "c" :
1165
- self ._engine = CParserWrapper (self .f , ** self .options )
1156
+ mapping = {
1157
+ "c" : CParserWrapper ,
1158
+ "python" : PythonParser ,
1159
+ "python-fwf" : FixedWidthFieldParser ,
1160
+ }
1161
+ try :
1162
+ klass = mapping [engine ]
1163
+ except KeyError :
1164
+ raise ValueError (
1165
+ f"Unknown engine: { engine } (valid options are { mapping .keys ()} )"
1166
+ )
1166
1167
else :
1167
- if engine == "python" :
1168
- klass = PythonParser
1169
- elif engine == "python-fwf" :
1170
- klass = FixedWidthFieldParser
1171
- else :
1172
- raise ValueError (
1173
- f"Unknown engine: { engine } (valid options "
1174
- 'are "c", "python", or "python-fwf")'
1175
- )
1176
- self ._engine = klass (self .f , ** self .options )
1168
+ return klass (self .f , ** self .options )
1177
1169
1178
1170
def _failover_to_python (self ):
1179
1171
raise AbstractMethodError (self )
1180
1172
1181
1173
def read (self , nrows = None ):
1182
1174
nrows = validate_integer ("nrows" , nrows )
1183
- ret = self ._engine .read (nrows )
1184
-
1185
- # May alter columns / col_dict
1186
- index , columns , col_dict = self ._create_index (ret )
1175
+ index , columns , col_dict = self ._engine .read (nrows )
1187
1176
1188
1177
if index is None :
1189
1178
if col_dict :
@@ -1203,10 +1192,6 @@ def read(self, nrows=None):
1203
1192
return df [df .columns [0 ]].copy ()
1204
1193
return df
1205
1194
1206
- def _create_index (self , ret ):
1207
- index , columns , col_dict = ret
1208
- return index , columns , col_dict
1209
-
1210
1195
def get_chunk (self , size = None ):
1211
1196
if size is None :
1212
1197
size = self .chunksize
0 commit comments