Skip to content

Handling of np.int64/np.float64 in Series.sum() #1464

@loicdiridollou

Description

@loicdiridollou

Describe the bug

At runtime, Series.sum returns a np.int64 or np.float64 when summing numbers. Yet the typing says we should get an int or a float.
The issue arises when we use method on that result that are only defined on np.int64 or np.float64 (for example the clip method).

How would we want to handle those cases, are we open to adjusting the stubs to better type the behavior?

To Reproduce

  1. Provide a minimal runnable pandas example that is not properly checked by the stubs.
  2. Indicate which type checker you are using (mypy or pyright).
  3. Show the error message received from that type checker while checking your example.
import pandas as pd
import numpy as np


a = pd.Series([1.,1.])
b = pd.Series([2.,2.])

a.mul(b).sum().clip(max=3)

The error we get is Cannot access attribute "clip" for class "float"

Please complete the following information:

  • OS: MacOS
  • OS Version latest
  • python version 3.13
  • version of type checker 1.18
  • version of installed pandas-stubs main

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ConstructorsSeries/DataFrame/Index/pd.array ConstructorsNeeds DiscussionRequires discussion from core team before further actionReduction Operationssum, mean, min, max, etc.SeriesSeries data structure

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions