From 839a0a2fdc887b3ba44177cf6e26675bd9886822 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Fri, 30 Jan 2026 15:55:28 -0800 Subject: [PATCH] Restore timedelta dtype based on the original dtype --- python/pyspark/pandas/data_type_ops/timedelta_ops.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/python/pyspark/pandas/data_type_ops/timedelta_ops.py b/python/pyspark/pandas/data_type_ops/timedelta_ops.py index 7a9da8511e622..aa3e8f64801fa 100644 --- a/python/pyspark/pandas/data_type_ops/timedelta_ops.py +++ b/python/pyspark/pandas/data_type_ops/timedelta_ops.py @@ -21,6 +21,7 @@ import pandas as pd from pandas.api.types import CategoricalDtype +from pyspark.loose_version import LooseVersion from pyspark.sql.types import ( BooleanType, DayTimeIntervalType, @@ -64,6 +65,13 @@ def prepare(self, col: pd.Series) -> pd.Series: """Prepare column when from_pandas.""" return col + def restore(self, col: pd.Series) -> pd.Series: + """Restore column when to_pandas.""" + if LooseVersion(pd.__version__) < "3.0.0": + return col + else: + return col.astype(self.dtype) + def sub(self, left: IndexOpsLike, right: Any) -> SeriesOrIndex: _sanitize_list_like(right)