Skip to content

Commit 6d0146e

Browse files
committed
SNOW-2090928: [Local Testing] Fix indexing issue in to_timestamp
1 parent 0fc87b3 commit 6d0146e

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

src/snowflake/snowpark/mock/_functions.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,10 +1065,13 @@ def convert_timestamp(row):
10651065
SnowparkLocalTestingException.raise_from_error(exc)
10661066

10671067
res = column.to_frame().apply(convert_timestamp, axis=1).replace({pandas.NaT: None})
1068-
return [
1069-
x.to_pydatetime() if x is not None and hasattr(x, "to_pydatetime") else x
1070-
for x in res
1071-
]
1068+
return pandas.Series(
1069+
[
1070+
x.to_pydatetime() if x is not None and hasattr(x, "to_pydatetime") else x
1071+
for x in res
1072+
],
1073+
index=column.index,
1074+
)
10721075

10731076

10741077
@patch("to_timestamp")
@@ -1102,6 +1105,7 @@ def mock_to_timestamp_ntz(
11021105
TimestampType(TimestampTimeZone.NTZ), column.sf_type.nullable
11031106
),
11041107
dtype=object,
1108+
index=result.index,
11051109
)
11061110

11071111

@@ -1121,6 +1125,7 @@ def mock_to_timestamp_ltz(
11211125
TimestampType(TimestampTimeZone.LTZ), column.sf_type.nullable
11221126
),
11231127
dtype=object,
1128+
index=result.index,
11241129
)
11251130

11261131

@@ -1132,12 +1137,14 @@ def mock_to_timestamp_tz(
11321137
):
11331138
# _to_timestamp will use the tz present in the data.
11341139
# Otherwise it adds an appropriate one by default.
1140+
result = _to_timestamp(column, fmt, try_cast, add_timezone=True)
11351141
return ColumnEmulator(
1136-
data=_to_timestamp(column, fmt, try_cast, add_timezone=True),
1142+
data=result,
11371143
sf_type=ColumnType(
11381144
TimestampType(TimestampTimeZone.TZ), column.sf_type.nullable
11391145
),
11401146
dtype=object,
1147+
index=result.index,
11411148
)
11421149

11431150

tests/integ/scala/test_function_suite.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
cume_dist,
8585
current_database,
8686
current_session,
87+
date_format,
8788
date_part,
8889
date_trunc,
8990
dateadd,
@@ -1990,6 +1991,41 @@ def test_to_date(session):
19901991
Utils.check_answer(df.select(to_date(df.a, "YYYY-MM-DD")), expected5)
19911992

19921993

1994+
def test_date_format(session):
1995+
df = session.create_dataframe(
1996+
[
1997+
(date(2025, 1, 1), "A"),
1998+
(date(2025, 2, 2), "B"),
1999+
(date(2025, 3, 3), "A"),
2000+
(date(2025, 4, 4), "A"),
2001+
],
2002+
["date", "cat"],
2003+
)
2004+
2005+
# Check format works
2006+
Utils.check_answer(
2007+
df.withColumn("FORMATTED", date_format("DATE", "MMYYYY")),
2008+
[
2009+
Row(date(2025, 1, 1), "A", "012025"),
2010+
Row(date(2025, 2, 2), "B", "022025"),
2011+
Row(date(2025, 3, 3), "A", "032025"),
2012+
Row(date(2025, 4, 4), "A", "042025"),
2013+
],
2014+
)
2015+
2016+
# Check format after filter works
2017+
Utils.check_answer(
2018+
df.filter(col("CAT") == "A").withColumn(
2019+
"FORMATTED", date_format("DATE", "MMYYYY")
2020+
),
2021+
[
2022+
Row(date(2025, 1, 1), "A", "012025"),
2023+
Row(date(2025, 3, 3), "A", "032025"),
2024+
Row(date(2025, 4, 4), "A", "042025"),
2025+
],
2026+
)
2027+
2028+
19932029
@pytest.mark.skipif(
19942030
"config.getoption('local_testing_mode', default=False)",
19952031
reason="arrays_overlap is not yet supported in local testing mode.",

0 commit comments

Comments
 (0)