diff --git a/src/dppd/base.py b/src/dppd/base.py index fb30e00..6f3c510 100644 --- a/src/dppd/base.py +++ b/src/dppd/base.py @@ -21,7 +21,7 @@ class register_verb: """ - def __init__(self, name=None, types=None, pass_dppd=False): + def __init__(self, name=None, types=None, pass_dppd=False, ignore_redefine=False): """ Parameters: ----------- @@ -43,6 +43,7 @@ def __init__(self, name=None, types=None, pass_dppd=False): if t not in property_registry: property_registry[t] = set() self.pass_dppd = pass_dppd + self.ignore_redefine = ignore_redefine def __call__(self, func): if self.names is None: @@ -76,9 +77,11 @@ def inner(*args, **kwargs): if (real_name, t) in verb_registry and verb_registry[ (real_name, t) ] != func: - warnings.warn(f"redefining verb {real_name} for type {t}") - if t in property_registry and real_name in property_registry[t]: - warnings.warn(f"verb {real_name} shadows property for type {t}") + if not self.ignore_redefine: + print(verb_registry.keys()) + warnings.warn(f"redefining verb {real_name} for type {t}") + if t in property_registry and real_name in property_registry[t]: + warnings.warn(f"verb {real_name} shadows property for type {t}") outer.__doc__ == func.__doc__ for t in self.types: @@ -167,6 +170,14 @@ def pd(self): self.X._self_update_wrapped(self.parent.df) return result + def dir_dppd(self): + """Return just the newly registered verbs, not the wrapped ones""" + total = set(dir(self)) + old = set(dir(self.df)) + new = total - old + return sorted(new) + + def __call__(self, df=None): if df is None: if self.df is None: diff --git a/src/dppd/single_verbs.py b/src/dppd/single_verbs.py index e14a2ce..1e8f8f8 100644 --- a/src/dppd/single_verbs.py +++ b/src/dppd/single_verbs.py @@ -989,7 +989,7 @@ def colspec_DataFrame(df, columns, invert=False): @register_verb("pca", types=pd.DataFrame) -def pca_dataframe(df, whiten=False, random_state=None): +def pca_dataframe(df, whiten=False, random_state=None, n_components=2): """Perform 2 component PCA using sklearn.decomposition.PCA. Expects samples in rows! Returns a tuple (DataFrame{sample, 1st, 2nd}, @@ -998,7 +998,7 @@ def pca_dataframe(df, whiten=False, random_state=None): from sklearn.decomposition import PCA import warnings - p = PCA(n_components=2, whiten=whiten, random_state=random_state) + p = PCA(n_components=n_components, whiten=whiten, random_state=random_state) df_fit = pd.DataFrame(p.fit_transform(df)) df_fit.columns = ["1st", "2nd"] df_fit.index = df.index @@ -1008,3 +1008,11 @@ def pca_dataframe(df, whiten=False, random_state=None): warnings.simplefilter("ignore") df_fit.explained_variance_ratio_ = p.explained_variance_ratio_ return df_fit + + +@register_verb("insert", types=pd.DataFrame, ignore_redefine=True) +def insert_return_self(df, loc, column, value, **kwargs): + """DataFrame.insert, but return self. + """ + df.insert(loc, column, value, **kwargs) + return df