-
Notifications
You must be signed in to change notification settings - Fork 358
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
Implements Index.putmask #1560
base: master
Are you sure you want to change the base?
Implements Index.putmask #1560
Changes from 8 commits
3d08718
5e2799b
51f6d4a
c83568f
c4bd2b4
c341272
9bd21b1
097cc3a
9bbfda4
f72ee50
d4e4cbd
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 |
---|---|---|
|
@@ -287,6 +287,36 @@ def test_dropna(self): | |
self.assert_eq(kidx.dropna(), pidx.dropna()) | ||
self.assert_eq((kidx + 1).dropna(), (pidx + 1).dropna()) | ||
|
||
def test_putmask(self): | ||
pidx = pd.Index(["a", "b", "c", "d", "e"]) | ||
kidx = ks.from_pandas(pidx) | ||
|
||
self.assert_eq( | ||
kidx.putmask(kidx < "c", "k").sort_values(), pidx.putmask(pidx < "c", "k").sort_values() | ||
) | ||
self.assert_eq( | ||
kidx.putmask(kidx < "c", ["g", "h", "i", "j", "k"]).sort_values(), | ||
pidx.putmask(pidx < "c", ["g", "h", "i", "j", "k"]).sort_values(), | ||
) | ||
self.assert_eq( | ||
kidx.putmask(kidx < "c", ("g", "h", "i", "j", "k")).sort_values(), | ||
pidx.putmask(pidx < "c", ("g", "h", "i", "j", "k")).sort_values(), | ||
) | ||
self.assert_eq( | ||
kidx.putmask(kidx < "c", ks.Index(["g", "h", "i", "j", "k"])).sort_values(), | ||
pidx.putmask(pidx < "c", pd.Index(["g", "h", "i", "j", "k"])).sort_values(), | ||
) | ||
self.assert_eq( | ||
kidx.putmask(kidx < "c", ks.Series(["g", "h", "i", "j", "k"])).sort_values(), | ||
pidx.putmask(pidx < "c", pd.Series(["g", "h", "i", "j", "k"])).sort_values(), | ||
) | ||
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. What if the length of 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. @ueshin Thanks for the comment! I will address it as you comments. :) 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. @ueshin >>> pidx
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
>>> pidx.putmask([True, False], pd.Series(["g", "h", "i", "j", "k"])).sort_values()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/hwpark/Desktop/git_koalas/venv/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 4041, in putmask
raise err
File "/Users/hwpark/Desktop/git_koalas/venv/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 4037, in putmask
np.putmask(values, mask, self._convert_for_op(value))
File "<__array_function__ internals>", line 6, in putmask
ValueError: putmask: mask and data must be the same size So I fixed Koalas to raise the same error as well. >>> kidx.putmask([True, False], ks.Series(["g", "h", "i", "j", "k"])).sort_values()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/hwpark/Desktop/git_koalas/koalas/databricks/koalas/indexes.py", line 1612, in putmask
raise ValueError("mask and data must be the same size")
ValueError: mask and data must be the same size If the >>> pidx
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
>>> pidx.putmask(pidx > 'c', pd.Series(["g", "h"])).sort_values()
Index(['a', 'b', 'c', 'g', 'h'], dtype='object')
>>> pidx.putmask(pidx < 'c', pd.Series(["g", "h"])).sort_values()
Index(['c', 'd', 'e', 'g', 'h'], dtype='object')
>>> pidx.putmask(pidx < 'c', pd.Series(["g"])).sort_values()
Index(['c', 'd', 'e', 'g', 'g'], dtype='object')
>>> pidx.putmask([True, False, True, False, True], pd.Series(["g", "h"])).sort_values()
Index(['b', 'd', 'g', 'g', 'g'], dtype='object') I thought the behavior of Pandas was ambiguous, so I left the comments at line 1593 for now.
|
||
|
||
with self.assertRaisesRegexp(ValueError, "value and data must be the same size"): | ||
kidx.putmask(kidx < "c", ks.Series(["g", "h"])) | ||
|
||
with self.assertRaisesRegexp(ValueError, "mask and data must be the same size"): | ||
kidx.putmask([True, False], ks.Series(["g", "h", "i", "j", "k"])) | ||
|
||
def test_index_symmetric_difference(self): | ||
pidx1 = pd.Index([1, 2, 3, 4]) | ||
pidx2 = pd.Index([2, 3, 4, 5]) | ||
|
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.
If we can support for only same size, I think we shouldn't support this API for non-scalar objects for now.
Since we're using
pd.Series(value)
andvalue.to_pandas()
above, It looks quite dangerous.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 think we better support this API only for the
ks.Index
so that we can avoid the collect all the data into single machine.Maybe I think we can apply almost same concept with implementation of
Series.where
. (https://koalas.readthedocs.io/en/latest/_modules/databricks/koalas/series.html#Series.where)Would you tell me what do you think about this way when you available, @ueshin @HyukjinKwon ?