HyukjinKwon opened a new pull request, #422:
URL: https://github.com/apache/spark-connect-rust/pull/422

   ### What changes were proposed in this pull request?
   
   [SPARK-61244](https://issues.apache.org/jira/browse/SPARK-61244): Convert 
pandas frames with MultiIndex or CategoricalIndex labels through Arrow, as 
PySpark does.
   
   - A pandas frame whose column labels are a plain `pd.MultiIndex` or 
`pd.CategoricalIndex` now takes the Arrow routes of `createDataFrame` (explicit 
schema, names, or inferred schema), as frames with a default `RangeIndex` 
already do. The check is `_has_canonical_columns`, which accepts these labels 
when they have the override rules of an `Index` (the exact class, no 
`__getattribute__`, `__iter__` or `__len__` override on the class or the 
instance, pandas' own values), and when their storage is pandas' own: the 
levels and codes of a MultiIndex, and the categorical values and categories of 
a CategoricalIndex. Each label container inside is checked as an `Index`, so a 
level that is not one (a `DatetimeIndex`, say) keeps the row path.
   - Official PySpark 4.3.0 names such columns by `str` of the label (`('a', 
'x')`), which the Arrow routes already do. Naive datetimes in a list become 
`array<timestamp_ntz>`, Decimal lists take the smallest precision and scale, 
and dict cells are structs of the value types of their keys (a map only under 
`spark.sql.execution.pandas.inferPandasDictAsMap`), as PySpark does. The row 
path gave `array<timestamp>` (shifted by the session time zone), 
`decimal(38,s)`, a map, or `Type mismatch in row data`.
   - Subclassed frames and the other label kinds keep the row path, as before: 
a subclass can override any method the Arrow routes call, so its method order 
stays that of the row path.
   - The Row scan of object columns (`inferred_pandas_row_columns`) skips a 
column whose cells all have exact builtin scalar types, by comparing the 
identities of their types (hashing a type could run a metaclass hook). Such a 
column holds no Row, so the result is unchanged. For the row-path frames that 
remain, the scan of a 10k x 5 object frame takes about 4-5 ms instead of about 
15-17 ms.
   
   Measured on a local Spark 4.3.0 server (UTC), 10k x 5 object strings, median 
of 5 runs, under a heavily loaded machine: MultiIndex str 31 ms before, 5 ms 
after (official 4.8 ms); CategoricalIndex str 32 ms before, 10 ms after; a 
subclass of DataFrame 32 ms before, 19 ms after (it keeps the row path); a 
plain named frame and a default RangeIndex frame are unchanged.
   
   ### Why are the changes needed?
   
   A pandas frame whose labels were a plain MultiIndex or CategoricalIndex took 
the native row path, which converts cells differently from PySpark:
   
   - `spark.createDataFrame(pd.DataFrame([[[datetime(2020, 1, 2, 3)]]], 
columns=pd.MultiIndex.from_tuples([("a", "x")])))` under 
`spark.sql.session.timeZone=Asia/Seoul` read back as `2020-01-02 12:00:00`: the 
naive datetime was taken as a time of the session time zone, and the list type 
was `array<timestamp>`. PySpark gives `array<timestamp_ntz>` and reads back 
`2020-01-02 03:00:00`.
   - A list of `Decimal("1.5"), Decimal("10.25")` became 
`array<decimal(38,2)>`; PySpark gives `array<decimal(4,2)>`.
   - `spark.createDataFrame(pd.DataFrame([[{"u": 1, "v": "a"}]], 
columns=pd.MultiIndex.from_tuples([("a", "x")])))` failed with 
`PySparkRuntimeError: Type mismatch in row data`; PySpark gives 
`struct<u:bigint,v:string>`. The same frame with `{"u": 1, "v": 2}` gave a map; 
PySpark gives a struct.
   - The Row scan that 60bb98ea added to the row path made frames of object 
string columns about 2.7x slower than before that change.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. Plain MultiIndex and CategoricalIndex column labels of a pandas frame 
now convert as PySpark converts them: the types of list, Decimal and dict 
cells, and the session time zone of naive datetimes, match PySpark 4.3.0. 
Subclassed frames are unchanged.
   
   ### How was this patch tested?
   
   - `test_multi_and_categorical_labels_convert_cells_as_pyspark` (offline, 12 
cases: 2- and 3-level MultiIndex and CategoricalIndex labels, each with a naive 
datetime list, a Decimal list, a mixed dict and a same-key dict): the Arrow 
type of the cells is the one PySpark infers (`list<timestamp[us]>`, 
`list<decimal128(4, 2)>`, `struct<u: int64, v: string>`, `struct<u: int64, v: 
int64>`) and the values are unchanged. All 12 fail without this change and pass 
with it.
   - `test_create_dataframe_of_multi_and_categorical_labels_as_pyspark` (live): 
with `spark.sql.session.timeZone=Asia/Seoul`, the names are the labels as 
strings, the schemas are PySpark's (`array<timestamp_ntz>`, 
`array<decimal(4,2)>`, `struct<u:bigint,v:string>`, 
`struct<u:bigint,v:bigint>`), and the naive datetime reads back as `2020-01-02 
03:00:00`. It fails without this change (`Type mismatch in row data`), and the 
same schemas and values were checked against official PySpark 4.3.0 with these 
frames.
   - Probes against official PySpark 4.3.0 over MultiIndex (2 and 3 levels, 
int, mixed, duplicate and `None` labels, empty frames), CategoricalIndex (str, 
int, mixed, duplicate labels), with no schema, a DDL or StructType schema, 
names, `inferPandasDictAsMap`, and `spark.sql.execution.arrow.pyspark.enabled` 
on and off, under UTC and Asia/Seoul: the results and errors are identical, 
except a MultiIndex with a `DatetimeIndex` level, which keeps the row path.
   - The offline Python suites (2831 passed, 8 skipped), the new tests also 
under pytest 9.2, and the live drop-in tests `-k pandas` (19 passed) against a 
local Spark 4.3.0 server. 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]

Reply via email to