Skip to content

Commit 86aa893

Browse files
committed
address a comment
1 parent 3f3c322 commit 86aa893

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

python/pyarrow/src/arrow/python/inference.cc

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,18 @@ class NumPyDtypeUnifier {
339339
private:
340340
static const char* DatetimeUnitName(NPY_DATETIMEUNIT unit) {
341341
switch (unit) {
342+
case NPY_FR_Y:
343+
return "Y";
344+
case NPY_FR_M:
345+
return "M";
346+
case NPY_FR_W:
347+
return "W";
348+
case NPY_FR_D:
349+
return "D";
350+
case NPY_FR_h:
351+
return "h";
352+
case NPY_FR_m:
353+
return "m";
342354
case NPY_FR_s:
343355
return "s";
344356
case NPY_FR_ms:
@@ -347,8 +359,12 @@ class NumPyDtypeUnifier {
347359
return "us";
348360
case NPY_FR_ns:
349361
return "ns";
350-
case NPY_FR_D:
351-
return "D";
362+
case NPY_FR_ps:
363+
return "ps";
364+
case NPY_FR_fs:
365+
return "fs";
366+
case NPY_FR_as:
367+
return "as";
352368
case NPY_FR_GENERIC:
353369
return "generic";
354370
default:

python/pyarrow/tests/test_array.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,6 +2493,30 @@ def test_array_from_different_numpy_datetime_units_raises():
24932493
pa.array(data)
24942494

24952495

2496+
@pytest.mark.numpy
2497+
@pytest.mark.parametrize('unit,unit_name', [
2498+
('Y', 'Y'), # year
2499+
('M', 'M'), # month
2500+
('W', 'W'), # week
2501+
('h', 'h'), # hour
2502+
('m', 'm'), # minute
2503+
('ps', 'ps'), # picosecond
2504+
('fs', 'fs'), # femtosecond
2505+
('as', 'as'), # attosecond
2506+
])
2507+
def test_array_from_unsupported_numpy_datetime_unit_names(unit, unit_name):
2508+
# Test that unsupported datetime units show meaningful names in error messages
2509+
s_data = [np.datetime64('2020-01-01', 's')]
2510+
unsupported_data = [np.datetime64('2020', unit)]
2511+
2512+
# Mix supported unit (s) with unsupported unit
2513+
data = s_data + unsupported_data
2514+
2515+
with pytest.raises(pa.ArrowInvalid,
2516+
match=f"Cannot mix NumPy datetime64 units s and {unit_name}"):
2517+
pa.array(data)
2518+
2519+
24962520
@pytest.mark.numpy
24972521
@pytest.mark.parametrize('unit', ['ns', 'us', 'ms', 's'])
24982522
def test_array_from_list_of_timestamps(unit):

0 commit comments

Comments
 (0)