@@ -65,135 +65,136 @@ def __init__(
65
65
self ._open_handles (src , kwds )
66
66
assert self .handles is not None
67
67
68
- # Have to pass int, would break tests using TextReader directly otherwise :(
69
- kwds ["on_bad_lines" ] = self .on_bad_lines .value
70
-
71
- for key in (
72
- "storage_options" ,
73
- "encoding" ,
74
- "memory_map" ,
75
- "compression" ,
76
- "error_bad_lines" ,
77
- "warn_bad_lines" ,
78
- ):
79
- kwds .pop (key , None )
80
-
81
- kwds ["dtype" ] = ensure_dtype_objs (kwds .get ("dtype" , None ))
82
68
try :
83
- self ._reader = parsers .TextReader (self .handles .handle , ** kwds )
84
- except Exception :
85
- self .handles .close ()
86
- raise
69
+ # Have to pass int, would break tests using TextReader directly otherwise :(
70
+ kwds ["on_bad_lines" ] = self .on_bad_lines .value
71
+
72
+ for key in (
73
+ "storage_options" ,
74
+ "encoding" ,
75
+ "memory_map" ,
76
+ "compression" ,
77
+ "error_bad_lines" ,
78
+ "warn_bad_lines" ,
79
+ ):
80
+ kwds .pop (key , None )
87
81
88
- self . unnamed_cols = self . _reader . unnamed_cols
82
+ kwds [ "dtype" ] = ensure_dtype_objs ( kwds . get ( "dtype" , None ))
89
83
90
- # error: Cannot determine type of 'names'
91
- passed_names = self .names is None # type: ignore[has-type]
84
+ self ._reader = parsers .TextReader (self .handles .handle , ** kwds )
85
+
86
+ self .unnamed_cols = self ._reader .unnamed_cols
92
87
93
- if self ._reader .header is None :
94
- self .names = None
95
- else :
96
88
# error: Cannot determine type of 'names'
97
- # error: Cannot determine type of 'index_names'
98
- (
99
- self .names , # type: ignore[has-type]
100
- self .index_names ,
101
- self .col_names ,
102
- passed_names ,
103
- ) = self ._extract_multi_indexer_columns (
104
- self ._reader .header ,
105
- self .index_names , # type: ignore[has-type]
106
- passed_names ,
107
- )
89
+ passed_names = self .names is None # type: ignore[has-type]
108
90
109
- # error: Cannot determine type of 'names'
110
- if self .names is None : # type: ignore[has-type]
111
- if self .prefix :
112
- # error: Cannot determine type of 'names'
113
- self .names = [ # type: ignore[has-type]
114
- f"{ self .prefix } { i } " for i in range (self ._reader .table_width )
115
- ]
91
+ if self ._reader .header is None :
92
+ self .names = None
116
93
else :
117
94
# error: Cannot determine type of 'names'
118
- self .names = list ( # type: ignore[has-type]
119
- range (self ._reader .table_width )
95
+ # error: Cannot determine type of 'index_names'
96
+ (
97
+ self .names , # type: ignore[has-type]
98
+ self .index_names ,
99
+ self .col_names ,
100
+ passed_names ,
101
+ ) = self ._extract_multi_indexer_columns (
102
+ self ._reader .header ,
103
+ self .index_names , # type: ignore[has-type]
104
+ passed_names ,
120
105
)
121
106
122
- # gh-9755
123
- #
124
- # need to set orig_names here first
125
- # so that proper indexing can be done
126
- # with _set_noconvert_columns
127
- #
128
- # once names has been filtered, we will
129
- # then set orig_names again to names
130
- # error: Cannot determine type of 'names'
131
- self .orig_names = self .names [:] # type: ignore[has-type]
132
-
133
- if self .usecols :
134
- usecols = self ._evaluate_usecols (self .usecols , self .orig_names )
135
-
136
- # GH 14671
137
- # assert for mypy, orig_names is List or None, None would error in issubset
138
- assert self .orig_names is not None
139
- if self .usecols_dtype == "string" and not set (usecols ).issubset (
140
- self .orig_names
141
- ):
142
- self ._validate_usecols_names (usecols , self .orig_names )
143
-
144
107
# error: Cannot determine type of 'names'
145
- if len (self .names ) > len (usecols ): # type: ignore[has-type]
146
- # error: Cannot determine type of 'names'
147
- self .names = [ # type: ignore[has-type]
148
- n
108
+ if self .names is None : # type: ignore[has-type]
109
+ if self .prefix :
149
110
# error: Cannot determine type of 'names'
150
- for i , n in enumerate (self .names ) # type: ignore[has-type]
151
- if (i in usecols or n in usecols )
152
- ]
153
-
111
+ self .names = [ # type: ignore[has-type]
112
+ f"{ self .prefix } { i } " for i in range (self ._reader .table_width )
113
+ ]
114
+ else :
115
+ # error: Cannot determine type of 'names'
116
+ self .names = list ( # type: ignore[has-type]
117
+ range (self ._reader .table_width )
118
+ )
119
+
120
+ # gh-9755
121
+ #
122
+ # need to set orig_names here first
123
+ # so that proper indexing can be done
124
+ # with _set_noconvert_columns
125
+ #
126
+ # once names has been filtered, we will
127
+ # then set orig_names again to names
154
128
# error: Cannot determine type of 'names'
155
- if len (self .names ) < len (usecols ): # type: ignore[has-type]
156
- # error: Cannot determine type of 'names'
157
- self ._validate_usecols_names (
158
- usecols ,
159
- self .names , # type: ignore[has-type]
160
- )
161
-
162
- # error: Cannot determine type of 'names'
163
- self ._validate_parse_dates_presence (self .names ) # type: ignore[has-type]
164
- self ._set_noconvert_columns ()
129
+ self .orig_names = self .names [:] # type: ignore[has-type]
165
130
166
- # error: Cannot determine type of 'names'
167
- self . orig_names = self . names # type: ignore[has-type]
131
+ if self . usecols :
132
+ usecols = self . _evaluate_usecols ( self . usecols , self . orig_names )
168
133
169
- if not self ._has_complex_date_col :
170
- # error: Cannot determine type of 'index_col'
171
- if self ._reader .leading_cols == 0 and is_index_col (
172
- self .index_col # type: ignore[has-type]
173
- ):
134
+ # GH 14671
135
+ # assert for mypy, orig_names is List or None, None would error in issubset
136
+ assert self .orig_names is not None
137
+ if self .usecols_dtype == "string" and not set (usecols ).issubset (
138
+ self .orig_names
139
+ ):
140
+ self ._validate_usecols_names (usecols , self .orig_names )
174
141
175
- self ._name_processed = True
176
- (
177
- index_names ,
178
- # error: Cannot determine type of 'names'
179
- self .names , # type: ignore[has-type]
180
- self .index_col ,
181
- ) = self ._clean_index_names (
142
+ # error: Cannot determine type of 'names'
143
+ if len (self .names ) > len (usecols ): # type: ignore[has-type]
182
144
# error: Cannot determine type of 'names'
183
- self .names , # type: ignore[has-type]
184
- # error: Cannot determine type of 'index_col'
185
- self .index_col , # type: ignore[has-type]
186
- self .unnamed_cols ,
187
- )
145
+ self .names = [ # type: ignore[has-type]
146
+ n
147
+ # error: Cannot determine type of 'names'
148
+ for i , n in enumerate (self .names ) # type: ignore[has-type]
149
+ if (i in usecols or n in usecols )
150
+ ]
188
151
189
- if self .index_names is None :
190
- self .index_names = index_names
152
+ # error: Cannot determine type of 'names'
153
+ if len (self .names ) < len (usecols ): # type: ignore[has-type]
154
+ # error: Cannot determine type of 'names'
155
+ self ._validate_usecols_names (
156
+ usecols ,
157
+ self .names , # type: ignore[has-type]
158
+ )
191
159
192
- if self . _reader . header is None and not passed_names :
193
- assert self .index_names is not None
194
- self .index_names = [ None ] * len ( self . index_names )
160
+ # error: Cannot determine type of 'names'
161
+ self ._validate_parse_dates_presence ( self . names ) # type: ignore[has-type]
162
+ self ._set_noconvert_columns ( )
195
163
196
- self ._implicit_index = self ._reader .leading_cols > 0
164
+ # error: Cannot determine type of 'names'
165
+ self .orig_names = self .names # type: ignore[has-type]
166
+
167
+ if not self ._has_complex_date_col :
168
+ # error: Cannot determine type of 'index_col'
169
+ if self ._reader .leading_cols == 0 and is_index_col (
170
+ self .index_col # type: ignore[has-type]
171
+ ):
172
+
173
+ self ._name_processed = True
174
+ (
175
+ index_names ,
176
+ # error: Cannot determine type of 'names'
177
+ self .names , # type: ignore[has-type]
178
+ self .index_col ,
179
+ ) = self ._clean_index_names (
180
+ # error: Cannot determine type of 'names'
181
+ self .names , # type: ignore[has-type]
182
+ # error: Cannot determine type of 'index_col'
183
+ self .index_col , # type: ignore[has-type]
184
+ self .unnamed_cols ,
185
+ )
186
+
187
+ if self .index_names is None :
188
+ self .index_names = index_names
189
+
190
+ if self ._reader .header is None and not passed_names :
191
+ assert self .index_names is not None
192
+ self .index_names = [None ] * len (self .index_names )
193
+
194
+ self ._implicit_index = self ._reader .leading_cols > 0
195
+ except Exception :
196
+ self .handles .close ()
197
+ raise
197
198
198
199
def close (self ) -> None :
199
200
super ().close ()
0 commit comments