0

I am baffled as to why this code does not run. A simple example:

import pandas
df = pandas.DataFrame({"var1" : [5], "var2" : [2]})
df.to_csv("csv_file.csv")

this code give the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/pandas/core/frame.py", line 1136, in to_csv
    formatter.save()
  File "/usr/lib/python3/dist-packages/pandas/core/format.py", line 1171, in save
    self._save()
  File "/usr/lib/python3/dist-packages/pandas/core/format.py", line 1277, in _save
    self._save_chunk(start_i, end_i)
  File "/usr/lib/python3/dist-packages/pandas/core/format.py", line 1289, in _save_chunk
    date_format=self.date_format)
  File "/usr/lib/python3/dist-packages/pandas/core/internals.py", line 552, in to_native_types
    values = values[:, slicer]
IndexError: invalid slice

NOTE:

It's the final line, df.to_csv("csv_file.csv") that gives the error

0

1 Answer 1

1

This was a likely bug that has been fixed as of recent versions of pandas, namely, in version 0.16.2:

df.to_csv("csv_file.csv")

Works as expected and outputs csv_file.csv in the working directory. If you encounter this bug check your pandas version with:

import pandas; print(pandas.version.version)

and if you're working with an older version use pip -U --force-reinstall pandas for Py2 or pip3 -U --force-reinstall pandas for Py3 to upgrade to the most recent version.

Sign up to request clarification or add additional context in comments.

1 Comment

upgrading to the latest version of pandas, as well as reinstalling pandas for Python 3 (as some of the dependencies between Python2 and Python3 may have been messed up) using pip3 install --upgrade --force-reinstall pandas fixed the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.