ImportError:沒有名為 dateutil.parser 的模塊

| | | | | | | | | | | | | | | | | | | | | | | | | | | |

我在 Python 程序中導入 pandas 時收到以下錯誤

monas-mbp:book mona$ sudo pip install python-dateutil 要求已經滿足(使用--upgrade 升級):/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python 中的python-dateutil 清理... monas-mbp:book mona $ python t1.py 沒有名為 dateutil.parser Traceback 的模塊(最近一次調用最後一次):文件“t1.py”,第 4 行,在 <module>將 pandas 導入為 pd 文件“/Library/Python/2.7/site-packages/pandas/__init__.py”,第 6 行,在 <module>從 。 import hashtable, tslib, lib File "tslib.pyx", line 31, init pandas.tslib (pandas/tslib.c:48782) ImportError: No module named dateutil.parser 

還有這是程序:

import codecs from math import sqrt import numpy as np import pandas as pd users = {"Angelica": {"Blues Traveler": 3.5, "Broken Bells": 2.0,“諾拉·瓊斯”:4.5,“鳳凰”:5.0,“略笨”:1.5,“中風”:2.5,“吸血鬼週末”:2.0},“比爾”:{“布魯斯旅行者”:2.0,“ Broken Bells”:3.5,“Deadmau5”:4.0,“Phoenix”:2.0,“Slightly Stoopid”:3.5,“Vampire Weekend”:3.0},“Chan”:{“Blues Traveler”:5.0,“Broken Bells”: 1.0,“Deadmau5”:1.0,“Norah Jones”:3.0,“鳳凰”:5,“有點笨”:1.0},“Dan”:{“Blues Traveler”:3.0,“Broken Bells”:4.0,“Deadmau5 》:4.5,《鳳凰》:3.0,《小笨蛋》:4.5,《中風》:4.0,《吸血鬼週末》:2.0},《海莉》:{《破鈴》:4.0,《Deadmau5》:1.0, “諾拉瓊斯”:4.0,“中風”:4.0,“吸血鬼週末”:1.0},“Jordyn”:{“Broke n Bells”:4.5,“Deadmau5”:4.0,“Norah Jones”:5.0,“Phoenix”:5.0,“Slightly Stoopid”:4.5,“The Strokes”:4.0,“Vampire Weekend”:4.0},“Sam” :{“布魯斯旅行者”:5.0,“破碎的鐘聲”:2.0,“諾拉瓊斯”:3.0,“鳳凰”:5.0,“有點愚蠢”:4.0,“中風”:5.0},“維羅妮卡”:{“ Blues Traveler": 3.0, "Norah Jones": 5.0, "Phoenix": 4.0, "Slightly Stoopid": 2.5, "The Strokes": 3.0} 課程推薦者: def __init__(self, data, k=1, metric= "pearson", n=5): """ 當前初始化推薦器,如果數據是字典,則推薦器初始化為它。對於所有其他數據類型的數據,不進行初始化 k 是 k 最近鄰的 k 值 度量是要使用哪個距離公式 n 是要提出的最大推薦數""" self.k = k self.n = n self .username2id = {} self.userid2name = {} self.productid2name = {} # 出於某種原因我想保存指標的名稱 self.metric = metric if self.metric == "pearson": self.fn = self .pearson # # 如果數據是字典集推薦數據給它 # if type(data).__name__ == "dict": self.data = data def convertProductID2name(self, id): """Given product id number return product name """ if id in self.productid2name: return self.productid2name[id] else: return id def userRatings(self, id, n): """Return n top rating for user with id""" print ("Ratings for " + self.userid2name[id]) rating = self.data[id] print(len(ratings)) rating = list(ratings.items()) rating = [(self.convertProductID2name(k), v) for (k , v) in rating] # 最後排序並返回 ratings.sort(key=lambda ar tistTuple: artistTuple[1], reverse = True) rating = ratings[:n] for rating in rating: print("%s %i" % (rating[0], rating[1])) def loadBookDB(self, path =""): """加載 BX 圖書數據集。路徑是 BX 文件所在的位置""" self.data = {} i = 0 # # 首先將圖書評級加載到 self.data # f = codecs.open(path + "BX-Book-Ratings.csv", " r", "utf8") for line in f: i += 1 #separate line into fields fields = line.split(";") user = fields[0].strip(""") book = fields[1] .strip(""") rating = int(fields[2].strip().strip(""")) if user in self.data: currentRatings = self.data[user] else: currentRatings = {} currentRatings[ book] = rating self.data[user] = currentRatings f.close() # # 現在將書籍加載到 self.productid2name # Books 包含 isbn、title 和 author 以及其他字段 # f = codecs.open(path + "BX- Books.csv", "r", "utf8") for line in f: i += 1 #separate line into fields fields = line.split(";") isbn = fields[0].strip(""") title = fields[1].strip(""") author = fields[2].strip().strip(""") title = title + " by " + author self.productid2name[isbn] = title f.close () # # 現在將用戶信息加載到 self.userid2name 和 #self.username2id # f = codecs.open(path + "BX-U sers.csv", "r", "utf8") for line in f: i += 1 #print(line) #separate line into fields fields = line.split(";") userid = fields[0].strip (""") location = fields[1].strip(""") if len(fields) > 3: age = fields[2].strip().strip(""") else: age = "NULL" if age != "NULL": value = location + " (age: " + age + ")" else : value = location self.userid2name[userid] = value self.username2id[location] = userid f.close() print(i) def pearson(self, rating1, rating2): sum_xy = 0 sum_x = 0 sum_y = 0 sum_x2 = 0 sum_y2 = 0 n = 0 for key in rating1: 如果 key in rating2: n += 1 x = rating1[key] y = rating2[key] sum_xy += x * y sum_x += x sum_y += y sum_x2 += pow(x, 2) sum_y2 += pow(y, 2) if n == 0: return 0 # 現在計算分母 denominator = (sqrt(sum_x2 - pow(sum_x, 2) / n) * sqrt(sum_y2 - pow( sum_y, 2) / n)) if denominator == 0: return 0 else: return (sum_xy - (sum_x * sum_y) / n) / denominator def computeNearestNeighbor(self, username): """ 創建一個基於用戶的排序列表他們與用戶名的距離“”“距離 = [] 例如在 self.data: if instance != username: distance = self.fn(self.data[username], self.data[instance]) distances.append(( instance, distance)) # 根據 d 排序istance -- 最接近的第一個 distances.sort(key=lambda artistTuple: artistTuple[1], reverse=True) return distances defRecommend(self, user): """Give list of Recommendation""" Recommendations = {} # first get按最近距離排序的用戶列表最近 = self.computeNearestNeighbor(user) # # 現在獲取用戶的評分 # userRatings = self.data[user] # # 確定範圍內 i 的總距離 totalDistance = 0.0(self.k) : totalDistance += nearest[i][1] # 現在遍歷 k 個最近的鄰居 # 在 range(self.k) 中累積他們對 i 的評分: # 計算餅圖的切片 weight = nearest[i][1] / totalDistance #獲取人名 name = 最近[i][0] # 獲取此人的評分 neighborRatings = self.data[name] # 獲取此人的姓名 # 現在找到該用戶沒有為藝術家評分的樂隊鄰居在 neighborRatings 中:如果不是藝術家在 userRatings 中:如果藝術家不在推薦中:推薦 [藝術家] = (neighborRatings [藝術家] * 權重) 否則:推薦 [藝術家st] = (recommendations[artist] + neighborRatings[artist] * weight) # 現在從字典中創建列表推薦 = list(recommendations.items()) 推薦 = [(self.convertProductID2name(k), v) for (k, v ) in Recommendation] # 最後排序並返回 Recommendations.sort(key=lambda ArtistTuple: ArtistTuple[1], reverse = True) # 返回前 n 項 return Recommendations[:self.n] r = Recommendationer(users) # 作者實現 r.loadBookDB("/Users/mona/Downloads/BX-Dump/") 評分 = pd.read_csv("/Users/danialt/BX-CSV-Dump/BX-Book-Ratings.csv", sep="; ", quotechar=""", escapechar="") books = pd.read_csv("/Users/danialt/BX-CSV-Dump/BX-Books.csv", sep=";", quotechar=""" , escapechar="") users = pd.read_csv("/Users/danialt/BX-CSV-Dump/BX-Users.csv", sep=";", quotechar=""", escapechar="") pivot_rating = rating.pivot(index="User-ID", columns="ISBN", values="Book-Rating")