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: Groupby.any(skipna=True) #40585

Closed
2 of 3 tasks
jaspersival opened this issue Mar 23, 2021 · 1 comment · Fixed by #40819
Closed
2 of 3 tasks

BUG: Groupby.any(skipna=True) #40585

jaspersival opened this issue Mar 23, 2021 · 1 comment · Fixed by #40819
Assignees
Labels
Bug Groupby NA - MaskedArrays Related to pd.NA and nullable extension arrays
Milestone

Comments

@jaspersival
Copy link

jaspersival commented Mar 23, 2021

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

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

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


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd


def test_groupby_any():
    df = pd.DataFrame(
        {
            "ids": ["id1", "id1", "id2", "id2"],
            "A": [True, False, pd.NA, True]
        }
    ).astype({"ids": pd.StringDtype(),
              "A": pd.BooleanDtype()})

    result = df.groupby("ids").any(skipna=True)

    expected = pd.DataFrame(
        {
            "A": [True, True]
        }, index=pd.Series(["id1", "id2"], name="ids")
    )

    pd.testing.assert_frame_equal(expected, result)

Problem description

When running df.groupby(field_to_group_on).any(skipna=True) it should be expected that pd.NA values are neglected for the .any() method. However the output is this:

self =
[True, False, , True]
Length: 4, dtype: boolean
dtype = dtype('bool'), copy = True

def astype(self, dtype, copy: bool = True) -> ArrayLike:
    """
    Cast to a NumPy array or ExtensionArray with 'dtype'.

    Parameters
    ----------
    dtype : str or dtype
        Typecode or data-type to which the array is cast.
    copy : bool, default True
        Whether to copy the data, even if not necessary. If False,
        a copy is made only if the old dtype does not match the
        new dtype.

    Returns
    -------
    ndarray or ExtensionArray
        NumPy ndarray, BooleanArray or IntegerArray with 'dtype' for its dtype.

    Raises
    ------
    TypeError
        if incompatible type with an BooleanDtype, equivalent of same_kind
        casting
    """
    from pandas.core.arrays.string_ import StringDtype

    dtype = pandas_dtype(dtype)

    if isinstance(dtype, BooleanDtype):
        values, mask = coerce_to_array(self, copy=copy)
        if not copy:
            return self
        else:
            return BooleanArray(values, mask, copy=False)
    elif isinstance(dtype, StringDtype):
        return dtype.construct_array_type()._from_sequence(self, copy=False)

    if is_bool_dtype(dtype):
        # astype_nansafe converts np.nan to True
        if self._hasna:
          raise ValueError("cannot convert float NaN to bool")

E ValueError: cannot convert float NaN to bool

Expected Output

    expected = pd.DataFrame(
        {
            "A": [True, True]
        }, index=pd.Series(["id1", "id2"], name="ids")
    )

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2c8480
python : 3.9.1.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : Intel64 Family 6 Model 85 Stepping 4, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252
pandas : 1.2.3
numpy : 1.20.1
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 52.0.0
Cython : None
pytest : 6.2.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.1
sqlalchemy : 1.4.2
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
numba : None

@jaspersival jaspersival added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 23, 2021
@mzeitlin11
Copy link
Member

Hi @Achilles2308, thanks for the report! Can confirm this is broken on master, looks like an issue for any extension type

@mzeitlin11 mzeitlin11 added Groupby Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate NA - MaskedArrays Related to pd.NA and nullable extension arrays and removed Needs Triage Issue that has not been reviewed by a pandas team member Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate labels Mar 24, 2021
@mzeitlin11 mzeitlin11 self-assigned this Mar 24, 2021
@jreback jreback added this to the 1.3 milestone Apr 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Groupby NA - MaskedArrays Related to pd.NA and nullable extension arrays
Projects
None yet
3 participants