@@ -36,10 +36,9 @@ class TestDataFrameToCSV(tm.TestCase, TestData):
3636
3737 _multiprocess_can_split_ = True
3838
39- def test_to_csv_from_csv (self ):
39+ def test_to_csv_from_csv1 (self ):
4040
41- pname = '__tmp_to_csv_from_csv__'
42- with ensure_clean (pname ) as path :
41+ with ensure_clean ('__tmp_to_csv_from_csv1__' ) as path :
4342 self .frame ['A' ][:5 ] = nan
4443
4544 self .frame .to_csv (path )
@@ -69,7 +68,9 @@ def test_to_csv_from_csv(self):
6968 recons = DataFrame .from_csv (path )
7069 assert_frame_equal (dm , recons )
7170
72- with ensure_clean (pname ) as path :
71+ def test_to_csv_from_csv2 (self ):
72+
73+ with ensure_clean ('__tmp_to_csv_from_csv2__' ) as path :
7374
7475 # duplicate index
7576 df = DataFrame (np .random .randn (3 , 3 ), index = ['a' , 'a' , 'b' ],
@@ -101,7 +102,9 @@ def test_to_csv_from_csv(self):
101102 self .assertRaises (ValueError , self .frame2 .to_csv , path ,
102103 header = ['AA' , 'X' ])
103104
104- with ensure_clean (pname ) as path :
105+ def test_to_csv_from_csv3 (self ):
106+
107+ with ensure_clean ('__tmp_to_csv_from_csv3__' ) as path :
105108 df1 = DataFrame (np .random .randn (3 , 1 ))
106109 df2 = DataFrame (np .random .randn (3 , 1 ))
107110
@@ -113,7 +116,9 @@ def test_to_csv_from_csv(self):
113116 xp .columns = lmap (int , xp .columns )
114117 assert_frame_equal (xp , rs )
115118
116- with ensure_clean () as path :
119+ def test_to_csv_from_csv4 (self ):
120+
121+ with ensure_clean ('__tmp_to_csv_from_csv4__' ) as path :
117122 # GH 10833 (TimedeltaIndex formatting)
118123 dt = pd .Timedelta (seconds = 1 )
119124 df = pd .DataFrame ({'dt_data' : [i * dt for i in range (3 )]},
@@ -129,8 +134,10 @@ def test_to_csv_from_csv(self):
129134
130135 assert_frame_equal (df , result , check_index_type = True )
131136
137+ def test_to_csv_from_csv5 (self ):
138+
132139 # tz, 8260
133- with ensure_clean (pname ) as path :
140+ with ensure_clean ('__tmp_to_csv_from_csv5__' ) as path :
134141
135142 self .tzframe .to_csv (path )
136143 result = pd .read_csv (path , index_col = 0 , parse_dates = ['A' ])
@@ -212,26 +219,56 @@ def _check_df(df, cols=None):
212219 cols = ['b' , 'a' ]
213220 _check_df (df , cols )
214221
222+ @slow
223+ def test_to_csv_dtnat (self ):
224+ # GH3437
225+ from pandas import NaT
226+
227+ def make_dtnat_arr (n , nnat = None ):
228+ if nnat is None :
229+ nnat = int (n * 0.1 ) # 10%
230+ s = list (date_range ('2000' , freq = '5min' , periods = n ))
231+ if nnat :
232+ for i in np .random .randint (0 , len (s ), nnat ):
233+ s [i ] = NaT
234+ i = np .random .randint (100 )
235+ s [- i ] = NaT
236+ s [i ] = NaT
237+ return s
238+
239+ chunksize = 1000
240+ # N=35000
241+ s1 = make_dtnat_arr (chunksize + 5 )
242+ s2 = make_dtnat_arr (chunksize + 5 , 0 )
243+
244+ # s3=make_dtnjat_arr(chunksize+5,0)
245+ with ensure_clean ('1.csv' ) as pth :
246+ df = DataFrame (dict (a = s1 , b = s2 ))
247+ df .to_csv (pth , chunksize = chunksize )
248+ recons = DataFrame .from_csv (pth )._convert (datetime = True ,
249+ coerce = True )
250+ assert_frame_equal (df , recons , check_names = False ,
251+ check_less_precise = True )
252+
215253 @slow
216254 def test_to_csv_moar (self ):
217- path = '__tmp_to_csv_moar__'
218255
219- def _do_test (df , path , r_dtype = None , c_dtype = None ,
256+ def _do_test (df , r_dtype = None , c_dtype = None ,
220257 rnlvl = None , cnlvl = None , dupe_col = False ):
221258
222259 kwargs = dict (parse_dates = False )
223260 if cnlvl :
224261 if rnlvl is not None :
225262 kwargs ['index_col' ] = lrange (rnlvl )
226263 kwargs ['header' ] = lrange (cnlvl )
227- with ensure_clean (path ) as path :
264+ with ensure_clean ('__tmp_to_csv_moar__' ) as path :
228265 df .to_csv (path , encoding = 'utf8' ,
229266 chunksize = chunksize , tupleize_cols = False )
230267 recons = DataFrame .from_csv (
231268 path , tupleize_cols = False , ** kwargs )
232269 else :
233270 kwargs ['header' ] = 0
234- with ensure_clean (path ) as path :
271+ with ensure_clean ('__tmp_to_csv_moar__' ) as path :
235272 df .to_csv (path , encoding = 'utf8' , chunksize = chunksize )
236273 recons = DataFrame .from_csv (path , ** kwargs )
237274
@@ -307,50 +344,21 @@ def _to_uni(x):
307344 N = 100
308345 chunksize = 1000
309346
310- # GH3437
311- from pandas import NaT
312-
313- def make_dtnat_arr (n , nnat = None ):
314- if nnat is None :
315- nnat = int (n * 0.1 ) # 10%
316- s = list (date_range ('2000' , freq = '5min' , periods = n ))
317- if nnat :
318- for i in np .random .randint (0 , len (s ), nnat ):
319- s [i ] = NaT
320- i = np .random .randint (100 )
321- s [- i ] = NaT
322- s [i ] = NaT
323- return s
324-
325- # N=35000
326- s1 = make_dtnat_arr (chunksize + 5 )
327- s2 = make_dtnat_arr (chunksize + 5 , 0 )
328- path = '1.csv'
329-
330- # s3=make_dtnjat_arr(chunksize+5,0)
331- with ensure_clean ('.csv' ) as pth :
332- df = DataFrame (dict (a = s1 , b = s2 ))
333- df .to_csv (pth , chunksize = chunksize )
334- recons = DataFrame .from_csv (pth )._convert (datetime = True ,
335- coerce = True )
336- assert_frame_equal (df , recons , check_names = False ,
337- check_less_precise = True )
338-
339347 for ncols in [4 ]:
340348 base = int ((chunksize // ncols or 1 ) or 1 )
341349 for nrows in [2 , 10 , N - 1 , N , N + 1 , N + 2 , 2 * N - 2 ,
342350 2 * N - 1 , 2 * N , 2 * N + 1 , 2 * N + 2 ,
343351 base - 1 , base , base + 1 ]:
344352 _do_test (mkdf (nrows , ncols , r_idx_type = 'dt' ,
345- c_idx_type = 's' ), path , 'dt' , 's' )
353+ c_idx_type = 's' ), 'dt' , 's' )
346354
347355 for ncols in [4 ]:
348356 base = int ((chunksize // ncols or 1 ) or 1 )
349357 for nrows in [2 , 10 , N - 1 , N , N + 1 , N + 2 , 2 * N - 2 ,
350358 2 * N - 1 , 2 * N , 2 * N + 1 , 2 * N + 2 ,
351359 base - 1 , base , base + 1 ]:
352360 _do_test (mkdf (nrows , ncols , r_idx_type = 'dt' ,
353- c_idx_type = 's' ), path , 'dt' , 's' )
361+ c_idx_type = 's' ), 'dt' , 's' )
354362 pass
355363
356364 for r_idx_type , c_idx_type in [('i' , 'i' ), ('s' , 's' ), ('u' , 'dt' ),
@@ -362,14 +370,14 @@ def make_dtnat_arr(n, nnat=None):
362370 base - 1 , base , base + 1 ]:
363371 _do_test (mkdf (nrows , ncols , r_idx_type = r_idx_type ,
364372 c_idx_type = c_idx_type ),
365- path , r_idx_type , c_idx_type )
373+ r_idx_type , c_idx_type )
366374
367375 for ncols in [1 , 2 , 3 , 4 ]:
368376 base = int ((chunksize // ncols or 1 ) or 1 )
369377 for nrows in [10 , N - 2 , N - 1 , N , N + 1 , N + 2 , 2 * N - 2 ,
370378 2 * N - 1 , 2 * N , 2 * N + 1 , 2 * N + 2 ,
371379 base - 1 , base , base + 1 ]:
372- _do_test (mkdf (nrows , ncols ), path )
380+ _do_test (mkdf (nrows , ncols ))
373381
374382 for nrows in [10 , N - 2 , N - 1 , N , N + 1 , N + 2 ]:
375383 df = mkdf (nrows , 3 )
@@ -381,19 +389,19 @@ def make_dtnat_arr(n, nnat=None):
381389 ix [- 2 :] = ["rdupe" , "rdupe" ]
382390 df .index = ix
383391 df .columns = cols
384- _do_test (df , path , dupe_col = True )
392+ _do_test (df , dupe_col = True )
385393
386- _do_test (DataFrame (index = lrange (10 )), path )
387- _do_test (mkdf (chunksize // 2 + 1 , 2 , r_idx_nlevels = 2 ), path , rnlvl = 2 )
394+ _do_test (DataFrame (index = lrange (10 )))
395+ _do_test (mkdf (chunksize // 2 + 1 , 2 , r_idx_nlevels = 2 ), rnlvl = 2 )
388396 for ncols in [2 , 3 , 4 ]:
389397 base = int (chunksize // ncols )
390398 for nrows in [10 , N - 2 , N - 1 , N , N + 1 , N + 2 , 2 * N - 2 ,
391399 2 * N - 1 , 2 * N , 2 * N + 1 , 2 * N + 2 ,
392400 base - 1 , base , base + 1 ]:
393- _do_test (mkdf (nrows , ncols , r_idx_nlevels = 2 ), path , rnlvl = 2 )
394- _do_test (mkdf (nrows , ncols , c_idx_nlevels = 2 ), path , cnlvl = 2 )
401+ _do_test (mkdf (nrows , ncols , r_idx_nlevels = 2 ), rnlvl = 2 )
402+ _do_test (mkdf (nrows , ncols , c_idx_nlevels = 2 ), cnlvl = 2 )
395403 _do_test (mkdf (nrows , ncols , r_idx_nlevels = 2 , c_idx_nlevels = 2 ),
396- path , rnlvl = 2 , cnlvl = 2 )
404+ rnlvl = 2 , cnlvl = 2 )
397405
398406 def test_to_csv_from_csv_w_some_infs (self ):
399407
@@ -428,8 +436,7 @@ def test_to_csv_from_csv_w_all_infs(self):
428436
429437 def test_to_csv_no_index (self ):
430438 # GH 3624, after appending columns, to_csv fails
431- pname = '__tmp_to_csv_no_index__'
432- with ensure_clean (pname ) as path :
439+ with ensure_clean ('__tmp_to_csv_no_index__' ) as path :
433440 df = DataFrame ({'c1' : [1 , 2 , 3 ], 'c2' : [4 , 5 , 6 ]})
434441 df .to_csv (path , index = False )
435442 result = read_csv (path )
@@ -451,10 +458,9 @@ def test_to_csv_with_mix_columns(self):
451458 def test_to_csv_headers (self ):
452459 # GH6186, the presence or absence of `index` incorrectly
453460 # causes to_csv to have different header semantics.
454- pname = '__tmp_to_csv_headers__'
455461 from_df = DataFrame ([[1 , 2 ], [3 , 4 ]], columns = ['A' , 'B' ])
456462 to_df = DataFrame ([[1 , 2 ], [3 , 4 ]], columns = ['X' , 'Y' ])
457- with ensure_clean (pname ) as path :
463+ with ensure_clean ('__tmp_to_csv_headers__' ) as path :
458464 from_df .to_csv (path , header = ['X' , 'Y' ])
459465 recons = DataFrame .from_csv (path )
460466 assert_frame_equal (to_df , recons )
@@ -466,14 +472,13 @@ def test_to_csv_headers(self):
466472
467473 def test_to_csv_multiindex (self ):
468474
469- pname = '__tmp_to_csv_multiindex__'
470475 frame = self .frame
471476 old_index = frame .index
472477 arrays = np .arange (len (old_index ) * 2 ).reshape (2 , - 1 )
473478 new_index = MultiIndex .from_arrays (arrays , names = ['first' , 'second' ])
474479 frame .index = new_index
475480
476- with ensure_clean (pname ) as path :
481+ with ensure_clean ('__tmp_to_csv_multiindex__' ) as path :
477482
478483 frame .to_csv (path , header = False )
479484 frame .to_csv (path , columns = ['A' , 'B' ])
@@ -514,7 +519,7 @@ def test_to_csv_multiindex(self):
514519 # needed if setUP becomes classmethod
515520 self .tsframe .index = old_index
516521
517- with ensure_clean (pname ) as path :
522+ with ensure_clean ('__tmp_to_csv_multiindex__' ) as path :
518523 # GH3571, GH1651, GH3141
519524
520525 def _make_frame (names = None ):
@@ -618,7 +623,7 @@ def _make_frame(names=None):
618623 'MultiIndex' ):
619624 df .to_csv (path , tupleize_cols = False , columns = ['foo' , 'bar' ])
620625
621- with ensure_clean (pname ) as path :
626+ with ensure_clean ('__tmp_to_csv_multiindex__' ) as path :
622627 # empty
623628 tsframe [:0 ].to_csv (path )
624629 recons = DataFrame .from_csv (path )
@@ -1022,8 +1027,7 @@ def test_to_csv_compression_value_error(self):
10221027
10231028 def test_to_csv_date_format (self ):
10241029 from pandas import to_datetime
1025- pname = '__tmp_to_csv_date_format__'
1026- with ensure_clean (pname ) as path :
1030+ with ensure_clean ('__tmp_to_csv_date_format__' ) as path :
10271031 for engine in [None , 'python' ]:
10281032 w = FutureWarning if engine == 'python' else None
10291033
0 commit comments