HyukjinKwon opened a new pull request, #433: URL: https://github.com/apache/spark-connect-rust/pull/433
### What changes were proposed in this pull request? [SPARK-61314](https://issues.apache.org/jira/browse/SPARK-61314): Convert pandas frames with DatetimeIndex, TimedeltaIndex, PeriodIndex or IntervalIndex labels through Arrow, as PySpark does. - A pandas frame whose column labels are an exact `DatetimeIndex`, `TimedeltaIndex`, `PeriodIndex` or `IntervalIndex` now takes the Arrow routes of `createDataFrame` (inferred schema, names, or explicit schema), as frames with str labels, a default `RangeIndex`, or a plain `MultiIndex`/`CategoricalIndex` already do. The check is `_has_canonical_columns` in `python/pyspark/sql/_pandas_input.py`. It accepts these labels with the override rules of an `Index` (the exact class, no override of `__getattribute__`, `__iter__` or `__len__` on the class or the instance, pandas' own iteration and values), and when their storage is pandas' own: the `DatetimeArray`, `TimedeltaArray` or `PeriodArray` of an int64 `_ndarray`, or the `IntervalArray` of NumPy or datetime-like endpoints. - MultiIndex levels and CategoricalIndex categories recurse through the same check, so a MultiIndex with a `DatetimeIndex` level now takes the Arrow routes too. The comment that a DatetimeIndex level keeps the row path is removed. - Subclasses, instance overrides and label containers whose storage is not pandas' own keep the row path, as before. - Official PySpark 4.3.0 names such columns by `str` of the label (`2020-01-01 00:00:00`, `(0, 1]`, `(Timestamp('2020-01-01 00:00:00'), 'x')`), which the Arrow routes already do. Its cell types are the Arrow types of the frame: a mixed int/float column is `double`, a naive datetime list is `array<timestamp_ntz>`, a dict is a `struct`, a NaN in a list is `null`, and a NumPy `int32` is `int`. The row path gave `Type mismatch in row data`, a `timestamp` shifted by the client's local offset, a `map`, `nan` kept in the list, or `bigint`. ### Why are the changes needed? A pandas frame with such labels converted its cells on the native row path, which differs from PySpark. For example, with `spark.sql.session.timeZone=Asia/Seoul` and a client whose local zone is Asia/Seoul: ```python pdf = pd.DataFrame({"x": pd.Series(pd.to_datetime(["2020-01-02 03:00", None]))}) pdf.columns = pd.DatetimeIndex(["2020-01-01"]) spark.createDataFrame(pdf, "p timestamp_ntz").collect() # ours: datetime(2020, 1, 1, 18, 0); PySpark 4.3.0: datetime(2020, 1, 2, 3, 0) ``` The shift follows the client's local zone, not the session zone: it happened for every non-UTC client zone tried (Asia/Seoul and America/Los_Angeles, with either session zone), and not with a UTC client. The same frame under a str label gave the official value. Other cases: `[1, 2.5]` in an object column (`PySparkRuntimeError: Type mismatch in row data`, PySpark: `double`), `{"u": 1, "v": 2}` (a `map`, PySpark: `struct<u:bigint,v:bigint>`), `np.int32` cells (`bigint`, PySpark: `int`), and `[1.0, NaN]` (kept as `nan`, PySpark: `null`). These labels come from `.T` of time-indexed frames and from pivots of datetime columns. ### Does this PR introduce _any_ user-facing change? Yes. Pandas frames whose labels are exact DatetimeIndex, TimedeltaIndex, PeriodIndex or IntervalIndex objects (and MultiIndex/CategoricalIndex labels of those) convert their cells as PySpark does: the values of naive datetimes under an explicit `timestamp_ntz` are no longer shifted by the client's local zone, and the types of mixed numbers, lists, dicts and NumPy scalars match PySpark 4.3.0. Subclassed frames and labels are unchanged. ### How was this patch tested? - `test_datetimelike_labels_convert_cells_as_pyspark` (offline, 25 cases: DatetimeIndex, TimedeltaIndex, PeriodIndex, IntervalIndex and a MultiIndex with a DatetimeIndex level, each with a mixed int/float column, a naive datetime list, a dict, a NaN in a list, and `np.int32` cells): the Arrow type and the values of the cells are PySpark's (`double`, `list<timestamp[us]>`, `struct<u: int64, v: string>`, `list<double>` with null, `int32`). All 25 fail without this change and pass with it. The same schemas were checked against official PySpark 4.3.0 with these frames (UTC client). - `test_create_dataframe_of_datetime_labels_keeps_ntz_values_as_pyspark` (live): with `spark.sql.session.timeZone=Asia/Seoul` and the client's local zone set to Asia/Seoul (restored afterwards), a datetime64 column under a DatetimeIndex label with an explicit `timestamp_ntz` reads back as `2020-01-02 03:00:00`, as official PySpark 4.3.0 does. It fails without this change (`2020-01-01 18:00:00`). The client zone must be set too: under a UTC client the row path does not shift. - Probes against official PySpark 4.3.0 over the label kinds above (plus tz-aware and NaT DatetimeIndex, empty DatetimeIndex, IntervalIndex of datetimes, CategoricalIndex of DatetimeIndex, PeriodIndex and IntervalIndex categories, MultiIndex levels of TimedeltaIndex, PeriodIndex, IntervalIndex and tz-aware DatetimeIndex) across 40+ cell kinds, with no schema, a DDL, and names: 0 divergences after this change (1050 and 350 result lines compared); the pre-fix code diverged in 145 lines of the same probe. - The offline Python suites (2952 passed, 8 skipped), the new tests also under pytest 9.2, and the live drop-in tests `-k pandas` (19 passed) and `-k "labels or ntz or empty_arrow_table"` (7 passed) against a local Spark 4.3.0 server. The pandas internals the check reads were checked against the pandas 3.1.1 sources, which CI's coverage job installs. No Rust code changes. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Opus 5.5 This pull request and its description were written by Isaac. -- 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]
