Skip to content

BUG GH23744 ufuncs on DataFrame keeps dtype sparseness #23755

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

Merged
merged 23 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b85bdb9
BUG-23744 DataFrame.apply keeps dtype sparseness
JustinZhengBC Nov 17, 2018
ad33f76
BUG-23744 Fix memory usage
JustinZhengBC Nov 17, 2018
c39fe11
BUG-23744 Remove unnecessary check
JustinZhengBC Nov 17, 2018
4aba3f8
BUG-23744 fix import lint
JustinZhengBC Nov 17, 2018
bcdf01b
BUG-23744 fix test
JustinZhengBC Nov 17, 2018
79be557
merge
JustinZhengBC Nov 18, 2018
99c8796
BUG-23744 move test and avoid inefficiency
JustinZhengBC Nov 19, 2018
0868c47
Merge master
JustinZhengBC Nov 23, 2018
de0ecf3
BUG-23744 make requested changes
JustinZhengBC Nov 23, 2018
491b908
BUG-23744 make requested changes
JustinZhengBC Nov 23, 2018
f6230f6
Merge branch 'BUG-23744' of https://github.com/justinzhengbc/pandas i…
JustinZhengBC Nov 23, 2018
42ca43a
Merge branch 'BUG-23744' of https://github.com/justinzhengbc/pandas i…
JustinZhengBC Nov 23, 2018
ee2c462
Merge branch 'BUG-23744' of https://github.com/justinzhengbc/pandas i…
JustinZhengBC Nov 23, 2018
bca539f
BUG-23744 use list comprehension
JustinZhengBC Nov 23, 2018
d8670ef
BUG-23744 use for loop instead
JustinZhengBC Nov 23, 2018
30d83a6
Merge branch 'master' into BUG-23744
JustinZhengBC Nov 24, 2018
c15afe3
BUG-23744 fix test
JustinZhengBC Nov 24, 2018
b4ab44b
BUG-23744 fix other test
JustinZhengBC Nov 24, 2018
d153f74
BUG-23744 use constructor properly
JustinZhengBC Nov 25, 2018
d6e22a8
BUG-23744 use block apply
JustinZhengBC Nov 26, 2018
8f151dc
BUG-23744 clarify test
JustinZhengBC Nov 27, 2018
be8750f
BUG-23744 clarify test
JustinZhengBC Nov 27, 2018
551ced8
Merge branch 'BUG-23744' of https://github.com/justinzhengbc/pandas i…
JustinZhengBC Nov 27, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,7 @@ Numeric
- Bug in :meth:`Series.rpow` with object dtype ``NaN`` for ``1 ** NA`` instead of ``1`` (:issue:`22922`).
- :meth:`Series.agg` can now handle numpy NaN-aware methods like :func:`numpy.nansum` (:issue:`19629`)
- Bug in :meth:`Series.rank` and :meth:`DataFrame.rank` when ``pct=True`` and more than 2:sup:`24` rows are present resulted in percentages greater than 1.0 (:issue:`18271`)
- Bug in :meth:`DataFrame.apply` where dtypes would lose sparseness (:issue:`23744`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this to the Sparse section


Strings
^^^^^^^
Expand Down
14 changes: 9 additions & 5 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
is_extension_type,
is_dict_like,
is_list_like,
is_sequence)
is_sequence,
is_sparse)
from pandas.util._decorators import cache_readonly

from pandas.io.formats.printing import pprint_thing
Expand Down Expand Up @@ -131,10 +132,13 @@ def get_result(self):

# ufunc
elif isinstance(self.f, np.ufunc):
with np.errstate(all='ignore'):
results = self.f(self.values)
return self.obj._constructor(data=results, index=self.index,
columns=self.columns, copy=False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably need to pass columns here to ensure that the columns are in the correct order. Add a test where the columns aren't sorted (like ['b', 'a', 'c']) and verify that it fails on py35 and older.

result = self.obj._constructor(index=self.index, copy=False)
for col in self.columns:
if is_sparse(self.obj.dtypes[col]):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is closer, but I don't think it should be special casing sparse. The bug applies to any extension array.

#23293 is fixing ufuncs on series. So extracting .values won't be necessary. I'd recommend waiting for #23293 and seeing if you can just do result[col] = self.f(self.obj[col]).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the sparse check in the last commit because it turns out calling values on normal columns is also acceptable (should generalize to other array types as well). I do agree that it would look neater if calling values wasn't required at all though.

result[col] = self.f(self.obj[col].values)
else:
result[col] = self.f(self.obj[col])
return result

# broadcasting
if self.result_type == 'broadcast':
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/frame/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,16 @@ def test_apply_dup_names_multi_agg(self):

tm.assert_frame_equal(result, expected)

def test_apply_keep_sparse_dtype(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be in the sparse frame tests

# GH 23744
df = pd.SparseDataFrame(np.array([[0, 1, 0], [0, 0, 0], [0, 0, 1]]),
columns=['a', 'b', 'c'], default_fill_value=1)
df2 = pd.DataFrame(df)

df = df.apply(np.exp)
df2 = df2.apply(np.exp)
tm.assert_frame_equal(df, df2)


class TestInferOutputShape(object):
# the user has supplied an opaque UDF where
Expand Down