forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_snap.py
67 lines (57 loc) · 1.87 KB
/
test_snap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import pytest
from pandas import (
DatetimeIndex,
date_range,
)
import pandas._testing as tm
def astype_non_nano(dti_nano, unit):
# TODO(2.0): remove once DTI/DTA.astype supports non-nano
if unit == "ns":
return dti_nano
dta_nano = dti_nano._data
arr_nano = dta_nano._ndarray
arr = arr_nano.astype(f"M8[{unit}]")
if dti_nano.tz is None:
dtype = arr.dtype
else:
dtype = type(dti_nano.dtype)(tz=dti_nano.tz, unit=unit)
dta = type(dta_nano)._simple_new(arr, dtype=dtype)
dti = DatetimeIndex(dta, name=dti_nano.name)
assert dti.dtype == dtype
return dti
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
@pytest.mark.parametrize("tz", [None, "Asia/Shanghai", "Europe/Berlin"])
@pytest.mark.parametrize("name", [None, "my_dti"])
@pytest.mark.parametrize("unit", ["ns", "us", "ms", "s"])
def test_dti_snap(name, tz, unit):
dti = DatetimeIndex(
[
"1/1/2002",
"1/2/2002",
"1/3/2002",
"1/4/2002",
"1/5/2002",
"1/6/2002",
"1/7/2002",
],
name=name,
tz=tz,
freq="D",
)
dti = astype_non_nano(dti, unit)
result = dti.snap(freq="W-MON")
expected = date_range("12/31/2001", "1/7/2002", name=name, tz=tz, freq="w-mon")
expected = expected.repeat([3, 4])
expected = astype_non_nano(expected, unit)
tm.assert_index_equal(result, expected)
assert result.tz == expected.tz
assert result.freq is None
assert expected.freq is None
result = dti.snap(freq="B")
expected = date_range("1/1/2002", "1/7/2002", name=name, tz=tz, freq="b")
expected = expected.repeat([1, 1, 1, 2, 2])
expected = astype_non_nano(expected, unit)
tm.assert_index_equal(result, expected)
assert result.tz == expected.tz
assert result.freq is None
assert expected.freq is None