-1

This may not be specific to Pandas, but I am getting this error, for a small test file created in the C: drive. Tried searching on "unicode error", but this one seems to be different. What is wrong?

import pandas as pd
import matplotlib.pyplot as plt
df  = pd.read_csv("C:\Users\arny\data.csv")
df.plot()  # plots all columns against index
df.plot(kind='scatter',x='x',y='y') # scatter plot
df.plot(kind='density')  # estimate density function
# df.plot(kind='hist')  # histogram

File "", line 3 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

arny
  • 27
  • 1
  • 3

1 Answers1

1

The problem is with the string

"C:\Users\arny\data.csv"

Here, \U starts an eight-character Unicode escape, such as '\U00014321`. In your code, the escape is followed by the character 's', which is invalid.

You either need to duplicate all backslashes ('\'), or prefix the string with r (to produce a raw string).

Hari_pb
  • 7,088
  • 3
  • 45
  • 53
  • Thank you. I am using Thonny, with all packages installed, and the script now runs without errors (after adding \\ to the path), but there is no plot being displayed. Is it because Pandas is dependent on the IDE being used (in this case, Thonny)? – arny Oct 25 '17 at 20:57
  • @arny, I have never used Tonny, but yes, most of the time, it depends on the IDE to beautify plots but are not usually affected by IDE. You may try `matplotlib` and try writing some plots that may work for you. – Hari_pb Oct 25 '17 at 21:12