@@ -600,9 +600,8 @@ def test_value_labels_old_format(self, datapath):
600
600
# Test that value_labels() returns an empty dict if the file format
601
601
# predates supporting value labels.
602
602
dpath = datapath ("io" , "data" , "stata" , "S4_EDUC1.dta" )
603
- reader = StataReader (dpath )
604
- assert reader .value_labels () == {}
605
- reader .close ()
603
+ with StataReader (dpath ) as reader :
604
+ assert reader .value_labels () == {}
606
605
607
606
def test_date_export_formats (self ):
608
607
columns = ["tc" , "td" , "tw" , "tm" , "tq" , "th" , "ty" ]
@@ -1820,9 +1819,9 @@ def test_utf8_writer(self, version):
1820
1819
data ["β" ].replace (value_labels ["β" ]).astype ("category" ).cat .as_ordered ()
1821
1820
)
1822
1821
tm .assert_frame_equal (data , reread_encoded )
1823
- reader = StataReader (path )
1824
- assert reader .data_label == data_label
1825
- assert reader .variable_labels () == variable_labels
1822
+ with StataReader (path ) as reader :
1823
+ assert reader .data_label == data_label
1824
+ assert reader .variable_labels () == variable_labels
1826
1825
1827
1826
data .to_stata (path , version = version , write_index = False )
1828
1827
reread_to_stata = read_stata (path )
@@ -1945,11 +1944,11 @@ def test_chunked_categorical(version):
1945
1944
df .index .name = "index"
1946
1945
with tm .ensure_clean () as path :
1947
1946
df .to_stata (path , version = version )
1948
- reader = StataReader (path , chunksize = 2 , order_categoricals = False )
1949
- for i , block in enumerate (reader ):
1950
- block = block .set_index ("index" )
1951
- assert "cats" in block
1952
- tm .assert_series_equal (block .cats , df .cats .iloc [2 * i : 2 * (i + 1 )])
1947
+ with StataReader (path , chunksize = 2 , order_categoricals = False ) as reader :
1948
+ for i , block in enumerate (reader ):
1949
+ block = block .set_index ("index" )
1950
+ assert "cats" in block
1951
+ tm .assert_series_equal (block .cats , df .cats .iloc [2 * i : 2 * (i + 1 )])
1953
1952
1954
1953
1955
1954
def test_chunked_categorical_partial (datapath ):
@@ -1975,7 +1974,8 @@ def test_chunked_categorical_partial(datapath):
1975
1974
def test_iterator_errors (datapath , chunksize ):
1976
1975
dta_file = datapath ("io" , "data" , "stata" , "stata-dta-partially-labeled.dta" )
1977
1976
with pytest .raises (ValueError , match = "chunksize must be a positive" ):
1978
- StataReader (dta_file , chunksize = chunksize )
1977
+ with StataReader (dta_file , chunksize = chunksize ):
1978
+ pass
1979
1979
1980
1980
1981
1981
def test_iterator_value_labels ():
@@ -2077,9 +2077,9 @@ def test_non_categorical_value_labels():
2077
2077
writer = StataWriter (path , data , value_labels = value_labels )
2078
2078
writer .write_file ()
2079
2079
2080
- reader = StataReader (path )
2081
- reader_value_labels = reader .value_labels ()
2082
- assert reader_value_labels == expected
2080
+ with StataReader (path ) as reader :
2081
+ reader_value_labels = reader .value_labels ()
2082
+ assert reader_value_labels == expected
2083
2083
2084
2084
msg = "Can't create value labels for notY, it wasn't found in the dataset."
2085
2085
with pytest .raises (KeyError , match = msg ):
@@ -2127,9 +2127,9 @@ def test_non_categorical_value_label_name_conversion():
2127
2127
with tm .assert_produces_warning (InvalidColumnName ):
2128
2128
data .to_stata (path , value_labels = value_labels )
2129
2129
2130
- reader = StataReader (path )
2131
- reader_value_labels = reader .value_labels ()
2132
- assert reader_value_labels == expected
2130
+ with StataReader (path ) as reader :
2131
+ reader_value_labels = reader .value_labels ()
2132
+ assert reader_value_labels == expected
2133
2133
2134
2134
2135
2135
def test_non_categorical_value_label_convert_categoricals_error ():
@@ -2148,8 +2148,8 @@ def test_non_categorical_value_label_convert_categoricals_error():
2148
2148
with tm .ensure_clean () as path :
2149
2149
data .to_stata (path , value_labels = value_labels )
2150
2150
2151
- reader = StataReader (path , convert_categoricals = False )
2152
- reader_value_labels = reader .value_labels ()
2151
+ with StataReader (path , convert_categoricals = False ) as reader :
2152
+ reader_value_labels = reader .value_labels ()
2153
2153
assert reader_value_labels == value_labels
2154
2154
2155
2155
col = "repeated_labels"
0 commit comments