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