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

BUG: binary operations with custom classes raise errors #5236

Closed
3 tasks done
mvashishtha opened this issue Nov 18, 2022 · 0 comments · Fixed by #5237
Closed
3 tasks done

BUG: binary operations with custom classes raise errors #5236

mvashishtha opened this issue Nov 18, 2022 · 0 comments · Fixed by #5237
Assignees
Labels
bug 🦗 Something isn't working P2 Minor bugs or low-priority feature requests pandas 🤔 Weird Behaviors of Pandas

Comments

@mvashishtha
Copy link
Collaborator

mvashishtha commented Nov 18, 2022

Modin version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest released version of Modin.

  • I have confirmed this bug exists on the main branch of Modin. (In order to do this you can follow this guide.)

Reproducible Example

import modin.pandas as pd
import pandas

class CustomNumber():
  def __init__(self, value: int):
    self.value = value
  def __add__(self, other):
    return self.value + other
  def __radd__(self, other):
    return other + self.value

# pandas works without error and gives series of [5]
print(pandas.Series([1]) + CustomNumber(4))
# modin raises error
print(pd.Series([1]) + CustomNumber(4))

Issue Description

other is not a scalar in _validate_other here, so we check dtype here and raise TypeError.

Expected Behavior

Can add custom classes that overload addition operators to pandas objects.

Error Logs

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [1], in <cell line: 15>()
     13 print(pandas.Series([1]) + CustomNumber(4))
     14 # raises error
---> 15 print(pd.Series([1]) + CustomNumber(4))

File ~/software_sources/modin/modin/logging/logger_decorator.py:128, in enable_logging.<locals>.decorator.<locals>.run_and_log(*args, **kwargs)
    113 """
    114 Compute function with logging if Modin logging is enabled.
    115
   (...)
    125 Any
    126 """
    127 if LogMode.get() == "disable":
--> 128     return obj(*args, **kwargs)
    130 logger = get_logger()
    131 logger_level = getattr(logger, log_level)

File ~/software_sources/modin/modin/pandas/series.py:172, in Series.__add__(self, right)
    170 @_doc_binary_op(operation="addition", bin_op="add")
    171 def __add__(self, right):
--> 172     return self.add(right)

File ~/software_sources/modin/modin/logging/logger_decorator.py:128, in enable_logging.<locals>.decorator.<locals>.run_and_log(*args, **kwargs)
    113 """
    114 Compute function with logging if Modin logging is enabled.
    115
   (...)
    125 Any
    126 """
    127 if LogMode.get() == "disable":
--> 128     return obj(*args, **kwargs)
    130 logger = get_logger()
    131 logger_level = getattr(logger, log_level)

File ~/software_sources/modin/modin/pandas/series.py:495, in Series.add(self, other, level, fill_value, axis)
    491 """
    492 Return Addition of series and other, element-wise (binary operator add).
    493 """
    494 new_self, new_other = self._prepare_inter_op(other)
--> 495 return super(Series, new_self).add(
    496     new_other, level=level, fill_value=fill_value, axis=axis
    497 )

File ~/software_sources/modin/modin/logging/logger_decorator.py:128, in enable_logging.<locals>.decorator.<locals>.run_and_log(*args, **kwargs)
    113 """
    114 Compute function with logging if Modin logging is enabled.
    115
   (...)
    125 Any
    126 """
    127 if LogMode.get() == "disable":
--> 128     return obj(*args, **kwargs)
    130 logger = get_logger()
    131 logger_level = getattr(logger, log_level)

File ~/software_sources/modin/modin/pandas/base.py:571, in BasePandasDataset.add(self, other, axis, level, fill_value)
    565 def add(
    566     self, other, axis="columns", level=None, fill_value=None
    567 ):  # noqa: PR01, RT01, D200
    568     """
    569     Return addition of `BasePandasDataset` and `other`, element-wise (binary operator `add`).
    570     """
--> 571     return self._binary_op(
    572         "add", other, axis=axis, level=level, fill_value=fill_value
    573     )

File ~/software_sources/modin/modin/logging/logger_decorator.py:128, in enable_logging.<locals>.decorator.<locals>.run_and_log(*args, **kwargs)
    113 """
    114 Compute function with logging if Modin logging is enabled.
    115
   (...)
    125 Any
    126 """
    127 if LogMode.get() == "disable":
--> 128     return obj(*args, **kwargs)
    130 logger = get_logger()
    131 logger_level = getattr(logger, log_level)

File ~/software_sources/modin/modin/pandas/base.py:396, in BasePandasDataset._binary_op(self, op, other, **kwargs)
    392     kwargs.pop("broadcast", None)
    393     return self._default_to_pandas(
    394         getattr(self._pandas_class, op), other, **kwargs
    395     )
--> 396 other = self._validate_other(other, axis, dtype_check=True)
    397 exclude_list = [
    398     "__add__",
    399     "__radd__",
   (...)
    405     "__rxor__",
    406 ]
    407 if op in exclude_list:

File ~/software_sources/modin/modin/logging/logger_decorator.py:128, in enable_logging.<locals>.decorator.<locals>.run_and_log(*args, **kwargs)
    113 """
    114 Compute function with logging if Modin logging is enabled.
    115
   (...)
    125 Any
    126 """
    127 if LogMode.get() == "disable":
--> 128     return obj(*args, **kwargs)
    130 logger = get_logger()
    131 logger_level = getattr(logger, log_level)

File ~/software_sources/modin/modin/pandas/base.py:317, in BasePandasDataset._validate_other(self, other, axis, dtype_check, compare_index)
    299         self_dtypes = [
    300             dtype
    301             for label, dtype in zip(
   (...)
    304             if label in other
    305         ]
    307     if not all(
    308         (is_numeric_dtype(self_dtype) and is_numeric_dtype(other_dtype))
    309         or (is_object_dtype(self_dtype) and is_object_dtype(other_dtype))
   (...)
    315         for self_dtype, other_dtype in zip(self_dtypes, other_dtypes)
    316     ):
--> 317         raise TypeError("Cannot do operation with improper dtypes")
    318 return result

TypeError: Cannot do operation with improper dtypes

Installed Versions

INSTALLED VERSIONS

commit : fd776a5
python : 3.8.13.final.0
python-bits : 64
OS : Darwin
OS-release : 21.5.0
Version : Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:22 PDT 2022; root:xnu-8020.121.3~4/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

Modin dependencies

modin : 0.17.0+9.gfd776a5a
ray : 2.0.1
dask : 2022.01.1
distributed : 2022.01.1
hdk : None

pandas dependencies

pandas : 1.5.1
numpy : 1.23.1
pytz : 2022.1
dateutil : 2.8.2
setuptools : 61.2.0
pip : 22.1.2
Cython : None
pytest : 7.1.2
hypothesis : None
sphinx : 5.1.1
blosc : None
feather : 0.4.1
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.4.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : 2022.11.0
fsspec : 2022.7.1
gcsfs : None
matplotlib : 3.5.2
numba : None
numexpr : 2.8.3
odfpy : None
openpyxl : 3.0.10
pandas_gbq : 0.17.7
pyarrow : 8.0.0
pyreadstat : None
pyxlsb : None
s3fs : 2022.7.1
scipy : 1.9.0
snappy : None
sqlalchemy : 1.4.39
tables : 3.7.0
tabulate : None
xarray : 2022.6.0
xlrd : 2.0.1
xlwt : None
zstandard : None
tzdata : None

@mvashishtha mvashishtha added bug 🦗 Something isn't working pandas 🤔 Weird Behaviors of Pandas P2 Minor bugs or low-priority feature requests labels Nov 18, 2022
@mvashishtha mvashishtha self-assigned this Nov 18, 2022
mvashishtha pushed a commit to mvashishtha/modin that referenced this issue Nov 18, 2022
Signed-off-by: mvashishtha <mahesh@ponder.io>
dchigarev pushed a commit that referenced this issue Nov 21, 2022
Signed-off-by: mvashishtha <mahesh@ponder.io>
dchigarev pushed a commit that referenced this issue Nov 25, 2022
Signed-off-by: mvashishtha <mahesh@ponder.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🦗 Something isn't working P2 Minor bugs or low-priority feature requests pandas 🤔 Weird Behaviors of Pandas
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant