Github user holdenk commented on a diff in the pull request:

    https://github.com/apache/spark/pull/16793#discussion_r104451583
  
    --- Diff: python/pyspark/sql/dataframe.py ---
    @@ -1307,43 +1309,75 @@ def replace(self, to_replace, value, subset=None):
             |null|  null|null|
             +----+------+----+
             """
    -        if not isinstance(to_replace, (float, int, long, basestring, list, 
tuple, dict)):
    +        # Helper functions
    +        def all_of(types):
    +            """Given a type or tuple of types
    +            and sequence of xs check if each x
    +            is instance of type(s)
    +
    +            >>> all_of(bool)([True, False])
    +            True
    +            >>> all_of(basestring)(["a", 1])
    +            False
    +            """
    +            def all_of_(xs):
    +                return all(isinstance(x, types) for x in xs)
    +            return all_of_
    +
    +        all_of_bool = all_of(bool)
    +        all_of_str = all_of(basestring)
    +        all_of_numeric = all_of((float, int, long))
    +
    +        # Validate input types
    +        valid_types = (bool, float, int, long, basestring, list, tuple)
    +        if not isinstance(to_replace, valid_types + (dict, )):
                 raise ValueError(
    -                "to_replace should be a float, int, long, string, list, 
tuple, or dict")
    +                "to_replace should be a float, int, long, string, list, 
tuple, or dict. "
    +                "Got {0}".format(type(to_replace)))
     
    -        if not isinstance(value, (float, int, long, basestring, list, 
tuple)):
    -            raise ValueError("value should be a float, int, long, string, 
list, or tuple")
    +        if (not isinstance(value, valid_types) and
    +                not isinstance(to_replace, dict)):
    +            raise ValueError("If to_replace is not a dict, value should be 
"
    +                             "a float, int, long, string, list, or tuple. "
    +                             "Got {0}".format(type(value)))
    +
    +        if isinstance(to_replace, (list, tuple)) and isinstance(value, 
(list, tuple)):
    +            if len(to_replace) != len(value):
    +                raise ValueError("to_replace and value lists should be of 
the same length. "
    +                                 "Got {0} and {1}".format(len(to_replace), 
len(value)))
     
    -        rep_dict = dict()
    +        if not (subset is None or isinstance(subset, (list, tuple, 
basestring))):
    +            raise ValueError("subset should be a list or tuple of column 
names, "
    +                             "column name or None. Got 
{0}".format(type(subset)))
     
    +        # Reshape input arguments if necessary
             if isinstance(to_replace, (float, int, long, basestring)):
                 to_replace = [to_replace]
     
    -        if isinstance(to_replace, tuple):
    -            to_replace = list(to_replace)
    +        if isinstance(value, (float, int, long, basestring)):
    +            value = [value for _ in range(len(to_replace))]
     
    -        if isinstance(value, tuple):
    -            value = list(value)
    -
    -        if isinstance(to_replace, list) and isinstance(value, list):
    -            if len(to_replace) != len(value):
    -                raise ValueError("to_replace and value lists should be of 
the same length")
    -            rep_dict = dict(zip(to_replace, value))
    -        elif isinstance(to_replace, list) and isinstance(value, (float, 
int, long, basestring)):
    -            rep_dict = dict([(tr, value) for tr in to_replace])
    -        elif isinstance(to_replace, dict):
    +        if isinstance(to_replace, dict):
                 rep_dict = to_replace
    +            if value is not None:
    +                warnings.warn("to_replace is a dict, but value is not 
None. "
    --- End diff --
    
    Does this need to be split?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to