ianmcook commented on code in PR #46529:
URL: https://github.com/apache/spark/pull/46529#discussion_r1610699914


##########
python/pyspark/sql/pandas/types.py:
##########
@@ -232,6 +312,124 @@ def _get_local_timezone() -> str:
     return os.environ.get("TZ", "dateutil/:")
 
 
+def _check_arrow_array_timestamps_localize(
+    a: Union["pa.Array", "pa.ChunkedArray"],
+    dt: DataType,
+    truncate: bool = True,
+    timezone: Optional[str] = None,
+) -> Union["pa.Array", "pa.ChunkedArray"]:
+    """
+    Convert Arrow timestamps to timezone-naive in the specified timezone if 
the specified Spark
+    data type is TimestampType, and optionally truncate nanosecond timestamps 
to microseconds.
+
+    This function works on Arrow Arrays and ChunkedArrays, and it recurses to 
convert nested
+    timestamps.
+
+    Parameters
+    ----------
+    a : :class:`pyarrow.Array` or :class:`pyarrow.ChunkedArray`
+    dt : :class:`DataType`
+        The Spark data type corresponding to the Arrow Array to be converted.
+    truncate : bool, default True
+        Whether to truncate nanosecond timestamps to microseconds. (default 
``True``)
+    timezone : str, optional
+        The timezone to convert from. If there is a timestamp type, it's 
required.
+
+    Returns
+    -------
+    :class:`pyarrow.Array` or :class:`pyarrow.ChunkedArray`
+    """
+    import pyarrow.types as types
+    import pyarrow as pa
+    import pyarrow.compute as pc
+
+    if isinstance(a, pa.ChunkedArray) and (types.is_nested(a.type) or 
types.is_dictionary(a.type)):
+        return pa.chunked_array(
+            [
+                _check_arrow_array_timestamps_localize(chunk, dt, truncate, 
timezone)
+                for chunk in a.iterchunks()
+            ]
+        )
+
+    if types.is_timestamp(a.type) and truncate and a.type.unit == "ns":
+        a = pc.floor_temporal(a, unit="microsecond")
+
+    if types.is_timestamp(a.type) and a.type.tz is None and type(dt) == 
TimestampType:
+        assert timezone is not None
+
+        # Only localize timestamps that will become Spark TimestampType 
columns.
+        # Do not localize timestamps that will become Spark TimestampNTZType 
columns.
+        return pc.assume_timezone(a, timezone)
+    if types.is_list(a.type):
+        at: ArrayType = cast(ArrayType, dt)
+        return pa.ListArray.from_arrays(
+            a.offsets,
+            _check_arrow_array_timestamps_localize(a.values, at.elementType, 
truncate, timezone),
+        )

Review Comment:
   I think it is worthwhile if it keeps zero-copy. Added in 7ed45e0. I did it 
for MapArrays and StructArrays too.



-- 
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-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to