@@ -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 ():
@@ -2056,9 +2056,9 @@ def test_non_categorical_value_labels():
2056
2056
writer = StataWriter (path , data , value_labels = value_labels )
2057
2057
writer .write_file ()
2058
2058
2059
- reader = StataReader (path )
2060
- reader_value_labels = reader .value_labels ()
2061
- assert reader_value_labels == expected
2059
+ with StataReader (path ) as reader :
2060
+ reader_value_labels = reader .value_labels ()
2061
+ assert reader_value_labels == expected
2062
2062
2063
2063
msg = "Can't create value labels for notY, it wasn't found in the dataset."
2064
2064
with pytest .raises (KeyError , match = msg ):
@@ -2106,9 +2106,9 @@ def test_non_categorical_value_label_name_conversion():
2106
2106
with tm .assert_produces_warning (InvalidColumnName ):
2107
2107
data .to_stata (path , value_labels = value_labels )
2108
2108
2109
- reader = StataReader (path )
2110
- reader_value_labels = reader .value_labels ()
2111
- assert reader_value_labels == expected
2109
+ with StataReader (path ) as reader :
2110
+ reader_value_labels = reader .value_labels ()
2111
+ assert reader_value_labels == expected
2112
2112
2113
2113
2114
2114
def test_non_categorical_value_label_convert_categoricals_error ():
@@ -2127,8 +2127,8 @@ def test_non_categorical_value_label_convert_categoricals_error():
2127
2127
with tm .ensure_clean () as path :
2128
2128
data .to_stata (path , value_labels = value_labels )
2129
2129
2130
- reader = StataReader (path , convert_categoricals = False )
2131
- reader_value_labels = reader .value_labels ()
2130
+ with StataReader (path , convert_categoricals = False ) as reader :
2131
+ reader_value_labels = reader .value_labels ()
2132
2132
assert reader_value_labels == value_labels
2133
2133
2134
2134
col = "repeated_labels"
0 commit comments