Skip to content

Commit e48dc82

Browse files
committed
[Python] Test timestamp from int without pandas dependency
1 parent 744f0ec commit e48dc82

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

python/pyarrow/tests/test_convert_builtin.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,12 +1381,8 @@ def test_sequence_timestamp_nanoseconds():
13811381
23, 34, 123456)
13821382

13831383

1384-
@pytest.mark.pandas
13851384
@pytest.mark.timezone_data
13861385
def test_sequence_timestamp_from_int_with_unit():
1387-
# TODO(wesm): This test might be rewritten to assert the actual behavior
1388-
# when pandas is not installed
1389-
13901386
data = [1]
13911387

13921388
s = pa.timestamp('s')
@@ -1418,13 +1414,16 @@ def test_sequence_timestamp_from_int_with_unit():
14181414
)
14191415
assert str(arr_us[0]) == "1970-01-01 00:00:00.000001"
14201416

1417+
# Without pandas, nanosecond timestamps raise ValueError if value
1418+
# is not safely convertible to microseconds (value % 1000 != 0)
14211419
arr_ns = pa.array(data, type=ns)
14221420
assert len(arr_ns) == 1
14231421
assert arr_ns.type == ns
1424-
assert repr(arr_ns[0].as_py()) == (
1425-
"Timestamp('1970-01-01 00:00:00.000000001')"
1426-
)
1427-
assert str(arr_ns[0]) == "1970-01-01 00:00:00.000000001"
1422+
try:
1423+
import pandas
1424+
except ImportError:
1425+
with pytest.raises(ValueError, match="not safely convertible to microseconds"):
1426+
arr_ns[0].as_py()
14281427

14291428
expected_exc = TypeError
14301429

@@ -1436,6 +1435,28 @@ class CustomClass():
14361435
pa.array([1, CustomClass()], type=ty)
14371436

14381437

1438+
@pytest.mark.pandas
1439+
@pytest.mark.timezone_data
1440+
def test_sequence_timestamp_from_int_with_unit_nanosecond():
1441+
# With pandas installed, nanosecond timestamps return pd.Timestamp
1442+
# with full nanosecond precision (see scalar.pxi in _datetime_from_int)
1443+
import pandas as pd
1444+
1445+
data = [1]
1446+
ns = pa.timestamp('ns')
1447+
1448+
arr_ns = pa.array(data, type=ns)
1449+
assert len(arr_ns) == 1
1450+
assert arr_ns.type == ns
1451+
1452+
result = arr_ns[0].as_py()
1453+
assert isinstance(result, pd.Timestamp)
1454+
assert repr(result) == (
1455+
"Timestamp('1970-01-01 00:00:00.000000001')"
1456+
)
1457+
assert str(arr_ns[0]) == "1970-01-01 00:00:00.000000001"
1458+
1459+
14391460
def test_sequence_duration():
14401461
td1 = datetime.timedelta(2, 3601, 1)
14411462
td2 = datetime.timedelta(1, 100, 1000)

0 commit comments

Comments
 (0)