@@ -248,6 +248,7 @@ def _return_result_expected(
248
248
self ,
249
249
df ,
250
250
chunksize ,
251
+ temp_file ,
251
252
r_dtype = None ,
252
253
c_dtype = None ,
253
254
rnlvl = None ,
@@ -260,15 +261,16 @@ def _return_result_expected(
260
261
kwargs ["index_col" ] = list (range (rnlvl ))
261
262
kwargs ["header" ] = list (range (cnlvl ))
262
263
263
- with tm .ensure_clean ("__tmp_to_csv_moar__" ) as path :
264
- df .to_csv (path , encoding = "utf8" , chunksize = chunksize )
265
- recons = self .read_csv (path , ** kwargs )
264
+
265
+ path = str (temp_file )
266
+ df .to_csv (path , encoding = "utf8" , chunksize = chunksize )
267
+ recons = self .read_csv (path , ** kwargs )
266
268
else :
267
269
kwargs ["header" ] = 0
268
270
269
- with tm . ensure_clean ( "__tmp_to_csv_moar__" ) as path :
270
- df .to_csv (path , encoding = "utf8" , chunksize = chunksize )
271
- recons = self .read_csv (path , ** kwargs )
271
+ path = str ( temp_file )
272
+ df .to_csv (path , encoding = "utf8" , chunksize = chunksize )
273
+ recons = self .read_csv (path , ** kwargs )
272
274
273
275
def _to_uni (x ):
274
276
if not isinstance (x , str ):
@@ -353,13 +355,13 @@ def _to_uni(x):
353
355
@pytest .mark .parametrize (
354
356
"nrows" , [2 , 10 , 99 , 100 , 101 , 102 , 198 , 199 , 200 , 201 , 202 , 249 , 250 , 251 ]
355
357
)
356
- def test_to_csv_nrows (self , nrows ):
358
+ def test_to_csv_nrows (self , nrows , temp_file ):
357
359
df = DataFrame (
358
360
np .ones ((nrows , 4 )),
359
361
index = date_range ("2020-01-01" , periods = nrows ),
360
362
columns = Index (list ("abcd" ), dtype = object ),
361
363
)
362
- result , expected = self ._return_result_expected (df , 1000 , "dt" , "s" )
364
+ result , expected = self ._return_result_expected (df , 1000 , temp_file , "dt" , "s" )
363
365
expected .index = expected .index .astype ("M8[ns]" )
364
366
tm .assert_frame_equal (result , expected , check_names = False )
365
367
@@ -372,7 +374,7 @@ def test_to_csv_nrows(self, nrows):
372
374
)
373
375
@pytest .mark .parametrize ("ncols" , [1 , 2 , 3 , 4 ])
374
376
@pytest .mark .filterwarnings (r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning" )
375
- def test_to_csv_idx_types (self , nrows , r_idx_type , c_idx_type , ncols ):
377
+ def test_to_csv_idx_types (self , nrows , r_idx_type , c_idx_type , ncols , temp_file ):
376
378
axes = {
377
379
"i" : lambda n : Index (np .arange (n ), dtype = np .int64 ),
378
380
"s" : lambda n : Index ([f"{ i } _{ chr (i )} " for i in range (97 , 97 + n )]),
@@ -387,6 +389,7 @@ def test_to_csv_idx_types(self, nrows, r_idx_type, c_idx_type, ncols):
387
389
result , expected = self ._return_result_expected (
388
390
df ,
389
391
1000 ,
392
+ temp_file ,
390
393
r_idx_type ,
391
394
c_idx_type ,
392
395
)
@@ -401,13 +404,13 @@ def test_to_csv_idx_types(self, nrows, r_idx_type, c_idx_type, ncols):
401
404
"nrows" , [10 , 98 , 99 , 100 , 101 , 102 , 198 , 199 , 200 , 201 , 202 , 249 , 250 , 251 ]
402
405
)
403
406
@pytest .mark .parametrize ("ncols" , [1 , 2 , 3 , 4 ])
404
- def test_to_csv_idx_ncols (self , nrows , ncols ):
407
+ def test_to_csv_idx_ncols (self , nrows , ncols , temp_file ):
405
408
df = DataFrame (
406
409
np .ones ((nrows , ncols )),
407
410
index = Index ([f"i-{ i } " for i in range (nrows )], name = "a" ),
408
411
columns = Index ([f"i-{ i } " for i in range (ncols )], name = "a" ),
409
412
)
410
- result , expected = self ._return_result_expected (df , 1000 )
413
+ result , expected = self ._return_result_expected (df , 1000 , temp_file )
411
414
tm .assert_frame_equal (result , expected , check_names = False )
412
415
413
416
@pytest .mark .slow
@@ -427,25 +430,25 @@ def test_to_csv_dup_cols(self, nrows):
427
430
ix [- 2 :] = ["rdupe" , "rdupe" ]
428
431
df .index = ix
429
432
df .columns = cols
430
- result , expected = self ._return_result_expected (df , 1000 , dupe_col = True )
433
+ result , expected = self ._return_result_expected (df , 1000 , temp_file , dupe_col = True )
431
434
tm .assert_frame_equal (result , expected , check_names = False )
432
435
433
436
@pytest .mark .slow
434
- def test_to_csv_empty (self ):
437
+ def test_to_csv_empty (self , temp_file ):
435
438
df = DataFrame (index = np .arange (10 , dtype = np .int64 ))
436
- result , expected = self ._return_result_expected (df , 1000 )
439
+ result , expected = self ._return_result_expected (df , 1000 , temp_file )
437
440
tm .assert_frame_equal (result , expected , check_column_type = False )
438
441
439
442
@pytest .mark .slow
440
- def test_to_csv_chunksize (self ):
443
+ def test_to_csv_chunksize (self , temp_file ):
441
444
chunksize = 1000
442
445
rows = chunksize // 2 + 1
443
446
df = DataFrame (
444
447
np .ones ((rows , 2 )),
445
448
columns = Index (list ("ab" )),
446
449
index = MultiIndex .from_arrays ([range (rows ) for _ in range (2 )]),
447
450
)
448
- result , expected = self ._return_result_expected (df , chunksize , rnlvl = 2 )
451
+ result , expected = self ._return_result_expected (df , chunksize , temp_file , rnlvl = 2 )
449
452
tm .assert_frame_equal (result , expected , check_names = False )
450
453
451
454
@pytest .mark .slow
@@ -461,7 +464,7 @@ def test_to_csv_chunksize(self):
461
464
[{"r_idx_nlevels" : 2 , "c_idx_nlevels" : 2 }, {"rnlvl" : 2 , "cnlvl" : 2 }],
462
465
],
463
466
)
464
- def test_to_csv_params (self , nrows , df_params , func_params , ncols ):
467
+ def test_to_csv_params (self , nrows , df_params , func_params , ncols , temp_file ):
465
468
if df_params .get ("r_idx_nlevels" ):
466
469
index = MultiIndex .from_arrays (
467
470
[f"i-{ i } " for i in range (nrows )]
@@ -478,7 +481,7 @@ def test_to_csv_params(self, nrows, df_params, func_params, ncols):
478
481
else :
479
482
columns = Index ([f"i-{ i } " for i in range (ncols )])
480
483
df = DataFrame (np .ones ((nrows , ncols )), index = index , columns = columns )
481
- result , expected = self ._return_result_expected (df , 1000 , ** func_params )
484
+ result , expected = self ._return_result_expected (df , 1000 , temp_file , ** func_params )
482
485
tm .assert_frame_equal (result , expected , check_names = False )
483
486
484
487
def test_to_csv_from_csv_w_some_infs (self , temp_file , float_frame ):
0 commit comments