@@ -27,28 +27,51 @@ def mock_session():
2727 return session
2828
2929
30- def test_pd_index_to_array_value_with_empty_index_creates_columns (mock_session ):
30+ def test_pd_index_to_array_value_with_empty_index_creates_no_columns (mock_session ):
3131 """
32- Tests that `_pd_index_to_array_value` correctly handles an empty pandas Index by creating
33- an ArrayValue with the expected columns (index column + offset column).
34- This prevents crashes in `unpivot` which expects these columns to exist.
32+ Tests that `_pd_index_to_array_value` with an empty pandas Index creates
33+ an ArrayValue with no columns.
3534 """
3635 empty_index = pd .Index ([], name = "test" )
3736
3837 array_val = blocks ._pd_index_to_array_value (mock_session , empty_index )
3938
40- # Should be 2: one for index, one for offset
41- assert len (array_val .column_ids ) == 2
39+ assert len (array_val .column_ids ) == 0
4240
4341
44- def test_pd_index_to_array_value_with_empty_multiindex_creates_columns (mock_session ):
42+ def test_pd_index_to_array_value_with_empty_multiindex_creates_no_columns (mock_session ):
4543 """
46- Tests that `_pd_index_to_array_value` correctly handles an empty pandas MultiIndex by creating
47- an ArrayValue with the expected columns (one for each level + offset column) .
44+ Tests that `_pd_index_to_array_value` with an empty pandas MultiIndex creates
45+ an ArrayValue with no columns.
4846 """
4947 empty_index = pd .MultiIndex .from_arrays ([[], []], names = ["a" , "b" ])
5048
5149 array_val = blocks ._pd_index_to_array_value (mock_session , empty_index )
5250
53- # Should have 3 columns: a, b, offset
54- assert len (array_val .column_ids ) == 3
51+ assert len (array_val .column_ids ) == 0
52+
53+
54+ def test_unpivot_with_empty_row_labels (mock_session ):
55+ """
56+ Tests that `unpivot` handles an empty `row_labels` index correctly.
57+ """
58+ import pyarrow as pa
59+
60+ # Create a dummy ArrayValue
61+ df = pd .DataFrame ({"a" : [1 , 2 , 3 ]})
62+ pa_table = pa .Table .from_pandas (df )
63+ array_value = blocks .core .ArrayValue .from_pyarrow (pa_table , session = mock_session )
64+
65+ # Call unpivot with an empty pd.Index
66+ unpivot_result , (index_cols , unpivot_cols , passthrough_cols ) = blocks .unpivot (
67+ array_value ,
68+ row_labels = pd .Index ([]),
69+ unpivot_columns = [("a" ,)],
70+ )
71+
72+ # The expected behavior is that the unpivot operation does nothing and returns
73+ # the original array_value and empty column tuples.
74+ assert unpivot_result is array_value
75+ assert index_cols == tuple ()
76+ assert unpivot_cols == tuple ()
77+ assert passthrough_cols == tuple ()
0 commit comments