You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Does there need to be a way to get back the underlying object?
I'm thinking about the pyjanitorclean_names example
Some user starts with a DataFrame (say, a pandas one) df, and calls clean_names(df). They would probably expect to get back what they started with, without caring that PyJanitor internally used the standard.
For example, PyJanitor could do
def clean_names(df, ...):
df = dataframe_standard(df) # or whatever the way to enable the standard will be
df = ... # clean names
return df.dataframe # return same type of DataFrame as was passed
So, should some .dataframe property be added, so that the library can "opt-out" of the standard once it has done all its work?
Or if it's about regular API methods/functions, then those should already give back instances of the correct dataframe type, right? There's no separate object to convert to/from.
My understanding was that the standard would be implemented as a separate class, with which would wrap the DataFrame, something like
classPandasStandardDataFrame:
def__init__(self, df):
_validate_df(df) # check all columns are strings, no duplicate columnsself.df=dfdefdrop_column(self, label):
returnPandasDataFrame(self.df.drop(label, axis=1))
defget_columns_by_name(self, names):
ifnotisinstance(names, list) andnotall(isinstance(name, str) fornameinnames):
raiseTypeError("Expected list of str")
returnPandasDataFrame(self.df.loc[:, names])
df# pandas dataframedf_standard=PandasStandardDataFrame(df) # enable standard modedf_standard=df_standard.drop_column("y") # use some method from the standarddf_standard=df_standard.get_columns_by_name(["x_0", "x_1"]) # keep using methods from the standarddf=df_standard.df# go back to having a pandas dataframe
If drop_column were to return a DataFrame of the correct type, then are you suggesting that the workflow be
df# pandas dataframedf=PandasStandardDataFrame(df).drop_column("y") # use some method from the standarddf=PandasStandardDataFrame(df).get_columns_by_name(["x_0", "x_1"]) # use another method from the standard
My understanding was that the standard would be implemented as a separate class
Ah fair enough, you are right here. I was applying my array intuition too much - we really need the separate dataframe class because we design with methods not functions.
So the question is how to spell what you need here. You suggested df.dataframe or df_standard.df, so attribute access. I'm thinking that this isn't something that one expects on the standard dataframe (at least it won't be part of the standard itself) and this would make sense as a regular constructor. So for pandas, pd.DataFrame(df_standard)?
That's pretty expensive though, having to iterate through memory. This is within a single library so I'd just use a private ._df_pandasbase attribute and then the constructor like:
I think accessing it from the object might be easier (with an attribute or method), because otherwise you need to know which namespace and function to use? For pandas it could be pd.DataFrame, but what is it for some standard dataframe from a library you don't know?
I think accessing it from the object might be easier (with an attribute or method),
That bakes in the assumption though that the "native dataframe" exists, and that there's a 1:1 relationship between any implementer of the standard and some other underlying dataframe object within the same library. I'm not sure that that assumption will hold - say you write a new library that only implements the standard, natively, plus the interchange protocol to transform itself into any other library's df object.
Or if you'd have a .df attribute in such cases, would you point it at self?
Yes, I think if the standard dataframe is the "native" object itself, it can just return itself, that doesn't seem like a problem (similarly like the interchange object also returns itself in __dataframe__)
But maybe we should also first think about the question: as a user of the standard API, how do you get a "standard" object given a random dataframe?
Let's keep the question of opting into the standard for a separate issue
Or if you'd have a .df attribute in such cases, would you point it at self?
Sounds fine
at least it won't be part of the standard itself
In order for this to be usable, I don't see how it can not be part of the standard - otherwise how can a library implement a function like
defmy_fancy_function(df: AnyDataFrame):
standard_df=dataframe_standard(df) # we still need to agree on how to opt-in to the standardstandard_df=standard_df.get_columns_by_name(...) # bunch of operations which use the standardreturnstandard_df.df
and be guaranteed that it'll work for any DataFrame?
Let's keep the question of opting into the standard for a separate issue
We can certainly discuss it separately, but I think the exact answer for this issue could depend on it. For example, if we define a namespace, we could also have a function in that namespace instead of a method or attribute.
(anyway, I also don't think the exact API for how to get the object is very important, the relevant discussion in this issue is probably about the concept that this can be useful essential to do)
we could also have a function in that namespace instead of a method or attribute.
sure, but it would still need to be the same for all DataFrame libraries taking part, right? Otherwise, in the example in #108 (comment) , how does one write DataFrame-agnostic code?
I'd have thought this was essential, not just useful
Okay, so there is agreement we do need this in some form. Do we think this is always an O(1) operation? If so, an attribute seems reasonable. If it can trigger computation, it should be either a method, or a way to retrieve a constructor function as in gh-85 (that's more complex, which is probably justified for the interchange protocol but not here).
Activity
rgommers commentedon Mar 14, 2023
I think this is gh-85?
rgommers commentedon Mar 14, 2023
Or if it's about regular API methods/functions, then those should already give back instances of the correct dataframe type, right? There's no separate object to convert to/from.
MarcoGorelli commentedon Mar 14, 2023
My understanding was that the standard would be implemented as a separate class, with which would wrap the DataFrame, something like
If
drop_columnwere to return a DataFrame of the correct type, then are you suggesting that the workflow be?
rgommers commentedon Mar 14, 2023
Ah fair enough, you are right here. I was applying my array intuition too much - we really need the separate dataframe class because we design with methods not functions.
So the question is how to spell what you need here. You suggested
df.dataframeordf_standard.df, so attribute access. I'm thinking that this isn't something that one expects on the standard dataframe (at least it won't be part of the standard itself) and this would make sense as a regular constructor. So for pandas,pd.DataFrame(df_standard)?jbrockmendel commentedon Mar 14, 2023
Is there a viable way to do this going through the interchange protocol?
rgommers commentedon Mar 14, 2023
That's pretty expensive though, having to iterate through memory. This is within a single library so I'd just use a private
._df_pandasbaseattribute and then the constructor like:jbrockmendel commentedon Mar 14, 2023
id like to find an alternative that fits with the "assume pandas changes nothing" mantra
jorisvandenbossche commentedon Mar 14, 2023
I think accessing it from the object might be easier (with an attribute or method), because otherwise you need to know which namespace and function to use? For pandas it could be
pd.DataFrame, but what is it for some standard dataframe from a library you don't know?jorisvandenbossche commentedon Mar 14, 2023
Unless it would be a method in the "standard namespace" (if we will have something like that)
rgommers commentedon Mar 14, 2023
That bakes in the assumption though that the "native dataframe" exists, and that there's a 1:1 relationship between any implementer of the standard and some other underlying dataframe object within the same library. I'm not sure that that assumption will hold - say you write a new library that only implements the standard, natively, plus the interchange protocol to transform itself into any other library's df object.
Or if you'd have a
.dfattribute in such cases, would you point it atself?jorisvandenbossche commentedon Mar 15, 2023
Yes, I think if the standard dataframe is the "native" object itself, it can just return itself, that doesn't seem like a problem (similarly like the interchange object also returns itself in
__dataframe__)But maybe we should also first think about the question: as a user of the standard API, how do you get a "standard" object given a random dataframe?
MarcoGorelli commentedon Mar 15, 2023
Let's keep the question of opting into the standard for a separate issue
Sounds fine
In order for this to be usable, I don't see how it can not be part of the standard - otherwise how can a library implement a function like
and be guaranteed that it'll work for any DataFrame?
jorisvandenbossche commentedon Mar 15, 2023
We can certainly discuss it separately, but I think the exact answer for this issue could depend on it. For example, if we define a namespace, we could also have a function in that namespace instead of a method or attribute.
(anyway, I also don't think the exact API for how to get the object is very important, the relevant discussion in this issue is probably about the concept that this can be
usefulessential to do)MarcoGorelli commentedon Mar 15, 2023
sure, but it would still need to be the same for all DataFrame libraries taking part, right? Otherwise, in the example in #108 (comment) , how does one write DataFrame-agnostic code?
I'd have thought this was essential, not just useful
jorisvandenbossche commentedon Mar 15, 2023
Yes, to be clear I fully agree with that. Updated my comment above to not use the mere "useful" ;)
rgommers commentedon Mar 15, 2023
Okay, so there is agreement we do need this in some form. Do we think this is always an O(1) operation? If so, an attribute seems reasonable. If it can trigger computation, it should be either a method, or a way to retrieve a constructor function as in gh-85 (that's more complex, which is probably justified for the interchange protocol but not here).
rgommers commentedon Mar 16, 2023
Discussed today: folks agreed that this should exist and be cheap. Hence: an attribute
.dataframe.