Python Pandas Cannot import QUOTE_MINIMAL - python

I'm working on a library using pandas.
To develop the script of the library, I created a basic script that works as desired. Then, I copied this code in a .py file in the library folder and I always get the following error message when loading pandas:
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
Here is the code of the module:
#! /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')
The script that works and the module folder use the same environment.
Thanks for your help!! (<-- I know that last sentence will be censored, but I'm just polite, you know... that what is called education...)

For those who are having with the Error: cannot import name QUOTE_MINIMAL
Make sure that you don't have a file or folder with the name "csv". This create a name collision problem with the pandas module.

i'm learning with the same project and had the same issue.
Solved it by installing an older pandas version :
pip install pandas==0.25.3
and renaming the file "csv.py".
if not done, 'import pandas as pd' misuses 'csv.py' as module "csv".
Hope it helps

I got the same error, I changed the directory where my code was saved(try Desktop) and it worked, still couln't figure out why I got that error.

Please make sure that you have installed the module you are looking for.

Related

Running Keypirinha plugin code got error: ModuleNotFoundError: No module named 'keypirinha_api'

I'm getting "ModuleNotFoundError: No module named 'keypirinha_api'" (inside Keyprinha.py file, "import keypirinha_api" command)
when I trying to run sample plugin file that imports "import keypirinha as kp".
Here's plugin sample I got error in the very beginning:
https://github.com/Keypirinha/SDK/blob/master/examples/YesNo/yesno.py
*I downloaded the sdk from here:
https://github.com/Keypirinha/SDK
** Pip install doesn't find Keypirinha
What I'm doing wrong?
Thanks
# Keypirinha launcher (keypirinha.com)
import keypirinha as kp
import keypirinha_util as kpu
from collections import namedtuple
import datetime
import os
AnswerTuple = namedtuple('AnswerTuple', ('value', 'datetime'))
class YesNo(kp.Plugin):
"""A simple "Yes or No" plugin"""
ITEMCAT_RESULT = kp.ItemCategory.USER_BASE + 1
history = []
def __init__(self):
super().__init__()
def on_start(self):
self.history = []
self.set_actions(self.ITEMCAT_RESULT, [
self.create_action(
name="copy",
label="Copy",
short_desc="Copy the name of the answer")])
def on_catalog(self):
self.set_catalog([self.create_item(
category=kp.ItemCategory.KEYWORD,
label="YesNo",
short_desc="Yes or No",
target="yesno",
args_hint=kp.ItemArgsHint.REQUIRED,
hit_hint=kp.ItemHitHint.NOARGS)])
def on_suggest(self, user_input, items_chain):
if not items_chain or items_chain[-1].category() != kp.ItemCategory.KEYWORD:
return
if not user_input:
self.history = []
# compute a new answer
self.history.append(AnswerTuple(
os.urandom(1)[0] % 2,
datetime.datetime.now()))
# suggest this new answer as well as the previous ones
suggestions = []
for idx in range(len(self.history) - 1, -1, -1):
answer = self.history[idx]
desc = "{}. {} (press Enter to copy)".format(
idx + 1, answer.datetime.strftime("%H:%M:%S"))
suggestions.append(self.create_item(
category=self.ITEMCAT_RESULT,
label=self._value_to_string(answer.value),
short_desc=desc,
target="{},{}".format(answer.value, idx),
args_hint=kp.ItemArgsHint.FORBIDDEN,
hit_hint=kp.ItemHitHint.IGNORE))
self.set_suggestions(suggestions, kp.Match.ANY, kp.Sort.NONE)
def on_execute(self, item, action):
if item and item.category() == self.ITEMCAT_RESULT:
value = int(item.target().split(',', maxsplit=1)[0])
kpu.set_clipboard(self._value_to_string(value))
def _value_to_string(self, value):
return "Yes" if value else "No"

Backtrader issue - optstrategy AttributeError: module 'collections' has no attribute 'Iterable'

I'm trying to optimize my strategy on Backtrader with python but keeps getting this error and i can't find anything on the web showing why I'm getting it.
My code is simple and loosely based on the quick start examples:
from alpaca_trade_api.rest import REST, TimeFrame, TimeFrameUnit
import backtrader as bt
from config import API_KEY, SECRET_KEY
class EMACross(bt.Strategy):
params = dict(
ema_short_period=5,
ema_long_period=10
)
def __init__(self):
self.order = None
self.short_ma = bt.indicators.ExponentialMovingAverage(period=self.p.ema_short_period)
self.long_ma = bt.indicators.ExponentialMovingAverage(period=self.p.ema_long_period)
self.crossover = bt.ind.CrossOver(self.short_ma, self.long_ma) # crossover signal
self.crossdown = bt.ind.CrossDown(self.short_ma, self.long_ma)
self.crossdown.plotinfo.subplot = False
self.crossover.plotinfo.subplot = False
def next(self):
self.log('Close, %.2f' % self.data.close[0])
if self.position.size > 0:
if self.crossdown > 0:
self.log('SELL CREATE, %.2f' % self.data.close[0])
self.close()
else:
if self.crossover > 0:
self.log('BUY CREATE, %.2f' % self.data.close[0])
self.buy()
def log(self, txt, dt=None):
dt = dt or self.data.datetime.datetime()
print('%s, %s' % (dt.isoformat(), txt))
def stop(self):
self.log('(short EMA Period %2d) (long EMA Period %2d) Ending Value %.2f' %
(self.p.ema_short_period, self.p.ema_long_period, self.broker.getvalue()))
rest_api = REST(API_KEY, SECRET_KEY, 'https://paper-api.alpaca.markets')
def run_backtest(strategy, symbols, start, end, timeframe, cash=100000):
# initialize backtrader broker
cerebro = bt.Cerebro()
cerebro.broker.setcash(cash)
cerebro.addsizer(bt.sizers.PercentSizer, percents=90)
cerebro.optstrategy(strategy, ema_short_period=4, ema_long_period=6)
# historical data request
if type(symbols) == str:
symbol = symbols
alpaca_data = rest_api.get_bars(symbol, timeframe, start, end, adjustment='all').df
data = bt.feeds.PandasData(dataname=alpaca_data, name=symbol)
cerebro.adddata(data)
elif type(symbols) == list or type(symbols) == set:
for symbol in symbols:
alpaca_data = rest_api.get_bars(symbol, timeframe, start, end, adjustment='all').df
data = bt.feeds.PandasData(dataname=alpaca_data, name=symbol)
cerebro.adddata(data)
# run
initial_portfolio_value = cerebro.broker.getvalue()
print(f'Starting Portfolio Value: {initial_portfolio_value}')
results = cerebro.run()
final_portfolio_value = cerebro.broker.getvalue()
print(
f'Final Portfolio Value: {final_portfolio_value} ---> Return: {(final_portfolio_value / initial_portfolio_value - 1) * 100}%')
run_backtest(EMACross, 'QQQ', '2018-01-01', '2022-01-01', TimeFrame(1, TimeFrameUnit.Day))
Running the script, i get this error:
Traceback (most recent call last):
File "/Users/usrname/PycharmProjects/test3/main.py", line 79, in <module>
run_backtest(EMACross, 'QQQ', '2018-01-01', '2022-01-01', TimeFrame(1, TimeFrameUnit.Day))
File "/Users/usrname/PycharmProjects/test3/main.py", line 54, in run_backtest
cerebro.optstrategy(strategy, ema_short_period=4, ema_long_period=6)
File "/Users/usrname/PycharmProjects/test3/venv/lib/python3.10/site-packages/backtrader/cerebro.py", line 893, in optstrategy
vals = self.iterize(kwargs.values())
File "/Users/usrname/PycharmProjects/test3/venv/lib/python3.10/site-packages/backtrader/cerebro.py", line 333, in iterize
elif not isinstance(elem, collections.Iterable):
AttributeError: module 'collections' has no attribute 'Iterable'
Process finished with exit code 1
When running the script without optstrategy() but rather with addstrategy(), evrything is working great. Only when changing to optstrategy is when i'm getting this error.
I also tried to run the same code on Google colab (with optstrategy() method) and everything worked great there, so this got me really puzzled...
I'm running python 3.10 with PyCharm CE on macOS.
Try downgrading from python 3.10 to python 3.8 version

how to open or create database by importing sqlite3 in python code [duplicate]

This question already exists:
how the error Tuple object error in sqlite3 can be resolved
Closed 1 year ago.
I have python code in which I have imported sqlite3 to open database if it is already present or else create new database in the specified location. When I run the code, it is not displaying any error. I have written testscript , When I try to run the testscript, it is displaying error "'tuple' object does not support item assignment". Can anyone suggest me how can I create the database in sqlite3.
import sqlite3
import os
class sqlite1():
def __init__(self, pa, na, rq):
self.dbname = None
self.name = None
self.id1 = 0
if rq == 'open':
print(pa, na)
self.hdl = (os.path.join(pa, na),'open')
if not b'id1' in self.hdl or not b'ns' in self.hdl:
print("open")
try:
self.id1 = int(self.hdl[b'id1'])
self.name = self.hdl[b'ns']
except:
pass
else:
self.hdl = (os.path.join(pa, na),'c')
self.id1 = 0
print("File created")
self.hdl[b'id1'] = str(self.id1)
def getID(self):
return self.hdl[b'id1']
def setID(self, id1):
self.hdl[b'id1'] = id1
The testscript is:
from sqlite123 import *
from sqlite123 import sqlite1
class Test1:
def __init__(self, pa, na, rq):
self.pa = pa
self.na = na
self.id1 = 0
self.abc1 = sqlite1(pa, na, rq)
self.abc1.setID(0)
def fileopen(self, pa, na, rq):
self.abc1 = sqlite1(pa, na, rq)
if __name__ == '__main__':
tst = Test1('', 'file.db', 'c')
tst.fileopen('', 'file.db', 'open')
Instead of sqlite3 i have imported sqlite3dbm as:
import sqlite3dbm
from sqlite3dbm.dbm import *
and changed the line:
self.hdl = sqlite3dbm.dbm.open(os.path.join(pa, na),'open')
the code is working fine but it is displaying keys as 'None' i.e it is not accepting keys.As a result the method getID is not returning the correct value. Any suggestions to resolve it.

NameError and AttributeError when attempting to run code that uses Sage Math in Python

I have the following SageMath code, which works perfectly in CoCalc:
def mean_x(factor, values):
return sum([cos(2*pi*v/factor) for v in values])/len(values)
def mean_y(factor, values):
return sum([sin(2*pi*v/factor) for v in values])/len(values)
def calculatePeriodAppeal(factor, values):
mx = mean_x(factor, values)
my = mean_y(factor, values)
appeal = sqrt(mx^2+my^2)
return appeal
def calculateBestLinear(factor, values):
mx = mean_x(factor, values).n()
my = mean_y(factor, values).n()
y0 = factor*atan2(my,mx)/(2*pi).n()
err = 1-sqrt(mx^2+my^2).n()
return [factor*x + y0, err]
def calculateGCDAppeal(factor, values):
mx = mean_x(factor, values)
my = mean_y(factor, values)
appeal = 1 - sqrt((mx-1)^2+my^2)/2
return appeal
testSeq = [0,125,211,287,408,520,650,735,816,942,1060]
gcd = calculateGCDAppeal(x, testSeq)
agcd = find_local_maximum(gcd,2,100)
print(agcd)
plot(gcd,(x, 2, 100))
The output is the best approximate greatest common divisor of the numbers from testSeq, along with a plot.
How can I use this code in Python?
Here is the current Python version, which does not yet work:
import numpy as np
import sage as sm
def mean_x(factor, values):
return sum([np.cos(2*np.pi*v/factor) for v in values])/len(values)
def mean_y(factor, values):
return sum([np.sin(2*np.pi*v/factor) for v in values])/len(values)
def calculatePeriodAppeal(factor, values):
mx = mean_x(factor, values)
my = mean_y(factor, values)
appeal = np.sqrt(mx**2+my**2)
return appeal
def calculateBestLinear(factor, values):
mx = mean_x(factor, values).n()
my = mean_y(factor, values).n()
y0 = factor*np.atan2(my,mx)/(2*np.pi).n()
err = 1-np.sqrt(mx**2+my**2).n()
return [factor*x + y0, err]
def calculateGCDAppeal(factor, values):
mx = mean_x(factor, values)
my = mean_y(factor, values)
appeal = 1 - np.sqrt((mx-1)**2+my**2)/2
return appeal
testSeq = [0,125,211,287,408,520,650,735,816,942,1060]
gcd = calculateGCDAppeal(x, testSeq)
agcd = sm.find_local_maximum(gcd,2,100)
print(agcd)
The errors I get are:
Traceback (most recent call last):
File "<ipython-input-805-d2a8b405fd43>", line 30, in <module>
gcd = calculateGCDAppeal(x, testSeq)
NameError: name 'x' is not defined
I don't understand this because because this code works in CoCalc.
Traceback (most recent call last):
File "<ipython-input-803-80cdeb0485a5>", line 33, in <module>
agcd = sm.find_local_maximum(gcd,2,100)
AttributeError: module 'sage' has no attribute 'find_local_maximum'
...but I know that SageMath has the function find_local_maximum.
If I use numerical.optimize.find_local_maximum instead, I get:
Traceback (most recent call last):
File "<ipython-input-842-949de8b03df5>", line 34, in <module>
agcd = sm.numerical.optimize.find_local_maximum(gcd,2,100)
AttributeError: module 'sage' has no attribute 'numerical'
I don't know how to add the "numerical" attribute.
First of all, there is no definition of x anywhere in this code. In addition, the syntax of find_local_maximum is sage.numerical.optimize.find_local_maximum not sage.find_local_maximum (from the SageMath documentation)
When Sage starts, it imports a lot of things.
When using Python, you need to import them yourself.
In particular, Sage defines x when it starts.
To import it if using Python:
from sage.calculus.predefined import x
Find this out using the import_statements function
in a Sage session.
The quick workaround is the catch-all
from sage.all import *
but it's good practice to import only what is needed.
See the answer to this related question
How can I translate this SageMath code to run in Python?

Python,Ctypes - WindowsError: access violation

I'm developing instrument drivers for testing at work. It's first developed in
C using the IVI/Visa standard. I am then using a python wrapper for a dll of the C to use the functions in the python environment. However with the current one I am getting an error:
Traceback (most recent call last):
File "D:/CAS/Development/LvDrivers/Python/Python_wrapper/ADIag81110a.py", line 1362, in <module>
PGEN.ConfigureTransitionCoupling(1, "1")
File "D:/CAS/Development/LvDrivers/Python/Python_wrapper/ADIag81110a.py", line 1305, in ConfigureTransitionCoupling
ADIag81110aLib().ADIag81110a_ConfigureTransitionCoupling( self.vi, transitionEnabling , channelName )
WindowsError: exception: access violation reading 0x00000031
It happens with any function I use from the C. All of the functions work correctly when they are ran directly in C. He is is the C definition of that function:
ViStatus _VI_FUNC ADIag81110a_ConfigureTransitionCoupling (ViSession vi,
ViInt32 transitionEnabling, ViChar channelName[])
{
ViStatus error = VI_SUCCESS;
checkErr( Ivi_LockSession (vi, VI_NULL));
viCheckParm(Ivi_SetAttributeViInt32 (vi, channelName, ADIAG81110A_ATTR_TRANSITION_COUPLING,
0, transitionEnabling), 2, "Transition Coupling");
Error:
Ivi_UnlockSession (vi, VI_NULL);
return error;
}
The python might seem a little complicated but i'll only be posting the code that I feel is relevant to declaration of the ConfigureTransitionCoupling function that I'm using as an example and the ctypes declarations:
import os
import visa_exceptions
from visa_messages import completion_and_error_messages
from vpp43_constants import *
from vpp43_types import *
from ctypes import cdll
if os.name == 'nt':
from ctypes import windll
else:
from ctypes import CFUNCTYPE as FUNCTYPE
import warnings
class Singleton(object):
def __new__(cls, *args, **kwds):
it = cls.__dict__.get("__it__")
if it is not None:
return it
cls.__it__ = it = object.__new__(cls)
it.init(*args, **kwds)
return it
class ADIag81110a_lib(Singleton):
def __call__(self, force_cdecl=False):
if self.__lib is None or self.__cdecl_lib is None:
self.load_library()
if force_cdecl:
return self.__cdecl_lib
return self.__lib
def init(self):
self.__lib = self.__cdecl_lib = None
def load_library(self, path=None):
if os.name == 'nt':
path = "C:\Program Files (x86)\IVI Foundation\IVI\Bin\ADIag81110a_32.dll"
self.__lib = windll.LoadLibrary(path)
self.__cdecl_lib = cdll.LoadLibrary(path)
elif os.name == 'posix':
if not path:
path = "please put path to unix/linix here"
self.__lib = self.__cdecl_lib = cdll.LoadLibrary(path)
else:
self.__lib = self.__cdecl_lib = None
raise visa_exceptions.OSNotSupported, os.name
self.__initialize_library_functions()
return None
def __initialize_library_functions(self):
self.__set_argument_types("ADIag81110a_ConfigureTransitionCoupling", [ ViSession , ViInt32 , ViChar ])
def __set_argument_types(self, inst_function, types, force_cdecl=False, may_be_missing=True):
if force_cdecl:
library = self.__cdecl_lib
else:
library = self.__lib
try:
getattr(library, inst_function).argtypes = types
except AttributeError:
if not may_be_missing:
raise
ADIag81110aLib = ADIag81110a_lib()
class ADIag81110a():
def ConfigureTransitionCoupling(self , transitionEnabling , channelName ):
ADIag81110aLib().ADIag81110a_ConfigureTransitionCoupling( self.vi, transitionEnabling , channelName )
if __name__ == '__main__':
#This initilises the communication with the instrument
PGEN = ADIag81110a("GPIB0::10::INSTR")
test = PGEN.ReadOutputImpedance("1")
print test
I have looked at other people posting about their error and I feel they are using C-types in a different way to me so I am unable to apply their solutions.
Any help is appreciated. This is also my first post on stack overflow so feel free to point out any issues with my post :)

Categories