python - Python Pandas 无法导入 QUOTE_MINIMAL
问题描述
我正在使用熊猫开发一个图书馆。为了开发库的脚本,我创建了一个可以根据需要工作的基本脚本。然后,我将此代码复制到库文件夹中的 .py 文件中,加载熊猫时总是收到以下错误消息:
Traceback (most recent call last):
File "D:/My/Path/Python Perfectionnement/analysis/csv.py", line 4, in <module>
import pandas as pd
File "C:\Users\Me\AppData\Local\Continuum\anaconda3\envs\pyperf\lib\site-packages\pandas\__init__.py", line 145, in <module>
from pandas.io.api import (
File "C:\Users\Me\AppData\Local\Continuum\anaconda3\envs\pyperf\lib\site-packages\pandas\io\api.py", line 8, in <module>
from pandas.io.excel import ExcelFile, ExcelWriter, read_excel
File "C:\Users\Me\AppData\Local\Continuum\anaconda3\envs\pyperf\lib\site-packages\pandas\io\excel\__init__.py", line 1, in <module>
from pandas.io.excel._base import ExcelFile, ExcelWriter, read_excel
File "C:\Users\Me\AppData\Local\Continuum\anaconda3\envs\pyperf\lib\site-packages\pandas\io\excel\_base.py", line 9, in <module>
from pandas._libs.parsers import STR_NA_VALUES
File "pandas\_libs\parsers.pyx", line 12, in init pandas._libs.parsers
ImportError: cannot import name QUOTE_MINIMAL
Process finished with exit code 1
这是模块的代码:
#! /usr/bin/env python3
# coding: utf-8
import os
import pprint
import logging as lg
import pandas as pd
import matplotlib
matplotlib.use('TkAgg') # you need this if you are on MacOS
import matplotlib.pyplot as plt
import numpy as np
class SetOfParliamentMember:
def __init__(self, name):
self.name = name
def data_from_csv(self, csv_file):
lg.info("Opening data file {}".format(csv_file))
self.dataframe = pd.read_csv(csv_file, sep=";")
def data_from_dataframe(self, dataframe):
self.dataframe = dataframe
def display_chart(self):
data = self.dataframe
female_mps = data[data.sexe == "F"]
male_mps = data[data.sexe == "H"]
counts = [len(female_mps), len(male_mps)]
counts = np.array(counts)
nb_mps = counts.sum()
proportions = counts / nb_mps
labels = ["Female ({})".format(counts[0]), "Male ({})".format(counts[1])]
fig, ax = plt.subplots()
ax.axis("equal")
ax.pie(
proportions,
labels=labels,
autopct="%1.1f%%"
)
plt.title("{} ({} MPs)".format(self.name, nb_mps))
plt.show()
def split_by_political_party(self):
result = {}
data = self.dataframe
# These 2 syntaxes are equivalent : data.parti_ratt_financier and data['parti_ratt_financier']
all_parties = data["parti_ratt_financier"].dropna().unique()
for party in all_parties:
data_subset = data[data.parti_ratt_financier == party]
subset = SetOfParliamentMember('MPs from party "{}"'.format(party))
subset.data_from_dataframe(data_subset)
result[party] = subset
return result
def __str__(self):
names = [] ## todo: remplacer a la fin par une comprehension
for row_index, mp in self.dataframe.iterrows(): ##todo: ici il y a du packing/unpacking
names += [mp.nom]
return str(names) # Python knows how to convert a list into a string
def __repr__(self):
return "SetOfParliamentMember: {} members".format(len(self.dataframe))
def __len__(self):
return self.number_of_mps
def __contains__(self, mp_name):
return mp_name in self.dataframe["nom"].values
def __getitem__(self, index):
try:
result = dict(self.dataframe.iloc[index])
except:
if index < 0:
raise Exception("Please select a positive index")
elif index >= len(self.dataframe):
raise Exception("There are only {} MPs!".format(len(self.dataframe)))
else:
raise Exception("Wrong index")
return result
def __add__(self, other):
if not isinstance(other, SetOfParliamentMember):
raise Exception("Can not add a SetOfParliamentMember with an object of type {}".format(type(other)))
df1, df2 = self.dataframe, other.dataframe ##todo: ici il y a du packing/unpacking
df = df1.append(df2)
df = df.drop_duplicates()
s = SetOfParliamentMember("{} - {}".format(self.name, other.name))
s.data_from_dataframe(df)
return s
def __radd__(self, other): ## todo: l'implementation de cette methode ne suit a mon avis pas les bonnes pratiques
return self
def __lt__(self, other):
return self.number_of_mps < other.number_of_mps
def __gt__(self, other):
return self.number_of_mps > other.number_of_mps
# The following 2 methods are a way to simulate a calculated attribute
# (attribute 'number_of_mps' is calculated from attribute 'seld.dataframe')
# There is a much better way to do it, using decorator '@property'
def __getattr__(self, attr):
if attr == "number_of_mps": ##todo: faire la version avec @property
return len(self.dataframe)
def __setattr__(self, attr, value):
if attr == "number_of_mps":
raise Exception("You can not set the number of MPs!")
self.__dict__[attr] = value ## todo: c'est l'occasion de parler de __dict__ dans le cours ;)
def launch_analysis(data_file,
by_party = False, info = False, displaynames = False,
searchname = None, index = None, groupfirst = None):
sopm = SetOfParliamentMember("All MPs")
sopm.data_from_csv(os.path.join("data",data_file))
sopm.display_chart()
if by_party:
for party, s in sopm.split_by_political_party().items():
s.display_chart()
if info:
print()
print(repr(sopm))
if displaynames:
print()
print(sopm)
if searchname != None:
is_present = searchname in sopm
print()
print("Testing if {} is present: {}".format(searchname, is_present))
if index is not None:
index = int(index)
print()
pprint.pprint(sopm[index]) # prints the dict a nice way
if groupfirst is not None:
groupfirst = int(groupfirst)
parties = sopm.split_by_political_party()
parties = parties.values()
parties_by_size = sorted(parties, reverse = True)
print()
print("Info: the {} biggest groups are :".format(groupfirst))
for p in parties_by_size[0:groupfirst]:
print(p.name)
s = sum(parties_by_size[0:groupfirst])
s.display_chart()
if __name__ == "__main__":
launch_analysis('current_mps.csv')
有效的脚本和模块文件夹使用相同的环境。
谢谢你的帮助!!(<--我知道最后一句话会被审查,但我只是有礼貌,你知道……所谓的教育……)
解决方案
对于那些遇到错误的人:cannot import name QUOTE_MINIMAL
确保您没有名为“csv”的文件或文件夹。这会与 pandas 模块产生名称冲突问题。
推荐阅读
- python - write_csv_rows() 在具有长字符串的大型数据集上非常慢
- c# - ASP.NET Core 2.1 代码生成错误 - 找到多个相同类型的自定义属性
- entity-framework-core - EF 核心,创建自定义默认约束名称
- twitter - Twitter API 返回推文链接,而推文没有链接
- css - 如何从左到右显示子组件
- css - 在引导程序上定位 .card-img-top
- c# - Wpf:使用 xaml 中的参数初始化类
- magnolia - 在 magnolia 管理中心的页面应用程序上创建页面时出错
- elasticsearch - 嵌套对象中的 Elasticsearch 布尔搜索
- android - WorkManager 如何安排对 REST API 的 GET 请求?