Python Forum
Index error - columns vs non-column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Index error - columns vs non-column
#1
Dear users,

I am getting an IndexError, and not sure why? The code (below) works perfectly when I run it using data.cvs. On that type of file, the index is a column (named Date).

But, if I run the code directly from an input library (e.i., df = yf.download(tickers=ticker, start=start, end=end), I receive the trackback message's below. In this example, the date is not considered a column. So, I think the error comes from that; however, I don't know how to fix it.

I hope I am being clear. It is my first post, and I am a beginner.

Thank you

This is my Code:
df_close_adj = pd.DataFrame(data=df[["Close", "Adj_Close"]].to_numpy(), index=df["Date"], columns=[["Close", "Adj_Close"]])
The Traceback I get is this:

Error:
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) /usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 2897 try: -> 2898 return self._engine.get_loc(casted_key) 2899 except KeyError as err: pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'Date' The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) 2 frames /usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 2898 return self._engine.get_loc(casted_key) 2899 except KeyError as err: -> 2900 raise KeyError(key) from err 2901 2902 if tolerance is not None: KeyError: 'Date'
Reply
#2
Hello,

I've not used Pandas too much but I believe the error is where you are referencing the index. Index, in case you didn't already know is how the code refers to items on a list or table, for example

0. Apple, 1. Pear, 2. Peach

So item 0 is always the first item on the list. Try changing "Date" to the index value of that column.

I don't know if that is the solution, however nobody else replied so I thought I would give it a go.

Kind regards,
James
while dad_has_cigs == True:
    happiness = True
    if dad_has_cigs == False:
    print("Dad come home!")
    happiness = not happiness
    break
Reply
#3
Hi James,

Thanks for stopping by. I really appreciate your time and effort in trying to help me. Unfortunately, it did not work, BUT I will persuade on my studies, and I hope to figure it out sooner or later.

I am a Python beginner.

Thank you, and have a great week!

Yours sincerely,

Vinny

(Aug-05-2021, 10:45 AM)jamesaarr Wrote: Hello,

I've not used Pandas too much but I believe the error is where you are referencing the index. Index, in case you didn't already know is how the code refers to items on a list or table, for example

0. Apple, 1. Pear, 2. Peach

So item 0 is always the first item on the list. Try changing "Date" to the index value of that column.

I don't know if that is the solution, however nobody else replied so I thought I would give it a go.

Kind regards,
James
jamesaarr likes this post
Reply
#4
The error is clear and the problem lay in df Dataframe.
When do index=df["Date"] means that the Dataframe most have column named Date.
To show a example.
import pandas as pd

data = {
    "Name": ["Tom", "nick", "krish", "jack"],
    "Date": [20, 21, 19, 18],
}

df = pd.DataFrame(data)
>>> df
    Name  Date
0    Tom    20
1   nick    21
2  krish    19
3   jack    18

>>> df['Date']
0    20
1    21
2    19
3    18
Name: Date, dtype: int64

>>> df.columns
Index(['Name', 'Date'], dtype='object')
If eg put a space in Date string it will fail with your error.
>>> df['Date']
Traceback (most recent call last):
.....  
KeyError: 'Date'

# Date most be the exact name and a column in df
>>> df.columns
Index(['Name', 'Date '], dtype='object')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting column of values into muliple columns of counts highland44 0 254 Feb-01-2024, 12:48 AM
Last Post: highland44
  pyscript index error while calling input from html form pyscript_dude 2 980 May-21-2023, 08:17 AM
Last Post: snippsat
  Index error help MRsquared 1 766 May-15-2023, 03:28 PM
Last Post: buran
  I'm getting a String index out of range error debian77 7 2,349 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  df column aggregate and group by multiple columns SriRajesh 0 1,043 May-06-2022, 02:26 PM
Last Post: SriRajesh
  Removing the index column from df bnadir55 7 3,081 Apr-05-2022, 02:49 PM
Last Post: snippsat
  Transform 3 Columns into Single Column DaveG 8 1,875 Apr-04-2022, 08:42 AM
Last Post: Pedroski55
  Split single column to multiple columns SriRajesh 1 1,330 Jan-07-2022, 06:43 PM
Last Post: jefsummers
  pandas pivot table: How to find count for each group in Index and Column JaneTan 0 3,310 Oct-23-2021, 04:35 AM
Last Post: JaneTan
  How to remove a column or two columns in a correlation heatmap? lulu43366 3 5,225 Sep-30-2021, 03:47 PM
Last Post: lulu43366

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020