title: Permission denied when pandas dataframe to tempfile csv
body: <p>I'm trying to store a pandas dataframe to a tempfile in csv format (in windows), but am being hit by:</p>
  
  <p>[Errno 13] Permission denied: 'C:\Users\Username\AppData\Local\Temp\tmpweymbkye'</p>
  
  <pre><code>import tempfile
  import pandas
  
  with tempfile.NamedTemporaryFile() as temp:
      df.to_csv(temp.name)
  </code></pre>
  
  <p>Where df is the dataframe. I've also tried changing the temp directory to one I am sure I have write permissions:</p>
  
  <pre><code>tempfile.tempdir='D:/Username/Temp/'
  </code></pre>
  
  <p>This gives me the same error message</p>
  
  <p>Edit:</p>
  
  <p>The tempfile appears to be locked for editing as when I change the loop to:</p>
  
  <pre><code>with tempfile.NamedTemporaryFile() as temp:
      df.to_csv(temp.name + '.csv')
  </code></pre>
  
  <p>I can write the file in the temp directory, but then it is not automatically deleted at the end of the loop, as it is no longer a temp file.</p>
  
  <p>However, if I change the code to:</p>
  
  <pre><code>with tempfile.NamedTemporaryFile(suffix='.csv') as temp:
      training_data.to_csv(temp.name)
  </code></pre>
  
  <p>I get the same error message as before. The file is not open anywhere else.</p>