test_unary_ops.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime
  3. from dateutil.tz import gettz
  4. import pytest
  5. import pytz
  6. from pytz import utc
  7. from pandas._libs.tslibs import conversion
  8. from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG
  9. from pandas.compat import PY3
  10. import pandas.util._test_decorators as td
  11. from pandas import NaT, Timestamp
  12. import pandas.util.testing as tm
  13. from pandas.tseries.frequencies import to_offset
  14. class TestTimestampUnaryOps(object):
  15. # --------------------------------------------------------------
  16. # Timestamp.round
  17. @pytest.mark.parametrize('timestamp, freq, expected', [
  18. ('20130101 09:10:11', 'D', '20130101'),
  19. ('20130101 19:10:11', 'D', '20130102'),
  20. ('20130201 12:00:00', 'D', '20130202'),
  21. ('20130104 12:00:00', 'D', '20130105'),
  22. ('2000-01-05 05:09:15.13', 'D', '2000-01-05 00:00:00'),
  23. ('2000-01-05 05:09:15.13', 'H', '2000-01-05 05:00:00'),
  24. ('2000-01-05 05:09:15.13', 'S', '2000-01-05 05:09:15')
  25. ])
  26. def test_round_frequencies(self, timestamp, freq, expected):
  27. dt = Timestamp(timestamp)
  28. result = dt.round(freq)
  29. expected = Timestamp(expected)
  30. assert result == expected
  31. def test_round_tzaware(self):
  32. dt = Timestamp('20130101 09:10:11', tz='US/Eastern')
  33. result = dt.round('D')
  34. expected = Timestamp('20130101', tz='US/Eastern')
  35. assert result == expected
  36. dt = Timestamp('20130101 09:10:11', tz='US/Eastern')
  37. result = dt.round('s')
  38. assert result == dt
  39. def test_round_30min(self):
  40. # round
  41. dt = Timestamp('20130104 12:32:00')
  42. result = dt.round('30Min')
  43. expected = Timestamp('20130104 12:30:00')
  44. assert result == expected
  45. def test_round_subsecond(self):
  46. # GH#14440 & GH#15578
  47. result = Timestamp('2016-10-17 12:00:00.0015').round('ms')
  48. expected = Timestamp('2016-10-17 12:00:00.002000')
  49. assert result == expected
  50. result = Timestamp('2016-10-17 12:00:00.00149').round('ms')
  51. expected = Timestamp('2016-10-17 12:00:00.001000')
  52. assert result == expected
  53. ts = Timestamp('2016-10-17 12:00:00.0015')
  54. for freq in ['us', 'ns']:
  55. assert ts == ts.round(freq)
  56. result = Timestamp('2016-10-17 12:00:00.001501031').round('10ns')
  57. expected = Timestamp('2016-10-17 12:00:00.001501030')
  58. assert result == expected
  59. def test_round_nonstandard_freq(self):
  60. with tm.assert_produces_warning(False):
  61. Timestamp('2016-10-17 12:00:00.001501031').round('1010ns')
  62. def test_round_invalid_arg(self):
  63. stamp = Timestamp('2000-01-05 05:09:15.13')
  64. with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG):
  65. stamp.round('foo')
  66. @pytest.mark.parametrize('test_input, rounder, freq, expected', [
  67. ('2117-01-01 00:00:45', 'floor', '15s', '2117-01-01 00:00:45'),
  68. ('2117-01-01 00:00:45', 'ceil', '15s', '2117-01-01 00:00:45'),
  69. ('2117-01-01 00:00:45.000000012', 'floor', '10ns',
  70. '2117-01-01 00:00:45.000000010'),
  71. ('1823-01-01 00:00:01.000000012', 'ceil', '10ns',
  72. '1823-01-01 00:00:01.000000020'),
  73. ('1823-01-01 00:00:01', 'floor', '1s', '1823-01-01 00:00:01'),
  74. ('1823-01-01 00:00:01', 'ceil', '1s', '1823-01-01 00:00:01'),
  75. ('NaT', 'floor', '1s', 'NaT'),
  76. ('NaT', 'ceil', '1s', 'NaT')
  77. ])
  78. def test_ceil_floor_edge(self, test_input, rounder, freq, expected):
  79. dt = Timestamp(test_input)
  80. func = getattr(dt, rounder)
  81. result = func(freq)
  82. if dt is NaT:
  83. assert result is NaT
  84. else:
  85. expected = Timestamp(expected)
  86. assert result == expected
  87. @pytest.mark.parametrize('test_input, freq, expected', [
  88. ('2018-01-01 00:02:06', '2s', '2018-01-01 00:02:06'),
  89. ('2018-01-01 00:02:00', '2T', '2018-01-01 00:02:00'),
  90. ('2018-01-01 00:04:00', '4T', '2018-01-01 00:04:00'),
  91. ('2018-01-01 00:15:00', '15T', '2018-01-01 00:15:00'),
  92. ('2018-01-01 00:20:00', '20T', '2018-01-01 00:20:00'),
  93. ('2018-01-01 03:00:00', '3H', '2018-01-01 03:00:00'),
  94. ])
  95. @pytest.mark.parametrize('rounder', ['ceil', 'floor', 'round'])
  96. def test_round_minute_freq(self, test_input, freq, expected, rounder):
  97. # Ensure timestamps that shouldnt round dont!
  98. # GH#21262
  99. dt = Timestamp(test_input)
  100. expected = Timestamp(expected)
  101. func = getattr(dt, rounder)
  102. result = func(freq)
  103. assert result == expected
  104. def test_ceil(self):
  105. dt = Timestamp('20130101 09:10:11')
  106. result = dt.ceil('D')
  107. expected = Timestamp('20130102')
  108. assert result == expected
  109. def test_floor(self):
  110. dt = Timestamp('20130101 09:10:11')
  111. result = dt.floor('D')
  112. expected = Timestamp('20130101')
  113. assert result == expected
  114. @pytest.mark.parametrize('method', ['ceil', 'round', 'floor'])
  115. def test_round_dst_border_ambiguous(self, method):
  116. # GH 18946 round near "fall back" DST
  117. ts = Timestamp('2017-10-29 00:00:00', tz='UTC').tz_convert(
  118. 'Europe/Madrid'
  119. )
  120. #
  121. result = getattr(ts, method)('H', ambiguous=True)
  122. assert result == ts
  123. result = getattr(ts, method)('H', ambiguous=False)
  124. expected = Timestamp('2017-10-29 01:00:00', tz='UTC').tz_convert(
  125. 'Europe/Madrid'
  126. )
  127. assert result == expected
  128. result = getattr(ts, method)('H', ambiguous='NaT')
  129. assert result is NaT
  130. with pytest.raises(pytz.AmbiguousTimeError):
  131. getattr(ts, method)('H', ambiguous='raise')
  132. @pytest.mark.parametrize('method, ts_str, freq', [
  133. ['ceil', '2018-03-11 01:59:00-0600', '5min'],
  134. ['round', '2018-03-11 01:59:00-0600', '5min'],
  135. ['floor', '2018-03-11 03:01:00-0500', '2H']])
  136. def test_round_dst_border_nonexistent(self, method, ts_str, freq):
  137. # GH 23324 round near "spring forward" DST
  138. ts = Timestamp(ts_str, tz='America/Chicago')
  139. result = getattr(ts, method)(freq, nonexistent='shift_forward')
  140. expected = Timestamp('2018-03-11 03:00:00', tz='America/Chicago')
  141. assert result == expected
  142. result = getattr(ts, method)(freq, nonexistent='NaT')
  143. assert result is NaT
  144. with pytest.raises(pytz.NonExistentTimeError,
  145. match='2018-03-11 02:00:00'):
  146. getattr(ts, method)(freq, nonexistent='raise')
  147. @pytest.mark.parametrize('timestamp', [
  148. '2018-01-01 0:0:0.124999360',
  149. '2018-01-01 0:0:0.125000367',
  150. '2018-01-01 0:0:0.125500',
  151. '2018-01-01 0:0:0.126500',
  152. '2018-01-01 12:00:00',
  153. '2019-01-01 12:00:00',
  154. ])
  155. @pytest.mark.parametrize('freq', [
  156. '2ns', '3ns', '4ns', '5ns', '6ns', '7ns',
  157. '250ns', '500ns', '750ns',
  158. '1us', '19us', '250us', '500us', '750us',
  159. '1s', '2s', '3s',
  160. '1D',
  161. ])
  162. def test_round_int64(self, timestamp, freq):
  163. """check that all rounding modes are accurate to int64 precision
  164. see GH#22591
  165. """
  166. dt = Timestamp(timestamp)
  167. unit = to_offset(freq).nanos
  168. # test floor
  169. result = dt.floor(freq)
  170. assert result.value % unit == 0, "floor not a {} multiple".format(freq)
  171. assert 0 <= dt.value - result.value < unit, "floor error"
  172. # test ceil
  173. result = dt.ceil(freq)
  174. assert result.value % unit == 0, "ceil not a {} multiple".format(freq)
  175. assert 0 <= result.value - dt.value < unit, "ceil error"
  176. # test round
  177. result = dt.round(freq)
  178. assert result.value % unit == 0, "round not a {} multiple".format(freq)
  179. assert abs(result.value - dt.value) <= unit // 2, "round error"
  180. if unit % 2 == 0 and abs(result.value - dt.value) == unit // 2:
  181. # round half to even
  182. assert result.value // unit % 2 == 0, "round half to even error"
  183. # --------------------------------------------------------------
  184. # Timestamp.replace
  185. def test_replace_naive(self):
  186. # GH#14621, GH#7825
  187. ts = Timestamp('2016-01-01 09:00:00')
  188. result = ts.replace(hour=0)
  189. expected = Timestamp('2016-01-01 00:00:00')
  190. assert result == expected
  191. def test_replace_aware(self, tz_aware_fixture):
  192. tz = tz_aware_fixture
  193. # GH#14621, GH#7825
  194. # replacing datetime components with and w/o presence of a timezone
  195. ts = Timestamp('2016-01-01 09:00:00', tz=tz)
  196. result = ts.replace(hour=0)
  197. expected = Timestamp('2016-01-01 00:00:00', tz=tz)
  198. assert result == expected
  199. def test_replace_preserves_nanos(self, tz_aware_fixture):
  200. tz = tz_aware_fixture
  201. # GH#14621, GH#7825
  202. ts = Timestamp('2016-01-01 09:00:00.000000123', tz=tz)
  203. result = ts.replace(hour=0)
  204. expected = Timestamp('2016-01-01 00:00:00.000000123', tz=tz)
  205. assert result == expected
  206. def test_replace_multiple(self, tz_aware_fixture):
  207. tz = tz_aware_fixture
  208. # GH#14621, GH#7825
  209. # replacing datetime components with and w/o presence of a timezone
  210. # test all
  211. ts = Timestamp('2016-01-01 09:00:00.000000123', tz=tz)
  212. result = ts.replace(year=2015, month=2, day=2, hour=0, minute=5,
  213. second=5, microsecond=5, nanosecond=5)
  214. expected = Timestamp('2015-02-02 00:05:05.000005005', tz=tz)
  215. assert result == expected
  216. def test_replace_invalid_kwarg(self, tz_aware_fixture):
  217. tz = tz_aware_fixture
  218. # GH#14621, GH#7825
  219. ts = Timestamp('2016-01-01 09:00:00.000000123', tz=tz)
  220. with pytest.raises(TypeError):
  221. ts.replace(foo=5)
  222. def test_replace_integer_args(self, tz_aware_fixture):
  223. tz = tz_aware_fixture
  224. # GH#14621, GH#7825
  225. ts = Timestamp('2016-01-01 09:00:00.000000123', tz=tz)
  226. with pytest.raises(ValueError):
  227. ts.replace(hour=0.1)
  228. def test_replace_tzinfo_equiv_tz_localize_none(self):
  229. # GH#14621, GH#7825
  230. # assert conversion to naive is the same as replacing tzinfo with None
  231. ts = Timestamp('2013-11-03 01:59:59.999999-0400', tz='US/Eastern')
  232. assert ts.tz_localize(None) == ts.replace(tzinfo=None)
  233. @td.skip_if_windows
  234. def test_replace_tzinfo(self):
  235. # GH#15683
  236. dt = datetime(2016, 3, 27, 1)
  237. tzinfo = pytz.timezone('CET').localize(dt, is_dst=False).tzinfo
  238. result_dt = dt.replace(tzinfo=tzinfo)
  239. result_pd = Timestamp(dt).replace(tzinfo=tzinfo)
  240. if PY3:
  241. # datetime.timestamp() converts in the local timezone
  242. with tm.set_timezone('UTC'):
  243. assert result_dt.timestamp() == result_pd.timestamp()
  244. assert result_dt == result_pd
  245. assert result_dt == result_pd.to_pydatetime()
  246. result_dt = dt.replace(tzinfo=tzinfo).replace(tzinfo=None)
  247. result_pd = Timestamp(dt).replace(tzinfo=tzinfo).replace(tzinfo=None)
  248. if PY3:
  249. # datetime.timestamp() converts in the local timezone
  250. with tm.set_timezone('UTC'):
  251. assert result_dt.timestamp() == result_pd.timestamp()
  252. assert result_dt == result_pd
  253. assert result_dt == result_pd.to_pydatetime()
  254. @pytest.mark.parametrize('tz, normalize', [
  255. (pytz.timezone('US/Eastern'), lambda x: x.tzinfo.normalize(x)),
  256. (gettz('US/Eastern'), lambda x: x)])
  257. def test_replace_across_dst(self, tz, normalize):
  258. # GH#18319 check that 1) timezone is correctly normalized and
  259. # 2) that hour is not incorrectly changed by this normalization
  260. ts_naive = Timestamp('2017-12-03 16:03:30')
  261. ts_aware = conversion.localize_pydatetime(ts_naive, tz)
  262. # Preliminary sanity-check
  263. assert ts_aware == normalize(ts_aware)
  264. # Replace across DST boundary
  265. ts2 = ts_aware.replace(month=6)
  266. # Check that `replace` preserves hour literal
  267. assert (ts2.hour, ts2.minute) == (ts_aware.hour, ts_aware.minute)
  268. # Check that post-replace object is appropriately normalized
  269. ts2b = normalize(ts2)
  270. assert ts2 == ts2b
  271. def test_replace_dst_border(self):
  272. # Gh 7825
  273. t = Timestamp('2013-11-3', tz='America/Chicago')
  274. result = t.replace(hour=3)
  275. expected = Timestamp('2013-11-3 03:00:00', tz='America/Chicago')
  276. assert result == expected
  277. # --------------------------------------------------------------
  278. # Timestamp.normalize
  279. @pytest.mark.parametrize('arg', ['2013-11-30', '2013-11-30 12:00:00'])
  280. def test_normalize(self, tz_naive_fixture, arg):
  281. tz = tz_naive_fixture
  282. ts = Timestamp(arg, tz=tz)
  283. result = ts.normalize()
  284. expected = Timestamp('2013-11-30', tz=tz)
  285. assert result == expected
  286. # --------------------------------------------------------------
  287. @td.skip_if_windows
  288. def test_timestamp(self):
  289. # GH#17329
  290. # tz-naive --> treat it as if it were UTC for purposes of timestamp()
  291. ts = Timestamp.now()
  292. uts = ts.replace(tzinfo=utc)
  293. assert ts.timestamp() == uts.timestamp()
  294. tsc = Timestamp('2014-10-11 11:00:01.12345678', tz='US/Central')
  295. utsc = tsc.tz_convert('UTC')
  296. # utsc is a different representation of the same time
  297. assert tsc.timestamp() == utsc.timestamp()
  298. if PY3:
  299. # datetime.timestamp() converts in the local timezone
  300. with tm.set_timezone('UTC'):
  301. # should agree with datetime.timestamp method
  302. dt = ts.to_pydatetime()
  303. assert dt.timestamp() == ts.timestamp()