Skip to content
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

Supports list-like Python objects for Series comparison. #2022

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
8 changes: 5 additions & 3 deletions databricks/koalas/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class Index(IndexOpsMixin):
Index(['a', 'b', 'c'], dtype='object')
"""

def __new__(cls, data: Union[DataFrame, list], dtype=None, name=None, names=None):
def __new__(cls, data: Union[DataFrame, list, tuple], dtype=None, name=None, names=None):
from databricks.koalas.indexes.datetimes import DatetimeIndex
from databricks.koalas.indexes.multi import MultiIndex
from databricks.koalas.indexes.numeric import Float64Index, Int64Index
Expand Down Expand Up @@ -353,8 +353,10 @@ def equals(self, other) -> bool:
# some exceptions when 'compute.ops_on_diff_frames' is enabled.
# Working around for now via using frame.
return (
self.to_series("self").reset_index(drop=True)
== other.to_series("other").reset_index(drop=True)
IndexOpsMixin.__eq__(
self.to_series("self").reset_index(drop=True),
other.to_series("other").reset_index(drop=True),
)
itholic marked this conversation as resolved.
Show resolved Hide resolved
).all()
else:
raise ValueError(ERROR_MESSAGE_CANNOT_COMBINE)
Expand Down
19 changes: 18 additions & 1 deletion databricks/koalas/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,10 +657,27 @@ def eq(self, other) -> bool:
d False
Name: b, dtype: bool
"""
return self == other
if isinstance(other, (list, tuple)):
other = ks.Index(other, name=self.name)
# pandas always returns False for all items with dict and set.
elif isinstance(other, (dict, set)):
return self != self
return IndexOpsMixin.__eq__(self, other)

equals = eq

def __eq__(self, other):
Copy link
Member

Choose a reason for hiding this comment

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

Does Index support this case too? it might be best to move to base.py.

if isinstance(other, (list, tuple)):
other = ks.Index(other, name=self.name)
# pandas always returns False for all items with dict and set.
Copy link
Member

Choose a reason for hiding this comment

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

I wonder why pandas behaves like this ..

elif isinstance(other, (dict, set)):
return self != self
# pandas doesn't support `==` for different Index, but support for `eq` function.
elif isinstance(other, ks.Series):
if not self.index.equals(other.index):
raise ValueError("Can only compare identically-labeled Series objects")
return IndexOpsMixin.__eq__(self, other)

itholic marked this conversation as resolved.
Show resolved Hide resolved
def gt(self, other) -> "Series":
"""
Compare if the current value is greater than the other.
Expand Down
83 changes: 83 additions & 0 deletions databricks/koalas/tests/test_ops_on_diff_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,73 @@ def test_index_ops(self):
else:
self.assert_eq(kidx1 * 10 + kidx3, (pidx1 * 10 + pidx3).rename(None))

def test_series_eq(self):
pser = pd.Series([1, 2, 3, 4, 5, 6], name="x")
kser = ks.from_pandas(pser)

# other = Series
pandas_other = pd.Series([np.nan, 1, 3, 4, np.nan, 6], name="x")
koalas_other = ks.from_pandas(pandas_other)
self.assert_eq(pser.eq(pandas_other), kser.eq(koalas_other).sort_index())
self.assert_eq(pser == pandas_other, (kser == koalas_other).sort_index())

# other = Series with different Index
pandas_other = pd.Series(
[np.nan, 1, 3, 4, np.nan, 6], index=[10, 20, 30, 40, 50, 60], name="x"
)
koalas_other = ks.from_pandas(pandas_other)
self.assert_eq(pser.eq(pandas_other), kser.eq(koalas_other).sort_index())
with self.assertRaisesRegex(
ValueError, "Can only compare identically-labeled Series objects"
):
kser == koalas_other

# other = Index
pandas_other = pd.Index([np.nan, 1, 3, 4, np.nan, 6], name="x")
koalas_other = ks.from_pandas(pandas_other)
self.assert_eq(pser.eq(pandas_other), kser.eq(koalas_other).sort_index())
self.assert_eq(pser == pandas_other, (kser == koalas_other).sort_index())

# other = list
other = [np.nan, 1, 3, 4, np.nan, 6]
if LooseVersion(pd.__version__) >= LooseVersion("1.2"):
self.assert_eq(pser.eq(other), kser.eq(other).sort_index())
self.assert_eq(pser == other, (kser == other).sort_index())
else:
self.assert_eq(pser.eq(other).rename("x"), kser.eq(other).sort_index())
self.assert_eq((pser == other).rename("x"), (kser == other).sort_index())

# other = tuple
other = (np.nan, 1, 3, 4, np.nan, 6)
if LooseVersion(pd.__version__) >= LooseVersion("1.2"):
self.assert_eq(pser.eq(other), kser.eq(other).sort_index())
self.assert_eq(pser == other, (kser == other).sort_index())
else:
self.assert_eq(pser.eq(other).rename("x"), kser.eq(other).sort_index())
self.assert_eq((pser == other).rename("x"), (kser == other).sort_index())

# other = list with the different length
other = [np.nan, 1, 3, 4, np.nan]
with self.assertRaisesRegex(
ValueError, "operands could not be broadcast together with shapes"
):
kser.eq(other)
with self.assertRaisesRegex(
ValueError, "operands could not be broadcast together with shapes"
):
kser == other

# other = tuple with the different length
other = (np.nan, 1, 3, 4, np.nan)
with self.assertRaisesRegex(
ValueError, "operands could not be broadcast together with shapes"
):
kser.eq(other)
with self.assertRaisesRegex(
ValueError, "operands could not be broadcast together with shapes"
):
kser == other

def test_align(self):
pdf1 = pd.DataFrame({"a": [1, 2, 3], "b": ["a", "b", "c"]}, index=[10, 20, 30])
pdf2 = pd.DataFrame({"a": [4, 5, 6], "c": ["d", "e", "f"]}, index=[10, 11, 12])
Expand Down Expand Up @@ -1626,6 +1693,22 @@ def test_mask(self):
with self.assertRaisesRegex(ValueError, "Cannot combine the series or dataframe"):
kdf1.mask(kdf2 > -250)

def test_series_eq(self):
pser = pd.Series([1, 2, 3, 4, 5, 6], name="x")
kser = ks.from_pandas(pser)

others = (
ks.Series([np.nan, 1, 3, 4, np.nan, 6], name="x"),
ks.Index([np.nan, 1, 3, 4, np.nan, 6], name="x"),
[np.nan, 1, 3, 4, np.nan, 6],
(np.nan, 1, 3, 4, np.nan, 6),
)
for other in others:
with self.assertRaisesRegex(ValueError, "Cannot combine the series or dataframe"):
kser.eq(other)
with self.assertRaisesRegex(ValueError, "Cannot combine the series or dataframe"):
kser == other

def test_align(self):
pdf1 = pd.DataFrame({"a": [1, 2, 3], "b": ["a", "b", "c"]}, index=[10, 20, 30])
pdf2 = pd.DataFrame({"a": [4, 5, 6], "c": ["d", "e", "f"]}, index=[10, 11, 12])
Expand Down
18 changes: 18 additions & 0 deletions databricks/koalas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2650,6 +2650,24 @@ def test_backfill(self):
kser.backfill(inplace=True)
self.assert_eq(expected, kser)

def test_eq(self):
pser = pd.Series([1, 2, 3, 4, 5, 6], name="x")
kser = ks.from_pandas(pser)

# other = Series
self.assert_eq(pser.eq(pser), kser.eq(kser))
self.assert_eq(pser == pser, kser == kser)

# other = dict
other = {1: None, 2: None, 3: None, 4: None, np.nan: None, 6: None}
self.assert_eq(pser.eq(other), kser.eq(other))
self.assert_eq(pser == other, kser == other)

# other = set
other = {1, 2, 3, 4, np.nan, 6}
self.assert_eq(pser.eq(other), kser.eq(other))
self.assert_eq(pser == other, kser == other)

def test_align(self):
pdf = pd.DataFrame({"a": [1, 2, 3], "b": ["a", "b", "c"]})
kdf = ks.from_pandas(pdf)
Expand Down