@@ -13,7 +13,7 @@ from cpython cimport (PyObject, PyBytes_FromString,
13
13
PyUnicode_Check, PyUnicode_AsUTF8String,
14
14
PyErr_Occurred, PyErr_Fetch)
15
15
from cpython.ref cimport PyObject, Py_XDECREF
16
- from io.common import ParserError, DtypeWarning, EmptyDataError
16
+ from io.common import ParserError, DtypeWarning, EmptyDataError, ParserWarning
17
17
18
18
# Import CParserError as alias of ParserError for backwards compatibility.
19
19
# Ultimately, we want to remove this import. See gh-12665 and gh-14479.
@@ -987,7 +987,7 @@ cdef class TextReader:
987
987
Py_ssize_t i, nused
988
988
kh_str_t * na_hashset = NULL
989
989
int start, end
990
- object name, na_flist
990
+ object name, na_flist, col_dtype = None
991
991
bint na_filter = 0
992
992
Py_ssize_t num_cols
993
993
@@ -1043,14 +1043,33 @@ cdef class TextReader:
1043
1043
else :
1044
1044
na_filter = 0
1045
1045
1046
+ col_dtype = None
1047
+ if self .dtype is not None :
1048
+ if isinstance (self .dtype, dict ):
1049
+ if name in self .dtype:
1050
+ col_dtype = self .dtype[name]
1051
+ elif i in self .dtype:
1052
+ col_dtype = self .dtype[i]
1053
+ else :
1054
+ if self .dtype.names:
1055
+ # structured array
1056
+ col_dtype = np.dtype(self .dtype.descr[i][1 ])
1057
+ else :
1058
+ col_dtype = self .dtype
1059
+
1046
1060
if conv:
1061
+ if col_dtype is not None :
1062
+ warnings.warn((" Both a converter and dtype were specified "
1063
+ " for column {0} - only the converter will "
1064
+ " be used" ).format(name), ParserWarning,
1065
+ stacklevel = 5 )
1047
1066
results[i] = _apply_converter(conv, self .parser, i, start, end,
1048
1067
self .c_encoding)
1049
1068
continue
1050
1069
1051
1070
# Should return as the desired dtype (inferred or specified)
1052
1071
col_res, na_count = self ._convert_tokens(
1053
- i, start, end, name, na_filter, na_hashset, na_flist)
1072
+ i, start, end, name, na_filter, na_hashset, na_flist, col_dtype )
1054
1073
1055
1074
if na_filter:
1056
1075
self ._free_na_set(na_hashset)
@@ -1075,32 +1094,17 @@ cdef class TextReader:
1075
1094
cdef inline _convert_tokens(self , Py_ssize_t i, int start, int end,
1076
1095
object name, bint na_filter,
1077
1096
kh_str_t * na_hashset,
1078
- object na_flist):
1079
- cdef:
1080
- object col_dtype = None
1081
-
1082
- if self .dtype is not None :
1083
- if isinstance (self .dtype, dict ):
1084
- if name in self .dtype:
1085
- col_dtype = self .dtype[name]
1086
- elif i in self .dtype:
1087
- col_dtype = self .dtype[i]
1088
- else :
1089
- if self .dtype.names:
1090
- # structured array
1091
- col_dtype = np.dtype(self .dtype.descr[i][1 ])
1092
- else :
1093
- col_dtype = self .dtype
1097
+ object na_flist, object col_dtype):
1094
1098
1095
- if col_dtype is not None :
1096
- col_res, na_count = self ._convert_with_dtype(
1097
- col_dtype, i, start, end, na_filter,
1098
- 1 , na_hashset, na_flist)
1099
+ if col_dtype is not None :
1100
+ col_res, na_count = self ._convert_with_dtype(
1101
+ col_dtype, i, start, end, na_filter,
1102
+ 1 , na_hashset, na_flist)
1099
1103
1100
- # Fallback on the parse (e.g. we requested int dtype,
1101
- # but its actually a float).
1102
- if col_res is not None :
1103
- return col_res, na_count
1104
+ # Fallback on the parse (e.g. we requested int dtype,
1105
+ # but its actually a float).
1106
+ if col_res is not None :
1107
+ return col_res, na_count
1104
1108
1105
1109
if i in self .noconvert:
1106
1110
return self ._string_convert(i, start, end, na_filter, na_hashset)
0 commit comments