test_iter.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import dateutil.tz
  2. import numpy as np
  3. import pytest
  4. from pandas import (
  5. DatetimeIndex,
  6. date_range,
  7. to_datetime,
  8. )
  9. from pandas.core.arrays import datetimes
  10. class TestDatetimeIndexIteration:
  11. @pytest.mark.parametrize(
  12. "tz", [None, "UTC", "US/Central", dateutil.tz.tzoffset(None, -28800)]
  13. )
  14. def test_iteration_preserves_nanoseconds(self, tz):
  15. # GH#19603
  16. index = DatetimeIndex(
  17. ["2018-02-08 15:00:00.168456358", "2018-02-08 15:00:00.168456359"], tz=tz
  18. )
  19. for i, ts in enumerate(index):
  20. assert ts == index[i] # pylint: disable=unnecessary-list-index-lookup
  21. def test_iter_readonly(self):
  22. # GH#28055 ints_to_pydatetime with readonly array
  23. arr = np.array([np.datetime64("2012-02-15T12:00:00.000000000")])
  24. arr.setflags(write=False)
  25. dti = to_datetime(arr)
  26. list(dti)
  27. def test_iteration_preserves_tz(self):
  28. # see GH#8890
  29. index = date_range("2012-01-01", periods=3, freq="h", tz="US/Eastern")
  30. for i, ts in enumerate(index):
  31. result = ts
  32. expected = index[i] # pylint: disable=unnecessary-list-index-lookup
  33. assert result == expected
  34. def test_iteration_preserves_tz2(self):
  35. index = date_range(
  36. "2012-01-01", periods=3, freq="h", tz=dateutil.tz.tzoffset(None, -28800)
  37. )
  38. for i, ts in enumerate(index):
  39. result = ts
  40. expected = index[i] # pylint: disable=unnecessary-list-index-lookup
  41. assert result._repr_base == expected._repr_base
  42. assert result == expected
  43. def test_iteration_preserves_tz3(self):
  44. # GH#9100
  45. index = DatetimeIndex(
  46. ["2014-12-01 03:32:39.987000-08:00", "2014-12-01 04:12:34.987000-08:00"]
  47. )
  48. for i, ts in enumerate(index):
  49. result = ts
  50. expected = index[i] # pylint: disable=unnecessary-list-index-lookup
  51. assert result._repr_base == expected._repr_base
  52. assert result == expected
  53. @pytest.mark.parametrize("offset", [-5, -1, 0, 1])
  54. def test_iteration_over_chunksize(self, offset, monkeypatch):
  55. # GH#21012
  56. chunksize = 5
  57. index = date_range(
  58. "2000-01-01 00:00:00", periods=chunksize - offset, freq="min"
  59. )
  60. num = 0
  61. with monkeypatch.context() as m:
  62. m.setattr(datetimes, "_ITER_CHUNKSIZE", chunksize)
  63. for stamp in index:
  64. assert index[num] == stamp
  65. num += 1
  66. assert num == len(index)