Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions python/pyarrow/table.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -2896,6 +2896,8 @@ cdef class RecordBatch(_Tabular):

if isinstance(field_, Field):
c_field = field_
if c_arr.type == null() and c_field.type != null():
c_arr = c_arr.cast(c_field.type)
else:
c_field = field(field_, c_arr.type)

Expand Down Expand Up @@ -2994,6 +2996,8 @@ cdef class RecordBatch(_Tabular):

if isinstance(field_, Field):
c_field = field_
if c_arr.type == null() and c_field.type != null():
c_arr = c_arr.cast(c_field.type)
else:
c_field = field(field_, c_arr.type)

Expand Down Expand Up @@ -5430,6 +5434,8 @@ cdef class Table(_Tabular):

if isinstance(field_, Field):
c_field = field_
if c_arr.type == null() and c_field.type != null():
c_arr = c_arr.cast(c_field.type)
else:
c_field = field(field_, c_arr.type)

Expand Down Expand Up @@ -5519,6 +5525,8 @@ cdef class Table(_Tabular):

if isinstance(field_, Field):
c_field = field_
if c_arr.type == null() and c_field.type != null():
c_arr = c_arr.cast(c_field.type)
else:
c_field = field(field_, c_arr.type)

Expand Down
51 changes: 51 additions & 0 deletions python/pyarrow/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,57 @@ def test_table_add_column(cls):
assert t4.equals(expected)


@pytest.mark.parametrize(
('cls'),
[
(pa.Table),
(pa.RecordBatch)
]
)
@pytest.mark.parametrize('method', ['add_column', 'set_column'])
def test_table_add_column_all_null_data_typed_field(cls, method):
# GH-46918: an explicitly typed field must win over the null type
# inferred from all-null column data
table = cls.from_arrays([pa.array([1.0, 2.0])], names=('a',))
column = [[None, None]] if cls is pa.Table else [None, None]

new_field = pa.field('b', pa.float64(), nullable=True)
result = getattr(table, method)(0, new_field, column)

assert result.schema.field('b').type == pa.float64()
assert result.column('b').to_pylist() == [None, None]

# a null field with null data stays null
null_field = pa.field('b', pa.null())
result = getattr(table, method)(0, null_field, column)
assert result.schema.field('b').type == pa.null()

# non-null data that does not match the field type is still rejected
values = [[1.5, 2.5]] if cls is pa.Table else [1.5, 2.5]
with pytest.raises(pa.ArrowInvalid if cls is pa.Table
else pa.ArrowTypeError):
getattr(table, method)(0, pa.field('b', pa.int64()), values)


@pytest.mark.parametrize(
('cls'),
[
(pa.Table),
(pa.RecordBatch)
]
)
def test_table_append_column_all_null_data_typed_field(cls):
# GH-46918
table = cls.from_arrays([pa.array([1.0, 2.0])], names=('a',))
column = [[None, None]] if cls is pa.Table else [None, None]

result = table.append_column(
pa.field('b', pa.float64(), nullable=True), column)

assert result.schema.field('b').type == pa.float64()
assert result.column('b').to_pylist() == [None, None]


@pytest.mark.parametrize(
('cls'),
[
Expand Down