Skip to content

DataFrame.replace incompatible with type "dict[str, dict[int, int]]" #1161

Closed
@ClementPinard

Description

@ClementPinard

Describe the bug
Calling DataFrame.replace(my_dict) triggers a pyright error when my_dict  is of the type dict[str, dict[int, int]], which is odd because it does fit the accepted type Mapping[Hashable, ReplaceValue]

To Reproduce

This code triggers a pyright error:

import pandas as pd

test_df = pd.DataFrame([[0,1],[1,0]], index=[*"ab"], columns=[*"cd"])
print(test_df)

replace_dict = {"c": {0:1}}
replaced_test_df = test_df.replace(replace_dict)

print(replaced_test_df)
test.py:8:36 - error: Argument of type "dict[str, dict[int, int]]" cannot be assigned to parameter "to_replace" of type "ReplaceValue | Mapping[Hashable, ReplaceValue]" in function "replace"
    Type "dict[str, dict[int, int]]" is not assignable to type "ReplaceValue | Mapping[Hashable, ReplaceValue]"
      "dict[str, dict[int, int]]" is not assignable to "str"
      "dict[str, dict[int, int]]" is not assignable to "bytes"
      "dict[str, dict[int, int]]" is not assignable to "date"
      "dict[str, dict[int, int]]" is not assignable to "datetime"
      "dict[str, dict[int, int]]" is not assignable to "timedelta"
      "dict[str, dict[int, int]]" is not assignable to "datetime64"
      "dict[str, dict[int, int]]" is not assignable to "timedelta64"
    ... (reportArgumentType)
1 error, 0 warnings, 0 informations

This code does not. instead of defining a dict and giving it, we construct directly in the function call.

import pandas as pd

test_df = pd.DataFrame([[0,1],[1,0]], index=[*"ab"], columns=[*"cd"])
print(test_df)

replaced_test_df = test_df.replace({"c": {0:1}})

print(replaced_test_df)

I am not sure exactly if this is a pyright or a pandas stubs problem.

Please complete the following information:

  • OS: MacOS
  • OS Version : Sonoma 14.7.4
  • python version: 3.12.9
  • version of type checker:  pyright 1.1.396
  • version of installed pandas-stubs : 2.2.3.250308

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions