-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
Keep subclassing in apply #19823
Keep subclassing in apply #19823
Changes from 8 commits
0df8689
c3c1e5f
0ef4bcf
7fd1178
021e681
c549397
956143c
dd929a8
06e11d2
e6bac37
e4f1c3e
8f7a0b4
87f2c5c
b22e090
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -514,3 +514,55 @@ def test_subclassed_wide_to_long(self): | |
long_frame = pd.wide_to_long(df, ["A", "B"], i="id", j="year") | ||
|
||
tm.assert_frame_equal(long_frame, expected) | ||
|
||
def test_subclassed_apply(self): | ||
# GH 19822 | ||
|
||
def check_row_subclass(row): | ||
assert isinstance(row, tm.SubclassedSeries) | ||
|
||
def strech(row): | ||
if row["variable"] == "height": | ||
row["value"] += 0.5 | ||
return row | ||
|
||
df = tm.SubclassedDataFrame([ | ||
['John', 'Doe', 'height', 5.5], | ||
['Mary', 'Bo', 'height', 6.0], | ||
['John', 'Doe', 'weight', 130], | ||
['Mary', 'Bo', 'weight', 150]], | ||
columns=['first', 'last', 'variable', 'value']) | ||
|
||
expected1 = tm.SubclassedDataFrame([ | ||
['John', 'Doe', 'height', 6.0], | ||
['Mary', 'Bo', 'height', 6.5], | ||
['John', 'Doe', 'weight', 130], | ||
['Mary', 'Bo', 'weight', 150]], | ||
columns=['first', 'last', 'variable', 'value']) | ||
|
||
expected2 = tm.SubclassedDataFrame([ | ||
[1, 2, 3], | ||
[1, 2, 3], | ||
[1, 2, 3], | ||
[1, 2, 3]]) | ||
|
||
expected3 = DesignSeries([[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this needs to be updated. |
||
|
||
df.apply(lambda x: check_row_subclass(x)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you need to check the result of this using |
||
df.apply(lambda x: check_row_subclass(x), axis=1) | ||
|
||
result1 = df.apply(lambda x: strech(x), axis=1) | ||
assert isinstance(result1, tm.SubclassedDataFrame) | ||
tm.assert_frame_equal(result1, expected1) | ||
|
||
result2 = df.apply(lambda x: DesignSeries([1, 2, 3]), axis=1) | ||
assert isinstance(result2, tm.SubclassedDataFrame) | ||
tm.assert_frame_equal(result2, expected2) | ||
|
||
result3 = df.apply(lambda x: [1, 2, 3], axis=1) | ||
assert not isinstance(result3, tm.SubclassedDataFrame) | ||
tm.assert_series_equal(result3, expected3) | ||
|
||
result4 = df.apply(lambda x: [1, 2, 3], axis=1, result_type="expand") | ||
assert isinstance(result4, tm.SubclassedDataFrame) | ||
tm.assert_frame_equal(result4, expected2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this not so clear. It is about the Series type that is passed to the function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
When
apply
ing a function to a subclassedDataFrame
I realised I could not call the specific methods of the subclassedSeries
that thisDataFrame
was supossed to generate through_constructor_sliced
. As far as I understood, the subclassing of theSeries
was lost duringapply
.With these changes the subclass is now kept inside
apply
, so method of the expectedSeries
subclass can be used.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, can you try to clarify the text a bit? Eg something like "For subclassed DataFrames, DataFrame.apply will now preserve the Series subclass (if defined) when passing the data to the applied function" (but adapt as you like)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure! I like it as this! :)
will change it!