xinrong-databricks commented on a change in pull request #32469:
URL: https://github.com/apache/spark/pull/32469#discussion_r633720057



##########
File path: python/pyspark/pandas/data_type_ops/datetime_ops.py
##########
@@ -0,0 +1,99 @@
+#
+# 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.
+#
+
+import datetime
+import warnings
+
+from pyspark.sql import functions as F
+from pyspark.sql.types import TimestampType
+
+from pyspark.pandas.base import IndexOpsMixin
+from pyspark.pandas.data_type_ops.base import DataTypeOps
+from pyspark.pandas.typedef import as_spark_type
+
+
+class DatetimeOps(DataTypeOps):
+    """
+    The class for binary operations of pandas-on-Spark objects with spark 
type: TimestampType.
+    """
+
+    def __add__(self, left, right):
+        raise TypeError("addition can not be applied to date times.")
+
+    def __sub__(self, left, right):
+        # Note that timestamp subtraction casts arguments to integer. This is 
to mimic pandas's
+        # behaviors. pandas returns 'timedelta64[ns]' from 'datetime64[ns]'s 
subtraction.
+        msg = (
+            "Note that there is a behavior difference of timestamp 
subtraction. "
+            "The timestamp subtraction returns an integer in seconds, "
+            "whereas pandas returns 'timedelta64[ns]'."
+        )
+        if isinstance(right, IndexOpsMixin) and 
isinstance(right.spark.data_type, TimestampType):
+            warnings.warn(msg, UserWarning)
+            return left.astype("long") - right.astype("long")
+        elif isinstance(right, datetime.datetime):
+            warnings.warn(msg, UserWarning)
+            return left.astype("long") - 
F.lit(right).cast(as_spark_type("long"))
+        else:
+            raise TypeError("datetime subtraction can only be applied to 
datetime series.")
+
+    def __mul__(self, left, right):
+        raise TypeError("multiplication can not be applied to date times.")

Review comment:
       Good idea! Would you think it fine to set `pretty_name` as an abstract 
property? That would be helpful when consolidating TypeError messages later.

##########
File path: python/pyspark/pandas/data_type_ops/string_ops.py
##########
@@ -0,0 +1,98 @@
+#
+# 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 pandas.api.types import CategoricalDtype
+
+from pyspark.sql import functions as F
+from pyspark.sql.types import IntegralType, StringType
+
+from pyspark.pandas.base import column_op, IndexOpsMixin
+from pyspark.pandas.data_type_ops.base import DataTypeOps
+from pyspark.pandas.spark import functions as SF
+
+
+class StringOps(DataTypeOps):
+    """
+    The class for binary operations of pandas-on-Spark objects with spark 
type: StringType.
+    """
+
+    @property
+    def pretty_name(self):
+        return 'strings'
+
+    def __add__(self, left, right):
+        if isinstance(right, IndexOpsMixin) and 
isinstance(right.spark.data_type, StringType):
+            return column_op(F.concat)(left, right)
+        elif isinstance(right, str):
+            return column_op(F.concat)(left, F.lit(right))
+        else:
+            raise TypeError("string addition can only be applied to string 
series or literals.")
+
+    def __sub__(self, left, right):
+        raise TypeError("subtraction can not be applied to string series or 
literals.")
+
+    def __mul__(self, left, right):
+        if isinstance(right, str):
+            raise TypeError("multiplication can not be applied to a string 
literal.")
+
+        if (
+                isinstance(right, IndexOpsMixin)
+                and isinstance(right.spark.data_type, IntegralType)
+                and not isinstance(right.dtype, CategoricalDtype)
+        ) or isinstance(right, int):
+            return column_op(SF.repeat)(left, right)
+        else:
+            raise TypeError("a string series can only be multiplied to an int 
series or literal")
+
+    def __truediv__(self, left, right):
+        raise TypeError("division can not be applied on string series or 
literals.")
+
+    def __floordiv__(self, left, right):

Review comment:
       TypeError messages in StringOps are not inherited from DateTypeOps as 
other classes do for preserving the existing behavior.
   We would consolidate TypeError messages later.




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

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to