eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I have a geopandas dataframe that has several rows with overlapping polygon geometries along with an index (unique and sequential). I want to merge the overlapping polygon geometries into a multi-polygon and keep the corresponding minimum index of the individual overlapping polygons. For example: The geodataframe is as follows: original geodataframe Lets say the polygon geometries with index 10233, 10235, 10238 overlap. I want a single row for this with these geometries merged in a multi-polygon (instead of 3 separate geometries) and the corresponding index should be the minimum index of the 3 rows that is 10233. I would like to do this for the entire geodataframe I tried using the dissolve function from geopandas: gdf = gdf.dissolve(by = 'index').reset_index() This does not do anything since 'index' is unique. I also tried: gdf = gdf.dissolve().reset_index() However, this combines all geometries into single row of multi-polygons

Answer: I think this is what you had in mind:  import geopandas as gpd # load your geodataframe .. # self join on geodataframe to get all polygon intersections intersects = gdf.sjoin(gdf, how="left", predicate="intersects") # dissolve intersections on right index indices using the minimum value intersects_diss = intersects.dissolve("id_right",aggfunc="min") # dissolve again on left index using minimum intersects_diss = intersects_diss.reset_index().dissolve("id_left",aggfunc="min")
Question: I have following install comand for a package: pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116  and would need to add this to my requirements.txt file. I have done it by adding this to the end of the file: -i https://download.pytorch.org/whl/cu116 torch torchvision torchaudio  after all the other requirements. I do not have the means to test it right now, and would need to know if I have done it correctly as I never did it before. Is this the proper way of adding it to the requirements.txt file? If yes, adding other packages after these, for example numpy, will it be affected by the url? do I have to sort of "clear" the url, or is it simply added to the list of urls it searches in? Any help appreciated

Answer: Found the solution, the command you used and the requirements.txt file were NOT in fact the same. It works with a requirements.txt like this --extra-index-url https://download.pytorch.org/whl/cu116 torch torchvision torchaudio  Turns out -i is not the same as --extra-index-url. Docs: https://pip.pypa.io/en/stable/reference/requirements-file-format/
Question: I keep getting: W503 line break before binary operator  Please help me fix my code, as I can't figure out what is wrong here: def check_actionable(self, user_name, op, changes_table): return any(user_name in row.inner_text() and row.query_selector( self.OPS[op]) is not None and row.query_selector(self.DISABLED) is not None for row in changes_table) 

Answer: the other (imo better) alternative to ignore (which resets the default ignore list) is to use extend-ignore by default both W503 and W504 are ignored (as they conflict and have flip-flopped historically). there are other rules which are ignored by default as well that you may want to preserve extend-ignore = ABC123  ignore on the other hand resets the ignore list removing the defaults  disclaimer: I'm the current flake8 maintainer
Question: I am using the following function pairwise to get the iteration of ordered pairs. For example, if the iterable is a list [1, 2, 3, 4, 5, 6] then I want to get (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 1). If I use the following function from itertools import tee, zip_longest def pairwise(iterable): "s -> (s0,s1), (s1,s2), (s2, s3), ..." a, b = tee(iterable) next(b, None) return zip_longest(a, b)  then it returns (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, None). I am using dataloader in my code as iterable, so I want to pass only iterable as input to the function pairwise and I don't want to pass extra inputs. How do I get the first element as the last element in the last item as mentioned above?

Answer: zip_longest has fillvalue parameter return zip_longest(a, b, fillvalue=iterable[0])  or as suggested in the comments use the returned value of the next(b, None) in fillvalue def pairwise(iterable): a, b = tee(iterable) return zip_longest(a, b, fillvalue=next(b, None))  Output print(list(pairwise(lst))) # [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 1)]  You can also do it without converting the list to iterators def pairwise(iterable): return zip_longest(iterable, iterable[1:], fillvalue=iterable[0])
Question: I have a column in my pandas dataframe: df = pd.DataFrame([[3000000, 2500000, 1800000, 800000, 500000]], columns=['Market value'])  I want to convert the numbers in this column to a format with millions and hundred thousands, for example:  3000000 -> €3M 2500000 -> €2.5M 1800000 -> €1.8M 800000 -> €800K 500000 -> €500K  This is my attempt so far: df['Market Value'] = np.select(condlist = [(df['Market value']/1000) >= 1000], choicelist = ['€'+(df['Market value'].astype(float)/1000000).astype(int).astype(str) + 'M'], default = '€'+(df['Market value'].astype(float)/1000).astype(int).astype(str) + 'K')  This produces the output:  3000000 -> €3M 2500000 -> €2M * this needs to be €2.5M 1800000 -> €1M * this needs to be €1.8M 800000 -> €800K 500000 -> €500K 

Answer: You can apply this function to the column: def format(num): if num > 1000000: if not num % 1000000: return f'€{num // 1000000}M' return f'€{round(num / 1000000, 1)}M' return f'€{num // 1000}K'  Testing: nums_list = [3000000, 2500000, 1800000, 800000, 500000] for num in nums_list: print(format(num))  Output: €3M €2.5M €1.8M €800K €500K
Question: I'm trying to install Pyenv, and I'm running on Ubuntu 22.04 LTS. but whenever I run this command sudo apt install -y make build-essential libssl-dev zlib1g-dev \ libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev \ libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl \ git  I get this error Unable to locate package python-openssl  I've tried searching for solutions online, but I think they have encountered it on older versions of Ubuntu and not on the latest version.

Answer: Make sure your list of packages is updated (sudo apt update). Python openssl bindings are available in 22.04 in python3-openssl (link), so you can install it by running sudo apt install python3-openssl
Question: I have a python dataclass that looks something like: @dataclass class MyDataClass: field0: int = 0 field1: int = 0 # --- Some other attribute that shouldn't be considered as _fields_ of the class attr0: int = 0 attr1: int = 0  I'd like to write the class in such a way that, when calling dataclasses.fields(my_data:=MyDataClass()), only field0 and field1 are reported. As a workaround, I've splitted the class in two inheriting classes, as: @dataclass class MyData: field0: int = 0 field1: int = 0 class MyDataClass(MyData): # --- Some other attribute that shouldn't be considered as _fields_ of the class attr0: int = 0 attr1: int = 0  It works, but I don't know if it's the right way (some drawback I'm not considering?) or if there is a more straightforward way to do it

Answer: Not the most elegant solution, but you may declare the non-field attributes as InitVar[int] and set them in a __post_init__() method. @dataclass class MyDataClass: field0: int = 0 field1: int = 0 # --- Some other attribute that shouldn't be considered as _fields_ of the class attr0: InitVar[int] = 0 attr1: InitVar[int] = 0 def __post_init__(self, attr0, attr1): self.attr0 = attr0 self.attr1 = attr1
Question: TL;DR running flask --app from the command line results in Error: No such option: --app. The current version of Anaconda (at the time of this question) includes Flask version 1.x. Here is the code from the Flask hello world tutorial at: palletsprojects.com quickstart from flask import Flask app = Flask(__name__) @app.route("/") def hello_world(): return "<p>Hello, World!</p>"  and running it with: flask --app hello run results in the error,  Error: No such option: --app  How can I run this with the "--app" option?

Answer: Display your flask version with flask --version. Using the defaults from the anaconda package manager condata update flask won't update flask to 2.x. --app works in >=2.2.x. In my case, at the time of this post, conda search flask did not show anything past version 2.1.3. To get this version or the latest available,  navigate to https://anaconda.org/search?q=flask select the appropriate result for your OS. Use one of the example provided and add your desired version to the end e.g. conda install -c conda-forge flask=2.2.2 Run flask --app and you should get: Error: Option '--app' requires an argument.  Note: If you're seeing an Exception: HTTPConnectionPool(..., make sure you are running these command from an anaconda terminal.
Question: I'm making an interactive map, there is no problem with the map itself, there is a problem with the way the elements are located in the additional hover marks. Is there any way to change this order? Is it possible not to display latitude and longitude indicators there? Sample code: @st.cache(hash_funcs={dict: lambda _: None}) def my_stat_map(df_region_map): fig_map = px.scatter_mapbox(df_region_map, hover_name='Region name', hover_data=['Confirmed', 'Deaths', 'Recovered', 'Daily confirmed', 'Daily deaths', 'Daily recovered'], lat='Latitude dd', lon='Longitude dd', size='Confirmed', color='Confirmed', color_continuous_scale='Sunsetdark', zoom=3, size_max=45, opacity=0.8, height=600) fig_map.update_layout(mapbox_style="carto-positron", showlegend=True) fig_map.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0}) dict_map = {'map_key': fig_map} return dict_map  What do I get:  How to change the order in this output? I would like to remove the latitude and longitude, or at least move them to the end of the output.

Answer: This is covered here: https://plotly.com/python/hover-text-and-formatting/#disabling-or-customizing-hover-of-columns-in-plotly-express  fig_map = px.scatter_mapbox( df_region_map, hover_name="Region name", hover_data={ "Confirmed":True, "Deaths":True, "Recovered":True, "Daily confirmed":True, "Daily deaths":True, "Daily recovered":True, "Latitude dd":False, "Longitude dd":False }, lat="Latitude dd", lon="Longitude dd", size="Confirmed", color="Confirmed", color_continuous_scale="Sunsetdark", zoom=3, size_max=45, opacity=0.8, height=600, ) fig_map.update_layout(mapbox_style="carto-positron", showlegend=True) fig_map.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0}) fig_map  create data frame import plotly.express as px import pandas as pd import requests df_ = pd.read_csv( "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/latest/owid-covid-latest.csv" ) # alpha3 df_geo = pd.json_normalize( requests.get( "https://raw.githubusercontent.com/eesur/country-codes-lat-long/master/country-codes-lat-long-alpha3.json" ).json()["ref_country_codes"] ).rename(columns={"latitude": "Latitude dd", "longitude": "Longitude dd"}) df = df_.loc[ :, [ "iso_code", "location", "total_cases", "total_deaths", "total_tests", "new_cases", "new_deaths", "new_tests", ], ].rename( columns={ "location": "Region name", "total_cases": "Confirmed", "total_deaths": "Deaths", "total_tests": "Recovered", "new_cases": "Daily confirmed", "new_deaths": "Daily deaths", "new_tests": "Daily recovered", } ) df_region_map = pd.merge(df, df_geo, left_on="iso_code", right_on="alpha3").dropna(subset="Confirmed")
Question: I am trying to understand what are the good practices when using inheritance with dataclasses. Let's say I want an "abstract" parent class containing a set of variables and methods, and then a series of child classes that inherit these methods and the variables, where in each of them the variables have a different default value. from dataclasses import dataclass @dataclass class ParentClass: a_variable: str def a_function(self) -> None: print("I am a class") # ONE @dataclass class DataclassChild1(ParentClass): a_variable: str = "DataclassChild" # TWO @dataclass class DataclassChild2(ParentClass): def __init__(self) -> None: super().__init__(a_variable="Child") # THREE class ClassChild(ParentClass): def __init__(self) -> None: super().__init__(a_variable="Child")  What would be the correct way to implement this (one/two/three), if any? Or is it an overkill and it would be best to just use different instances of ParentClass, passing different values to the constructor? I think I should use the @dataclass decorator also for the child classes, but if I check the type of the child class, that seems to be a dataclass even if I don't use it. Plus, I feel like overwriting __init__ defeats the purpose of using a dataclass in the first place, but on the other hand the standard dataclass syntax seems useless because it would mean having to rewrite all the variables in the child classes (a_variable: str = "DataclassChild").

Answer: I would argue that #1 is the most correct method. For the example you showed, it appears to be irrelevant which method you use, but if you add a second variable, the differences become apparent. This is implicitly confirmed by the Inheritance section in the documentation. @dataclass class ParentClass: a: str b: str = "parent-b" # This works smoothly @dataclass class ChildClass1(ParentClass): a: str = "child-a" # This works, but is a maintenance nightmare @dataclass class ChildClass2(ParentClass): def __init__(self, a="child-a", b="parent-b"): super().__init__(a, b) # This works, but it changes the signature and only works if a is first @dataclass class ChildClass3(ParentClass): def __init__(self, a="child-a", **kwargs): super().__init__(a, **kwargs)  Right now, the dataclass decorator is adding default methods, including __init__ to your class. That means that if you wanted to use option #2 or #3, you would have to know and copy the function signature for all the parameters. At the same time, option #1 allows you to change the default for just a. The other way to do what you're doing is to create a __post_init__ method for your child classes, which can then override the parent default value: @dataclass class ParentClass: a: str = '' # Or pick some other universally acceptable marker @dataclass class ChildClass(ParentClass): def __post_init__(self): if self.a == '': self.a = "child-a"  This is also needlessly complex for most scenarios, but may be useful for a more complex situation. Normally __post_init__ is meant to be used to initialize derived fields, as in the example in the linked documentation.
Question: Context: I'd like to add a new multi-index/row on top of the columns. For example if I have this dataframe: tt = pd.DataFrame({'A':[1,2,3],'B':[4,5,6],'C':[7,8,9]})  Desired Output: How could I make it so that I can add "Table X" on top of the columns A,B, and C?  Table X A B C 0 1 4 7 1 2 5 8 2 3 6 9  Possible solutions(?): I was thinking about transposing the dataframe, adding the multi-index, and transpose it back again, but not sure how to do that without having to write the dataframe columns manually (I've checked other SO posts about this as well) Thank you!

Answer: If you want a data frame like you wrote, you need a Multiindex data frame, try this: import pandas as pd # you need a nested dict first dict_nested = {'Table X': {'A':[1,2,3],'B':[4,5,6],'C':[7,8,9]}} # then you have to reform it reformed_dict = {} for outer_key, inner_dict in dict_nested.items(): for inner_key, values in inner_dict.items(): reformed_dict[(outer_key, inner_key)] = values # last but not least convert it to a multiindex dataframe multiindex_df = pd.DataFrame(reformed_dict) print(multiIndex_df) # >> Table X # >> A B C # >> 0 1 4 7 # >> 1 2 5 8 # >> 2 3 6 9
Question: I'm trying to understand typing.overload and have applied it in a simple case where I want a function that takes input x: Literal["foo", "bar"] and returns the list [x]. I'd like mypy to type the resulting list as list[Literal["foo"]] or list[Literal["bar"]] depending on the value of x. I know I could achieve this with a TypeVar, but I'd still like to understand why the code below fails with the following error: test.py:14: error: Overloaded function implementation cannot produce return type of signature 1 test.py:14: error: Overloaded function implementation cannot produce return type of signature 2  from typing import Literal, overload @overload def f(x: Literal["foo"]) -> list[Literal["foo"]]: ... @overload def f(x: Literal["bar"]) -> list[Literal["bar"]]: ... def f(x: Literal["foo", "bar"]) -> list[Literal["foo", "bar"]]: return [x] 

Answer: Lists in Python are invariant. That means that, even if B is a subtype of A, there is no relation between the types list[A] and list[B]. If list[B] were allowed to be a subtype of list[A], then someone could come along and do this. my_b_list: list[B] = [] my_a_list: list[A] = my_b_list my_a_list.append(A()) print(my_b_list) # Oh no, a list[B] contains an A value!  If you plan to modify the returned list, then what you're doing isn't safe. End of story. If you plan to treat the list as immutable, then consider what operations you actually need, and you may be able to find a covariant supertype of list in typing. For example, Sequence is a popular choice. It supports iteration, random access, and length access, while explicitly not allowing mutation. from typing import Literal, overload, Sequence @overload def f(x: Literal["foo"]) -> Sequence[Literal["foo"]]: ... @overload def f(x: Literal["bar"]) -> Sequence[Literal["bar"]]: ... def f(x: Literal["foo", "bar"]) -> Sequence[Literal["foo", "bar"]]: return [x]  (Note: typing.Sequence is deprecated in Python 3.9; if you only plan to support 3.9+, you might use collections.abc.Sequence instead)
Question: I'm trying to count the number of instances of a certain sting in a row in a pandas dataframe. In the example here I utilized a lambda function and pandas .count() to try and count the number of times 'True' exists in each row. Though instead of a count of 'True' it is just returning a boolean whether or not it exists in the row... #create dataframe d = {'Period': [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4], 'Result': ['True','None','False','True','False','True','False','True','True','False','False','True','False','True','False','False'], 'Result1': ['True','None','False','True','False','True','False','True','True','False','False','True','False','True','False','False'], 'Result2': ['True','None','False','True','False','True','False','True','True','False','False','True','False','True','False','False']} df = pd.DataFrame(data=d) #count instances of Trus or False in each row df['Count'] = df.apply(lambda row: row.astype(str).str.count('True').any(), axis=1) print(df)  The desired outcome is: Period Result Result1 Result2 Count 1 True True True 3 2 None None None 0 3 False False False 0 4 True True True 3 1 False False False 0 2 True True True 3 3 False False False 0 ... ... ... ... ...... 

Answer: You can use np.where: df['count'] = np.where(df == 'True', 1, 0).sum(axis=1)  Regarding why your apply returns a boolean: both any and all returns boolean, not numbers Edit: You can include df.isin for multiple conditions: df['count'] = np.where(df.isin(['True', 'False']), 1, 0).sum(axis=1)
Question: I am trying to deploy the Django application in AWS ubuntu os with Django channels, Using Nginx. I had configured the Django server in Nginx. But I don't know how to configure channels or Redis-server in Nginx.  My nginx config is as follows: server { listen 80; server_name 52.77.215.218; location / { include proxy_params; proxy_pass http://localhost:8000/ } }  My settings.py: CHANNEL_LAYERS = { "default": { "BACKEND": "channels_redis.core.RedisChannelLayer", "CONFIG": { "hosts": [("127.0.0.1", 6379)], }, }, }  My requirements.txt: aioredis==1.3.1 asgiref==3.5.2 async-timeout==4.0.2 attrs==22.1.0 autobahn==22.7.1 Automat==20.2.0 certifi==2022.6.15 cffi==1.15.1 channels==3.0.5 channels-redis==2.4.2 charset-normalizer==2.1.1 constantly==15.1.0 coreapi==2.3.3 coreschema==0.0.4 cryptography==37.0.4 daphne==3.0.2 defusedxml==0.7.1 Django==4.1 django-cors-headers==3.13.0 django-templated-mail==1.1.1 djangorestframework==3.13.1 djangorestframework-simplejwt==4.8.0 djoser==2.1.0 gunicorn==20.1.0 hiredis==2.0.0 hyperlink==21.0.0 idna==3.3 incremental==21.3.0 itypes==1.2.0 Jinja2==3.1.2 MarkupSafe==2.1.1 msgpack==0.6.2 mysql==0.0.3 mysqlclient==2.1.1 oauthlib==3.2.0 pyasn1==0.4.8 pyasn1-modules==0.2.8 pycparser==2.21 PyJWT==2.4.0 pyOpenSSL==22.0.0 python3-openid==3.2.0 pytz==2022.2.1 requests==2.28.1 requests-oauthlib==1.3.1 service-identity==21.1.0 six==1.16.0 social-auth-app-django==4.0.0 social-auth-core==4.3.0 sqlparse==0.4.2 Twisted==22.4.0 twisted-iocpsupport==1.0.2 txaio==22.2.1 typing_extensions==4.3.0 tzdata==2022.2 uritemplate==4.1.1 urllib3==1.26.12 zope.interface==5.4.0  When I run server with python3 manage.py runserver 0.0.0.0:8000 Server running good and also connecting with redis-server but when I run server with gunicorn app.wsgi -b 0.0.0.0:800 then failed to connect with webbsocket.  I also tried Hostinger VPS as well, but Same Issue.

Answer: You will need to download Daphne. Daphne is a high-performance websocket server for Django channels. https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/daphne/ https://github.com/django/daphne https://channels.readthedocs.io/en/stable/deploying.html How you can run daphne: daphne -b 0.0.0.0 -p 8070 django_project.asgi:application Here is my Nginx conf for channels: upstream django { server 127.0.0.1:8080; } upstream websockets{ server 127.0.0.1:8070; } server { ... ... location /ws { proxy_pass http://websockets; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; ... } location / { proxy_pass http://django; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; proxy_set_header X-Real-IP $remote_addr; ... } }
Question: The Python docs for the enum module contains the following example of subclassing Enum. The resulting class can be used to create enums that also validate that they have no two members with the same value. >>> class DuplicateFreeEnum(Enum): ... def __init__(self, *args): ... cls = self.__class__ ... if any(self.value == e.value for e in cls): ... a = self.name ... e = cls(self.value).name ... raise ValueError( ... "aliases not allowed in DuplicateFreeEnum: %r --> %r" ... % (a, e))  However, as a method of adding validation, this method is inelegant and restrictive. __init__ is called once for each member, whereas in order to validate an enum as a whole, it makes more sense to look at every member of the enum together. For instance, how would I validate that an enum has precisely two members, as below? class PreciselyTwoEnum(Enum): ... # ??? class Allowed(PreciselyTwoEnum): FOO = 1 BAR = 2 class Disallowed(PreciselyTwoEnum): # Should raise an error BAZ = 3  Can this be accomplished with a clever implementation of __init__? Is there another method that could be used — perhaps one that is called on the enum after it has been fully created?

Answer: __init_subclass__ is what you are looking for1: class PreciselyTwoEnum(Enum): def __init_subclass__(cls): if len(cls.__members__) != 2: raise TypeError("only two members allowed")  and in use: >>> class Allowed(PreciselyTwoEnum): ... FOO = 1 ... BAR = 2 ... >>> class Disallowed(PreciselyTwoEnum): # Should raise an error ... BAZ = 3 ... Traceback (most recent call last): ... TypeError: only two members allowed   [1] __init_subclass__ for Enum only works correctly in Python 3.11 or later, or by using the external aenum library v3.0 or later.2. [2] Disclosure: I am the author of the Python stdlib Enum, the enum34 backport, and the Advanced Enumeration (aenum) library.