Skip to content

Commit 7df7ac9

Browse files
committed
Use dateutil.tz.tzlocal directly.
1 parent 0f182d0 commit 7df7ac9

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

python/pyspark/sql/dataframe.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1763,11 +1763,13 @@ def toPandas(self):
17631763

17641764
if self.sql_ctx.getConf("spark.sql.execution.pandas.timeZoneAware", "false").lower() \
17651765
== "true":
1766+
from dateutil import tz
1767+
tzlocal = tz.tzlocal()
17661768
timezone = self.sql_ctx.getConf("spark.sql.session.timeZone")
17671769
for field in self.schema:
17681770
if type(field.dataType) == TimestampType:
17691771
pdf[field.name] = pdf[field.name].apply(
1770-
lambda ts: ts.tz_localize('tzlocal()').tz_convert(timezone))
1772+
lambda ts: ts.tz_localize(tzlocal).tz_convert(timezone))
17711773

17721774
return pdf
17731775

python/pyspark/sql/tests.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2510,6 +2510,8 @@ def test_to_pandas(self):
25102510
@unittest.skipIf(not _have_pandas, "Pandas not installed")
25112511
def test_to_pandas_timezone_aware(self):
25122512
import pandas as pd
2513+
from dateutil import tz
2514+
tzlocal = tz.tzlocal()
25132515
ts = datetime.datetime(1970, 1, 1)
25142516
pdf = pd.DataFrame.from_records([[ts]], columns=['ts'])
25152517

@@ -2530,7 +2532,7 @@ def test_to_pandas_timezone_aware(self):
25302532

25312533
pdf_pst_naive = pdf_pst.copy()
25322534
pdf_pst_naive['ts'] = pdf_pst_naive['ts'].apply(
2533-
lambda ts: ts.tz_convert('tzlocal()').tz_localize(None))
2535+
lambda ts: ts.tz_convert(tzlocal).tz_localize(None))
25342536
self.assertTrue(pdf_pst_naive.equals(pdf))
25352537

25362538
self.spark.conf.unset('spark.sql.execution.pandas.timeZoneAware')

0 commit comments

Comments
 (0)