8
8
BytesIO ,
9
9
StringIO ,
10
10
)
11
- from pathlib import Path
12
11
13
12
import numpy as np
14
13
import pytest
@@ -642,7 +641,7 @@ def test_default_delimiter():
642
641
643
642
644
643
@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 ):
646
645
data = """1111111111
647
646
2222222222
648
647
3333333333""" .strip ()
@@ -655,17 +654,17 @@ def test_fwf_compression(compression_only, infer, compression_to_extension):
655
654
656
655
data = bytes (data , encoding = "utf-8" )
657
656
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 )
660
659
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
663
662
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 )
666
665
667
666
668
- def test_binary_mode ():
667
+ def test_binary_mode (temp_file ):
669
668
"""
670
669
read_fwf supports opening files in binary mode.
671
670
@@ -676,31 +675,31 @@ def test_binary_mode():
676
675
df_reference = DataFrame (
677
676
[["bba" , "bab" , "b a" ]], columns = ["aaa" , "aaa.1" , "aaa.2" ], index = [0 ]
678
677
)
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 )
685
684
686
685
687
686
@pytest .mark .parametrize ("memory_map" , [True , False ])
688
- def test_encoding_mmap (memory_map ):
687
+ def test_encoding_mmap (memory_map , temp_file ):
689
688
"""
690
689
encoding should be working, even when using a memory-mapped file.
691
690
692
691
GH 23254.
693
692
"""
694
693
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
+ )
704
703
df_reference = DataFrame ([[1 , "A" , "Ä" , 2 ]])
705
704
tm .assert_frame_equal (df , df_reference )
706
705
0 commit comments