Skip to content

BUG: Unexpected Code Segment Executed, Causing Logical Error #61932

@LAUNAHC

Description

@LAUNAHC

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

# 示例数据
df = pd.DataFrame({
    'product_code': ['X', 'X', 'X', 'Y', 'Y'],
    'units': ['P', 'P', 'Q', 'Q', 'Q']
})

df2 = df.head(2)
df2 = df2.sort_values('product_code', ascending=False)\
            .groupby(['product_code',
                      'unit_name'])\
            .first().reset_index(drop=True)
print(df2)

Issue Description

When there are only 2 lines of data in df, this code will run successfully, even if the fields do not exist. We have not found this situation in other rows so far

Expected Behavior

It exists in version 2.2.2 of pandas。

Installed Versions

Replace this line with the output of pd.show_versions()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions