ueshin commented on a change in pull request #33817:
URL: https://github.com/apache/spark/pull/33817#discussion_r696112596
##########
File path: python/pyspark/pandas/groupby.py
##########
@@ -2251,11 +2251,20 @@ def pandas_transform(pdf: pd.DataFrame) -> pd.DataFrame:
dtype = cast(SeriesType, return_type).dtype
spark_type = cast(SeriesType, return_type).spark_type
- data_fields = [
- InternalField(dtype=dtype, struct_field=StructField(name=c,
dataType=spark_type))
- for c in psdf._internal.data_spark_column_names
- if c not in groupkey_names
- ]
+ data_fields = []
+ for c in psdf._internal.data_spark_column_names:
+ if c not in groupkey_names:
+ # If the unique values of CategoricalDtype are the same,
use the original one.
+ # This behavior is changed from pandas 1.3.
+ original_dtype = psdf[c].dtype
+ if isinstance(original_dtype, CategoricalDtype) and
original_dtype == dtype:
+ dtype = original_dtype
+ data_fields.append(
+ InternalField(
+ dtype=dtype, struct_field=StructField(name=c,
dataType=spark_type)
+ )
+ )
+
Review comment:
On second thoughts, I think this update is wrong.
##########
File path: python/pyspark/pandas/tests/test_categorical.py
##########
@@ -443,15 +445,16 @@ def identity(x) -> ps.Series[psdf.b.dtype]: # type:
ignore
pdf.groupby("a").transform(identity).sort_values("b").reset_index(drop=True),
)
- dtype = CategoricalDtype(categories=["a", "b", "c", "d"])
+ # The behavior for CategoricalDtype is changed from pandas 1.3
+ if LooseVersion(pd.__version__) >= LooseVersion("1.3"):
+ dtype = CategoricalDtype(categories=["a", "b", "c", "d"])
+ else:
+ dtype = pdf.b.dtype
def astype(x) -> ps.Series[dtype]:
return x.astype(dtype)
Review comment:
Instead, the return type of this function `astype` should be the
original dtype instead of the new one.
so, I think this should be:
```py
dtype = CategoricalDtype(categories=["a", "b", "c", "d"])
# The behavior for CategoricalDtype is changed from pandas 1.3
if LooseVersion(pd.__version__) >= LooseVersion("1.3"):
ret_dtype = pdf.b.dtype
else:
ret_dtype = dtype
def astype(x) -> ps.Series[ret_dtype]:
return x.astype(dtype)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]