Github user holdenk commented on a diff in the pull request:
https://github.com/apache/spark/pull/16793#discussion_r103089653
--- Diff: python/pyspark/sql/dataframe.py ---
@@ -1307,43 +1307,66 @@ 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):
+ 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. "
+ "value will be ignored.")
+ else:
+ rep_dict = dict(zip(to_replace, value))
- if subset is None:
- return DataFrame(self._jdf.na().replace('*', rep_dict),
self.sql_ctx)
- elif isinstance(subset, basestring):
+ if isinstance(subset, basestring):
subset = [subset]
- if not isinstance(subset, (list, tuple)):
- raise ValueError("subset should be a list or tuple of column
names")
+ # Check if we won't pass mixed type generics
--- End diff --
This reads a bit awkwardly. How about "Verify we were not passed in mixed
type generics."?
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]