@@ -1862,6 +1862,62 @@ def test_invalid_columns(self):
1862
1862
with pytest .raises (KeyError ):
1863
1863
write_frame .to_excel (path , 'test1' , columns = ['C' , 'D' ])
1864
1864
1865
+ def test_comment_arg (self ):
1866
+ # Test the comment argument functionality to read_excel
1867
+ with ensure_clean (self .ext ) as path :
1868
+
1869
+ # Create file to read in
1870
+ write_frame = DataFrame ({'A' : ['one' , '#one' , 'one' ],
1871
+ 'B' : ['two' , 'two' , '#two' ]})
1872
+ write_frame .to_excel (path , 'test_c' )
1873
+
1874
+ # Read file without comment arg
1875
+ read_frame = read_excel (path , 'test_c' )
1876
+ read_frame_commented = read_excel (path , 'test_c' , comment = '#' )
1877
+ tm .assert_class_equal (read_frame , read_frame_commented )
1878
+
1879
+ def test_comment_default (self ):
1880
+ # Test the comment argument default to read_excel
1881
+ with ensure_clean (self .ext ) as path :
1882
+
1883
+ # Create file to read in
1884
+ write_frame = DataFrame ({'A' : ['one' , '#one' , 'one' ],
1885
+ 'B' : ['two' , 'two' , '#two' ]})
1886
+ write_frame .to_excel (path , 'test_c' )
1887
+
1888
+ # Read file with default and explicit comment=None
1889
+ read_frame = read_excel (path , 'test_c' )
1890
+ read_frame_uncommented = read_excel (path , 'test_c' , comment = None )
1891
+ tm .assert_frame_equal (read_frame , read_frame_uncommented )
1892
+
1893
+ def test_comment_used (self ):
1894
+ # Test the comment argument is working as expected when used
1895
+ with ensure_clean (self .ext ) as path :
1896
+
1897
+ # Create file to read in
1898
+ write_frame = DataFrame ({'A' : ['one' , '#one' , 'one' ],
1899
+ 'B' : ['two' , 'two' , '#two' ]})
1900
+ write_frame .to_excel (path , 'test_c' )
1901
+
1902
+ # Test read_frame_comment against manually produced expected output
1903
+ read_frame_commented = read_excel (path , 'test_c' , comment = '#' )
1904
+ expected = read_excel (path , 'test_c' )
1905
+ expected .iloc [1 , 0 ] = None
1906
+ expected .iloc [1 , 1 ] = None
1907
+ expected .iloc [2 , 1 ] = None
1908
+ tm .assert_frame_equal (read_frame_commented , expected )
1909
+
1910
+ def test_comment_emptyline (self ):
1911
+ # Test that read_excel ignores commented lines at the end of file
1912
+ with ensure_clean (self .ext ) as path :
1913
+
1914
+ write_frame = DataFrame ({'a' : ['1' , '#2' ], 'b' : ['2' , '3' ]})
1915
+ write_frame .to_excel (path , index = False )
1916
+
1917
+ # Test that all-comment lines at EoF are ignored
1918
+ read_frame_short = read_excel (path , comment = '#' )
1919
+ assert (read_frame_short .shape == write_frame .iloc [0 :1 , :].shape )
1920
+
1865
1921
def test_datetimes (self ):
1866
1922
1867
1923
# Test writing and reading datetimes. For issue #9139. (xref #9185)
0 commit comments