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 2021/12/09 18:38:33 UTC

[GitHub] [spark] ueshin commented on a change in pull request #34825: [SPARK-37563][PYTHON] Implement days, seconds, microseconds properties of TimedeltaIndex

ueshin commented on a change in pull request #34825:
URL: https://github.com/apache/spark/pull/34825#discussion_r766054166



##########
File path: python/pyspark/pandas/indexes/timedelta.py
##########
@@ -111,3 +126,80 @@ def __getattr__(self, item: str) -> Any:
                 return partial(property_or_func, self)
 
         raise AttributeError("'TimedeltaIndex' object has no attribute '{}'".format(item))
+
+    @property
+    def days(self) -> Index:
+        """
+        Number of days for each element.
+        """
+
+        @no_type_check
+        def pandas_days(x) -> int:
+            return x.days
+
+        return ps.Index(self.to_series().transform(pandas_days))
+
+    @property
+    def seconds(self) -> Index:
+        """
+        Number of seconds (>= 0 and less than 1 day) for each element.
+        """
+        sdf = self._internal.spark_frame
+        hour_scol_name = verify_temp_column_name(sdf, "__hour_column__")
+        minute_scol_name = verify_temp_column_name(sdf, "__minute_column__")
+        second_scol_name = verify_temp_column_name(sdf, "__second_column__")
+        sum_scol_name = verify_temp_column_name(sdf, "__sum_column__")
+
+        # Extract the hours part, minutes part, seconds part and its fractional part with microseconds
+        sdf = sdf.select(
+            F.expr("date_part('HOUR', %s)" % SPARK_DEFAULT_INDEX_NAME),
+            F.expr("date_part('MINUTE', %s)" % SPARK_DEFAULT_INDEX_NAME),
+            F.expr("date_part('SECOND', %s)" % SPARK_DEFAULT_INDEX_NAME),

Review comment:
       We can add the function in `.../pandas/spark/functions.py` and use `_call_udf` in it

##########
File path: python/pyspark/pandas/tests/indexes/test_timedelta.py
##########
@@ -0,0 +1,84 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+from datetime import timedelta
+
+import pandas as pd
+
+import pyspark.pandas as ps
+from pyspark.testing.pandasutils import PandasOnSparkTestCase, TestUtils
+
+
+class TimedeltaIndexTest(PandasOnSparkTestCase, TestUtils):

Review comment:
       We need to add this to `dev/sparktestsupport/modules.py` file?




-- 
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