peacock-data-public-datasets-idc-cronscript
/
venv
/lib
/python3.10
/site-packages
/pandas
/tests
/tslibs
/test_period.py
import numpy as np | |
import pytest | |
from pandas._libs.tslibs import ( | |
iNaT, | |
to_offset, | |
) | |
from pandas._libs.tslibs.period import ( | |
extract_ordinals, | |
get_period_field_arr, | |
period_asfreq, | |
period_ordinal, | |
) | |
import pandas._testing as tm | |
def get_freq_code(freqstr: str) -> int: | |
off = to_offset(freqstr, is_period=True) | |
# error: "BaseOffset" has no attribute "_period_dtype_code" | |
code = off._period_dtype_code # type: ignore[attr-defined] | |
return code | |
def test_intra_day_conversion_factors(freq1, freq2, expected): | |
assert ( | |
period_asfreq(1, get_freq_code(freq1), get_freq_code(freq2), False) == expected | |
) | |
def test_period_ordinal_start_values(freq, expected): | |
# information for Jan. 1, 1970. | |
assert period_ordinal(1970, 1, 1, 0, 0, 0, 0, 0, get_freq_code(freq)) == expected | |
def test_period_ordinal_week(dt, expected): | |
args = dt + (get_freq_code("W"),) | |
assert period_ordinal(*args) == expected | |
def test_period_ordinal_business_day(day, expected): | |
# 5000 is PeriodDtypeCode for BusinessDay | |
args = (2013, 10, day, 0, 0, 0, 0, 0, 5000) | |
assert period_ordinal(*args) == expected | |
class TestExtractOrdinals: | |
def test_extract_ordinals_raises(self): | |
# with non-object, make sure we raise TypeError, not segfault | |
arr = np.arange(5) | |
freq = to_offset("D") | |
with pytest.raises(TypeError, match="values must be object-dtype"): | |
extract_ordinals(arr, freq) | |
def test_extract_ordinals_2d(self): | |
freq = to_offset("D") | |
arr = np.empty(10, dtype=object) | |
arr[:] = iNaT | |
res = extract_ordinals(arr, freq) | |
res2 = extract_ordinals(arr.reshape(5, 2), freq) | |
tm.assert_numpy_array_equal(res, res2.reshape(-1)) | |
def test_get_period_field_array_raises_on_out_of_range(): | |
msg = "Buffer dtype mismatch, expected 'const int64_t' but got 'double'" | |
with pytest.raises(ValueError, match=msg): | |
get_period_field_arr(-1, np.empty(1), 0) | |