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
49 changes: 48 additions & 1 deletion python/pyarrow/src/arrow/python/inference.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ class NumPyDtypeUnifier {
GetNumPyTypeName(new_dtype));
}

Status InvalidDatetimeUnitMix(PyArray_Descr* new_descr) {
auto new_meta = reinterpret_cast<PyArray_DatetimeDTypeMetaData*>(
PyDataType_C_METADATA(new_descr));
auto current_meta = reinterpret_cast<PyArray_DatetimeDTypeMetaData*>(
PyDataType_C_METADATA(current_dtype_));

return Status::Invalid("Cannot mix NumPy datetime64 units ",
DatetimeUnitName(current_meta->meta.base), " and ",
DatetimeUnitName(new_meta->meta.base));
}

int Observe_BOOL(PyArray_Descr* descr, int dtype) { return INVALID; }

int Observe_INT8(PyArray_Descr* descr, int dtype) {
Expand Down Expand Up @@ -255,7 +266,17 @@ class NumPyDtypeUnifier {
}

int Observe_DATETIME(PyArray_Descr* dtype_obj) {
// TODO: check that units are all the same
// Check that datetime units are consistent across all values
auto datetime_meta = reinterpret_cast<PyArray_DatetimeDTypeMetaData*>(
PyDataType_C_METADATA(dtype_obj));
auto current_meta = reinterpret_cast<PyArray_DatetimeDTypeMetaData*>(
PyDataType_C_METADATA(current_dtype_));

if (datetime_meta->meta.base != current_meta->meta.base) {
// Units don't match - this is invalid
return INVALID;
}

return OK;
}

Expand All @@ -267,6 +288,13 @@ class NumPyDtypeUnifier {
current_type_num_ = dtype;
return Status::OK();
} else if (current_type_num_ == dtype) {
// Same type, but for datetime we still need to check units match
if (dtype == NPY_DATETIME) {
int action = Observe_DATETIME(descr);
if (action == INVALID) {
return InvalidDatetimeUnitMix(descr);
}
}
return Status::OK();
}

Expand Down Expand Up @@ -309,6 +337,25 @@ class NumPyDtypeUnifier {
int current_type_num() const { return current_type_num_; }

private:
static const char* DatetimeUnitName(NPY_DATETIMEUNIT unit) {
switch (unit) {
case NPY_FR_s:
return "s";
case NPY_FR_ms:
return "ms";
case NPY_FR_us:
return "us";
case NPY_FR_ns:
return "ns";
case NPY_FR_D:
return "D";
case NPY_FR_GENERIC:
return "generic";
default:
return "unknown";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Numpy has other time units. Could we perhaps print which was used in case this is hit instead of unknown? I don't know if this is practical, just asking.

Copy link
Member Author

@HyukjinKwon HyukjinKwon Dec 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me check and update it!

}
}

int current_type_num_;
PyArray_Descr* current_dtype_;
};
Expand Down
6 changes: 3 additions & 3 deletions python/pyarrow/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2489,7 +2489,7 @@ def test_array_from_different_numpy_datetime_units_raises():
ms = np.array(data, dtype='datetime64[ms]')
data = list(s[:2]) + list(ms[2:])

with pytest.raises(pa.ArrowNotImplementedError):
with pytest.raises(pa.ArrowInvalid, match="datetime64 units s and ms"):
pa.array(data)


Expand All @@ -2514,8 +2514,8 @@ def test_array_from_timestamp_with_generic_unit():
x = np.datetime64('2017-01-01 01:01:01.111111111')
y = np.datetime64('2018-11-22 12:24:48.111111111')

with pytest.raises(pa.ArrowNotImplementedError,
match='Unbound or generic datetime64 time unit'):
with pytest.raises(pa.ArrowInvalid,
match='Cannot mix NumPy datetime64 units'):
pa.array([n, x, y])


Expand Down
Loading