-
Notifications
You must be signed in to change notification settings - Fork 28.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-22566][PYTHON] Better error message for _merge_type
in Pandas to Spark DF conversion
#19792
Changes from 11 commits
518fdd4
b29434e
6aa9963
1e0072b
61ace28
3346a6c
8665115
c603251
2240a42
41766fa
0103045
5131db2
44a1879
404fdbb
6d171dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -324,11 +324,12 @@ def range(self, start, end=None, step=1, numPartitions=None): | |
|
||
return DataFrame(jdf, self._wrapped) | ||
|
||
def _inferSchemaFromList(self, data): | ||
def _inferSchemaFromList(self, data, names=None): | ||
""" | ||
Infer schema from list of Row or tuple. | ||
|
||
:param data: list of Row or tuple | ||
:param names: list of column names | ||
:return: :class:`pyspark.sql.types.StructType` | ||
""" | ||
if not data: | ||
|
@@ -337,12 +338,12 @@ def _inferSchemaFromList(self, data): | |
if type(first) is dict: | ||
warnings.warn("inferring schema from dict is deprecated," | ||
"please use pyspark.sql.Row instead") | ||
schema = reduce(_merge_type, map(_infer_schema, data)) | ||
schema = reduce(_merge_type, (_infer_schema(row, names) for row in data)) | ||
if _has_nulltype(schema): | ||
raise ValueError("Some of types cannot be determined after inferring") | ||
return schema | ||
|
||
def _inferSchema(self, rdd, samplingRatio=None): | ||
def _inferSchema(self, rdd, samplingRatio=None, names=None): | ||
""" | ||
Infer schema from an RDD of Row or tuple. | ||
|
||
|
@@ -359,10 +360,10 @@ def _inferSchema(self, rdd, samplingRatio=None): | |
"Use pyspark.sql.Row instead") | ||
|
||
if samplingRatio is None: | ||
schema = _infer_schema(first) | ||
schema = _infer_schema(first, names=names) | ||
if _has_nulltype(schema): | ||
for row in rdd.take(100)[1:]: | ||
schema = _merge_type(schema, _infer_schema(row)) | ||
schema = _merge_type(schema, _infer_schema(row, names=names)) | ||
if not _has_nulltype(schema): | ||
break | ||
else: | ||
|
@@ -371,22 +372,17 @@ def _inferSchema(self, rdd, samplingRatio=None): | |
else: | ||
if samplingRatio < 0.99: | ||
rdd = rdd.sample(False, float(samplingRatio)) | ||
schema = rdd.map(_infer_schema).reduce(_merge_type) | ||
schema = rdd.map(lambda row: _infer_schema(row, names)).reduce(_merge_type) | ||
return schema | ||
|
||
def _createFromRDD(self, rdd, schema, samplingRatio): | ||
""" | ||
Create an RDD for DataFrame from an existing RDD, returns the RDD and schema. | ||
""" | ||
if schema is None or isinstance(schema, (list, tuple)): | ||
struct = self._inferSchema(rdd, samplingRatio) | ||
converter = _create_converter(struct) | ||
schema = self._inferSchema(rdd, samplingRatio, names=schema) | ||
converter = _create_converter(schema) | ||
rdd = rdd.map(converter) | ||
if isinstance(schema, (list, tuple)): | ||
for i, name in enumerate(schema): | ||
struct.fields[i].name = name | ||
struct.names[i] = name | ||
schema = struct | ||
|
||
elif not isinstance(schema, StructType): | ||
raise TypeError("schema should be StructType or list or None, but got: %s" % schema) | ||
|
@@ -405,7 +401,7 @@ def _createFromLocal(self, data, schema): | |
data = list(data) | ||
|
||
if schema is None or isinstance(schema, (list, tuple)): | ||
struct = self._inferSchemaFromList(data) | ||
struct = self._inferSchemaFromList(data, names=schema) | ||
converter = _create_converter(struct) | ||
data = map(converter, data) | ||
if isinstance(schema, (list, tuple)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gberger, could we remove this branch too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed with commit 5131db2 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. >>> spark.createDataFrame([{'a': 1}], ["b"])
DataFrame[a: bigint] Hm.. sorry actually I think we should not remove this one and L385 because we should primarily respect user's input and it should be |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1072,7 +1072,7 @@ def _infer_type(obj): | |
raise TypeError("not supported type: %s" % type(obj)) | ||
|
||
|
||
def _infer_schema(row): | ||
def _infer_schema(row, names=None): | ||
"""Infer the schema from dict/namedtuple/object""" | ||
if isinstance(row, dict): | ||
items = sorted(row.items()) | ||
|
@@ -1083,7 +1083,8 @@ def _infer_schema(row): | |
elif hasattr(row, "_fields"): # namedtuple | ||
items = zip(row._fields, tuple(row)) | ||
else: | ||
names = ['_%d' % i for i in range(1, len(row) + 1)] | ||
if names is None: | ||
names = ['_%d' % i for i in range(1, len(row) + 1)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gberger, Let's revert this change too. Seems it's going to introduce a behaviour change: Before
After
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, but by reverting we lose the nice message. Notice below reverted it says Instead, I am adding a new elif branch where we check len(names) vs len(row). If we have fewer names than we have columns, we extend the names list, completing it with entries such as Reverted
Modified
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, yup. I noticed it too but I think the same thing applies to other cases, for example:
So, let's revert this change here for now. There are some subtleties here but I think it's fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we revert it, then the original purpose of the PR is lost:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @HyukjinKwon just pushed the elif branch change that I talked about above, please see if it is suitable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, will take another look soon anyway. |
||
items = zip(names, row) | ||
|
||
elif hasattr(row, "__dict__"): # object | ||
|
@@ -1108,19 +1109,27 @@ def _has_nulltype(dt): | |
return isinstance(dt, NullType) | ||
|
||
|
||
def _merge_type(a, b): | ||
def _merge_type(a, b, name=None): | ||
if name is None: | ||
new_msg = lambda msg: msg | ||
new_name = lambda n: "field %s" % n | ||
else: | ||
new_msg = lambda msg: "%s: %s" % (name, msg) | ||
new_name = lambda n: "field %s in %s" % (n, name) | ||
|
||
if isinstance(a, NullType): | ||
return b | ||
elif isinstance(b, NullType): | ||
return a | ||
elif type(a) is not type(b): | ||
# TODO: type cast (such as int -> long) | ||
raise TypeError("Can not merge type %s and %s" % (type(a), type(b))) | ||
raise TypeError(new_msg("Can not merge type %s and %s" % (type(a), type(b)))) | ||
|
||
# same type | ||
if isinstance(a, StructType): | ||
nfs = dict((f.name, f.dataType) for f in b.fields) | ||
fields = [StructField(f.name, _merge_type(f.dataType, nfs.get(f.name, NullType()))) | ||
fields = [StructField(f.name, _merge_type(f.dataType, nfs.get(f.name, NullType()), | ||
name=new_name(f.name))) | ||
for f in a.fields] | ||
names = set([f.name for f in fields]) | ||
for n in nfs: | ||
|
@@ -1129,11 +1138,12 @@ def _merge_type(a, b): | |
return StructType(fields) | ||
|
||
elif isinstance(a, ArrayType): | ||
return ArrayType(_merge_type(a.elementType, b.elementType), True) | ||
return ArrayType(_merge_type(a.elementType, b.elementType, | ||
name='element in array %s' % name), True) | ||
|
||
elif isinstance(a, MapType): | ||
return MapType(_merge_type(a.keyType, b.keyType), | ||
_merge_type(a.valueType, b.valueType), | ||
return MapType(_merge_type(a.keyType, b.keyType, name='key of map %s' % name), | ||
_merge_type(a.valueType, b.valueType, name='value of map %s' % name), | ||
True) | ||
else: | ||
return a | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we set the
names
for_createFromRDD
->_inferSchema
too?: