eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I have a Polars dataframe with 3 columns - group, date, value. The goal is to calculate cumsum(value) for each expanding window ends at the first time point at each year for each group. For example, for the following sample dataframe: import polars as pl df = pl.DataFrame( { "date": [ "2020-03-01", "2020-05-01", "2020-11-01", "2021-01-01", "2021-02-03", "2021-06-08", "2022-01-05", "2020-07-01", "2020-09-01", "2022-01-05", "2023-02-04", ], "group": [1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2], "value": [1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4], }, ).with_columns(pl.col("date").str.strptime(pl.Date))  The result I am looking for is: ┌────────────┬───────┬───────┐ │ date ┆ group ┆ value │ │ --- ┆ --- ┆ --- │ │ date ┆ i64 ┆ i64 │ ╞════════════╪═══════╪═══════╡ │ 2020-03-01 ┆ 1 ┆ 1 │ │ 2021-01-01 ┆ 1 ┆ 10 │ │ 2022-01-05 ┆ 1 ┆ 28 │ │ 2020-07-01 ┆ 2 ┆ 1 │ │ 2022-01-05 ┆ 2 ┆ 6 │ │ 2023-02-04 ┆ 2 ┆ 10 │ └────────────┴───────┴───────┘  Basically, at the first date of each year, calculate the cumulative sum of value from beginning up to (including) this particular date, for each group respectively. I tried group_by_dynamic and rolling, but still unable to find a concise and clear way to solve this problem. Any idea is welcome. Thanks!

Answer: A "naive" approach could be to calculate the full cum_sum and then keep the first yearly row of each group. df.with_columns( cum_sum = pl.col("value").cum_sum().over("group") ).filter( pl.col("date").dt.year().is_first_distinct().over("group") )  shape: (6, 4) ┌────────────┬───────┬───────┬─────────┐ │ date ┆ group ┆ value ┆ cum_sum │ │ --- ┆ --- ┆ --- ┆ --- │ │ date ┆ i64 ┆ i64 ┆ i64 │ ╞════════════╪═══════╪═══════╪═════════╡ │ 2020-03-01 ┆ 1 ┆ 1 ┆ 1 │ │ 2021-01-01 ┆ 1 ┆ 4 ┆ 10 │ │ 2022-01-05 ┆ 1 ┆ 7 ┆ 28 │ │ 2020-07-01 ┆ 2 ┆ 1 ┆ 1 │ │ 2022-01-05 ┆ 2 ┆ 3 ┆ 6 │ │ 2023-02-04 ┆ 2 ┆ 4 ┆ 10 │ └────────────┴───────┴───────┴─────────┘  A more "direct" (efficient) approach could be to take the yearly sum first, then calculate the cum_sum: (df.group_by( "group", pl.col("date").dt.year().alias("year"), maintain_order=True ) .agg( pl.col("date").first(), pl.col("value").first(), pl.col("value").sum().alias("sum"), ) .with_columns( pl.when(pl.col("group").is_first_distinct()) .then(pl.col("value")) .otherwise( pl.col("value") + pl.col("sum").cum_sum().shift().over("group") ) .alias("cum_sum") ) )  shape: (6, 6) ┌───────┬──────┬────────────┬───────┬─────┬─────────┐ │ group ┆ year ┆ date ┆ value ┆ sum ┆ cum_sum │ │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ │ i64 ┆ i32 ┆ date ┆ i64 ┆ i64 ┆ i64 │ ╞═══════╪══════╪════════════╪═══════╪═════╪═════════╡ │ 1 ┆ 2020 ┆ 2020-03-01 ┆ 1 ┆ 6 ┆ 1 │ │ 1 ┆ 2021 ┆ 2021-01-01 ┆ 4 ┆ 15 ┆ 10 │ │ 1 ┆ 2022 ┆ 2022-01-05 ┆ 7 ┆ 7 ┆ 28 │ │ 2 ┆ 2020 ┆ 2020-07-01 ┆ 1 ┆ 3 ┆ 1 │ │ 2 ┆ 2022 ┆ 2022-01-05 ┆ 3 ┆ 3 ┆ 6 │ │ 2 ┆ 2023 ┆ 2023-02-04 ┆ 4 ┆ 4 ┆ 10 │ └───────┴──────┴────────────┴───────┴─────┴─────────┘
Question: I have a huge model because of its many member functions. I would like to organize the functions into separate classes according to their purpose, so everything is a bit more organized. Using OneToOneField is not a good option for me, because my app heavily relies on query caching (with cacheops). And I noticed that getting the latest version of a data object (from db) may be attached to a an outdated OneToOneField (from cache). Also, the number of data members is not a problem, only the number of member functions. So I would prefer to use just one data model. For example, I could do it this way: # models.py class MyModel(models.Model): # members # separate member functions class CustomModelFunctions: @staticmethod def some_function(my_model_instance): # logic # call the function on the my_model obj # instead of calling my_model.some_function() CustomModelFunctions.some_function(my_model)  This works, but the problem is that these functions can not be used in django templates, since the my_model argument can not be passed. For example, I don't see how I could replace the following template code: {% if my_model.some_function %}  Does anyone know a workaround for this? Or a different approach to organize models with a lot of member functions?

Answer:  I have a huge model because of its many member functions. I would like to organize the functions into separate classes according to their purpose, so everything is a bit more organized.  Probably the best way would be to work with a proxy, like: class FunctionCategory(type): def __get__(self, obj, objtype=None): return self(obj) class FunctionCategoryBase(metaclass=FunctionCategory): def __init__(self, obj): self.obj = obj # separate member functions class FunctionCategory1(FunctionCategoryBase): def some_function11(self): # use self.obj return None def some_function12(self): # use self.obj return None # separate member functions class FunctionCategory2(FunctionCategoryBase): def some_function21(self): # use self.obj return None def some_function22(self): # use self.obj return None def some_function23(self): # use self.obj return None class MyModel(models.Model): # members functions1 = FunctionCategory1 functions2 = FunctionCategory2 Then you can use: my_model_object.functions1.some_function12() This will thus for my_model_object.functions1 create a FunctionCategory1 where it injects the model object.
Question:  I'd like to create a conditional incremented column in polars. It should start from 1 and increment only if a certain condition (pl.col('code') == 'L') is met. import polars as pl df = pl.DataFrame({'file': ['a.txt','a.txt','a.txt','a.txt','b.txt','b.txt','c.txt','c.txt','c.txt','c.txt','c.txt'], 'code': ['X','Y','Z','L','A','A','B','L','C','L','X'] }) df.with_columns(pl.int_range(start=1, end=pl.len()+1).over('file').alias('rrr') )  This produces a simple unconditional increment. But how do I add conditions?

Answer: Not sure which output exactly you're expecting, but here's an example of incrementing the counter only at rows which meet the criteria, using cum_sum(): df.with_columns( pl.when(pl.col('code') == 'L').then(pl.lit(1)).otherwise(pl.lit(0)).alias('rrr') ).with_columns( pl.col('rrr').cum_sum().over('file') + 1 ) ┌───────┬──────┬─────┐ │ file ┆ code ┆ rrr │ │ --- ┆ --- ┆ --- │ │ str ┆ str ┆ i32 │ ╞═══════╪══════╪═════╡ │ a.txt ┆ X ┆ 1 │ │ a.txt ┆ Y ┆ 1 │ │ a.txt ┆ Z ┆ 1 │ │ a.txt ┆ L ┆ 2 │ │ b.txt ┆ A ┆ 1 │ │ b.txt ┆ A ┆ 1 │ │ c.txt ┆ B ┆ 1 │ │ c.txt ┆ L ┆ 2 │ │ c.txt ┆ C ┆ 2 │ │ c.txt ┆ L ┆ 3 │ │ c.txt ┆ X ┆ 3 │ └───────┴──────┴─────┘
Question: I need to get the number of the row that contains a specific value in the given column. It is guaranteed that such value exists in the column and in a unique row. My attempt: import pandas as pd pos = pd.DataFrame(columns=['id', 'pred']) pos.loc[1,'id'] = [4, 4, 4] pos.loc[2,'id'] = [2, 3, 3] pos.loc[3,'id'] = [1, 1, 2] pos.loc[4,'id'] = [1, 2, 1] print(pos) print(pos[pos['id'] == [1, 1, 2]].index[0])  I am getting an error: ValueError: ('Lengths must match to compare', (6,), (3,)) 

Answer: You need to loop. If you want the position: next(i for i, x in enumerate(pos['id']) if [1,1,2] == x)  Output: 2 Or, to have the index: next(i for i, x in zip(pos.index, pos['id']) if [1,1,2] == x)  Output: 3
Question: This boggles my mind, is this a known bug or am I missing something? If a bug is there a way to circumvent it?  Suppose I have a relatively small binary (0/1) n x q scipy.sparse.csr_matrix, as in: import numpy as np from scipy import sparse def get_dummies(vec, vec_max): vec_size = vec.size Z = sparse.csr_matrix((np.ones(vec_size), (np.arange(vec_size), vec)), shape=(vec_size, vec_max), dtype=np.uint8) return Z q = 100 ns = np.round(np.random.random(q)*100).astype(np.int16) Z_idx = np.repeat(np.arange(q), ns) Z = get_dummies(Z_idx, q) Z  <5171x100 sparse matrix of type '<class 'numpy.uint8'>' with 5171 stored elements in Compressed Sparse Row format>  Here Z is a standard dummy variables matrix, with n=5171 observations and q=100 variables: Z[:5, :5].toarray()  array([[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]], dtype=uint8)  E.g., if the first 5 variables have... ns[:5]  array([21, 22, 37, 24, 99], dtype=int16)  frequencies, we would also see this in Z's column sums: Z[:, :5].sum(axis=0)  matrix([[21, 22, 37, 24, 99]], dtype=uint64)  Now, as expected, if I multiply Z.T @ Z I should get a q x q diagonal matrix, on the diagonal the frequencies of the q variables: print((Z.T @ Z).shape) print((Z.T @ Z)[:5, :5].toarray()  (100, 100) [[21 0 0 0 0] [ 0 22 0 0 0] [ 0 0 37 0 0] [ 0 0 0 24 0] [ 0 0 0 0 99]]  Now for the bug: If n is really large (for me it happens around n = 100K already): q = 1000 ns = np.round(np.random.random(q)*1000).astype(np.int16) Z_idx = np.repeat(np.arange(q), ns) Z = get_dummies(Z_idx, q) Z  <495509x1000 sparse matrix of type '<class 'numpy.uint8'>' with 495509 stored elements in Compressed Sparse Row format>  The frequencies are large, the sum of Z's columns is as expected: print(ns[:5]) Z[:, :5].sum(axis=0)  array([485, 756, 380, 87, 454], dtype=int16) matrix([[485, 756, 380, 87, 454]], dtype=uint64)  But the Z.T @ Z is messed up! In the sense that I'm not getting the right frequencies on the diagonal: print((Z.T @ Z).shape) print((Z.T @ Z)[:5, :5].toarray())  (1000, 1000) [[229 0 0 0 0] [ 0 244 0 0 0] [ 0 0 124 0 0] [ 0 0 0 87 0] [ 0 0 0 0 198]]  Amazingly, there is some relation to the true frequencies: import matplotlib.pyplot as plt plt.scatter(ns, (Z.T @ Z).diagonal()) plt.xlabel('real frequencies') plt.ylabel('values on ZZ diagonal') plt.show()   What is going on? I'm using standard colab: import scipy as sc print(np.__version__) print(sc.__version__)  1.25.2 1.11.4  PS: Obviously if I wanted just Z.T @ Z's output matrix there are easier ways to get it, this is a very simplified reduced problem, thanks.

Answer: You are using uint8 in get_dummies print(Z.dtype, (Z.T @ Z)[:5, :5].dtype) # uint8, uint8  So the result is overflowing. The pattern is because the result is the true result modulo 256. If you change the dtype to uint16, the problem disappears. The fact that sum increases the precision of the dtype is a bit surprising, but it is documented and consistent with what NumPy sum does.
Question: I have a list of tuples. Each tuple contains coordinate points (x, y, x1, y1,...) forming a line. All these lines form a drawing.  There are 5 closed objects in this picture. How can I get the number of these objects with their coordinates? Coordinates_list = [(939, 1002, 984, 993, 998, 1001, 1043, 995, 1080, 1004, 1106, 994, 1147, 1003, 1182, 995, 1223, 1005), (939, 1002, 939, 900), (939, 900, 961, 916), (961, 916, 1031, 898), (1031, 898, 1080, 906), (1080, 906, 1190, 896), (1190, 896, 1225, 897), (1223, 1005, 1225, 897), (939, 1002, 1031, 898, 1106, 994, 1190, 896, 1182, 995)]  I tried to use the DFS (Depth First Search) Algorithm, but it always returned fewer objects than there actually were. But the data here is presented differently - here there are no more than two points in the tuple, but this does not change the drawing def find_closed_figures(lines): def dfs(line_idx, visited): visited.add(line_idx) for neighbor_idx, line in enumerate(lines): if neighbor_idx not in visited and lines[line_idx][3:6] == line[0:3]: dfs(neighbor_idx, visited) closed_figures_count = 0 visited_lines = set() for idx, line in enumerate(lines): if idx not in visited_lines: dfs(idx, visited_lines) closed_figures_count += 1 return closed_figures_count coordinates_list = [(939, 1002, 0, 984, 993, 0), (984, 993, 0, 998, 1001, 0), (998, 1001, 0, 1043, 995, 0), (1043, 995, 0, 1080, 1004, 0), (1080, 1004, 0, 1106, 994, 0), (1106, 994, 0, 1147, 1003, 0), (1147, 1003, 0, 1182, 995, 0), (1182, 995, 0, 1223, 1005, 0), (939, 1002, 0, 939, 900, 0), (939, 900, 0, 961, 916, 0), (961, 916, 0, 1031, 898, 0), (1031, 898, 0, 1080, 906, 0), (1080, 906, 0, 1190, 896, 0), (1190, 896, 0, 1225, 897, 0), (1223, 1005, 0, 1225, 897, 0), (939, 1002, 0, 1031, 898, 0), (1031, 898, 0, 1106, 994, 0), (1106, 994, 0, 1190, 896, 0), (1190, 896, 0, 1182, 995, 0)] closed_figures_count = find_closed_figures(coordinates_list) print(closed_figures_count) 

Answer: Here is a condensed version of Grismar's answer, where he has a good explanation as well. I just removed the manual search for segments and treated each point as a node directly. import networkx as nx lines = [ (939, 1002, 984, 993, 998, 1001, 1043, 995, 1080, 1004, 1106, 994, 1147, 1003, 1182, 995, 1223, 1005), (939, 1002, 939, 900), (939, 900, 961, 916), (961, 916, 1031, 898), (1031, 898, 1080, 906), (1080, 906, 1190, 896), (1190, 896, 1225, 897), (1223, 1005, 1225, 897), (939, 1002, 1031, 898, 1106, 994, 1190, 896, 1182, 995) ] point_lists = [[pair for pair in zip(line[::2],line[1::2])] for line in lines] edges = [segment for point_list in point_lists for segment in zip(point_list[:-1], point_list[1:])] G = nx.from_edgelist(edges) cycles = list(nx.chordless_cycles(G)) print('The number of chord-less cycles:', len(cycles)) print('The cycles:') for cycle in cycles: print("\t", cycle)
Question: As a minimum example, let's say we have next polars.DataFrame: df = pl.DataFrame({"sub_id": [1,2,3], "engagement": ["one:one,two:two", "one:two,two:one", "one:one"], "total_duration": [123, 456, 789]})    sub_id engagement total_duration     1 one:one,two:two 123   2 one:two,two:one 456   3 one:one 789    then, we explode "engagement" column df = df.with_columns(pl.col("engagement").str.split(",")).explode("engagement") and receive:    sub_id engagement total_duration     1 one:one 123   1 two:two 123   2 one:two 456   2 two:one 456   3 one:one 789    For visualization I use Plotly, and code would be following: import plotly.express as px fig = px.bar(df, x="sub_id", y="total_duration", color="engagement") fig.show()  Resulting plot:  Now it basically means that subscribers 1 and 2 have their total_duration (total watched time) doubled. How could I remain total_duration per sub, but leaving engagement groups as shown on the plot legend?

Answer: An option to handle this in polars would be to split total_duration equally between engagement rows within sub_id. For this, we simply divide total_duration by the number of rows of the given sub_id. ( df .with_columns( pl.col("engagement").str.split(",") ) .explode("engagement") .with_columns( pl.col("total_duration") / pl.len().over("sub_id") ) )  shape: (5, 3) ┌────────┬────────────┬────────────────┐ │ sub_id ┆ engagement ┆ total_duration │ │ --- ┆ --- ┆ --- │ │ i64 ┆ str ┆ f64 │ ╞════════╪════════════╪════════════════╡ │ 1 ┆ one:one ┆ 61.5 │ │ 1 ┆ two:two ┆ 61.5 │ │ 2 ┆ one:two ┆ 228.0 │ │ 2 ┆ two:one ┆ 228.0 │ │ 3 ┆ one:one ┆ 789.0 │ └────────┴────────────┴────────────────┘
Question: Problem statement: I have 100s of py files which defines pydantic schema. Suddenly I need to treat empty string as None. I am expecting minimal changes across all the files. Approach I implemented: I created an inherited class such as class ConstrainedStr(str): @classmethod def __get_validators__(cls): yield cls.validate @classmethod def validate(cls, v: str, field: Field) -> Optional[str]: v = v.strip() if v == "": return None return v  Then, in all the py files I just added an import statement from package.module import ConstrainedStr as str  Luckily, It worked at the first sight. But I ended up with an issue where I have a function User file: from package.module import ConstrainedStr as str def validate(cls, value:str): //sample value 'asd' if isinstance(value, str): validation_rule()  Here, this conditional statement failed. Question  How could I avoid major changes to achieve this? Is there a way? Why that isinstance check failed. Constrainedstr is also a str right? Is my understanding wrong?  When I digged into this further, just to understand the behaviour for the question #2, I found the below. type(ConstrainedStr)  return type<class>. `type('123')`  returns type<str> But when I check the builtins package, str is also a class. But type function returns the type for str as str but for ConstrainedStr as class. Hence, My question #2 popped up. Another example: class A(int): pass isinstance(2, A) # False  This is my #2 question.

Answer: While an instance of a subclass is automatically an instance of the base class, the reverse isn't true. This is analogous to that every cat is a mammal, but not every mammal is a cat. Similarly, since ConstrainedStr is a subclass of str, an instance of the child class ConstrainedStr is automatically an instance of the base class str, while conversely an instance of the base class str, such as 'asd', cannot automatically be considered an instance of a child class such as ConstrainedStr. To force a reverse instance check to be also true, you can customize the behavior of isinstance by defining __instancecheck__ on a metaclass. Also customize issubclass by defining __subclasscheck___ to keep the relationships consistent. To achieve the desired behavior, your custom __instancecheck__ should return True if the given instance is an instance of any of the base classes: class SubstitutableMeta(type): def __instancecheck__(cls, instance): return any(isinstance(instance, base) for base in cls.__bases__) def __subclasscheck__(cls, subclass): return any(issubclass(subclass, base) for base in cls.__bases__) class ConstrainedStr(str, metaclass=SubstitutableMeta): pass print(isinstance('asd', ConstrainedStr)) # outputs True
Question: In Pandas we can replace the first n rows with certain values: import pandas as pd import numpy as np df = pd.DataFrame({"test": np.arange(0, 10)}) df.iloc[0:5] = np.nan df out: test 0 NaN 1 NaN 2 NaN 3 NaN 4 NaN 5 5.0 6 6.0 7 7.0 8 8.0 9 9.0  What would be the equivalent operations in Python Polars?

Answer: You can use DataFrame.with_row_index(): import polars as pl df = pl.DataFrame({"test": np.arange(1, 11)}) print( df.with_row_index() .with_columns( pl.when(pl.col("index") < 5) .then(None) .otherwise(pl.col("test")) .alias("test") ) .drop("index") )  Prints: shape: (10, 1) ┌──────┐ │ test │ │ --- │ │ f64 │ ╞══════╡ │ Null │ │ Null │ │ Null │ │ Null │ │ Null │ │ 6 │ │ 7 │ │ 8 │ │ 9 │ │ 10 │ └──────┘  or by pl.int_range() which gives more flexibility in cases of groups. print( (df .with_columns( pl.int_range(0, pl.len(), dtype=pl.UInt32).over(True).alias("index") ) .with_columns( pl.when(pl.col("index") < 5) .then(None) .otherwise(pl.col("test")) .alias("test") ) .drop("index") ) )
Question: I was wondering if there's a Pythonic/hacky way to check for all (or none) items and dependencies in a set to be null. Suppose I have a variable foo that is a dict, but can be nullable: foo: Optional[dict] = None  And I want to check that both foo is not None or one of its keys: if foo is not None and foo.get('bar') is not None: pass  Using AND, it will "short-circuit" before anything else, so I'm safe checking for other keys on a nullable item. I was thinking about ways of doing that more neatly, like we can do with any() or all(). I though about doing it in a list comprehension, but it would fail:  if not all(item is not None for item in [foo, foo.get('bar'), foo.get('baz')]: # Value Error! :sad  One way could be using a try/catch, but that is ugly and won't make it look fun... We can't go for walrus operator as it is not assignable in a list comprehension:  if not all (item for item in [foo := foo or {}, foo.get('bar'), foo.get('baz')]: # SyntaxError, can't use walrus in list comprehension  I was also wondering if there's not a very well maintened tool that serves this purpose. Any better ideas?

Answer: Simply separate the foo from all the foo.gets and use an and: if foo is not None and all(foo.get(key) is not None for key in ('bar', 'baz')):  You can also use key in foo to check if the key exists in the dictionary, this is actually safer because if the key exists and has a value of None in the dictionary, then using the above approach won't distinguish between the two cases: if foo is not None and all(key in foo for key in ('bar', 'baz')):
Question: I am trying to remove rows whose all values are the same, or a combination of the same values. I have, for example a dataframe like: data = {'A': ['1, 1, 1', '1', '2', '3', '1'], 'B': ['1', '1,1,1,1', '2', '4', '1'], 'C': ['1, 1', '2', '3', '5', '1']}  I want to remove rows whose values in all columns are '1' or any combination of '1'. The final result should be something like: data = {'A': ['1', '2', '3'], 'B': ['1,1,1,1', '2', '4'], 'C': ['2', '3', '5']}  I've tried the following: def remove_rows_with_ones(value): return all(x == '1' for x in value.split(',')) mask = df.apply(lambda row: any(remove_rows_with_ones(val) for val in row), axis=1) df_filtered = df[~mask]  But it does not seem to work.

Answer: You could convert to string and check if each cell contains a character other than 1 (or space/comma), if at least one True, keep the row: out = df[df.apply(lambda s: s.astype(str).str.contains('[^1 ,]')).any(axis=1)]  Or, with your original idea of splitting the strings on ', ': import re out = df[~df.applymap(lambda c: all(x=='1' for x in re.split(', *', c))).all(axis=1)] # pandas ≥ 2.1 out = df[~df.map(lambda c: all(x=='1' for x in re.split(', *', c))).all(axis=1)]  Output:  A B C 1 1 1,1,1,1 2 2 2 2 3 3 3 4 5  Intermediates: # df.apply(lambda s: s.astype(str).str.contains('[^1 ,]')) A B C 0 False False False 1 False False True 2 True True True 3 True True True 4 False False False # df.map(lambda c: all(x=='1' for x in re.split(', *', c))) A B C 0 True True True 1 True True False 2 False False False 3 False False False 4 True True True
Question: I have a DataFrame that contains two columns composed by lists. One column has the categories of certain item, the other column has the points associated to this category. import pandas as pd cat = [ ['speed', 'health', 'strength', 'health'], ['strength', 'speed', 'speed'], ['strength', 'speed', 'health', 'speed'] ] pts = [ [1, 2, 1.5, -1], [2, -1.5, 1.5], [-1, 2, 0, 1.5] ] s_cat = pd.Series(cat, name='cat') s_pts = pd.Series(pts, name='pts') df = pd.concat([s_cat, s_pts], axis=1)  Output:  cat pts 0 [speed, health, strength, health] [1, 2, 1.5, -1] 1 [strength, speed, speed] [2, -1.5, 1.5] 2 [strength, speed, health, speed] [-1, 2, 0, 1.5]  I would like to relate these lists to get the sum of the points from each category to a new column, and if possible, the count of positive and negative points of each category. I expect something like this:  cat pts speed_sum health_sum strength_sum speed_pos speed_neg health_pos health_neg strength_pos strength_neg 0 [speed, health, strength, health] [1, 2, 1.5, -1] 1.0 1.0 1.5 1 0 1 1 1 0 1 [strength, speed, speed] [2, -1.5, 1.5] 0.0 NaN 2.0 1 1 0 0 1 0 2 [strength, speed, health, speed] [-1, 2, 0, 1.5] 3.5 0.0 -1.0 2 0 0 0 0 1 

Answer: Try: def pos(vals): return (vals > 0).sum() def neg(vals): return (vals < 0).sum() tmp = df.explode(["cat", "pts"]) tmp = tmp.pivot_table( index=tmp.index, columns="cat", values="pts", aggfunc=["sum", pos, neg], ) tmp.columns = [f"{b}_{a}" for a, b in tmp.columns] out = pd.concat([df, tmp], axis=1) print(out)  Prints:  cat pts health_sum speed_sum strength_sum health_pos speed_pos strength_pos health_neg speed_neg strength_neg 0 [speed, health, strength, health] [1, 2, 1.5, -1] 1 1 1.5 1.0 1.0 1.0 1.0 0.0 0.0 1 [strength, speed, speed] [2, -1.5, 1.5] NaN 0.0 2 NaN 1.0 1.0 NaN 1.0 0.0 2 [strength, speed, health, speed] [-1, 2, 0, 1.5] 0 3.5 -1 0.0 2.0 0.0 0.0 0.0 1.0
Question: I am trying to write cross-platform app in python. I want to use uvloop (only if possible), using this code: import asyncio try: import uvloop asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) except ImportError: pass  However, pylance shows message that "Import "uvloop" could not be resolved" in Windows (because uvloop is not available for this OS, so that's okay). It's not a big deal, everything works, but this message is annoying. I can set ignore rule, but I want to keep other unresolved import warnings, because in other cases they are pretty useful. How can I tell Pylance, that in this specific case I'm okay with it?

Answer: Set an inline ignore statement: import asyncio try: import uvloop # type: ignore asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) except ImportError: pass  It'd be better to be more specific with the rule you want to ignore. The above will ignore all type checking errors. https://github.com/microsoft/pylance-release/issues/196#issuecomment-668099106