test_period_asfreq.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. from pandas._libs.tslibs.frequencies import get_freq
  4. from pandas._libs.tslibs.period import period_asfreq, period_ordinal
  5. @pytest.mark.parametrize("freq1,freq2,expected", [
  6. ("D", "H", 24),
  7. ("D", "T", 1440),
  8. ("D", "S", 86400),
  9. ("D", "L", 86400000),
  10. ("D", "U", 86400000000),
  11. ("D", "N", 86400000000000),
  12. ("H", "T", 60),
  13. ("H", "S", 3600),
  14. ("H", "L", 3600000),
  15. ("H", "U", 3600000000),
  16. ("H", "N", 3600000000000),
  17. ("T", "S", 60),
  18. ("T", "L", 60000),
  19. ("T", "U", 60000000),
  20. ("T", "N", 60000000000),
  21. ("S", "L", 1000),
  22. ("S", "U", 1000000),
  23. ("S", "N", 1000000000),
  24. ("L", "U", 1000),
  25. ("L", "N", 1000000),
  26. ("U", "N", 1000)
  27. ])
  28. def test_intra_day_conversion_factors(freq1, freq2, expected):
  29. assert period_asfreq(1, get_freq(freq1),
  30. get_freq(freq2), False) == expected
  31. @pytest.mark.parametrize("freq,expected", [
  32. ("A", 0),
  33. ("M", 0),
  34. ("W", 1),
  35. ("D", 0),
  36. ("B", 0)
  37. ])
  38. def test_period_ordinal_start_values(freq, expected):
  39. # information for Jan. 1, 1970.
  40. assert period_ordinal(1970, 1, 1, 0, 0, 0,
  41. 0, 0, get_freq(freq)) == expected
  42. @pytest.mark.parametrize("dt,expected", [
  43. ((1970, 1, 4, 0, 0, 0, 0, 0), 1),
  44. ((1970, 1, 5, 0, 0, 0, 0, 0), 2),
  45. ((2013, 10, 6, 0, 0, 0, 0, 0), 2284),
  46. ((2013, 10, 7, 0, 0, 0, 0, 0), 2285)
  47. ])
  48. def test_period_ordinal_week(dt, expected):
  49. args = dt + (get_freq("W"),)
  50. assert period_ordinal(*args) == expected
  51. @pytest.mark.parametrize("day,expected", [
  52. # Thursday (Oct. 3, 2013).
  53. (3, 11415),
  54. # Friday (Oct. 4, 2013).
  55. (4, 11416),
  56. # Saturday (Oct. 5, 2013).
  57. (5, 11417),
  58. # Sunday (Oct. 6, 2013).
  59. (6, 11417),
  60. # Monday (Oct. 7, 2013).
  61. (7, 11417),
  62. # Tuesday (Oct. 8, 2013).
  63. (8, 11418)
  64. ])
  65. def test_period_ordinal_business_day(day, expected):
  66. args = (2013, 10, day, 0, 0, 0, 0, 0, get_freq("B"))
  67. assert period_ordinal(*args) == expected