New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use #3
Comments
Hi @ericmjl, great to meet you too! There are two ways you could expose 1. Add an accessor with methods underneathThe recommended way is to add them underneath an accessor object. This would look like: import pandas as pd
import janitor
df = pd.DataFrame(...)
df = df.janitor.clean_names()
df = df.janitor.remove_empty() When you import To add an accessor and methods: import pandas_flavor
@pandas_flavor.register_dataframe_accessor('janitor')
class JanitorAccessor(object):
def __init__(self, df):
self.df = df
def clean_names(self):
... 2. Add methods directly to the DataFrameYour second option is to add methods directly to the DataFrame. This would allow you to chain commands like in your example above. The methods are added to the DataFrame object itself, before initialization. This would look like: import pandas as pd
import janitor
df = pd.DataFrame(...).clean_names().remove_empty() To add methods, simple write them as functions and register them with the DF. import pandas_flavor
@pandas_flavor.register_dataframe_method
def clean_names(df):
... Does this help answer your question? |
The part that I was missing was that I just had to import janitor, and do nothing with it afterwards 😄. Thanks for clarifying! One thing that does happen with Pyjanitor though, is that upon decoration, my functions (which all return a dataframe) now return |
Ah, you're totally right! There should be return statements inside the We need to add a and If you'd like to put in a PR, that would be great! Otherwise, I can do it later today. Thanks! |
I'm on it! |
Hey @Zsailer, great to meet you at SciPy 2018!
I think
pandas_flavor
is what I'd like to switch over to inpyjanitor
, where I simply register functions as a pandas accessor rather than subclass the entire dataframe outright.There is something a bit magical about how
pandas_flavor
works though. With subclassing, everything is quite transparent - I subclass pandas DataFrames, then have the users wrap their existing dataframe inside a Janitor dataframe, following which, all of the data cleaning methods are available:Say I decorated the Janitor functions as pandas accessors. How would things look like for an end-user? Would it be like the following?
I guess I'm just wondering, where and when does a decorated function get exposed up to pandas?
Thanks again for putting this out!
The text was updated successfully, but these errors were encountered: