Skip to content

[SPARK-39821][PYTHON][PS] Fix error during using DatetimeIndex #37232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions python/pyspark/pandas/tests/indexes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ def test_map(self):
mapper_pser = pd.Series([1, 2, 3], index=pidx)
self.assert_eq(psidx.map(mapper_pser), pidx.map(mapper_pser))

def test_repr(self):
pidx_repr = pd.DatetimeIndex(['1970-01-01', '1970-02-01', '1970-03-01']).__repr__()
psidx_repr = ps.DatetimeIndex(['1970-01-01', '1970-02-01', '1970-03-01']).__repr__()
self.assert_eq(pidx_repr, psidx_repr)


if __name__ == "__main__":
import unittest
Expand Down
7 changes: 5 additions & 2 deletions python/pyspark/sql/pandas/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def toPandas(self) -> "PandasDataFrameLike":

import numpy as np
import pandas as pd
from pandas.core.dtypes.common import is_timedelta64_dtype
from pandas.core.dtypes.common import is_timedelta64_dtype, is_datetime64_dtype

jconf = self.sparkSession._jconf
timezone = jconf.sessionLocalTimeZone()
Expand Down Expand Up @@ -244,7 +244,10 @@ def toPandas(self) -> "PandasDataFrameLike":
# No need to cast for non-empty series for timedelta. The type is already correct.
should_check_timedelta = is_timedelta64_dtype(t) and len(pdf) == 0

if (t is not None and not is_timedelta64_dtype(t)) or should_check_timedelta:
if (t is not None and
not all([
is_timedelta64_dtype(t),
is_datetime64_dtype(t)])) or should_check_timedelta:
series = series.astype(t, copy=False)

with catch_warnings():
Expand Down