Pandas dataframe not allowing indexing

Hi there,
I am currently facing problems from transferring a working psychopy coder script from one laptop to another (both Mac OS).

OS : Mac OS 10.15
PsychoPy version: 2020.2.5
Standard Standalone? Yes
What are you trying to achieve?: The script works perfectly on my personal laptop (Mac OS 10.14, PsychoPy v2020.1.2, Python 2.7.16), but it fails when I try to index the first column of an imported pandas data frame.

### Import packages ####
from psychopy import visual, core, clock, data, event, logging, gui
from psychopy.hardware import keyboard
from numpy.random import random, randint, normal, shuffle
import pandas as pd
import os

JTCblock1 = pd.read_csv('/Users/…/Main study/Materials_Instruments/Questionnaires_Tasks/fMRI/SessB/block_conditions/JTCblock1.csv')
block = JTCblock1

# Save block name
print(block)
print(block['Block_name'])
block_name = block['Block_name'][0]
print(block_name)

The data frame “block” looks like this if I print it (which is fine):

Block_name;Image;file_fish;file_fishlist;file_lakes
0 JTCblock1;1;/Users/ausleihe2/Documents/AMBIZIO…
1 JTCblock1;2;/Users/ausleihe2/Documents/AMBIZIO…
2 JTCblock1;3;/Users/ausleihe2/Documents/AMBIZIO…
3 JTCblock1;4;/Users/ausleihe2/Documents/AMBIZIO…
4 JTCblock1;5;/Users/ausleihe2/Documents/AMBIZIO…
5 JTCblock1;6;/Users/ausleihe2/Documents/AMBIZIO…
6 JTCblock1;7;/Users/ausleihe2/Documents/AMBIZIO…
7 JTCblock1;8;/Users/ausleihe2/Documents/AMBIZIO…
8 JTCblock1;9;/Users/ausleihe2/Documents/AMBIZIO…
9 JTCblock1;10;/Users/ausleihe2/Documents/AMBIZI…

I am interested in the value of the column “Block_name”.
If I run the code posted above, I get the following error message:

print(block[‘Block_name’])
Traceback (most recent call last):
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/pandas/core/indexes/base.py”, line 2646, in get_loc
File “pandas/_libs/index.pyx”, line 111, in pandas._libs.index.IndexEngine.get_loc
File “pandas/_libs/index.pyx”, line 138, in pandas._libs.index.IndexEngine.get_loc
File “pandas/_libs/hashtable_class_helper.pxi”, line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File “pandas/_libs/hashtable_class_helper.pxi”, line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: ‘Block_name’

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “”, line 1, in
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/pandas/core/frame.py”, line 2800, in getitem
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.6/pandas/core/indexes/base.py”, line 2648, in get_loc
File “pandas/_libs/index.pyx”, line 111, in pandas._libs.index.IndexEngine.get_loc
File “pandas/_libs/index.pyx”, line 138, in pandas._libs.index.IndexEngine.get_loc
File “pandas/_libs/hashtable_class_helper.pxi”, line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File “pandas/_libs/hashtable_class_helper.pxi”, line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: ‘Block_name’

Solution attempts: I googled why that is, I re-started PsychoPy and my laptop, and re-installed Psychopy and Python. My interpretation is that the error is based on the pandas package (version is 1.0.3), but I do not know how to solve it and how to obtain the value of the first column.

I’d be really happy for any responses as I am so close to finishing the experiment…
Thanks!

Sarah

Indexing like that seems to work fine when I try it on my install, so I think you’re right that this is a version problem with pandas. You can update packages using the terminal app - find the folder your PsychoPy is installed to, then type into the terminal:

cd [path of your PsychoPy folder]

then

pip install pandas --upgrade

Hello @TParsons,

thank you very much for your reply.

I have tried your suggested code (also with some adaptations), but if I enter it into my terminal the way you proposed, I get this:

ausleihe2@ctaskforce ~ % cd /Applications/PsychoPy.app/Contents

–> for step 1.

ausleihe2@ctaskforce Contents % pip install pandas --upgrade
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.

Requirement already up-to-date: pandas in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (0.24.2)

Requirement already satisfied, skipping upgrade: python-dateutil>=2.5.0 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from pandas) (2.8.1)

Requirement already satisfied, skipping upgrade: pytz>=2011k in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from pandas) (2020.4)

Requirement already satisfied, skipping upgrade: numpy>=1.12.0 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from pandas) (1.16.6)

Requirement already satisfied, skipping upgrade: six>=1.5 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from python-dateutil>=2.5.0->pandas) (1.15.0)

In the applications folder, there is both a Python2.7 and Python3.9 version installed. I am wondering if Psychopy tries to access 2.7 instead of 3.9 and if that is the issue here… (?)

That’s possible! If you once again navigate to your PsychoPy folder (cd [path of your PsychoPy folder]), try typing:

python

so that any subsequent commands are executed in the version of Python present at that location, then:

import sys
sys.version_info.major

This should return either a 2 (if PsychoPy is using Python 2.7) or a 3 (if it’s using Python 3.9). Based on the error message when you try to update Pandas, I would hazard a guess that your PsychoPy is linked to a 2.7 install for some reason.