Skip to content

Commit

Permalink
pandas.DataFrame insert returning self, dp.dir_dppd() returning just …
Browse files Browse the repository at this point in the history
…the newly added functions
  • Loading branch information
TyberiusPrime committed Nov 27, 2023
1 parent 2553a2c commit 5e345b1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
19 changes: 15 additions & 4 deletions src/dppd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
-----------
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 10 additions & 2 deletions src/dppd/single_verbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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
Expand All @@ -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

0 comments on commit 5e345b1

Please sign in to comment.