Skip to content

BUG: ExtensionArray TestMethods.test_where_series (override is_scalar) #33825

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

Open
1 of 3 tasks
andrewgsavage opened this issue Apr 27, 2020 · 4 comments
Open
1 of 3 tasks
Labels
Bug ExtensionArray Extending pandas with custom dtypes or arrays.

Comments

@andrewgsavage
Copy link

andrewgsavage commented Apr 27, 2020

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

  • I have confirmed this bug exists on the latest version of pandas.
    I'm working on updating pint-pandas to 0.25.1 before the latest version...

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

TypeError                                 Traceback (most recent call last)
c:\users\a\repos\pint\pint\quantity.py in __getitem__(self, key)
   1737         try:
-> 1738             return type(self)(self._magnitude[key], self._units)
   1739         except PintTypeError:

TypeError: 'float' object is not subscriptable

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-14-67c12fe72115> in <module>
----> 1 self.test_where_series(data, na_value, as_frame)

c:\users\a\repos\pint-pandas\pintpandas\test_pandas_interface.py in test_where_series(self, data, na_value, as_frame)
    266             cond = cond.reshape(-1, 1)
    267 
--> 268         result = ser.where(cond)
    269         expected = pd.Series(
    270             cls._from_sequence([a, a, na_value, na_value], dtype=data.dtype)

~\anaconda3\envs\py37_pd251\lib\site-packages\pandas\core\generic.py in where(self, cond, other, inplace, axis, level, errors, try_cast)
   9274         other = com.apply_if_callable(other, self)
   9275         return self._where(
-> 9276             cond, other, inplace, axis, level, errors=errors, try_cast=try_cast
   9277         )
   9278 

~\anaconda3\envs\py37_pd251\lib\site-packages\pandas\core\generic.py in _where(self, cond, other, inplace, axis, level, errors, try_cast)
   9121                 errors=errors,
   9122                 try_cast=try_cast,
-> 9123                 axis=block_axis,
   9124             )
   9125 

~\anaconda3\envs\py37_pd251\lib\site-packages\pandas\core\internals\managers.py in where(self, **kwargs)
    555 
    556     def where(self, **kwargs):
--> 557         return self.apply("where", **kwargs)
    558 
    559     def setitem(self, **kwargs):

~\anaconda3\envs\py37_pd251\lib\site-packages\pandas\core\internals\managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
    436                     kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
    437 
--> 438             applied = getattr(b, f)(**kwargs)
    439             result_blocks = _extend_blocks(applied, result_blocks)
    440 

~\anaconda3\envs\py37_pd251\lib\site-packages\pandas\core\internals\blocks.py in where(self, other, cond, align, errors, try_cast, axis)
   2005             set_other = other
   2006         else:
-> 2007             set_other = other[icond]
   2008         try:
   2009             result[icond] = set_other

c:\users\a\repos\pint\pint\quantity.py in __getitem__(self, key)
   1742             raise TypeError(
   1743                 "Neither Quantity object nor its magnitude ({})"
-> 1744                 "supports indexing".format(self._magnitude)
   1745             )
   1746 

TypeError: Neither Quantity object nor its magnitude (nan)supports indexing

Problem description

I'm having trouble with this test as pandas is trying to index into a scalar. I believe this is because this line returns false:

if lib.is_scalar(other):

import pintpandas
import matplotlib.pyplot as plt
import numpy as np
import pint
import pandas as pd

ureg=pint.UnitRegistry()
PA_= pintpandas.PintArray
PT_ = pintpandas.PintType
PT_.ureg = ureg
Q_=ureg.Quantity

from pandas._libs import NaT, lib, tslib, tslibs

lib.is_scalar(Q_(1,'m'))
False

Expected Output

lib.is_scalar(Q_(1,'m'))
True

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.7.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : AMD64 Family 23 Model 8 Stepping 2, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None

pandas : 0.25.1
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.1.3.post20200330
Cython : 0.29.15
pytest : 5.4.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.13.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.2.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None

@andrewgsavage andrewgsavage added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 27, 2020
@znicholls
Copy link
Contributor

znicholls commented Jun 21, 2020

Expected Output

lib.is_scalar(Q_(1,'m'))
False

should this be

Expected Output

lib.is_scalar(Q_(1,'m'))
True

?

@andrewgsavage
Copy link
Author

ah yes, changed that

@TomAugspurger
Copy link
Contributor

We can repurpose this as a general "we need a way for extension types to say what is / isn't a scalar. xref #35176.

@TomAugspurger TomAugspurger added ExtensionArray Extending pandas with custom dtypes or arrays. and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 4, 2020
@jbrockmendel
Copy link
Member

Discussed overriding is_scalar on the dev call yesterday. General agreement to attempt this long-term. Shorter-term we are moving away from using is_scalar internally, and in fact the usage from the OP is gone. Can you confirm whether the motivating bug still exists?

@jbrockmendel jbrockmendel changed the title BUG: ExtensionArray TestMethods.test_where_series BUG: ExtensionArray TestMethods.test_where_series (override is_scalar) Jul 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug ExtensionArray Extending pandas with custom dtypes or arrays.
Projects
None yet
Development

No branches or pull requests

4 participants