-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
BUG: Consistent division by zero behavior for Index/Series #27321
Changes from 24 commits
6bd4fb1
d9943be
eb8b6d7
3edebac
eb60fc3
3cf8b1e
c34a9cc
158b003
b59b81a
f085d6b
339ed84
f03bd93
cd2f564
e362a08
7c6e127
97611c3
c30b40c
1ed12b8
45ffbc2
591eaaa
2841be0
518e2e9
2580048
f440c59
188ac99
6b411c4
77c2383
7b8880c
14010c8
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import operator | ||
from types import LambdaType | ||
|
||
import numpy as np | ||
from numpy import nan | ||
|
@@ -9,6 +10,7 @@ | |
|
||
import pandas as pd | ||
from pandas import DataFrame, Series, bdate_range, compat | ||
from pandas.core import ops | ||
from pandas.core.indexes.datetimes import DatetimeIndex | ||
from pandas.core.sparse import frame as spf | ||
from pandas.core.sparse.api import ( | ||
|
@@ -424,6 +426,11 @@ def _compare_to_dense(a, b, da, db, op): | |
sparse_result = op(a, b) | ||
dense_result = op(da, db) | ||
|
||
if op in [operator.floordiv, ops.rfloordiv] or isinstance(op, LambdaType): | ||
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. why is this not just 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. Because we don't want e.g. 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. k can you add that as a comment (future PR ok), as its not obvious 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. sure |
||
# Series sets 1//0 to np.inf, which SparseArray does not do (yet) | ||
mask = np.isinf(dense_result) & ~np.isinf(sparse_result.to_dense()) | ||
dense_result[mask] = np.nan | ||
|
||
fill = sparse_result.default_fill_value | ||
dense_result = dense_result.to_sparse(fill_value=fill) | ||
tm.assert_sp_frame_equal(sparse_result, dense_result, exact_indices=False) | ||
|
@@ -436,7 +443,6 @@ def _compare_to_dense(a, b, da, db, op): | |
) | ||
|
||
opnames = ["add", "sub", "mul", "truediv", "floordiv"] | ||
ops = [getattr(operator, name) for name in opnames] | ||
|
||
fidx = frame.index | ||
|
||
|
@@ -466,6 +472,7 @@ def _compare_to_dense(a, b, da, db, op): | |
f = lambda a, b: getattr(a, op)(b, axis="index") | ||
_compare_to_dense(frame, s, frame.to_dense(), s.to_dense(), f) | ||
|
||
# FIXME: dont leave commented-out | ||
# rops are not implemented | ||
# _compare_to_dense(s, frame, s.to_dense(), | ||
# frame.to_dense(), f) | ||
|
@@ -479,13 +486,14 @@ def _compare_to_dense(a, b, da, db, op): | |
frame.xs(fidx[5])[:2], | ||
] | ||
|
||
for op in ops: | ||
for name in opnames: | ||
op = getattr(operator, name) | ||
for s in series: | ||
_compare_to_dense(frame, s, frame.to_dense(), s, op) | ||
_compare_to_dense(s, frame, s, frame.to_dense(), op) | ||
|
||
# it works! | ||
result = frame + frame.loc[:, ["A", "B"]] # noqa | ||
frame + frame.loc[:, ["A", "B"]] | ||
|
||
def test_op_corners(self, float_frame, empty_frame): | ||
empty = empty_frame + empty_frame | ||
|
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.
in future PR can you give some comments on what you ar doing here (filling np.inf on the integer divide)
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