@@ -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 )
@@ -1922,11 +1921,11 @@ def test_chunked_categorical(version):
1922
1921
df .index .name = "index"
1923
1922
with tm .ensure_clean () as path :
1924
1923
df .to_stata (path , version = version )
1925
- reader = StataReader (path , chunksize = 2 , order_categoricals = False )
1926
- for i , block in enumerate (reader ):
1927
- block = block .set_index ("index" )
1928
- assert "cats" in block
1929
- tm .assert_series_equal (block .cats , df .cats .iloc [2 * i : 2 * (i + 1 )])
1924
+ with StataReader (path , chunksize = 2 , order_categoricals = False ) as reader :
1925
+ for i , block in enumerate (reader ):
1926
+ block = block .set_index ("index" )
1927
+ assert "cats" in block
1928
+ tm .assert_series_equal (block .cats , df .cats .iloc [2 * i : 2 * (i + 1 )])
1930
1929
1931
1930
1932
1931
def test_chunked_categorical_partial (datapath ):
@@ -1952,7 +1951,8 @@ def test_chunked_categorical_partial(datapath):
1952
1951
def test_iterator_errors (datapath , chunksize ):
1953
1952
dta_file = datapath ("io" , "data" , "stata" , "stata-dta-partially-labeled.dta" )
1954
1953
with pytest .raises (ValueError , match = "chunksize must be a positive" ):
1955
- StataReader (dta_file , chunksize = chunksize )
1954
+ with StataReader (dta_file , chunksize = chunksize ):
1955
+ pass
1956
1956
1957
1957
1958
1958
def test_iterator_value_labels ():
@@ -2058,9 +2058,9 @@ def test_non_categorical_value_labels():
2058
2058
writer = StataWriter (path , data , value_labels = value_labels )
2059
2059
writer .write_file ()
2060
2060
2061
- reader = StataReader (path )
2062
- reader_value_labels = reader .value_labels ()
2063
- assert reader_value_labels == expected
2061
+ with StataReader (path ) as reader :
2062
+ reader_value_labels = reader .value_labels ()
2063
+ assert reader_value_labels == expected
2064
2064
2065
2065
msg = "Can't create value labels for notY, it wasn't found in the dataset."
2066
2066
with pytest .raises (KeyError , match = msg ):
@@ -2108,9 +2108,9 @@ def test_non_categorical_value_label_name_conversion():
2108
2108
with tm .assert_produces_warning (InvalidColumnName ):
2109
2109
data .to_stata (path , value_labels = value_labels )
2110
2110
2111
- reader = StataReader (path )
2112
- reader_value_labels = reader .value_labels ()
2113
- assert reader_value_labels == expected
2111
+ with StataReader (path ) as reader :
2112
+ reader_value_labels = reader .value_labels ()
2113
+ assert reader_value_labels == expected
2114
2114
2115
2115
2116
2116
def test_non_categorical_value_label_convert_categoricals_error ():
@@ -2129,8 +2129,8 @@ def test_non_categorical_value_label_convert_categoricals_error():
2129
2129
with tm .ensure_clean () as path :
2130
2130
data .to_stata (path , value_labels = value_labels )
2131
2131
2132
- reader = StataReader (path , convert_categoricals = False )
2133
- reader_value_labels = reader .value_labels ()
2132
+ with StataReader (path , convert_categoricals = False ) as reader :
2133
+ reader_value_labels = reader .value_labels ()
2134
2134
assert reader_value_labels == value_labels
2135
2135
2136
2136
col = "repeated_labels"
0 commit comments