Skip to content

Refactor pandas.core.generic.NDFrame.to_replace() to appease static type analysis #34898

Open
@oguzhanogreden

Description

@oguzhanogreden
Contributor

Current implementation of pandas.core.to_replace() method won't allow type hints, as noted in #32542 . Contributor guidelines suggest a refactor in this case.

Activity

WillAyd

WillAyd commented on Jun 22, 2020

@WillAyd
Member

Can you share the errors you were seeing?

added
RefactorInternal refactoring of code
replacereplace method
Typingtype annotations, mypy/pyright type checking
on Jun 23, 2020
changed the title [-]Refactor pandas.core.to_replace() to appease static type analysis[/-] [+]Refactor pandas.core.Series.to_replace() to appease static type analysis[/+] on Jun 26, 2020
changed the title [-]Refactor pandas.core.Series.to_replace() to appease static type analysis[/-] [+]Refactor pandas.core.generic.NDFrame.to_replace() to appease static type analysis[/+] on Jun 26, 2020
oguzhanogreden

oguzhanogreden commented on Jun 26, 2020

@oguzhanogreden
ContributorAuthor

For instance, here mypy would complain that I specified to_replace can be a list, which doesn't have a items() method.

Here's the relevant mypy output (line numbers don't agree with upstream):

pandas/core/generic.py:6475: error: Item "List[Any]" of "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]" has no attribute "items"
pandas/core/generic.py:6475: error: Item "str" of "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]" has no attribute "items"
pandas/core/generic.py:6475: error: Item "int" of "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]" has no attribute "items"
pandas/core/generic.py:6475: error: Item "float" of "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]" has no attribute "items"
pandas/core/generic.py:6475: error: Item "bool" of "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]" has no attribute "items"
pandas/core/generic.py:6475: error: Item "None" of "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]" has no attribute "items"
pandas/core/generic.py:6476: error: 'builtins.object' object is not iterable
pandas/core/generic.py:6478: error: Cannot determine type of 'values'
pandas/core/generic.py:6514: error: Value of type "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]" is not indexable
pandas/core/generic.py:6514: error: Value of type "Union[Dict[Any, Any], Any, List[Any], str, float, Series]" is not indexable
pandas/core/generic.py:6515: error: Item "List[Any]" of "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]" has no attribute "keys"
pandas/core/generic.py:6515: error: Item "str" of "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]" has no attribute "keys"
pandas/core/generic.py:6515: error: Item "int" of "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]" has no attribute "keys"
pandas/core/generic.py:6515: error: Item "float" of "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]" has no attribute "keys"
pandas/core/generic.py:6515: error: Item "bool" of "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]" has no attribute "keys"
pandas/core/generic.py:6515: error: Item "None" of "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]" has no attribute "keys"
pandas/core/generic.py:6516: error: Item "List[Any]" of "Union[Dict[Any, Any], Any, List[Any], str, float, Series]" has no attribute "keys"
pandas/core/generic.py:6516: error: Item "str" of "Union[Dict[Any, Any], Any, List[Any], str, float, Series]" has no attribute "keys"
pandas/core/generic.py:6516: error: Item "float" of "Union[Dict[Any, Any], Any, List[Any], str, float, Series]" has no attribute "keys"
pandas/core/generic.py:6529: error: Item "List[Any]" of "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]" has no attribute "items"
pandas/core/generic.py:6529: error: Item "str" of "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]" has no attribute "items"
pandas/core/generic.py:6529: error: Item "int" of "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]" has no attribute "items"
pandas/core/generic.py:6529: error: Item "float" of "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]" has no attribute "items"
pandas/core/generic.py:6529: error: Item "bool" of "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]" has no attribute "items"
pandas/core/generic.py:6529: error: Item "None" of "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]" has no attribute "items"
pandas/core/generic.py:6537: error: Argument 1 to "len" has incompatible type "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]"; expected "Sized"
pandas/core/generic.py:6537: error: Argument 1 to "len" has incompatible type "Union[Dict[Any, Any], Any, List[Any], str, float, Series]"; expected "Sized"
pandas/core/generic.py:6539: error: Argument 1 to "len" has incompatible type "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]"; expected "Sized"
pandas/core/generic.py:6539: error: Argument 1 to "len" has incompatible type "Union[Dict[Any, Any], Any, List[Any], str, float, Series]"; expected "Sized"
pandas/core/generic.py:6547: error: Argument "regex" to "replace_list" of "BlockManager" has incompatible type "Union[Dict[Any, Any], List[Any], str, int, float, bool, Any, Any, Any, Any, Series, None]"; expected "bool"
pandas/core/generic.py:6578: error: Item "List[Any]" of "Union[Dict[Any, Any], Any, List[Any], str, float, Series]" has no attribute "items"
pandas/core/generic.py:6578: error: Item "str" of "Union[Dict[Any, Any], Any, List[Any], str, float, Series]" has no attribute "items"
pandas/core/generic.py:6578: error: Item "float" of "Union[Dict[Any, Any], Any, List[Any], str, float, Series]" has no attribute "items"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    RefactorInternal refactoring of codeTypingtype annotations, mypy/pyright type checkingreplacereplace method

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @WillAyd@oguzhanogreden@jbrockmendel

        Issue actions

          Refactor pandas.core.generic.NDFrame.to_replace() to appease static type analysis · Issue #34898 · pandas-dev/pandas