Github user HyukjinKwon commented on a diff in the pull request:

    https://github.com/apache/spark/pull/18933#discussion_r133209362
  
    --- Diff: python/pyspark/sql/tests.py ---
    @@ -2507,6 +2507,37 @@ def test_to_pandas(self):
             self.assertEquals(types[2], np.bool)
             self.assertEquals(types[3], np.float32)
     
    +    @unittest.skipIf(not _have_pandas, "Pandas not installed")
    +    def test_to_pandas_timezone_aware(self):
    +        import pandas as pd
    +        from dateutil import tz
    +        tzlocal = tz.tzlocal()
    +        ts = datetime.datetime(1970, 1, 1)
    +        pdf = pd.DataFrame.from_records([[ts]], columns=['ts'])
    +
    +        self.spark.conf.set('spark.sql.session.timeZone', 
'America/Los_Angeles')
    +
    +        schema = StructType().add("ts", TimestampType())
    +        df = self.spark.createDataFrame([(ts,)], schema)
    +
    +        pdf_naive = df.toPandas()
    +        self.assertEqual(pdf_naive['ts'][0].tzinfo, None)
    +        self.assertTrue(pdf_naive.equals(pdf))
    +
    +        self.spark.conf.set('spark.sql.execution.pandas.timeZoneAware', 
'true')
    +
    +        pdf_pst = df.toPandas()
    +        self.assertEqual(pdf_pst['ts'][0].tzinfo.zone, 
'America/Los_Angeles')
    +        self.assertFalse(pdf_pst.equals(pdf))
    +
    +        pdf_pst_naive = pdf_pst.copy()
    +        pdf_pst_naive['ts'] = pdf_pst_naive['ts'].apply(
    +            lambda ts: ts.tz_convert(tzlocal).tz_localize(None))
    +        self.assertTrue(pdf_pst_naive.equals(pdf))
    +
    +        self.spark.conf.unset('spark.sql.execution.pandas.timeZoneAware')
    +        self.spark.conf.unset('spark.sql.session.timeZone')
    --- End diff --
    
    (Not a big deal but we could use `finally` just in case this test fails and 
other tests do not get affected by this test failure in the future)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

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

Reply via email to