@@ -1024,28 +1024,29 @@ def test_read_chunks_117(self):
10241024 check_datetimelike_compat = True )
10251025
10261026 pos += chunksize
1027+ itr .close ()
10271028
10281029 def test_iterator (self ):
10291030
10301031 fname = self .dta3_117
10311032
10321033 parsed = read_stata (fname )
10331034
1034- itr = read_stata (fname , iterator = True )
1035- chunk = itr .read (5 )
1036- tm .assert_frame_equal (parsed .iloc [0 :5 , :], chunk )
1035+ with read_stata (fname , iterator = True ) as itr :
1036+ chunk = itr .read (5 )
1037+ tm .assert_frame_equal (parsed .iloc [0 :5 , :], chunk )
10371038
1038- itr = read_stata (fname , chunksize = 5 )
1039- chunk = list (itr )
1040- tm .assert_frame_equal (parsed .iloc [0 :5 , :], chunk [0 ])
1039+ with read_stata (fname , chunksize = 5 ) as itr :
1040+ chunk = list (itr )
1041+ tm .assert_frame_equal (parsed .iloc [0 :5 , :], chunk [0 ])
10411042
1042- itr = read_stata (fname , iterator = True )
1043- chunk = itr .get_chunk (5 )
1044- tm .assert_frame_equal (parsed .iloc [0 :5 , :], chunk )
1043+ with read_stata (fname , iterator = True ) as itr :
1044+ chunk = itr .get_chunk (5 )
1045+ tm .assert_frame_equal (parsed .iloc [0 :5 , :], chunk )
10451046
1046- itr = read_stata (fname , chunksize = 5 )
1047- chunk = itr .get_chunk ()
1048- tm .assert_frame_equal (parsed .iloc [0 :5 , :], chunk )
1047+ with read_stata (fname , chunksize = 5 ) as itr :
1048+ chunk = itr .get_chunk ()
1049+ tm .assert_frame_equal (parsed .iloc [0 :5 , :], chunk )
10491050
10501051 # GH12153
10511052 from_chunks = pd .concat (read_stata (fname , chunksize = 4 ))
@@ -1089,22 +1090,23 @@ def test_read_chunks_115(self):
10891090 check_datetimelike_compat = True )
10901091
10911092 pos += chunksize
1093+ itr .close ()
10921094
10931095 def test_read_chunks_columns (self ):
10941096 fname = self .dta3_117
10951097 columns = ['quarter' , 'cpi' , 'm1' ]
10961098 chunksize = 2
10971099
10981100 parsed = read_stata (fname , columns = columns )
1099- itr = read_stata (fname , iterator = True )
1100- pos = 0
1101- for j in range (5 ):
1102- chunk = itr .read (chunksize , columns = columns )
1103- if chunk is None :
1104- break
1105- from_frame = parsed .iloc [pos :pos + chunksize , :]
1106- tm .assert_frame_equal (from_frame , chunk , check_dtype = False )
1107- pos += chunksize
1101+ with read_stata (fname , iterator = True ) as itr :
1102+ pos = 0
1103+ for j in range (5 ):
1104+ chunk = itr .read (chunksize , columns = columns )
1105+ if chunk is None :
1106+ break
1107+ from_frame = parsed .iloc [pos :pos + chunksize , :]
1108+ tm .assert_frame_equal (from_frame , chunk , check_dtype = False )
1109+ pos += chunksize
11081110
11091111
11101112if __name__ == '__main__' :
0 commit comments