@@ -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 ():
@@ -2051,9 +2051,9 @@ def test_non_categorical_value_labels():
2051
2051
writer = StataWriter (path , data , value_labels = value_labels )
2052
2052
writer .write_file ()
2053
2053
2054
- reader = StataReader (path )
2055
- reader_value_labels = reader .value_labels ()
2056
- assert reader_value_labels == expected
2054
+ with StataReader (path ) as reader :
2055
+ reader_value_labels = reader .value_labels ()
2056
+ assert reader_value_labels == expected
2057
2057
2058
2058
msg = "Can't create value labels for notY, it wasn't found in the dataset."
2059
2059
with pytest .raises (KeyError , match = msg ):
@@ -2101,9 +2101,9 @@ def test_non_categorical_value_label_name_conversion():
2101
2101
with tm .assert_produces_warning (InvalidColumnName ):
2102
2102
data .to_stata (path , value_labels = value_labels )
2103
2103
2104
- reader = StataReader (path )
2105
- reader_value_labels = reader .value_labels ()
2106
- assert reader_value_labels == expected
2104
+ with StataReader (path ) as reader :
2105
+ reader_value_labels = reader .value_labels ()
2106
+ assert reader_value_labels == expected
2107
2107
2108
2108
2109
2109
def test_non_categorical_value_label_convert_categoricals_error ():
@@ -2122,8 +2122,8 @@ def test_non_categorical_value_label_convert_categoricals_error():
2122
2122
with tm .ensure_clean () as path :
2123
2123
data .to_stata (path , value_labels = value_labels )
2124
2124
2125
- reader = StataReader (path , convert_categoricals = False )
2126
- reader_value_labels = reader .value_labels ()
2125
+ with StataReader (path , convert_categoricals = False ) as reader :
2126
+ reader_value_labels = reader .value_labels ()
2127
2127
assert reader_value_labels == value_labels
2128
2128
2129
2129
col = "repeated_labels"
0 commit comments