Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "title already used as a name or title" error while reading SPSS (.sav) file in Python

I've working on reading an SPSS file (.sav). My code below can read .sav files. However, I've encounter a very strange error. When I try to read another .sav file, it gives the following error

Traceback (most recent call last):
File "C:\Users\fatihshen\Documents\Merjek 
Project\Predictive_Analytics\sav_reader.py", line 28, in <module>
read_spss_file(file_path)
File "C:\Users\fatihshen\Documents\Merjek 
Project\Predictive_Analytics\sav_reader.py", line 10, in read_spss_file
records = reader.all()
File "C:\Users\fatihshen\AppData\Local\Programs\Python\Python36-32\lib\site- 
packages\savReaderWriter\savReaderNp.py", line 541, in all
return self.to_structured_array(filename)
File "C:\Users\fatihshen\AppData\Local\Programs\Python\Python36-32\lib\site- 
packages\savReaderWriter\savReaderNp.py", line 122, in _convert_datetimes
array = func(self, *args)
File "C:\Users\fatihshen\AppData\Local\Programs\Python\Python36-32\lib\site- 
packages\savReaderWriter\savReaderNp.py", line 148, in _convert_missings
array = func(self, *args)
File "C:\Users\fatihshen\AppData\Local\Programs\Python\Python36-32\lib\site- 
packages\savReaderWriter\savReaderNp.py", line 531, in to_structured_array
array = np.fromiter(self, self.trunc_dtype, self.nrows)
File "C:\Users\fatihshen\AppData\Local\Programs\Python\Python36-32\lib\site- 
packages\savReaderWriter\helpers.py", line 17, in fget_memoized
setattr(self, attr_name, fget(self))
File "C:\Users\fatihshen\AppData\Local\Programs\Python\Python36-32\lib\site- 
packages\savReaderWriter\savReaderNp.py", line 376, in trunc_dtype
return np.dtype(obj)
ValueError: title already used as a name or title.

Here is my code:

import savReaderWriter as spss
import pandas as pd

my_df = None

def read_spss_file(file_name):
    global my_df
    with spss.SavReaderNp(file_name) as reader:
         records = reader.all()
         my_df = pd.DataFrame(records)

file_path = "dataset/child_abilities.sav"           
read_spss_file(file_path)
print(my_df)

The .sav file is running properly on SPSS. However, while using these Python codes, some .sav files don't work (this code works most of other .sav files).

Here is the file you can use: child abilities

Any idea what is going on here? I would appreciate your help.

like image 579
user3288051 Avatar asked Apr 04 '18 21:04

user3288051


1 Answers

There is an easier way to read SPSS files into pd.DataFrame(), by using "pd.read_spss(filepath)" method. It works with your file.

import pandas as pd

file_path = "./child_abilities.sav"
df = pd.read_spss(file_path)

Please be noted that you must install Pyreadstat.

% pip install pyreadstst
or
% conda install -c conda-forge pyreadstat
like image 92
Hsuning Avatar answered Nov 18 '22 04:11

Hsuning