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

TypeError when accessing .real and .imag attribute on sparse array of string type #608

Closed
andersy005 opened this issue Oct 27, 2023 · 0 comments · Fixed by #609
Closed
Labels

Comments

@andersy005
Copy link
Member

Describe the bug

attempting to access the .real attribute on a sparse.COO object with string data type results in a TypeError: string indices must be integers.

To Reproduce

In [18]: import numpy as np

In [19]: import sparse

In [20]: a = np.array(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])

In [21]: a.real
Out[21]: array(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'], dtype='<U1')

In [22]: data = sparse.COO(a)

In [23]: data
Out[23]: <COO: shape=(10,), dtype=<U1, nnz=10, fill_value=>
In [24]: data.real
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[24], line 1
----> 1 data.real

File ~/mambaforge/envs/playground/lib/python3.10/site-packages/sparse/_sparse_array.py:884, in SparseArray.real(self)
    859 @property
    860 def real(self):
    861     """The real part of the array.
    862 
    863     Examples
   (...)
    882     numpy.real : NumPy equivalent function.
    883     """
--> 884     return self.__array_ufunc__(np.real, "__call__", self)

File ~/mambaforge/envs/playground/lib/python3.10/site-packages/sparse/_sparse_array.py:305, in SparseArray.__array_ufunc__(self, ufunc, method, *inputs, **kwargs)
    302     inputs = tuple(reversed(inputs_transformed))
    304 if method == "__call__":
--> 305     result = elemwise(ufunc, *inputs, **kwargs)
    306 elif method == "reduce":
    307     result = SparseArray._reduce(ufunc, *inputs, **kwargs)

File ~/mambaforge/envs/playground/lib/python3.10/site-packages/sparse/_umath.py:49, in elemwise(func, *args, **kwargs)
     12 def elemwise(func, *args, **kwargs):
     13     """
     14     Apply a function to any number of arguments.
     15 
   (...)
     46     it is necessary to convert Numpy arrays to :obj:`COO` objects.
     47     """
---> 49     return _Elemwise(func, *args, **kwargs).get_result()

File ~/mambaforge/envs/playground/lib/python3.10/site-packages/sparse/_umath.py:466, in _Elemwise.__init__(self, func, *args, **kwargs)
    463 self._dense_result = False
    465 self._check_broadcast()
--> 466 self._get_fill_value()

File ~/mambaforge/envs/playground/lib/python3.10/site-packages/sparse/_umath.py:535, in _Elemwise._get_fill_value(self)
    525 """
    526 A function that finds and returns the fill-value.
    527 
   (...)
    531     If the fill-value is inconsistent.
    532 """
    533 from ._coo import COO
--> 535 zero_args = tuple(
    536     arg.fill_value[...] if isinstance(arg, COO) else arg for arg in self.args
    537 )
    539 # Some elemwise functions require a dtype argument, some abhorr it.
    540 try:

File ~/mambaforge/envs/playground/lib/python3.10/site-packages/sparse/_umath.py:536, in <genexpr>(.0)
    525 """
    526 A function that finds and returns the fill-value.
    527 
   (...)
    531     If the fill-value is inconsistent.
    532 """
    533 from ._coo import COO
    535 zero_args = tuple(
--> 536     arg.fill_value[...] if isinstance(arg, COO) else arg for arg in self.args
    537 )
    539 # Some elemwise functions require a dtype argument, some abhorr it.
    540 try:

TypeError: string indices must be integers

Expected behavior

given that NumPy arrays with string data type return the array itself when accessing .real, i was expecting the same behavior for sparse.COO objects.

System

  • OS and version: macOS-14.0-arm64-arm-64bit
  • sparse version (0.14.0)
  • NumPy version (1.24.4)
  • Numba version (numba.__version__)

Additional context
Add any other context about the problem here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant