eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I have this two following classes in my model: class Answer(models.Model): answer = models.CharField(max_length=300) question = models.ForeignKey('Question', on_delete=models.CASCADE) def __str__(self): return "{0}, view: {1}".format(self.answer, self.answer_number) class Vote(models.Model): answer = models.OneToOneField(Answer, related_name="votes", on_delete=models.CASCADE) users = models.ManyToManyField(User) def __str__(self): return str(self.answer.answer)[:30]  In the shell I take the first Answer: >>> Answer.objects.all()[0] <Answer: choix 1 , view: 0>  I want to get the Vote object: >>> Answer.objects.all()[0].votes Traceback (most recent call last): File "<console>", line 1, in <module> File "C:\Users\Hippolyte\AppData\Roaming\Python\Python38\site-packages\django\db\models\fields\related_descriptors.py", line 420, in __get__ raise self.RelatedObjectDoesNotExist( questions.models.Answer.votes.RelatedObjectDoesNotExist: Answer has no votes.  But an error occured. I don't understand why the related_name is not recognized. Could you help me ?

Answer: Your related_name IS recognized, but it is only assigned to the instance if the related object exists.  In your case, there is no Vote instance in your database where its answer field points to your Answer instance Just catch the exception and return None if you want to proceed: answer = Answer.objects.all().first() try: vote = answer.votes except Answer._meta.model.related_field.RelatedObjectDoesNotExist as e: vote = None  If you want to shorten this you can use hasattr(answer, 'vote'), to check, but this will mask ALL exceptions arising from the db lookup if any answer = Answer.objects.all().first() vote = answer.votes if hasattr(answer, 'votes') else None  Note that since you used a OneToOneField, answer.votes will always return a single instance if the related Vote exists. As such, it would be more appropriate to use related_name='vote' (without the s)
Question: I have done this small package that I want to distribute in my community. It is now on test.pypi and when I want to try to install it, it gives an error that dependencies couldn't be found. setup.py  ... install_requires=[ 'defcon>=0.6.0', 'fonttools>=3.31.0' ] ...  throws this error ERROR: Could not find a version that satisfies the requirement defcon>=0.6.0 (from sameWidther==0.6) (from versions: none) ERROR: No matching distribution found for defcon>=0.6.0 (from sameWidther==0.6)  but when I manually install, it works pip install 'fonttools>=3.6.0' pip install 'defcon>=0.6.0' 

Answer: -i URL, or --index-url URL means "use URL for installing packages from exclusively". By passing -i https://test.pypi.org/simple/, you thus prohibit searching and downloading packages from PyPI (https://pypi.org/simple). To use both indexes, use --extra-index-url: $ python -m pip install --extra-index-url https://test.pypi.org/simple/ sameWidther
Question: I am trying to install the geopandas package with Anaconda Prompt, but after I use conda install geopandas an unexpected thing happened: Collecting package metadata (current_repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve. Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source. Collecting package metadata (repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve. Solving environment: - Found conflicts! Looking for incompatible packages  After this, it proceeds to search for conflicts, but hours pass without finishing. In the end, I still cannot use geopandas. I have also tried installing geopandas in a different virtual environment and it works but I do not know how to use the environment in Jupyter Notebooks. I would like to know, how can install geopandas without a separate environment? Or, alternatively, how can I use geopandas in Jupyter Notebooks after install it in a separate environment?

Answer: You can install geopandas with pip, however, geopandas requires other dependencies (such as pandas, fiona, shapely, pyproj, rtree). You need to make sure that they are properly installed. After that you should be able to use them in jupyter with a simple import geopandas.
Question: I am trying to learn opencv and implementing a research project by testing some used cases. I am trying to crop the bounding box of the inside the image using python opencv . I have successfully created the bounding box but failed in crop this is the image   import cv2 import matplotlib.pyplot as plt img = cv2.imread("Segmentacion/Img_183.png") gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) dst = cv2.Canny(gray, 0, 150) blured = cv2.blur(dst, (5,5), 0) MIN_CONTOUR_AREA=200 img_thresh = cv2.adaptiveThreshold(blured, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2) Contours,imgContours = cv2.findContours(img_thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) for contour in Contours: if cv2.contourArea(contour) > MIN_CONTOUR_AREA: [X, Y, W, H] = cv2.boundingRect(contour) box=cv2.rectangle(img, (X, Y), (X + W, Y + H), (0,0,255), 2) cropped_image = img[X:W, Y:H] print([X,Y,W,H]) cv2.imwrite('contour.png', cropped_image ) 

Answer: I figured it out the formula for cropping the bounding box from the original image cropped_image = img[Y:Y+H, X:X+W] print([X,Y,W,H]) plt.imshow(cropped_image) cv2.imwrite('contour1.png', cropped_image)
Question: I want to list a dataframe where a specific column is either null or not null, I have it working using - df[df.Survive.notnull()] # contains no missing values df[df.Survive.isnull()] #---> contains missing values  This works perfectly but I want to make my code more dynamic and pass the column "Survive" as a variable but it's not working for me.  I tried: variableToPredict = ['Survive'] df[df[variableToPredict].notnull()]  I get the error - ValueError: cannot reindex from a duplicate axis I'm sure I'm making a silly mistake, what can I do to fix this?

Answer: So idea is always is necessary Series or list or 1d array for mask for filtering. If want test only one column use scalar: variableToPredict = 'Survive' df[df[variableToPredict].notnull()]  But if add [] output is one column DataFrame, so is necessaty change function for test by any (test if at least one NaN per row, sense in multiple columns) or all (test if all NaNs per row, sense in multiple columns) functions: variableToPredict = ['Survive'] df[df[variableToPredict].notnull().any(axis=1)] variableToPredict = ['Survive', 'another column'] df[df[variableToPredict].notnull().any(axis=1)]  Sample: df = pd.DataFrame({'Survive':[np.nan, 'A', 'B', 'B', np.nan], 'another column':[np.nan, np.nan, 'a','b','b']}) print (df) Survive another column 0 NaN NaN 1 A NaN 2 B a 3 B b 4 NaN b  First if test only one column: variableToPredict = 'Survive' df1 = df[df[variableToPredict].notnull()] print (df1) Survive another column 1 A NaN 2 B a 3 B b print (type(df[variableToPredict])) <class 'pandas.core.series.Series'> #Series print (df[variableToPredict]) 0 NaN 1 A 2 B 3 B 4 NaN Name: Survive, dtype: object print (df[variableToPredict].isnull()) 0 True 1 False 2 False 3 False 4 True Name: Survive, dtype: bool  If use list - here one element list: variableToPredict = ['Survive'] print (type(df[variableToPredict])) <class 'pandas.core.frame.DataFrame'> #one element DataFrame print (df[variableToPredict]) Survive 0 NaN 1 A 2 B 3 B 4 NaN  If testing per rows it is same output for any or all: print (df[variableToPredict].notnull().any(axis=1)) 0 False 1 True 2 True 3 True 4 False dtype: bool print (df[variableToPredict].notnull().all(axis=1)) 0 False 1 True 2 True 3 True 4 False dtype: bool  If test one or more columns in list: variableToPredict = ['Survive', 'another column'] print (type(df[variableToPredict])) <class 'pandas.core.frame.DataFrame'> print (df[variableToPredict]) Survive another column 0 NaN NaN 1 A NaN 2 B a 3 B b 4 NaN b   print (df[variableToPredict].notnull()) Survive another column 0 False False 1 True False 2 True True 3 True True 4 False True #at least one NaN per row, at least one True print (df[variableToPredict].notnull().any(axis=1)) 0 False 1 True 2 True 3 True 4 True dtype: bool #all NaNs per row, all Trues print (df[variableToPredict].notnull().all(axis=1)) 0 False 1 False 2 True 3 True 4 False dtype: bool
Question: I'm running the process of feature selection on classification problem, using the embedded method (L1 - Lasso) With LogisticRegression. I'm running the following code: from sklearn.linear_model import Lasso, LogisticRegression from sklearn.feature_selection import SelectFromModel # using logistic regression with penalty l1. selection = SelectFromModel(LogisticRegression(C=1, penalty='l1')) selection.fit(x_train, y_train)  But I'm getting exception (on the fit command):  selection.fit(x_train, y_train) File "C:\Python37\lib\site-packages\sklearn\feature_selection\_from_model.py", line 222, in fit self.estimator_.fit(X, y, **fit_params) File "C:\Python37\lib\site-packages\sklearn\linear_model\_logistic.py", line 1488, in fit solver = _check_solver(self.solver, self.penalty, self.dual) File "C:\Python37\lib\site-packages\sklearn\linear_model\_logistic.py", line 445, in _check_solver "got %s penalty." % (solver, penalty)) ValueError: Solver lbfgs supports only 'l2' or 'none' penalties, got l1 penalty.  I'm running under python 3.7.6 and sscikit-learn version is 0.22.2.post1 What is wrong and how can I fix it ?

Answer: This is cleared up in the documentation.  solver : {‘newton-cg’, ‘lbfgs’, ‘liblinear’, ‘sag’, ‘saga’}, default=’lbfgs’ ...  ‘newton-cg’, ‘lbfgs’, ‘sag’ and ‘saga’ handle L2 or no penalty  ‘liblinear’ and ‘saga’ also handle L1 penalty    Call it like this: LogisticRegression(C=1, penalty='l1', solver='liblinear')
Question: I opened a number of tabs at the same time. I think that's why Google Colab was not able to support the heavy load. The message stated:  Save failed This file could not be saved. Please use the File menu to download the .ipynb and upload the notebook to make a copy that includes your recent changes.  Is downloading the file and uploading again the only solution?

Answer: This can happen if you open the same notebook in multiple tabs and make incompatible edits to the notebook. At this point, the only way to save your work is to follow the advice in the dialog. To prevent this in the future, avoid simultaneously editing the same notebook in multiple browser windows.
Question: Im generating a random sample of data and plotting its pdf using scipy.stats.norm.fit to generate my loc and scale parameters. I wanted to see how different my pdf would look like if I just calculated the mean and std using numpy without any actual fitting. To my surprise when I plot both pdfs and print both sets of mu and std the results I get are exactly the same. So my question is, what is the point of norm.fit if I can just calculate the mean and std of my sample and still get the same results? This is my code: import numpy as np from scipy.stats import norm import matplotlib.pyplot as plt data = norm.rvs(loc=0,scale=1,size=200) mu1 = np.mean(data) std1 = np.std(data) print(mu1) print(std1) mu, std = norm.fit(data) plt.hist(data, bins=25, density=True, alpha=0.6, color='g') xmin, xmax = plt.xlim() x = np.linspace(xmin, xmax, 100) p = norm.pdf(x, mu, std) q = norm.pdf(x, mu1, std1) plt.plot(x, p, 'k', linewidth=2) plt.plot(x, q, 'r', linewidth=1) title = "Fit results: mu = %.5f, std = %.5f" % (mu, std) plt.title(title) plt.show()  And this is the results I got: Pdf of a random set of values mu1 = 0.034824979915482716 std1 = 0.9945453455908072

Answer: The point is that there are several other distributions out there besides the normal distribution. Scipy provides a consistent API for learning the parameters of these distributions from data. (Want an exponential distribution instead of a normal distribution? It’s scipy.stats.expon.fit.) So sure, your way also works because the parameters of the normal distribution happen to be the mean and standard deviation. But this is about providing a consistent interface across distributions, including ones where that’s not true.
Question: I have to write a function that takes a string, and will return the string with added "asteriks" or "*" symbols to signal multiplication. As we know 4(3) is another way to show multiplication, as well as 4*3 or (4)(3) or 4*(3) etc. Anyway, my code needs to fix that problem by adding an asterik between the 4 and the 3 for when multiplication is shown WITH PARENTHESIS but without the multiplication operator " * ". Some examples:  "4(3)" -> "4*(3)" "(4)(3)" -> "(4)*(3)" "4*2 + 9 -4(-3)" - > "4*2 + 9 -4*(-3)" "(-9)(-2) (4)" -> "(-9)*(2) *(4)" "4^(3)" -> "4^(3)" "(4-3)(4+2)" -> "(4-3)*(4+2)" "(Aflkdsjalkb)(g)" -> "(Aflkdsjalkb)*(g)" "g(d)(f)" -> "g*(d)*(f)"  "(4) (3)" -> "(4)*(3)"  I'm not exactly sure how to do this, I am thinking about finding the left parenthesis and then simply adding a " * " at that location but that wouldn't work hence the start of my third example would output "* (-9)" which is what I don't want or my fourth example that would output "4^*(3)". Any ideas on how to solve this problem? Thank you. Here's something I've tried, and obviously it doesn't work: while index < len(stringtobeconverted) parenthesis = stringtobeconverted[index] if parenthesis == "(": stringtobeconverted[index-1] = "*" 

Answer: I'll share mine. def insertAsteriks(string): lstring = list(string) c = False for i in range(1, len(lstring)): if c: c = False pass elif lstring[i] == '(' and (lstring[i - 1] == ')' or lstring[i - 1].isdigit() or lstring[i - 1].isalpha() or (lstring[i - 1] == ' ' and not lstring[i - 2] in "*^-+/")): lstring.insert(i, '*') c = True return ''.join(lstring)  Let's check against your inputs. print(insertAsteriks("4(3)")) print(insertAsteriks("(4)(3)")) print(insertAsteriks("4*2 + 9 -4(-3)")) print(insertAsteriks("(-9)(-2) (4)")) print(insertAsteriks("(4)^(-3)")) print(insertAsteriks("ABC(DEF)")) print(insertAsteriks("g(d)(f)")) print(insertAsteriks("(g)-(d)"))  The output is: 4*(3) (4)*(3) 4*2 + 9 -4*(-3) (-9)*(-2) (4) (4)^(-3) ABC*(DEF) g*(d)*(f) (g)-(d) [Finished in 0.0s]
Question: The body of an HTTP PUT request is a JSON list - like this: [item1, item2, item3, ...]  I can't change this. (If the root was a JSON object rather than a list there would be no problem.) Using FastAPI I seem to be unable to access this content in the normal way: @router.put('/data') def set_data(data: DataModel): # This doesn't work; how do I even declare DataModel?   I found the following workaround, which seems like a very ugly hack: class DataModel(BaseModel): __root__: List[str] from fastAPI import Request @router.put('/data') async def set_data(request: Request): # Get the request object directly data = DataModel(__root__=await request.json())  This surely can't be the 'approved' way to achieve this. I've scoured the documentation both of FastAPI and pydantic. What am I missing?

Answer: Descending from the model perspective to primitives In FastAPI, you derive from BaseModel to describe the data models you send and receive (i.e. FastAPI also parses for you from a body and translates to Python objects). Also, it relies on the modeling and processing from pydantic. from typing import List from pydantic import BaseModel class Item(BaseModel): name: str class ItemList(BaseModel): items: List[Item] def process_item_list(items: ItemList): pass  This example would be able to parse JSON like {"items": [{"name": "John"}, {"name": "Mary"}]}  In your case - depending on what shape your list entries have - you'd also go for proper type modeling, but you want to directly receive and process the list without the JSON dict wrapper around it. You could go for: from typing import List from pydantic import BaseModel class Item(BaseModel): name: str def process_item_list(items: List[Item]): pass  Which is now able to process JSON like: [{"name": "John"}, {"name": "Mary"}]  This is probably what you're looking for and the last adaption to take is depending on the shape of your item* in the list you receive. If it's plain strings, you can also go for: from typing import List def process_item_list(items: List[str]): pass  Which could process JSON like ["John", "Mary"]  I outlined the path from models down to primitives in lists because I think it's worth knowing where this can go if one needs more complexity in the data models.
Question: I have a list: my_list = ['a', 'b']  and a pandas dataframe: d = {'a': [1, 2], 'b': [3, 4], 'c': [1, 2], 'd': [3, 4]} df = pd.DataFrame(data=d)  What can I do to remove the columns in df based on list my_list, in this case remove columns a and b

Answer: This is very simple: df = df.drop(columns=my_list)  drop removes columns by specifying a list of column names
Question: I have a csv file that has 25000 rows. I want to put the average of every 30 rows in another csv file. I've given an example with 9 rows as below and the new csv file has 3 rows (3, 1, 2): | H | ======== | 1 |---\ | 3 | |--->| 3 | | 5 |---/ | -1 |---\ | 3 | |--->| 1 | | 1 |---/ | 0 |---\ | 5 | |--->| 2 | | 1 |---/  What I did: import numpy as np import pandas as pd m_path = "file.csv" m_df = pd.read_csv(m_path, usecols=['Col-01']) m_arr = np.array([]) temp = m_df.to_numpy() step = 30 for i in range(1, 25000, step): arr = np.append(m_arr,np.array([np.average(temp[i:i + step])])) data = np.array(m_arr)[np.newaxis] m_df = pd.DataFrame({'Column1': data[0, :]}) m_df.to_csv('AVG.csv')  This works well but Is there any other option to do this?

Answer: You can use integer division by step for consecutive groups and pass to groupby for aggregate mean: step = 30 m_df = pd.read_csv(m_path, usecols=['Col-01']) df = m_df.groupby(m_df.index // step).mean()  Or: df = m_df.groupby(np.arange(len(dfm_df// step).mean()  Sample data: step = 3 df = m_df.groupby(m_df.index // step).mean() print (df) H 0 3 1 1 2 2
Question: My Python package has optional features (extras_require) and I would prefer them to be selected by default. More specifically, I'd like that pip install mypackage behaves like pip install mypackage[extra] and that I can install a minimal version with something like pip install mypackage[core]. setup( name="mypackage", ... extras_require={ "extra": ["extra1>=1.2", "extra2"], "core": [], } )  Is it possible to achieve this with a setup script similar to above?

Answer: Unfortunately this is not possible with the current state of Python packaging metadata & tooling. See a long discussion here as to why.
Question: I used pyenv to install python 3.8.2 and to create a virtualenv. In the virtualenv, I used pipenv to install pandas. However, when importing pandas, I'm getting the following:  [...] File "/home/luislhl/.pyenv/versions/poc-prefect/lib/python3.8/site-packages/pandas/io/common.py", line 3, in <module> import bz2 File "/home/luislhl/.pyenv/versions/3.8.2/lib/python3.8/bz2.py", line 19, in <module> from _bz2 import BZ2Compressor, BZ2Decompressor ModuleNotFoundError: No module named '_bz2'  After some googling, I found out some people suggesting I rebuild Python from source after installing bzip2 library in my system. However, after trying installing it with sudo dnf install bzip2-devel I see that I already had it installed. As far as I know, pyenv builds python from source when installing some version. So, why wasn't it capable of including the bzip2 module when building? How can I manage to rebuild Python using pyenv in order to make bzip2 available? I'm in Fedora 30 Thanks in advance UPDATE I tried installing another version of python with pyenv in verbose mode, to see the compilation output. There is this message in the end of the compilation: WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?  But as I stated before, I checked I already have bzip2 installed in my system. So I don't know what to do.

Answer: On Ubuntu 22 LTS Missing Library Problem in Python Installation with Pyenv Before the fix: $> pyenv install 3.11.0  command result: pyenv: /home/user/.pyenv/versions/3.11.0 already exists continue with installation? (y/N) y Downloading Python-3.11.0.tar.xz... -> https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tar.xz Installing Python-3.11.0... WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib? WARNING: The Python readline extension was not compiled. Missing the GNU readline lib? WARNING: The Python lzma extension was not compiled. Missing the lzma lib?  TLDR; Recipe to fix: sudo apt-get install build-essential zlib1g-dev libffi-dev libssl-dev libbz2-dev libreadline-dev libsqlite3-dev liblzma-dev libncurses-dev tk-dev  Result After the fix: $> pyenv install 3.11.0  Command result: pyenv: /home/user/.pyenv/versions/3.11.0 already exists continue with installation? (y/N) y Downloading Python-3.11.0.tar.xz... -> https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tar.xz Installing Python-3.11.0... Installed Python-3.11.0 to /home/user/.pyenv/versions/3.11.0
Question: I am using Paramiko for sshing from Python script. My ssh command is listed below: ssh -A -o strictHostKeyChecking=no <hostname>  I need same Paramiko code for Python. 

Answer: In Paramiko, an equivalent of OpenSSH StrictHostKeyChecking=no is the default behaviour of MissingHostKeyPolicy, which implements missing_host_key to simply do nothing. client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy()) client.connect(hostname, ...)   Though you should not do this (and neither StrictHostKeyChecking=no). You are losing a protection against Man-in-the-middle attacks this way. For correct solution, see Paramiko "Unknown Server".
Question: I installed poetry, however I'm getting the following error when attempting to call poetry zsh: command not found: poetry  I know I have it installed because I get the following output when trying to run the following install script $ curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python Retrieving Poetry metadata Latest version already installed. 

Answer: The default install location is ~/.poetry/bin/poetry I added the following to my .zshrc export PATH="$HOME/.local/bin:$PATH"
Question: I am forced to download python packages from local mirror PyPi repository. I do this by using the -i and --trusted-host options. Whole installation command looks like this: pip install -i https://sampleurl.com/pypi-remote/simple --trusted-host sample.host.com package_name  Having to type in that options each time is kinda annoying though (in reality those are long URL's). I've tried to create get_package.bat file (I'm working on Windows 10) with following content: pip install -i https://sampleurl.com/pypi-remote/simple --trusted-host sample.host.com "%1"  It works perfectly fine, although when I wanted to execute pip search command, it turned out to be useless since it has hard-coded install command and there is no way to use it with search. Is there any way in which I can setup pip to download from mirror repository by default, so that I can execute pip install package_name or pip search package_name without any additional options? Eventually I could try making .bat file that would take 2 parameters like this: pip %1 -i https://sampleurl.com/pypi-remote/simple --trusted-host sample.host.com "%2"  But I wonder if there's more "elegant" way to do this.

Answer: using pip config, on user or global level. I have /etc/pip.conf configured like this: [global] index=https://my-company/nexus/repository/pypi-group/pypi index-url=https://my-company/nexus/repository/pypi-group/simple trusted-host=my-company  but you can configure this using pip config on user or global level, something like: pip config --user set global.index https://my-company/nexus/repository/pypi-group/pypi pip config --user set global.index-url https://my-company/nexus/repository/pypi-group/simple pip config --user set global.trusted-host my-company  #NOTES  --index-url is used by pip install --index is used by pip search