You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/02/03 05:26:41 UTC
[GitHub] [spark] itholic commented on a change in pull request #35191: [SPARK-37491][PYTHON]Fix Series.asof for unsorted values
itholic commented on a change in pull request #35191:
URL: https://github.com/apache/spark/pull/35191#discussion_r798208506
##########
File path: python/pyspark/pandas/series.py
##########
@@ -5228,22 +5228,128 @@ def asof(self, where: Union[Any, List]) -> Union[Scalar, "Series"]:
where = [where]
index_scol = self._internal.index_spark_columns[0]
index_type = self._internal.spark_type_for(index_scol)
+
+ # e.g where = [10, 20]
+ # In the comments below , will explain how the dataframe will look after transformations.
+ # e.g pd.Series([2, 1, np.nan, 4], index=[10, 20, 30, 40], name="Koalas")
+
+ column_prefix_constant = "col_"
cond = [
- F.max(F.when(index_scol <= SF.lit(index).cast(index_type), self.spark.column))
- for index in where
+ F.when(
+ index_scol <= SF.lit(index).cast(index_type),
+ F.struct(
+ F.lit(column_prefix_constant + str(index) + "_" + str(idx)).alias("identifier"),
+ self.spark.column.alias("col_value"),
+ ),
+ ).alias(column_prefix_constant + str(index) + "_" + str(idx))
Review comment:
Can we make `column_prefix_constant + str(index) + "_" + str(idx)` as variable and reuse here and there ??
##########
File path: python/pyspark/pandas/series.py
##########
@@ -5228,22 +5228,128 @@ def asof(self, where: Union[Any, List]) -> Union[Scalar, "Series"]:
where = [where]
index_scol = self._internal.index_spark_columns[0]
index_type = self._internal.spark_type_for(index_scol)
+
+ # e.g where = [10, 20]
+ # In the comments below , will explain how the dataframe will look after transformations.
+ # e.g pd.Series([2, 1, np.nan, 4], index=[10, 20, 30, 40], name="Koalas")
+
+ column_prefix_constant = "col_"
cond = [
- F.max(F.when(index_scol <= SF.lit(index).cast(index_type), self.spark.column))
- for index in where
+ F.when(
+ index_scol <= SF.lit(index).cast(index_type),
+ F.struct(
+ F.lit(column_prefix_constant + str(index) + "_" + str(idx)).alias("identifier"),
+ self.spark.column.alias("col_value"),
+ ),
+ ).alias(column_prefix_constant + str(index) + "_" + str(idx))
Review comment:
I think we can simply use `F.last` with `ignorenulls=True` instead of `F.max` ??
(https://spark.apache.org/docs/3.1.1/api/python/reference/api/pyspark.sql.functions.last.html)
e.g.
```python
cond = [
F.last(F.when(index_scol <= SF.lit(index).cast(index_type), self.spark.column), ignorenulls=True)
for index in where
]
```
It returns the last non-null value from given column.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org