test_timedeltas.py 780 B

1234567891011121314151617181920212223242526272829
  1. # -*- coding: utf-8 -*-
  2. import numpy as np
  3. import pytest
  4. from pandas._libs.tslibs.timedeltas import delta_to_nanoseconds
  5. import pandas as pd
  6. from pandas import Timedelta
  7. @pytest.mark.parametrize("obj,expected", [
  8. (np.timedelta64(14, "D"), 14 * 24 * 3600 * 1e9),
  9. (Timedelta(minutes=-7), -7 * 60 * 1e9),
  10. (Timedelta(minutes=-7).to_pytimedelta(), -7 * 60 * 1e9),
  11. (pd.offsets.Nano(125), 125),
  12. (1, 1),
  13. (np.int64(2), 2),
  14. (np.int32(3), 3)
  15. ])
  16. def test_delta_to_nanoseconds(obj, expected):
  17. result = delta_to_nanoseconds(obj)
  18. assert result == expected
  19. def test_delta_to_nanoseconds_error():
  20. obj = np.array([123456789], dtype="m8[ns]")
  21. with pytest.raises(TypeError, match="<(class|type) 'numpy.ndarray'>"):
  22. delta_to_nanoseconds(obj)