eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I have the following dataframe import pandas as pd test = pd.DataFrame({'y':[1,2,3,4,5,6], 'label': ['bottom', 'top','bottom', 'top','bottom', 'top']}) y label 0 1 bottom 1 2 top 2 3 bottom 3 4 top 4 5 bottom 5 6 top  I would like to add a new column, agg_y, which would be the the max(y) if label=="bottom" and min(y) if label=="top". I have tried this test['min_y'] = test.groupby('label').y.transform('min') test['max_y'] = test.groupby('label').y.transform('max') test['agg_y'] = np.where(test.label == "bottom", test.max_y, test.min_y) test.drop(columns=['min_y', 'max_y'], inplace=True)  which gives the correct result y label agg_y 0 1 bottom 5 1 2 top 2 2 3 bottom 5 3 4 top 2 4 5 bottom 5 5 6 top 2  I am just looking fora one-liner solution, if possible

Answer: Your solution in one line solution is: test['agg_y'] = np.where(test.label == "bottom", test.groupby('label').y.transform('max'), test.groupby('label').y.transform('min'))  Solution without groupby, thank you @ouroboros1: test['agg_y'] = np.where(test.label == 'bottom', test.loc[test.label.eq('bottom'), 'y'].max(), test.loc[test.label.ne('bottom'), 'y'].min())  Another idea is mapping values, idea is similar like ouroboros1 solution: d = {'bottom':'max', 'top':'min'} test['agg_y'] = test['label'].map({val:test.loc[test.label.eq(val),'y'].agg(func) for val, func in d.items()}) print (test) y label agg_y 0 1 bottom 5 1 2 top 2 2 3 bottom 5 3 4 top 2 4 5 bottom 5 5 6 top 2
Question: I have data that looks like this [[{'title': 'Line'}], [{'title': 'asd'}]]. I want to add a new key and value for every list inside of lists. I have tried this but I'm having an error 'list' object is not a mapping. Any suggestion? data = [[{'title': 'Line'}], [{'title': 'asd'}]] titleID = [{'id': 373}, {'id': 374}] combine = [{**dict_1, **dict_2} for dict_1, dict_2 in zip(char_id, data )]  the output I want is like this: [[{'id': 373, 'title': 'Line'}], [{'id': 374, 'title': 'asd'}]] 

Answer: Try this list comphrehension and unpacking data = [[{'title': 'Line'}], [{'title': 'asd'}]] titleID = [{'id': 373}, {'id': 374}] [[{**i[0], **j}] for i,j in zip(data, titleID)]  Output [[{'title': 'Line', 'id': 373}], [{'title': 'asd', 'id': 374}]]
Question: In my Django project I am trying to make the http://127.0.0.1:8000/ which is the home page to redirect to the Login in Page if user is not logged in however there is a user who is logged in I want http://127.0.0.1:8000/ to become http://127.0.0.1:8000/username/ I have tried different answers but nothing specific lead to this answer: Here is the login view after login: class LoginView(LoginView): template_name = 'login.html' def get_success_url(self): user=self.request.user.username return f'/{user}/'  Here is the login urls:  path('accounts/login/', LoginView.as_view(redirect_authenticated_user=True,template_name='users/login.html'), name='login'),  Here is the home views: class home(LoginRequiredMixin, ListView): model = Item template_name = 'app/home.html' context_object_name = 'items'  Here is the app urls:  path('<str:username>/', home.as_view(), name='home'),  My question: How to redirect home page to http://127.0.0.1:8000/username/ if user is logged in and if not to login page

Answer: You can use settings.LOGIN_URL and settings.LOGIN_REDIRECT_URL so: class LoginView(LoginView): template_name = 'login.html' login_url='login' def get_success_url(self): user=self.request.user.username return reverse('home', args=(user))  In settings.py: LOGIN_URL='some_app_name:login' #Redirect to login page if not logged in. LOGIN_REDIRECT_URL='some_app_name:home' #Redirect to home page after successful login  It defaults to LOGIN_URL and LOGIN_REDIRECT_URL of settings.py if not specified in views.  Note: It's not a good practice to name same your view with actual authentication view so it should be MyLoginView or anything you can give instead of actual view name.
Question: I am working on one of the transformer models that has been proposed for video classification. My input tensor has the shape of [batch=16 ,channels=3 ,frames=16, H=224, W=224] and for applying the patch embedding on the input tensor it uses the following scenario: patch_dim = in_channels * patch_size ** 2 self.to_patch_embedding = nn.Sequential( Rearrange('b t c (h p1) (w p2) -> b t (h w) (p1 p2 c)', p1 = patch_size, p2 = patch_size), nn.Linear(patch_dim, dim), ***** (Root of the error)****** )  The parameters that I am using are as follows:  patch_size =16 dim = 192 in_channels = 3  Unfortunately I receive the following error that corresponds to the line that has been shown in the code: Exception has occured: RuntimeError mat1 and mat2 shapes cannot be multiplied (9408x4096 and 768x192)  I thought a lot on the reason of the error but I couldn't find out what is the reason. How can I solve the problem?

Answer: The input tensor has shape [batch=16, channels=3, frames=16, H=224, W=224], while Rearrange expects dimensions in order [ b t c h w ]. You expect channels but pass frames. This leads to a last dimension of (p1 * p2 * c) = 16 * 16 * 16 = 4096. Please try to align positions of channels and frames: from torch import torch, nn from einops.layers.torch import Rearrange patch_size = 16 dim = 192 b, f, c, h, w = 16, 16, 3, 224, 224 input_tensor = torch.randn(b, f, c, h, w) patch_dim = c * patch_size ** 2 m = nn.Sequential( Rearrange('b t c (h p1) (w p2) -> b t (h w) (p1 p2 c)', p1=patch_size, p2=patch_size), nn.Linear(patch_dim, dim) ) print(m(input_tensor).size())  Output: torch.Size([16, 16, 196, 192])
Question: Given the following code in python which checks whether an string of n size is palindromic: def is_palindromic(s): return all(s[i] == s[~i] for i in range(len(s) // 2))  What is the space complexity of this code? Is it O(1) or O(n)? The all function gets an iterable as a parameter; So does it mean the s[i] == s[~i] for i in range(len(s) // 2) expression is an iterable (container) that stores n values in memory? Or maybe it behaves like an iterator that computes and returns the values one by one without any additional space?

Answer: You are using a generator expression, which only needs enough memory to store one item at a time. The other parts, len, range and the all function itself are all O(1) too, so your suggestion that "it behaves like an iterator that computes and returns the values one by one without any additional space" is correct. If you used a list expression instead all([s[i] == s[~i] for i in range(len(s) // 2)])  it would briefly use O(n) memory to build the list, which would be passed as a parameter to all.
Question: I am trying to get the number of rows matched in a one to many relationship. When I try parent.children_count I get :  sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place? (Background on this error at: https://sqlalche.me/e/14/xd2s)  I added expire_on_commit=False but still get the same error. How can I fix this? import asyncio from uuid import UUID, uuid4 from sqlmodel import SQLModel, Relationship, Field from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession class Parent(SQLModel, table=True): id: UUID = Field(default_factory=uuid4, primary_key=True) children: list["Child"] = Relationship(back_populates="parent") @property def children_count(self): return len(self.children) class Child(SQLModel, table=True): id: UUID = Field(default_factory=uuid4, primary_key=True) parent_id: UUID = Field(default=None, foreign_key=Parent.id) parent: "Parent" = Relationship(back_populates="children") async def main(): engine = create_async_engine("sqlite+aiosqlite://") async with engine.begin() as conn: await conn.run_sync(SQLModel.metadata.create_all) async with AsyncSession(engine) as session: parent = Parent() session.add(parent) await session.commit() await session.refresh(parent) print(parent.children_count) # I expect 0 here, as of now this parent has no children asyncio.run(main()) 

Answer: I think the problem here is that by default SQLAlchemy lazy-loads relationships, so accessing parent.children_count implicitly triggers a database query leading to the reported error. One way around this would be to specify a load strategy other than "lazy" in the relationship definition. Using SQLModel, this would look like: children: list['Child'] = Relationship( back_populates='parent', sa_relationship_kwargs={'lazy': 'selectin'} )  This will cause SQLAlchemy to issue an additional query to fetch the relationship while still in "async mode". Another option would be to pass {'lazy': 'joined'}, which would cause SQLAlchemy to fetch the all the results in a single JOIN query. If configuring the relationship is undesirable, you could issue a query specifying the option: from sqlalchemy.orm import selectinload from sqlmodel import select ... async with AsyncSession(engine) as session: parent = Parent() session.add(parent) await session.commit() result = await session.scalars( select(Parent).options(selectinload(Parent.children)) ) parent = result.first() print( parent.children_count ) # I need 0 here, as of now this parent has no children
Question: I want to generate data using random numbers and then generate random samples with replacement using the generated data. The problem is that using random.seed(10) only fixes the initial random numbers for the generated data but it does not fix the random samples generated inside the loop, everytime I run the code I get the same generated data but different random samples and I would like to get the same random samples in order to get reproducible results. The code is the following: import numpy as np import random np.random.seed(10) data = list(np.random.binomial(size = 215 , n=1, p= 0.3)) sample_mean = [] for i in range(1000): sample = random.choices(data, k=215) mean = np.mean(sample) sample_mean.append(mean) print(np.mean(sample_mean))  np.mean(sample_mean) should retrieve the same value every time the code is ran but it does not happen. I tried typing random.seed(i) inside the loop but it didn't work.

Answer: Fixing the seed for np.random doesn't fix the seed for random... So adding a simple line for fixing both seeds will give you reproducible results: import numpy as np import random np.random.seed(10) random.seed(10) data = list(np.random.binomial(size=215, n=1, p=0.3)) sample_mean = [] for i in range(1000): sample = random.choices(data, k=215) mean = np.mean(sample) sample_mean.append(mean) print(np.mean(sample_mean))  Or, alternatively, you can use np.random.choices instead of random.choices.
Question: I need to install pipenv on Windows and I use this tutorial. However I get an error. I use Python 3.9.13 and pip 22.3. I installed pipenv with this command pip install pipenv, then I have to do this:  but I didn`t get it. So I passed it and entered this command pipenv -h. Finaly I got this error:  Could you help me please?

Answer: You must add the Scripts directory to PATH environment variable as in the example below:
Question: Is there a better way to initialize all this boilerplate? class Type(Enum): Null=auto() Bool=auto() Int=auto() Float=auto() Decimal=auto() String=auto() Bytes=auto() Date=auto() Time=auto() Datetime=auto() Timestamp=auto() Interval=auto() Struct=auto() Array=auto() Json=auto()  I wanted to do something like the following but unfortunately it kind of screws up how Pylance works (everything shows up as an error referencing related types): _Types = ['Null','Bool','Int','Float','Decimal','String','Bytes','Date','Time','Datetime','Timestamp','Interval','Struct','Array','Json'] Type = Enum('Type', {v:i for i,v in enumerate(_Types)}) 

Answer: I can't speak for Pylance, but if you want auto implementations, you can just pass your list of types directly to the Enum function. Type = Enum('Type', ['Null','Bool','Int','Float','Decimal','String','Bytes', 'Date','Time','Datetime','Timestamp','Interval','Struct', 'Array','Json'])
Question: I have the following bar chart. I would like to put it around a border. Does anyone know how to do it? data = {'Programming Languages': ['Python', 'C#', 'Java', 'Ruby', 'C++', 'C', 'JavaScript', 'PHP', 'TypeScript', 'Haskell', 'Closure', 'Kotlin'], 'Responses': [12,11,10,5,3,2,2,1,1,1,1,1]} df = pd.DataFrame(data, columns = ['Programming Languages', 'Responses']) df = df.sort_values(by='Responses', ascending=True) fig = px.bar(df, x='Responses', y='Programming Languages', color_discrete_sequence=["black"]) fig.update_layout({'plot_bgcolor': 'rgba(0, 0, 0, 0)'}, font=dict(size=15)) fig.write_image("../images/pl2.pdf") fig.show()  Unfortunately, I cannot paste the figure as it is a pdf file.

Answer: fig.update_xaxes(showline=True, linewidth=1, linecolor='black', mirror=True) fig.update_yaxes(showline=True, linewidth=1, linecolor='black', mirror=True) fig.show()
Question: I am using mkdocs with the mkdocstrings plugin to generate the documentation of my Python package. My package is organized in a standard fashion - setup.py - mkdocs.yaml - docs/ - mypackage/ - __init__.py - module1.py - module2.py - subpackage1/ - __init__.py - submodule1.py - submodule2.py - [...]  in mkdocs.yml: plugins: - mkdocstrings nav: - API: references.md  and in references.md: # API references ::: mypackage  This does not generate any documentation (i.e. the 'API' page remains blanck) On the other hand, this works: ::: mypackage.module1  Running mkdocs build -v: DEBUG - mkdocstrings: Matched '::: mypackage' DEBUG - mkdocstrings: Using handler 'python' DEBUG - mkdocstrings: Collecting data DEBUG - griffe: Found mypackage: loading DEBUG - griffe: Loading path mypackage/__init__.py DEBUG - griffe: Loading path mypackages/module1.py [...] DEBUG - griffe: Iteration 1 finished, 0 aliases resolved, still 0 to go DEBUG - mkdocstrings: Updating renderer s env DEBUG - mkdocstrings: Rendering templates DEBUG - mkdocstrings: python/templates/material/module.html: Rendering mypackage DEBUG - mkdocstrings: python/templates/material/children.html: Rendering children of mypackage DEBUG - Running 1 `page_content` events DEBUG - mkdocs_autorefs.plugin: Mapping identifiers to URLs for page references.md [...] mkdocs_autorefs.plugin: Fixing references in page index.md DEBUG - Building page references.md DEBUG - Running 1 `page_context` events DEBUG - Running 2 `post_page` events DEBUG - mkdocs_autorefs.plugin: Fixing references in page references.md  I am not familiar with mkdocs and mkdocstrings, but it seems this does not indicate any error, and shows the sources are found. I guess I am missing something? (Or should I manually list the path to all modules?)

Answer: Ah, it's because by default submodules are not rendered. Try setting the show_submodules option to true. Globally: plugins: - mkdocstrings: handlers: python: options: show_submodules: true  Locally: ::: mypackage options: show_submodules: true
Question: Good evening everyone, I have an issue with scikit-image while installing this package I installed python 3.11 on windows 11 pip install scikit-image  I got these issues as below in the screenshot got previous issue EDIT: I installed Microsoft C++ 14 but I got another issue at the below screenshot enter image description here

Answer: The issue is that no wheels are available for scikit-image for Python 3.11 Python libraries can have code that is not written in Python (like C, C++, FORTRAN, Rust, ...) and in order to use those, you need to compile that code for your version of Python, your OS and CPU architecture. Fortunately, Python offers a packaging mechanism known as wheel to provide that precompiled code. Package maintainers can generate those wheels to avoid the end user having to compile it from sources. In your case, as Python 3.11 just got out (beginning of the week), the maintainers of scikit-image didn't have the time to package the wheel and pip tries to install it from sources (hence the message asking you to install Microsoft Visual C++ 14). Your options are:  stay on Python 3.10 until the maintainers of scikit-image provides a wheel for Python 3.11 (I would recommend that) check https://github.com/scikit-image/scikit-image to see how to compile the package from sources
Question: We want to check if the array items in text_array column start with "a" in the input table, and store the results into the third column, so we get the following output table.  My first question is: Is there any way to get output table from input table using presto? In python we can define a function for it, like: def myFunc(text): out = [] for word in text: out.append(word.startswith("a")) return out  My second question is: Is there any way to use the python function with presto? I was looking for something like this: SELECT id, text_array, myFunc(text_array) AS starts_with_a FROM t1 

Answer: You can use transform from array functions: - sample data with dataset(id, text_array) AS ( values (1, array['ax', 'by']), (2, array['ax', 'ay', 'cz']) ) -- query select *, transform(text_array, el -> el like 'a%') starts_with_a from dataset;  Output:     id text_array starts_with_a     1 [ax, by] [true, false]   2 [ax, ay, cz] [true, true, false]
Question: I want my dataclass to have a field that can either be provided manually, or if it isn't, it is inferred at initialization from the other fields. MWE: from collections.abc import Sized from dataclasses import dataclass from typing import Optional @dataclass class Foo: data: Sized index: Optional[list[int]] = None def __post_init__(self): if self.index is None: self.index = list(range(len(self.data))) reveal_type(Foo.index) # Union[None, list[int]] reveal_type(Foo([1,2,3]).index) # Union[None, list[int]]  How can this be implemented in a way such that:  It complies with mypy type checking index is guaranteed to be of type list[int]  I considered using default_factory(list), however, then how does one distinguish the User passing index=[] from the sentinel value? Is there a proper solution besides doing index: list[int] = None # type: ignore[assignment] 

Answer: Use NotImplemented from collections.abc import Sized from dataclasses import dataclass @dataclass class Foo: data: Sized index: list[int] = NotImplemented def __post_init__(self): if self.index is NotImplemented: self.index = list(range(len(self.data)))
Question: im having the dataframe: test = pd.DataFrame({'Date': [2020 - 12 - 30, 2020 - 12 - 30, 2020 - 12 - 30, 2020 - 12 - 31, 2020 - 12 - 31, 2021 - 0o1 - 0o1, 2021 - 0o1 - 0o1], 'label': ['Positive', 'Positive', 'Negative', 'Negative','Negative', 'Positive', 'Positive'], 'score': [70, 80, 50, 50, 30, 90, 70]})  Output:  Date label score 2020-12-30 Positive 70 2020-12-30 Positive 80 2020-12-30 Negative 50 2020-12-31 Negative 50 2020-12-31 Negative 30 2021-01-01 Positive 90 2021-01-01 Positive 70  My goal is to group by the date and count the labels. In addition the score should be calculating only the mean with the labels/score which are higher at that day. For example if there is more positive than negative at the day it should calculate the mean of the positive scores without the negative scores and the other way around. I though about the function new_df = test.groupby(['Date', 'label']).agg({'label' : 'count', 'score' : 'mean'}) Output should be like that. Maybe .pivot function would help?  Date label new_score count_pos count_neg 2020-12-30 Positive 150 2 1 2020-12-31 Negative 80 0 2 2021-01-01 Positive 160 2 0 new_score = 70 + 80 = 150 of the two days with positive label count_pos = count "Positive" at day X count_neg = count "Negative" at day X  Im a beginner in python and any help or hints how to tackle this task is appreciated! Thanks!

Answer: Another possible solution, which is based on the following ideas:  Doing a pivot table indexing only on Date and aggregating with sum and count.  Using the pivot table to construct a dataframe with the wanted result.   aux = df.pivot_table(index=['Date'], columns='label', values='score', aggfunc=[ 'sum', 'count'], fill_value=0) (pd.DataFrame({'label': aux['sum'].idxmax(axis=1), 'score': aux['sum'].max(axis=1), 'count_pos': aux['count']['Positive'], 'count_neg': aux['count']['Negative']}) .reset_index())  Output:  Date label score count_pos count_neg 0 2020-12-30 Positive 150 2 1 1 2020-12-31 Negative 80 0 2 2 2021-01-01 Positive 160 2 0
Question: In each new row of dataframe, I need to keep track of min and max values for a previous group of records.  Create dataframe with input data:  import pandas as pd columns = ['timestamp','groupid','value'] data = [['2022-10-14 11:47:38',1000,200], ['2022-10-14 11:47:39',1000,210], ['2022-10-14 11:47:40',1000,220], ['2022-10-14 11:47:41',1000,230], ['2022-10-14 11:47:42',1001,240], ['2022-10-14 11:47:43',1001,250], ['2022-10-14 11:47:44',1002,260], ['2022-10-14 11:47:45',1002,270]] df = pd.DataFrame(data=data,columns=columns) print(df)   Calculate min and max values for each group:  df['min'] = df.groupby('groupid')['value'].transform('min') df['max'] = df.groupby('groupid')['value'].transform('max')   Create new columns in dataframe:  df['pmin'] = 0 df['pmax'] = 0 df['ppmin'] = 0 df['ppmax'] = 0 df['ppmin'] = df.groupby('groupid')['pmin'].transform('first').shift(1) df['ppmax'] = df.groupby('groupid')['pmax'].transform('first').shift(1) df['pmin'] = df.groupby('groupid')['min'].transform('first').shift(1) df['pmax'] = df.groupby('groupid')['max'].transform('first').shift(1) print(df)  The code in step 3 fails to return expected result as shown below: columns2 = ['timestamp','groupid','value','min','max','pmin','pmax','ppmin','ppmax'] data2 = [['2022-10-14 11:47:38',1000,200,200,230,0,0,0,0], ['2022-10-14 11:47:39',1000,210,200,230,0,0,0,0], ['2022-10-14 11:47:40',1000,220,200,230,0,0,0,0], ['2022-10-14 11:47:41',1000,230,200,230,0,0,0,0], ['2022-10-14 11:47:42',1001,240,240,250,200,230,0,0], ['2022-10-14 11:47:43',1001,250,240,250,200,230,0,0], ['2022-10-14 11:47:44',1002,260,260,270,240,250,200,230], ['2022-10-14 11:47:45',1002,270,260,270,240,250,200,230]] df = pd.DataFrame(data=data2,columns=columns2) print(df) 

Answer: Maybe not the prettiest solution: df["tmp"] = (df["groupid"] != df["groupid"].shift()).cumsum() grps = df.groupby("groupid")["value"] df["min"] = grps.transform("min") df["max"] = grps.transform("max") mapper = df.drop_duplicates("tmp").set_index("tmp")[["min", "max"]] tmp1 = df["tmp"] - 1 tmp2 = df["tmp"] - 2 df["pmin"] = tmp1.map(mapper["min"]) df["pmax"] = tmp1.map(mapper["max"]) df["ppmin"] = tmp2.map(mapper["min"]) df["ppmax"] = tmp2.map(mapper["max"]) print(df.fillna(0).drop(columns="tmp"))  Prints:  timestamp groupid value min max pmin pmax ppmin ppmax 0 2022-10-14 11:47:38 1000 200 200 230 0.0 0.0 0.0 0.0 1 2022-10-14 11:47:39 1000 210 200 230 0.0 0.0 0.0 0.0 2 2022-10-14 11:47:40 1000 220 200 230 0.0 0.0 0.0 0.0 3 2022-10-14 11:47:41 1000 230 200 230 0.0 0.0 0.0 0.0 4 2022-10-14 11:47:42 1001 240 240 250 200.0 230.0 0.0 0.0 5 2022-10-14 11:47:43 1001 250 240 250 200.0 230.0 0.0 0.0 6 2022-10-14 11:47:44 1002 260 260 270 240.0 250.0 200.0 230.0 7 2022-10-14 11:47:45 1002 270 260 270 240.0 250.0 200.0 230.0
Question: I want to create only one object for the same users. class MyModel(models.Model): user1 = models.ForeignKey(settings.AUTH_USER_MODEL,...) user2 = models.ForeignKey(settings.AUTH_USER_MODEL,...) class Meta: constraints = [ UniqueConstraint( fields=['user1', 'user2'], name='user_unique', ), # UniqueConstraint( # fields=['user2', 'user1'], # name='user_unique2', # ), ]  I can solve the problem in another way, I just want to know how to do it with UniqueConstraint. Adding another UniqueConstraint and moving the fields didn't solve the problem. For example, for users X and Y, I only need one object.

Answer: Since Django 4.0 constraints now support expressions, this allows us to use database functions in our constraints allowing us to create a unique constraint with sorted fields: from django.db.models.functions import Least, Greatest class MyModel(models.Model): user1 = models.ForeignKey(settings.AUTH_USER_MODEL,...) user2 = models.ForeignKey(settings.AUTH_USER_MODEL,...) class Meta: constraints = [ UniqueConstraint( Least("user1", "user2"), Greatest("user1", "user2"), name='user_unique', ) ]