pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
42.59k stars 17.57k forks source link

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

Open oguzhanogreden opened 4 years ago

oguzhanogreden commented 4 years ago

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.

WillAyd commented 4 years ago

Can you share the errors you were seeing?

oguzhanogreden commented 4 years ago

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"