@@ -1881,18 +1881,19 @@ def test_chunked_categorical(version):
1881
1881
1882
1882
def test_chunked_categorical_partial (dirpath ):
1883
1883
dta_file = os .path .join (dirpath , "stata-dta-partially-labeled.dta" )
1884
- reader = StataReader (dta_file , chunksize = 2 )
1885
1884
values = ["a" , "b" , "a" , "b" , 3.0 ]
1886
- with pytest .warns (CategoricalConversionWarning , match = "One or more series" ):
1887
- for i , block in enumerate (reader ):
1888
- assert list (block .cats ) == values [2 * i : 2 * (i + 1 )]
1889
- if i < 2 :
1890
- idx = pd .Index (["a" , "b" ])
1891
- else :
1892
- idx = pd .Float64Index ([3.0 ])
1893
- tm .assert_index_equal (block .cats .cat .categories , idx )
1894
- reader = StataReader (dta_file , chunksize = 5 )
1895
- large_chunk = reader .__next__ ()
1885
+ with StataReader (dta_file , chunksize = 2 ) as reader :
1886
+ with tm .assert_produces_warning (CategoricalConversionWarning ):
1887
+ for i , block in enumerate (reader ):
1888
+ assert list (block .cats ) == values [2 * i : 2 * (i + 1 )]
1889
+ if i < 2 :
1890
+ idx = pd .Index (["a" , "b" ])
1891
+ else :
1892
+ idx = pd .Float64Index ([3.0 ])
1893
+ tm .assert_index_equal (block .cats .cat .categories , idx )
1894
+ with tm .assert_produces_warning (CategoricalConversionWarning ):
1895
+ with StataReader (dta_file , chunksize = 5 ) as reader :
1896
+ large_chunk = reader .__next__ ()
1896
1897
direct = read_stata (dta_file )
1897
1898
tm .assert_frame_equal (direct , large_chunk )
1898
1899
@@ -1906,4 +1907,5 @@ def test_iterator_errors(dirpath):
1906
1907
with pytest .raises (ValueError , match = "chunksize must be a positive" ):
1907
1908
StataReader (dta_file , chunksize = "apple" )
1908
1909
with pytest .raises (ValueError , match = "chunksize must be set to a positive" ):
1909
- StataReader (dta_file ).__next__ ()
1910
+ with StataReader (dta_file ) as reader :
1911
+ reader .__next__ ()
0 commit comments