Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
714 views
in Technique[技术] by (71.8m points)

python - TypeError: '(slice(0, 15, None), 15)' is an invalid key

I have a code in Python that looks something like the code pasted below. For context, the all csv files print [15 rows x 16 columns], I just changed the name for privacy purposes.

import numpy as np
import pandas as pd 

C = pd.read_csv('/Users/name/Desktop/filename1.csv')
Chome = pd.read_csv('/Users/name/Desktop/filename2.csv')
Cwork = pd.read_csv('/Users/name/Desktop/filename3.csv')
Cschool = pd.read_csv('/Users/name/Desktop/filename4.csv')
Cother = pd.read_csv('/Users/name/Desktop/filename5.csv')

Cf = np.zeros([17,17])
Cf = C
Cf[0:15,16] = C[0:15,15]
Cf[16,0:15] = C[15,0:15] 
Cf[16,16] = C[15,15]

print(Cf)

When I run the code I get the following error:

runfile('/Users/name/.spyder-py3/untitled12.py', wdir='/Users/name/.spyder-py3')
Traceback (most recent call last):

  File "/Users/name/.spyder-py3/untitled12.py", line 23, in <module>
    Cf[0:15,16] = C[0:15,15]

  File "/opt/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py", line 2800, in __getitem__
    indexer = self.columns.get_loc(key)

  File "/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2646, in get_loc
    return self._engine.get_loc(key)

  File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc

  File "pandas/_libs/index.pyx", line 116, in pandas._libs.index.IndexEngine.get_loc

TypeError: '(slice(0, 15, None), 15)' is an invalid key

I am not exactly sure what this error means. I am pretty new to python, so debugging is a skill I am trying to better understand. So any advice on what I can do to fix this error, or what it means would be helpful. Thank you.

question from:https://stackoverflow.com/questions/65645461/typeerror-slice0-15-none-15-is-an-invalid-key

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Note the following sequence in your code sample:

C = pd.read_csv(...)
... # Other cases of pd.read_csv
Cf = np.zeros([17,17])

So, at least till now, C is a DataFrame and Cf is a Numpy array.

Then Cf = C is probably a logical error, since it overwrites the Numpy array (full of zeroes) with another reference to C.

And now as the offending instruction (Cf[0:15,16] = C[0:15,15]) is concerned:

Note that C[0:15,15] is wrong (run this code on your own to see it). In case of pandasonic DataFrames you can use "positional addressing", including slices, using iloc.

On the other hand, this notation is allowed for Numpy arrays.

So, assuming that Cf = C is not needed and Cf should remain a Numpy array, you probably should correct this instruction to:

Cf[0:15,16] = C.iloc[0:15,15]

And make analogous corrections in remaining instructions in your code.

Edit

Another option is to refer to the underlying Numpy array in C DataFrame, using values attribute. In this case you can use Numpythonic addressing style, e.g.:

C.values[0:15,15]

causes no error.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...