|
19 | 19 | from enum import Enum |
20 | 20 | from itertools import chain |
21 | 21 | import datetime |
| 22 | +import unittest |
22 | 23 |
|
23 | 24 | from pyspark.sql import Column, Row |
24 | 25 | from pyspark.sql import functions as sf |
25 | 26 | from pyspark.sql.types import StructType, StructField, IntegerType, LongType |
26 | 27 | from pyspark.errors import AnalysisException, PySparkTypeError, PySparkValueError |
27 | | -from pyspark.testing.sqlutils import ReusedSQLTestCase |
| 28 | +from pyspark.testing.sqlutils import ReusedSQLTestCase, have_pandas, pandas_requirement_message |
28 | 29 |
|
29 | 30 |
|
30 | 31 | class ColumnTestsMixin: |
@@ -289,6 +290,26 @@ def test_lit_time_representation(self): |
289 | 290 | ts = datetime.datetime(2021, 3, 4, 12, 34, 56, 1234) |
290 | 291 | self.assertEqual(str(sf.lit(ts)), "Column<'2021-03-04 12:34:56.001234'>") |
291 | 292 |
|
| 293 | + @unittest.skipIf(not have_pandas, pandas_requirement_message) |
| 294 | + def test_lit_delta_representation(self): |
| 295 | + for delta in [ |
| 296 | + datetime.timedelta(days=1), |
| 297 | + datetime.timedelta(hours=2), |
| 298 | + datetime.timedelta(minutes=3), |
| 299 | + datetime.timedelta(seconds=4), |
| 300 | + datetime.timedelta(microseconds=5), |
| 301 | + datetime.timedelta(days=2, hours=21, microseconds=908), |
| 302 | + datetime.timedelta(days=1, minutes=-3, microseconds=-1001), |
| 303 | + datetime.timedelta(days=1, hours=2, minutes=3, seconds=4, microseconds=5), |
| 304 | + ]: |
| 305 | + import pandas as pd |
| 306 | + |
| 307 | + # Column<'PT69H0.000908S'> or Column<'P2DT21H0M0.000908S'> |
| 308 | + s = str(sf.lit(delta)) |
| 309 | + |
| 310 | + # Parse the ISO string representation and compare |
| 311 | + self.assertTrue(pd.Timedelta(s[8:-2]).to_pytimedelta() == delta) |
| 312 | + |
292 | 313 | def test_enum_literals(self): |
293 | 314 | class IntEnum(Enum): |
294 | 315 | X = 1 |
|
0 commit comments