Skip to content

BUG/inconsistency: pd.Series(dtype=object).sum(skipna=True) does not respect skipna=True #59764

Open
@Code0x58

Description

@Code0x58

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np


class X:
    def __init__(self, n, generation=0):
        self.n = n
        self.generation = generation

    def __add__(self, other):
        return X(self.n + other.n, max(self.generation, other.generation) + 1)

    def __mul__(self, other):
        return X(self.n * other.n, max(self.generation, other.generation) + 1)

    def __repr__(self):
        return f'X({self.n}, {self.generation})'
    

s = pd.Series([X(1), X(2), np.nan, X(0)])

# these implicitly have skipna=True anyway
s.sum(skipna=True)

Issue Description

aggregation methods over objects does not respect skipna. Exception produced above

# sum, cumsum, prod, cumprod, ...
AttributeError: 'int' object has no attribute 'n'

Expected Behavior

NaN values are skipped.

Given that all-NaN values are documented to produce NaN

Installed Versions

INSTALLED VERSIONS
------------------
commit                : d9cdd2ee5a58015ef6f4d15c7226110c9aab8140
python                : 3.12.5.final.0
python-bits           : 64
OS                    : Linux
OS-release            : 4.15.0-213-generic
Version               : #224-Ubuntu SMP Mon Jun 19 13:30:12 UTC 2023
machine               : x86_64
processor             :
byteorder             : little
LC_ALL                : None
LANG                  : C.UTF-8
LOCALE                : C.UTF-8

pandas                : 2.2.2
numpy                 : 2.1.1
pytz                  : 2024.1
dateutil              : 2.9.0.post0
setuptools            : None
pip                   : 24.2
Cython                : None
pytest                : None
hypothesis            : None
sphinx                : None
blosc                 : None
feather               : None
xlsxwriter            : None
lxml.etree            : None
html5lib              : None
pymysql               : None
psycopg2              : None
jinja2                : None
IPython               : None
pandas_datareader     : None
adbc-driver-postgresql: None
adbc-driver-sqlite    : None
bs4                   : None
bottleneck            : None
dataframe-api-compat  : None
fastparquet           : None
fsspec                : None
gcsfs                 : None
matplotlib            : None
numba                 : None
numexpr               : None
odfpy                 : None
openpyxl              : None
pandas_gbq            : None
pyarrow               : None
pyreadstat            : None
python-calamine       : None
pyxlsb                : None
s3fs                  : None
scipy                 : None
sqlalchemy            : None
tables                : None
tabulate              : None
xarray                : None
xlrd                  : None
zstandard             : None
tzdata                : 2024.1
qtpy                  : None
pyqt5                 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions