Skip to content

BUG: AttributeError in pandas.core.algorithms.diff when passing non-numeric types #61728

Open
@akshat62

Description

@akshat62

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
pd.Series([1, 2, 3]).diff("hello")


Raises:

AttributeError: 'str' object has no attribute 'is_integer'

Issue Description

When passing non-numeric types (like strings, None, or other objects) to the diff function in pandas/core/algorithms.py, it raises an AttributeError instead of the expected ValueError. This affects any code that uses Series.diff(), DataFrame.diff(), or calls the diff function directly.

The issue occurs because the validation logic tries to call n.is_integer() on non-float objects that don't have this method, resulting in an AttributeError.

Expected Behavior

import pandas as pd
pd.Series([1, 2, 3]).diff("hello")

Should raise:

ValueError: periods must be an integer

Installed Versions

python : 3.12.10

OS : Linux
OS-release : 4.18.0-553.56.1.el8_10.x86_64

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions