The implementation of Sequence interface is not sufficient to be a Sequence

Advertisements I’ve noticed that duck typing works reasonably well up to a certain point in checking whether a class is an instance of an abstract class. For example, to be an instance of collections.abc.Sized, we only need to define a __len__() method: import collections.abc class MySized: def __len__(self): return 0 assert isinstance(MySized(), collections.abc.Sized) # It… Read More The implementation of Sequence interface is not sufficient to be a Sequence

Alternative that doesn't violate the Liskov substitution principle

Advertisements Considering the following structure of classes: from abc import ABC, abstractmethod class ModelSettings(ABC): … class BlueModelSettings(ModelSettings): … class RedModelSettings(ModelSettings): … class Model(ABC): @abstractmethod def compute(self, settings: ModelSettings): … class BlueModel(Model): def compute(self, settings: BlueModelSettings): … class RedModel(Model): def compute(self, settings: RedModelSettings): … MyPy is complaining that the implementations of compute in BlueModel and RedModel… Read More Alternative that doesn't violate the Liskov substitution principle

solving nested renamer is not supported with dynamic arguments

Advertisements if cat_vars: df["static_cat"] = ( df.groupby("group_col") .agg({i: "first" for i in cat_vars}) .values.tolist() ) Error: packages\pandas\core\groupby\generic.py in aggregate(self, func, *args, **kwargs) 926 func = _maybe_mangle_lambdas(func) 927 –> 928 result, how = self._aggregate(func, *args, **kwargs) 929 if how is None: 930 return result packages\pandas\core\base.py in _aggregate(self, arg, *args, **kwargs) 355 obj.columns.intersection(keys) 356 ) != len(keys):… Read More solving nested renamer is not supported with dynamic arguments

pandas multIndex from product – ignore same row comparison

Advertisements I have a pandas dataframe like as shown below Company,year T123 Inc Ltd,1990 T124 PVT ltd,1991 ABC Limited,1992 ABCDE Ltd,1994 tf = pd.read_clipboard(sep=’,’) tf[‘Company_copy’] = tf[‘Company’] I would like to compare each value from tf[‘company’] against each value of tf[‘company_copy] but exclude same matching row number or index number, string For ex: I want… Read More pandas multIndex from product – ignore same row comparison

Dynamic top 3 and percentage total using pandas groupby

Advertisements I have a dataframe like as shown below id,Name,country,amount,qty 1,ABC,USA,123,4500 1,ABC,USA,156,3210 1,BCE,USA,687,2137 1,DEF,UK,456,1236 1,ABC,nan,216,324 1,DEF,nan,12678,11241 1,nan,nan,637,213 1,BCE,nan,213,543 1,XYZ,KOREA,432,321 1,XYZ,AUS,231,321 sf = pd.read_clipboard(sep=’,’) I would like to do the below a) Get top 3 based on amount for each id and other selected columns such as Name and country. Meaning, we get top 3 based… Read More Dynamic top 3 and percentage total using pandas groupby