0

I am pretty sure I should do like:

df['date'] = pd.to_datetime(df['date'])

But is keep getting the ERROR: "KeyError: 'date'" The data frame looks like this:

         date       value
0  2004-01-01       0.265
1  2004-01-02       0.440
2  2004-01-05      -0.955
3  2004-01-06      -1.892
prre72
  • 697
  • 2
  • 12
  • 23
  • check if there are whitespaces or `\t` in your column name – rafaelc May 18 '18 at 16:51
  • Can't reproduce, can you show how the dataframe is being created? – user3483203 May 18 '18 at 16:51
  • What is `print(df.columns)`? – cs95 May 18 '18 at 16:53
  • If you [edit](https://stackoverflow.com/posts/50415981/edit) your question with the output of `df.head().to_dict()`, we may be able to quickly pinpoint the problem. – jpp May 18 '18 at 16:54
  • Do `df.columns = [x.strip() for x in list(df.columns)]` and then try your code. – harvpan May 18 '18 at 18:26
  • output of df.head().to_dict() is {'value': {0: 0.26500000000000001, 1: 0.44, 2: -0.95499999999999996, 3: -1.8919999999999999, 4: -2.597}, '\ufeffdate': {0: '2004-01-01', 1: '2004-01-02', 2: '2004-01-05', 3: '2004-01-06', 4: '2004-01-07'}} – prre72 May 18 '18 at 19:58

1 Answers1

0

I have found the problem. The problem arises when you save the .csv in CSV UTF-8.

prre72
  • 697
  • 2
  • 12
  • 23