3

For an assignment at Uni we have to use PySal to calculate Moran's I. I have found an examplar code and am going from that but I am getting the error message "'file' object has no attribute 'by_col'"

Any Ideas would be much appreciated. The dataset is a text file of new york and the "T0_MINC" attribute is the column containing median household income for the different areas. The script is attached below

import os
import pysal
import numpy as np

os.chdir = ("C:\\Users\\hsv0336\\Desktop")
wd = os.getcwd()

w = pysal.open(wd + "\\Rook.gal")
f = pysal.open(wd + "\\NewYorkData.txt")
y = np.array(f.by_col["T0_MINC"])

mi = pysal.Moran(y, w, two_tailed=False)

The example script I am working from is located under "Moran" in this link: http://pysal.readthedocs.io/en/latest/users/tutorials/autocorrelation.html#moran-s-i

1 Answers1

0

pysal.open tries to determine the file type based on the extensions and by inspecting the file. The type returned by pysal.open is not always clear and should be checked with the built-in type function. In your case it is returning a plain python file object, meaning pysal wasn't able to parse it for you. The "by_col" method is only available when PySAL recognizes your file as a DataTable.

Supported DataTable types include .csv, .dbf or .txt files with a special "GeoDa" header. The GeoDa header contains the number of data rows and columns in the first line and the names of the columns in the seconds line.

For example the first few lines of the example STL_HOM.txt data set. Which has 78 data rows and 4 columns.

78,4
"FIPSNO","HR8488","HR8893","HC8488"
17107,1.290722,1.624458,2

You could either reformat your file to one of the supported DataTable types, or write your own code to parse your text file.

Charles
  • 1,820
  • 13
  • 16