88 BytesIO ,
99 StringIO ,
1010)
11- from pathlib import Path
1211
1312import numpy as np
1413import pytest
@@ -642,7 +641,7 @@ def test_default_delimiter():
642641
643642
644643@pytest .mark .parametrize ("infer" , [True , False ])
645- def test_fwf_compression (compression_only , infer , compression_to_extension ):
644+ def test_fwf_compression (compression_only , infer , compression_to_extension , temp_file ):
646645 data = """1111111111
647646 2222222222
648647 3333333333""" .strip ()
@@ -655,17 +654,17 @@ def test_fwf_compression(compression_only, infer, compression_to_extension):
655654
656655 data = bytes (data , encoding = "utf-8" )
657656
658- with tm . ensure_clean ( filename = "tmp." + extension ) as path :
659- tm .write_to_compressed (compression , path , data )
657+ path = temp_file . parent / f"tmp. { extension } "
658+ tm .write_to_compressed (compression , path , data )
660659
661- if infer is not None :
662- kwargs ["compression" ] = "infer" if infer else compression
660+ if infer is not None :
661+ kwargs ["compression" ] = "infer" if infer else compression
663662
664- result = read_fwf (path , ** kwargs )
665- tm .assert_frame_equal (result , expected )
663+ result = read_fwf (path , ** kwargs )
664+ tm .assert_frame_equal (result , expected )
666665
667666
668- def test_binary_mode ():
667+ def test_binary_mode (temp_file ):
669668 """
670669 read_fwf supports opening files in binary mode.
671670
@@ -676,31 +675,31 @@ def test_binary_mode():
676675 df_reference = DataFrame (
677676 [["bba" , "bab" , "b a" ]], columns = ["aaa" , "aaa.1" , "aaa.2" ], index = [0 ]
678677 )
679- with tm . ensure_clean () as path :
680- Path ( path ) .write_text (data , encoding = "utf-8" )
681- with open (path , "rb" ) as file :
682- df = read_fwf (file )
683- file .seek (0 )
684- tm .assert_frame_equal (df , df_reference )
678+ path = temp_file
679+ path .write_text (data , encoding = "utf-8" )
680+ with open (path , "rb" ) as file :
681+ df = read_fwf (file )
682+ file .seek (0 )
683+ tm .assert_frame_equal (df , df_reference )
685684
686685
687686@pytest .mark .parametrize ("memory_map" , [True , False ])
688- def test_encoding_mmap (memory_map ):
687+ def test_encoding_mmap (memory_map , temp_file ):
689688 """
690689 encoding should be working, even when using a memory-mapped file.
691690
692691 GH 23254.
693692 """
694693 encoding = "iso8859_1"
695- with tm . ensure_clean () as path :
696- Path ( path ) .write_bytes (" 1 A Ä 2\n " .encode (encoding ))
697- df = read_fwf (
698- path ,
699- header = None ,
700- widths = [2 , 2 , 2 , 2 ],
701- encoding = encoding ,
702- memory_map = memory_map ,
703- )
694+ path = temp_file
695+ path .write_bytes (" 1 A Ä 2\n " .encode (encoding ))
696+ df = read_fwf (
697+ path ,
698+ header = None ,
699+ widths = [2 , 2 , 2 , 2 ],
700+ encoding = encoding ,
701+ memory_map = memory_map ,
702+ )
704703 df_reference = DataFrame ([[1 , "A" , "Ä" , 2 ]])
705704 tm .assert_frame_equal (df , df_reference )
706705
0 commit comments