Repository: pandas-dev/pandas

Issue:
COMPAT: openpyxl deprecation warnings
other issues about warnings
xref #14519 
xref #14539 

tons of warnings on openpyxl 2.4.0 (this is under 3.5), but we have the latest on lots of travis builds.

so need to fix these based on the version, IOW. have a function that introspects the version then does a) if < 2.4.0 (or whatever version triggers these), else does the the new thing.

```
(pandas) bash-3.2$ nosetests pandas/io/tests/test_excel.py  -v
test_ExcelWriter_dispatch (pandas.io.tests.test_excel.ExcelWriterEngineTests) ... /Users/jreback/miniconda3/envs/pandas/lib/python3.5/site-packages/xlwt/ExcelFormulaLexer.py:53: DeprecationWarning: LOCALE flag with a str pattern is deprecated. Will be an error in 3.6
  VERBOSE+LOCALE+IGNORECASE)
ok
test_register_writer (pandas.io.tests.test_excel.ExcelWriterEngineTests) ... ok
SKIP: openpyxl 2.4.0 >= 2.2
test_basics_with_nan (pandas.io.tests.test_excel.Openpyxl22Tests) ... /Users/jreback/miniconda3/envs/pandas/lib/python3.5/site-packages/openpyxl/workbook/workbook.py:179: DeprecationWarning: Call to deprecated function or class remove_sheet (Use wb.remove(worksheet) or del wb[sheetname]).
  def remove_sheet(self, worksheet):
/Users/jreback/miniconda3/envs/pandas/lib/python3.5/site-packages/openpyxl/workbook/workbook.py:179: DeprecationWarning: Call to deprecated function or class remove_sheet (Use wb.remove(worksheet) or del wb[sheetname]).
  def remove_sheet(self, worksheet):
/Users/jreback/miniconda3/envs/pandas/lib/python3.5/site-packages/openpyxl/workbook/workbook.py:179: DeprecationWarning: Call to deprecated function or class remove_sheet (Use wb.remove(worksheet) or del wb[sheetname]).
  def remove_sheet(self, worksheet):
/Users/jreback/miniconda3/envs/pandas/lib/python3.5/site-packages/openpyxl/workbook/workbook.py:179: DeprecationWarning: Call to deprecated function or class remove_sheet (Use wb.remove(worksheet) or del wb[sheetname]).
  def remove_sheet(self, worksheet):
ok
test_bool_types (pandas.io.tests.test_excel.Openpyxl22Tests) ... /Users/jreback/miniconda3/envs/pandas/lib/python3.5/site-packages/openpyxl/workbook/workbook.py:179: DeprecationWarning: Call to deprecated function or class remove_sheet (Use wb.remove(worksheet) or del wb[sheetname]).
  def remove_sheet(self, worksheet):
/Users/jreback/miniconda3/envs/pandas/lib/python3.5/site-packages/openpyxl/workbook/workbook.py:179: DeprecationWarning: Call to deprecated function or class remove_sheet (Use wb.remove(worksheet) or del wb[sheetname]).
  def remove_sheet(self, worksheet):
ok
test_bytes_io (pandas.io.tests.test_excel.Openpyxl22Tests) ... /Users/jreback/miniconda3/envs/pandas/lib/python3.5/site-packages/openpyxl/workbook/workbook.py:179: DeprecationWarning: Call to deprecated function or class remove_sheet (Use wb.remove(worksheet) or del wb[sheetname]).
  def remove_sheet(self, worksheet):
ok
```


Discussion:

Resolving patch:
diff --git a/pandas/io/excel.py b/pandas/io/excel.py
--- a/pandas/io/excel.py
+++ b/pandas/io/excel.py
@@ -788,9 +788,15 @@ def __init__(self, path, engine=None, **engine_kwargs):
 
         # Create workbook object with default optimized_write=True.
         self.book = Workbook()
+
         # Openpyxl 1.6.1 adds a dummy sheet. We remove it.
         if self.book.worksheets:
-            self.book.remove_sheet(self.book.worksheets[0])
+            try:
+                self.book.remove(self.book.worksheets[0])
+            except AttributeError:
+
+                # compat
+                self.book.remove_sheet(self.book.worksheets[0])
 
     def save(self):
         """
@@ -911,7 +917,7 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0):
 
         for cell in cells:
             colletter = get_column_letter(startcol + cell.col + 1)
-            xcell = wks.cell("%s%s" % (colletter, startrow + cell.row + 1))
+            xcell = wks["%s%s" % (colletter, startrow + cell.row + 1)]
             xcell.value = _conv_value(cell.val)
             style_kwargs = {}
 
@@ -952,7 +958,7 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0):
                                 # Ignore first cell. It is already handled.
                                 continue
                             colletter = get_column_letter(col)
-                            xcell = wks.cell("%s%s" % (colletter, row))
+                            xcell = wks["%s%s" % (colletter, row)]
                             xcell.style = xcell.style.copy(**style_kwargs)
 
     @classmethod


Test patch:
diff --git a/pandas/io/tests/test_excel.py b/pandas/io/tests/test_excel.py
--- a/pandas/io/tests/test_excel.py
+++ b/pandas/io/tests/test_excel.py
@@ -2006,8 +2006,8 @@ def test_write_cells_merge_styled(self):
             writer.write_cells(merge_cells, sheet_name=sheet_name)
 
             wks = writer.sheets[sheet_name]
-            xcell_b1 = wks.cell('B1')
-            xcell_a2 = wks.cell('A2')
+            xcell_b1 = wks['B1']
+            xcell_a2 = wks['A2']
             self.assertEqual(xcell_b1.style, openpyxl_sty_merged)
             self.assertEqual(xcell_a2.style, openpyxl_sty_merged)
 
@@ -2118,8 +2118,8 @@ def test_write_cells_merge_styled(self):
             writer.write_cells(merge_cells, sheet_name=sheet_name)
 
             wks = writer.sheets[sheet_name]
-            xcell_b1 = wks.cell('B1')
-            xcell_a2 = wks.cell('A2')
+            xcell_b1 = wks['B1']
+            xcell_a2 = wks['A2']
             self.assertEqual(xcell_b1.font, openpyxl_sty_merged)
             self.assertEqual(xcell_a2.font, openpyxl_sty_merged)
 
@@ -2213,11 +2213,18 @@ def test_column_format(self):
             writer.save()
 
             read_workbook = openpyxl.load_workbook(path)
-            read_worksheet = read_workbook.get_sheet_by_name(name='Sheet1')
+            try:
+                read_worksheet = read_workbook['Sheet1']
+            except TypeError:
+                # compat
+                read_worksheet = read_workbook.get_sheet_by_name(name='Sheet1')
 
-            # Get the number format from the cell. This method is backward
-            # compatible with older versions of openpyxl.
-            cell = read_worksheet.cell('B2')
+            # Get the number format from the cell.
+            try:
+                cell = read_worksheet['B2']
+            except TypeError:
+                # compat
+                cell = read_worksheet.cell('B2')
 
             try:
                 read_num_format = cell.number_format