Spenserrrr commented on code in PR #57435: URL: https://github.com/apache/spark/pull/57435#discussion_r3633826301
########## python/pyspark/tests/upstream/pyarrow/test_pyarrow_arrow_to_pandas_coerce_temporal.py: ########## @@ -0,0 +1,276 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +Tests for PyArrow Array.to_pandas(coerce_temporal_nanoseconds=True) using golden +file comparison. + +PySpark relies on this argument in production: when building pandas objects from +Arrow data (see ``python/pyspark/sql/pandas/conversion.py``), it calls +``to_pandas(coerce_temporal_nanoseconds=True)`` so that the Arrow path produces the +same nanosecond-resolution ``datetime64[ns]`` / ``timedelta64[ns]`` values as the +non-Arrow path. This test records how each Arrow temporal type behaves under that +argument so CI fails loudly if the behavior drifts across pandas/PyArrow/NumPy +upgrades. + +``coerce_temporal_nanoseconds=True`` forces temporal values to nanosecond +resolution. The interesting rows are therefore the temporal types (timestamp and +duration in units s/ms/us/ns, plus tz-aware timestamp, date, and time); a handful +of non-temporal control rows (int/float/string) are included to demonstrate that +the argument leaves non-temporal types unaffected. + +## Golden File Cell Format + +Each cell uses the value@type format: +- numpy ndarray: "python_list_repr@ndarray[dtype]" +- pandas Series: "python_list_repr@Series[dtype]" +- Error: "ERR@ExceptionClassName" + +Values are formatted via tolist() for stable, Python-native representation. + +## Regenerating Golden Files + +Set SPARK_GENERATE_GOLDEN_FILES=1 before running: + + SPARK_GENERATE_GOLDEN_FILES=1 python -m pytest \\ + python/pyspark/tests/upstream/pyarrow/test_pyarrow_arrow_to_pandas_coerce_temporal.py + +## PyArrow and pandas Version Compatibility + +The golden files capture behavior for specific PyArrow and pandas versions. +Regenerate when upgrading either dependency, as to_pandas() behavior may change. +The committed golden files were generated with pandas 2.3.3, pyarrow 24.0.0, and +numpy 2.4.1. +""" + +import datetime +import inspect +import os +import unittest +from typing import Callable, List, Optional + +from pyspark.loose_version import LooseVersion +from pyspark.testing.utils import ( + have_pyarrow, + have_pandas, + have_numpy, + pyarrow_requirement_message, + pandas_requirement_message, + numpy_requirement_message, +) +from pyspark.testing.goldenutils import GoldenFileTestMixin + +if have_pandas: + import pandas as pd +if have_pyarrow: + import pyarrow as pa + + [email protected]( + not have_pyarrow or not have_pandas or not have_numpy, + pyarrow_requirement_message or pandas_requirement_message or numpy_requirement_message, +) +class PyArrowArrayToPandasCoerceTemporalTests(GoldenFileTestMixin, unittest.TestCase): + """ + Tests pa.Array.to_pandas(coerce_temporal_nanoseconds=True) via golden file comparison. + + Covers the temporal Arrow types the argument affects (timestamp and duration in + units s/ms/us/ns, tz-aware timestamp, date, and time), plus an overflow case and + a few non-temporal control rows. Each type is tested without nulls, with a null, + and empty. + """ + + def compare_or_generate_golden_matrix( Review Comment: Hi @uros-b, I will add the two test cases. For putting `compare_or_generate_golden_matrix` into `GoldenFileTestMixin`, I agreed that we should do so. Also note that it is also duplicated in test_pyarrow_array_cast.py, so I think a proper edit would move it into mixin and update all three. Would you prefer I do that refactor as a separate PR or fold this cleanup here? Happy to do either way. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
