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 8 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 @@ -1275,6 +1275,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
13 changes: 12 additions & 1 deletion pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pandas.util._decorators import cache_readonly

from pandas.core.dtypes.common import (
is_dict_like, is_extension_type, is_list_like, is_sequence)
is_dict_like, is_extension_type, is_list_like, is_sequence, is_sparse)
from pandas.core.dtypes.generic import ABCSeries

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

# ufunc
elif isinstance(self.f, np.ufunc):
for dtype in self.obj.dtypes:
Copy link
Contributor

Choose a reason for hiding this comment

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

so just do

if any(is_sparse(dtype) for dtype in self.obj.dtypes)):
   ....

# Column-by-column construction is slow, so only use
# when necessary (e.g. to preserve special dtypes)
if is_sparse(dtype): # GH 23744
result = self.obj._constructor(index=self.index,
copy=False)
with np.errstate(all='ignore'):
for col in self.columns:
result[col] = self.f(self.obj[col].values)
return result

with np.errstate(all='ignore'):
results = self.f(self.values)
return self.obj._constructor(data=results, index=self.index,
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/sparse/frame/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,14 @@ def test_applymap(frame):
# just test that it works
result = frame.applymap(lambda x: x * 2)
assert isinstance(result, SparseDataFrame)


def test_apply_keep_sparse_dtype():
# GH 23744
df = SparseDataFrame(np.array([[0, 1, 0], [0, 0, 0], [0, 0, 1]]),
columns=['a', 'b', 'c'], default_fill_value=1)
df2 = DataFrame(df)
Copy link
Contributor

Choose a reason for hiding this comment

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

call this expected


df = df.apply(np.exp)
Copy link
Contributor

Choose a reason for hiding this comment

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

call this result

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