9
9
10
10
import csv as csvlib
11
11
from zipfile import ZipFile
12
+
12
13
import numpy as np
13
14
14
- from pandas .core . dtypes . missing import notna
15
- from pandas . core . index import Index , MultiIndex
15
+ from pandas ._libs import writers as libwriters
16
+
16
17
from pandas import compat
17
- from pandas .compat import (StringIO , range , zip )
18
+ from pandas .compat import StringIO , range , zip
19
+
20
+ from pandas .core .dtypes .missing import notna
21
+ from pandas .core .dtypes .generic import (
22
+ ABCMultiIndex , ABCPeriodIndex , ABCDatetimeIndex , ABCIndexClass )
18
23
19
24
from pandas .io .common import (_get_handle , UnicodeWriter , _expand_user ,
20
25
_stringify_path )
21
- from pandas ._libs import writers as libwriters
22
- from pandas .core .indexes .datetimes import DatetimeIndex
23
- from pandas .core .indexes .period import PeriodIndex
24
26
25
27
26
28
class CSVFormatter (object ):
@@ -68,7 +70,7 @@ def __init__(self, obj, path_or_buf=None, sep=",", na_rep='',
68
70
self .date_format = date_format
69
71
70
72
self .tupleize_cols = tupleize_cols
71
- self .has_mi_columns = (isinstance (obj .columns , MultiIndex ) and
73
+ self .has_mi_columns = (isinstance (obj .columns , ABCMultiIndex ) and
72
74
not self .tupleize_cols )
73
75
74
76
# validate mi options
@@ -78,7 +80,7 @@ def __init__(self, obj, path_or_buf=None, sep=",", na_rep='',
78
80
"columns" )
79
81
80
82
if cols is not None :
81
- if isinstance (cols , Index ):
83
+ if isinstance (cols , ABCIndexClass ):
82
84
cols = cols .to_native_types (na_rep = na_rep ,
83
85
float_format = float_format ,
84
86
date_format = date_format ,
@@ -90,7 +92,7 @@ def __init__(self, obj, path_or_buf=None, sep=",", na_rep='',
90
92
# update columns to include possible multiplicity of dupes
91
93
# and make sure sure cols is just a list of labels
92
94
cols = self .obj .columns
93
- if isinstance (cols , Index ):
95
+ if isinstance (cols , ABCIndexClass ):
94
96
cols = cols .to_native_types (na_rep = na_rep ,
95
97
float_format = float_format ,
96
98
date_format = date_format ,
@@ -111,8 +113,9 @@ def __init__(self, obj, path_or_buf=None, sep=",", na_rep='',
111
113
self .chunksize = int (chunksize )
112
114
113
115
self .data_index = obj .index
114
- if (isinstance (self .data_index , (DatetimeIndex , PeriodIndex )) and
116
+ if (isinstance (self .data_index , (ABCDatetimeIndex , ABCPeriodIndex )) and
115
117
date_format is not None ):
118
+ from pandas import Index
116
119
self .data_index = Index ([x .strftime (date_format ) if notna (x ) else
117
120
'' for x in self .data_index ])
118
121
@@ -197,7 +200,8 @@ def _save_header(self):
197
200
header = self .header
198
201
encoded_labels = []
199
202
200
- has_aliases = isinstance (header , (tuple , list , np .ndarray , Index ))
203
+ has_aliases = isinstance (header , (tuple , list , np .ndarray ,
204
+ ABCIndexClass ))
201
205
if not (has_aliases or self .header ):
202
206
return
203
207
if has_aliases :
@@ -214,7 +218,7 @@ def _save_header(self):
214
218
# should write something for index label
215
219
if index_label is not False :
216
220
if index_label is None :
217
- if isinstance (obj .index , MultiIndex ):
221
+ if isinstance (obj .index , ABCMultiIndex ):
218
222
index_label = []
219
223
for i , name in enumerate (obj .index .names ):
220
224
if name is None :
@@ -227,7 +231,7 @@ def _save_header(self):
227
231
else :
228
232
index_label = [index_label ]
229
233
elif not isinstance (index_label ,
230
- (list , tuple , np .ndarray , Index )):
234
+ (list , tuple , np .ndarray , ABCIndexClass )):
231
235
# given a string for a DF with Index
232
236
index_label = [index_label ]
233
237
0 commit comments