-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 8 commits
b85bdb9
ad33f76
c39fe11
4aba3f8
bcdf01b
79be557
99c8796
0868c47
de0ecf3
491b908
f6230f6
42ca43a
ee2c462
bca539f
d8670ef
30d83a6
c15afe3
b4ab44b
d153f74
d6e22a8
8f151dc
be8750f
551ced8
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 |
---|---|---|
|
@@ -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 | ||
|
@@ -131,6 +131,17 @@ def get_result(self): | |
|
||
# ufunc | ||
elif isinstance(self.f, np.ufunc): | ||
for dtype in self.obj.dtypes: | ||
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. so just do
|
||
# 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, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
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. call this expected |
||
|
||
df = df.apply(np.exp) | ||
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. call this result |
||
df2 = df2.apply(np.exp) | ||
tm.assert_frame_equal(df, df2) |
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.
move this to the Sparse section