title: How to fix "TypeError: cannot serialize '_io.BufferedReader' object" when writing pandas dataframe to_pickle?
body: <p>I have a simple code where I am reading an excel .xlsx file as a dataframe and then writing it back as a pickle file using <em>to_pickle</em>. I have been using the same code for months to read and write as new excel files arrive. However, this time when I tried my code it is giving me <strong>TypeError: cannot serialize '_io.BufferedReader' object</strong> error for some reason. Here is the code, </p>
  
  <pre><code># Path to .xlsx
  MasterItem = MonthlyFolder + "MasterItem__Nov2019.xlsx"
  
  # Function to read the excel file
  def ReadExcel(filename, sheetname=None, header=0):
      from openpyxl import load_workbook
  
      wb = load_workbook(filename, read_only=True)
  
      if sheetname is None:  # If sheetname is not provided then grab the first sheet
          print("\t Reading " + wb.sheetnames[0])
          ws = wb[wb.sheetnames[0]]
      else:
          print("\t Reading " + sheetname)
          ws = wb[sheetname]
  
      data = ws.values
  
      if header is None:
          columns = None
      elif header &gt; 0:
          # Skip non header rows
          for i in range(0, header):
              next(data)
          # Save header row
          columns = next(data)[0:]
      else:
          columns = next(data)[0:]
  
      # Create a DataFrame based on the subsequent lines of data
      df_Out = pd.DataFrame(data, columns=columns)
  
      return df_Out
  
  # Reading .xlsx and writing as pickle
  RawMasterItem = ReadExcel(MasterItem)
  pd.to_pickle(RawMasterItem, MonthlyFolder+"RawMasterItem.pkl") # This fails to run
  </code></pre>
  
  <p>Following is the output and error I am getting, </p>
  
  <pre><code>    ../Data/2019Nov/MasterItem__Nov2019.xlsx
           Reading Sheet1
      Traceback (most recent call last):
        File "C:\Users\Eulhaq\AppData\Local\conda\conda\envs\DataScience\lib\site-packages\IPython\core\interactiveshell.py", line 3326, in run_code
          exec(code_obj, self.user_global_ns, self.user_ns)
        File "&lt;ipython-input-10-07041bb51f98&gt;", line 3, in &lt;module&gt;
          pd.to_pickle(RawMasterItem, MonthlyFolder+"RawMasterItem.pkl")
        File "C:\Users\Eulhaq\AppData\Local\conda\conda\envs\DataScience\lib\site-packages\pandas\io\pickle.py", line 76, in to_pickle
          f.write(pickle.dumps(obj, protocol=protocol))
      TypeError: cannot serialize '_io.BufferedReader' object
  </code></pre>