demo + utils venv
This commit is contained in:
@@ -0,0 +1 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
@@ -0,0 +1,25 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Assertion helpers for offsets tests
|
||||
"""
|
||||
|
||||
|
||||
def assert_offset_equal(offset, base, expected):
|
||||
actual = offset + base
|
||||
actual_swapped = base + offset
|
||||
actual_apply = offset.apply(base)
|
||||
try:
|
||||
assert actual == expected
|
||||
assert actual_swapped == expected
|
||||
assert actual_apply == expected
|
||||
except AssertionError:
|
||||
raise AssertionError("\nExpected: %s\nActual: %s\nFor Offset: %s)"
|
||||
"\nAt Date: %s" %
|
||||
(expected, actual, offset, base))
|
||||
|
||||
|
||||
def assert_onOffset(offset, date, expected):
|
||||
actual = offset.onOffset(date)
|
||||
assert actual == expected, ("\nExpected: %s\nActual: %s\nFor Offset: %s)"
|
||||
"\nAt Date: %s" %
|
||||
(expected, actual, offset, date))
|
||||
@@ -0,0 +1,21 @@
|
||||
import pytest
|
||||
|
||||
import pandas.tseries.offsets as offsets
|
||||
|
||||
|
||||
@pytest.fixture(params=[getattr(offsets, o) for o in offsets.__all__])
|
||||
def offset_types(request):
|
||||
"""
|
||||
Fixture for all the datetime offsets available for a time series.
|
||||
"""
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(params=[getattr(offsets, o) for o in offsets.__all__ if
|
||||
issubclass(getattr(offsets, o), offsets.MonthOffset)
|
||||
and o != 'MonthOffset'])
|
||||
def month_classes(request):
|
||||
"""
|
||||
Fixture for month based datetime offsets available for a time series.
|
||||
"""
|
||||
return request.param
|
||||
@@ -0,0 +1,657 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Tests for Fiscal Year and Fiscal Quarter offset classes
|
||||
"""
|
||||
from datetime import datetime
|
||||
|
||||
from dateutil.relativedelta import relativedelta
|
||||
import pytest
|
||||
|
||||
from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG
|
||||
|
||||
from pandas import Timestamp
|
||||
|
||||
from pandas.tseries.frequencies import get_offset
|
||||
from pandas.tseries.offsets import FY5253, FY5253Quarter
|
||||
|
||||
from .common import assert_offset_equal, assert_onOffset
|
||||
from .test_offsets import Base, WeekDay
|
||||
|
||||
|
||||
def makeFY5253LastOfMonthQuarter(*args, **kwds):
|
||||
return FY5253Quarter(*args, variation="last", **kwds)
|
||||
|
||||
|
||||
def makeFY5253NearestEndMonthQuarter(*args, **kwds):
|
||||
return FY5253Quarter(*args, variation="nearest", **kwds)
|
||||
|
||||
|
||||
def makeFY5253NearestEndMonth(*args, **kwds):
|
||||
return FY5253(*args, variation="nearest", **kwds)
|
||||
|
||||
|
||||
def makeFY5253LastOfMonth(*args, **kwds):
|
||||
return FY5253(*args, variation="last", **kwds)
|
||||
|
||||
|
||||
def test_get_offset_name():
|
||||
assert (makeFY5253LastOfMonthQuarter(
|
||||
weekday=1, startingMonth=3,
|
||||
qtr_with_extra_week=4).freqstr == "REQ-L-MAR-TUE-4")
|
||||
assert (makeFY5253NearestEndMonthQuarter(
|
||||
weekday=1, startingMonth=3,
|
||||
qtr_with_extra_week=3).freqstr == "REQ-N-MAR-TUE-3")
|
||||
|
||||
|
||||
def test_get_offset():
|
||||
with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG):
|
||||
get_offset('gibberish')
|
||||
with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG):
|
||||
get_offset('QS-JAN-B')
|
||||
|
||||
pairs = [
|
||||
("RE-N-DEC-MON",
|
||||
makeFY5253NearestEndMonth(weekday=0, startingMonth=12)),
|
||||
("RE-L-DEC-TUE",
|
||||
makeFY5253LastOfMonth(weekday=1, startingMonth=12)),
|
||||
("REQ-L-MAR-TUE-4",
|
||||
makeFY5253LastOfMonthQuarter(weekday=1,
|
||||
startingMonth=3,
|
||||
qtr_with_extra_week=4)),
|
||||
("REQ-L-DEC-MON-3",
|
||||
makeFY5253LastOfMonthQuarter(weekday=0,
|
||||
startingMonth=12,
|
||||
qtr_with_extra_week=3)),
|
||||
("REQ-N-DEC-MON-3",
|
||||
makeFY5253NearestEndMonthQuarter(weekday=0,
|
||||
startingMonth=12,
|
||||
qtr_with_extra_week=3))]
|
||||
|
||||
for name, expected in pairs:
|
||||
offset = get_offset(name)
|
||||
assert offset == expected, ("Expected %r to yield %r (actual: %r)" %
|
||||
(name, expected, offset))
|
||||
|
||||
|
||||
class TestFY5253LastOfMonth(Base):
|
||||
offset_lom_sat_aug = makeFY5253LastOfMonth(1, startingMonth=8,
|
||||
weekday=WeekDay.SAT)
|
||||
offset_lom_sat_sep = makeFY5253LastOfMonth(1, startingMonth=9,
|
||||
weekday=WeekDay.SAT)
|
||||
|
||||
on_offset_cases = [
|
||||
# From Wikipedia (see:
|
||||
# http://en.wikipedia.org/wiki/4%E2%80%934%E2%80%935_calendar#Last_Saturday_of_the_month_at_fiscal_year_end)
|
||||
(offset_lom_sat_aug, datetime(2006, 8, 26), True),
|
||||
(offset_lom_sat_aug, datetime(2007, 8, 25), True),
|
||||
(offset_lom_sat_aug, datetime(2008, 8, 30), True),
|
||||
(offset_lom_sat_aug, datetime(2009, 8, 29), True),
|
||||
(offset_lom_sat_aug, datetime(2010, 8, 28), True),
|
||||
(offset_lom_sat_aug, datetime(2011, 8, 27), True),
|
||||
(offset_lom_sat_aug, datetime(2012, 8, 25), True),
|
||||
(offset_lom_sat_aug, datetime(2013, 8, 31), True),
|
||||
(offset_lom_sat_aug, datetime(2014, 8, 30), True),
|
||||
(offset_lom_sat_aug, datetime(2015, 8, 29), True),
|
||||
(offset_lom_sat_aug, datetime(2016, 8, 27), True),
|
||||
(offset_lom_sat_aug, datetime(2017, 8, 26), True),
|
||||
(offset_lom_sat_aug, datetime(2018, 8, 25), True),
|
||||
(offset_lom_sat_aug, datetime(2019, 8, 31), True),
|
||||
|
||||
(offset_lom_sat_aug, datetime(2006, 8, 27), False),
|
||||
(offset_lom_sat_aug, datetime(2007, 8, 28), False),
|
||||
(offset_lom_sat_aug, datetime(2008, 8, 31), False),
|
||||
(offset_lom_sat_aug, datetime(2009, 8, 30), False),
|
||||
(offset_lom_sat_aug, datetime(2010, 8, 29), False),
|
||||
(offset_lom_sat_aug, datetime(2011, 8, 28), False),
|
||||
|
||||
(offset_lom_sat_aug, datetime(2006, 8, 25), False),
|
||||
(offset_lom_sat_aug, datetime(2007, 8, 24), False),
|
||||
(offset_lom_sat_aug, datetime(2008, 8, 29), False),
|
||||
(offset_lom_sat_aug, datetime(2009, 8, 28), False),
|
||||
(offset_lom_sat_aug, datetime(2010, 8, 27), False),
|
||||
(offset_lom_sat_aug, datetime(2011, 8, 26), False),
|
||||
(offset_lom_sat_aug, datetime(2019, 8, 30), False),
|
||||
|
||||
# From GMCR (see for example:
|
||||
# http://yahoo.brand.edgar-online.com/Default.aspx?
|
||||
# companyid=3184&formtypeID=7)
|
||||
(offset_lom_sat_sep, datetime(2010, 9, 25), True),
|
||||
(offset_lom_sat_sep, datetime(2011, 9, 24), True),
|
||||
(offset_lom_sat_sep, datetime(2012, 9, 29), True)]
|
||||
|
||||
@pytest.mark.parametrize('case', on_offset_cases)
|
||||
def test_onOffset(self, case):
|
||||
offset, dt, expected = case
|
||||
assert_onOffset(offset, dt, expected)
|
||||
|
||||
def test_apply(self):
|
||||
offset_lom_aug_sat = makeFY5253LastOfMonth(startingMonth=8,
|
||||
weekday=WeekDay.SAT)
|
||||
offset_lom_aug_sat_1 = makeFY5253LastOfMonth(n=1, startingMonth=8,
|
||||
weekday=WeekDay.SAT)
|
||||
|
||||
date_seq_lom_aug_sat = [datetime(2006, 8, 26), datetime(2007, 8, 25),
|
||||
datetime(2008, 8, 30), datetime(2009, 8, 29),
|
||||
datetime(2010, 8, 28), datetime(2011, 8, 27),
|
||||
datetime(2012, 8, 25), datetime(2013, 8, 31),
|
||||
datetime(2014, 8, 30), datetime(2015, 8, 29),
|
||||
datetime(2016, 8, 27)]
|
||||
|
||||
tests = [
|
||||
(offset_lom_aug_sat, date_seq_lom_aug_sat),
|
||||
(offset_lom_aug_sat_1, date_seq_lom_aug_sat),
|
||||
(offset_lom_aug_sat, [
|
||||
datetime(2006, 8, 25)] + date_seq_lom_aug_sat),
|
||||
(offset_lom_aug_sat_1, [
|
||||
datetime(2006, 8, 27)] + date_seq_lom_aug_sat[1:]),
|
||||
(makeFY5253LastOfMonth(n=-1, startingMonth=8,
|
||||
weekday=WeekDay.SAT),
|
||||
list(reversed(date_seq_lom_aug_sat))),
|
||||
]
|
||||
for test in tests:
|
||||
offset, data = test
|
||||
current = data[0]
|
||||
for datum in data[1:]:
|
||||
current = current + offset
|
||||
assert current == datum
|
||||
|
||||
|
||||
class TestFY5253NearestEndMonth(Base):
|
||||
|
||||
def test_get_year_end(self):
|
||||
assert (makeFY5253NearestEndMonth(
|
||||
startingMonth=8, weekday=WeekDay.SAT).get_year_end(
|
||||
datetime(2013, 1, 1)) == datetime(2013, 8, 31))
|
||||
assert (makeFY5253NearestEndMonth(
|
||||
startingMonth=8, weekday=WeekDay.SUN).get_year_end(
|
||||
datetime(2013, 1, 1)) == datetime(2013, 9, 1))
|
||||
assert (makeFY5253NearestEndMonth(
|
||||
startingMonth=8, weekday=WeekDay.FRI).get_year_end(
|
||||
datetime(2013, 1, 1)) == datetime(2013, 8, 30))
|
||||
|
||||
offset_n = FY5253(weekday=WeekDay.TUE, startingMonth=12,
|
||||
variation="nearest")
|
||||
assert (offset_n.get_year_end(datetime(2012, 1, 1)) ==
|
||||
datetime(2013, 1, 1))
|
||||
assert (offset_n.get_year_end(datetime(2012, 1, 10)) ==
|
||||
datetime(2013, 1, 1))
|
||||
|
||||
assert (offset_n.get_year_end(datetime(2013, 1, 1)) ==
|
||||
datetime(2013, 12, 31))
|
||||
assert (offset_n.get_year_end(datetime(2013, 1, 2)) ==
|
||||
datetime(2013, 12, 31))
|
||||
assert (offset_n.get_year_end(datetime(2013, 1, 3)) ==
|
||||
datetime(2013, 12, 31))
|
||||
assert (offset_n.get_year_end(datetime(2013, 1, 10)) ==
|
||||
datetime(2013, 12, 31))
|
||||
|
||||
JNJ = FY5253(n=1, startingMonth=12, weekday=6, variation="nearest")
|
||||
assert (JNJ.get_year_end(datetime(2006, 1, 1)) ==
|
||||
datetime(2006, 12, 31))
|
||||
|
||||
offset_lom_aug_sat = makeFY5253NearestEndMonth(1, startingMonth=8,
|
||||
weekday=WeekDay.SAT)
|
||||
offset_lom_aug_thu = makeFY5253NearestEndMonth(1, startingMonth=8,
|
||||
weekday=WeekDay.THU)
|
||||
offset_n = FY5253(weekday=WeekDay.TUE, startingMonth=12,
|
||||
variation="nearest")
|
||||
|
||||
on_offset_cases = [
|
||||
# From Wikipedia (see:
|
||||
# http://en.wikipedia.org/wiki/4%E2%80%934%E2%80%935_calendar
|
||||
# #Saturday_nearest_the_end_of_month)
|
||||
# 2006-09-02 2006 September 2
|
||||
# 2007-09-01 2007 September 1
|
||||
# 2008-08-30 2008 August 30 (leap year)
|
||||
# 2009-08-29 2009 August 29
|
||||
# 2010-08-28 2010 August 28
|
||||
# 2011-09-03 2011 September 3
|
||||
# 2012-09-01 2012 September 1 (leap year)
|
||||
# 2013-08-31 2013 August 31
|
||||
# 2014-08-30 2014 August 30
|
||||
# 2015-08-29 2015 August 29
|
||||
# 2016-09-03 2016 September 3 (leap year)
|
||||
# 2017-09-02 2017 September 2
|
||||
# 2018-09-01 2018 September 1
|
||||
# 2019-08-31 2019 August 31
|
||||
(offset_lom_aug_sat, datetime(2006, 9, 2), True),
|
||||
(offset_lom_aug_sat, datetime(2007, 9, 1), True),
|
||||
(offset_lom_aug_sat, datetime(2008, 8, 30), True),
|
||||
(offset_lom_aug_sat, datetime(2009, 8, 29), True),
|
||||
(offset_lom_aug_sat, datetime(2010, 8, 28), True),
|
||||
(offset_lom_aug_sat, datetime(2011, 9, 3), True),
|
||||
|
||||
(offset_lom_aug_sat, datetime(2016, 9, 3), True),
|
||||
(offset_lom_aug_sat, datetime(2017, 9, 2), True),
|
||||
(offset_lom_aug_sat, datetime(2018, 9, 1), True),
|
||||
(offset_lom_aug_sat, datetime(2019, 8, 31), True),
|
||||
|
||||
(offset_lom_aug_sat, datetime(2006, 8, 27), False),
|
||||
(offset_lom_aug_sat, datetime(2007, 8, 28), False),
|
||||
(offset_lom_aug_sat, datetime(2008, 8, 31), False),
|
||||
(offset_lom_aug_sat, datetime(2009, 8, 30), False),
|
||||
(offset_lom_aug_sat, datetime(2010, 8, 29), False),
|
||||
(offset_lom_aug_sat, datetime(2011, 8, 28), False),
|
||||
|
||||
(offset_lom_aug_sat, datetime(2006, 8, 25), False),
|
||||
(offset_lom_aug_sat, datetime(2007, 8, 24), False),
|
||||
(offset_lom_aug_sat, datetime(2008, 8, 29), False),
|
||||
(offset_lom_aug_sat, datetime(2009, 8, 28), False),
|
||||
(offset_lom_aug_sat, datetime(2010, 8, 27), False),
|
||||
(offset_lom_aug_sat, datetime(2011, 8, 26), False),
|
||||
(offset_lom_aug_sat, datetime(2019, 8, 30), False),
|
||||
|
||||
# From Micron, see:
|
||||
# http://google.brand.edgar-online.com/?sym=MU&formtypeID=7
|
||||
(offset_lom_aug_thu, datetime(2012, 8, 30), True),
|
||||
(offset_lom_aug_thu, datetime(2011, 9, 1), True),
|
||||
|
||||
(offset_n, datetime(2012, 12, 31), False),
|
||||
(offset_n, datetime(2013, 1, 1), True),
|
||||
(offset_n, datetime(2013, 1, 2), False)]
|
||||
|
||||
@pytest.mark.parametrize('case', on_offset_cases)
|
||||
def test_onOffset(self, case):
|
||||
offset, dt, expected = case
|
||||
assert_onOffset(offset, dt, expected)
|
||||
|
||||
def test_apply(self):
|
||||
date_seq_nem_8_sat = [datetime(2006, 9, 2), datetime(2007, 9, 1),
|
||||
datetime(2008, 8, 30), datetime(2009, 8, 29),
|
||||
datetime(2010, 8, 28), datetime(2011, 9, 3)]
|
||||
|
||||
JNJ = [datetime(2005, 1, 2), datetime(2006, 1, 1),
|
||||
datetime(2006, 12, 31), datetime(2007, 12, 30),
|
||||
datetime(2008, 12, 28), datetime(2010, 1, 3),
|
||||
datetime(2011, 1, 2), datetime(2012, 1, 1),
|
||||
datetime(2012, 12, 30)]
|
||||
|
||||
DEC_SAT = FY5253(n=-1, startingMonth=12, weekday=5,
|
||||
variation="nearest")
|
||||
|
||||
tests = [
|
||||
(makeFY5253NearestEndMonth(startingMonth=8,
|
||||
weekday=WeekDay.SAT),
|
||||
date_seq_nem_8_sat),
|
||||
(makeFY5253NearestEndMonth(n=1, startingMonth=8,
|
||||
weekday=WeekDay.SAT),
|
||||
date_seq_nem_8_sat),
|
||||
(makeFY5253NearestEndMonth(startingMonth=8, weekday=WeekDay.SAT),
|
||||
[datetime(2006, 9, 1)] + date_seq_nem_8_sat),
|
||||
(makeFY5253NearestEndMonth(n=1, startingMonth=8,
|
||||
weekday=WeekDay.SAT),
|
||||
[datetime(2006, 9, 3)] + date_seq_nem_8_sat[1:]),
|
||||
(makeFY5253NearestEndMonth(n=-1, startingMonth=8,
|
||||
weekday=WeekDay.SAT),
|
||||
list(reversed(date_seq_nem_8_sat))),
|
||||
(makeFY5253NearestEndMonth(n=1, startingMonth=12,
|
||||
weekday=WeekDay.SUN), JNJ),
|
||||
(makeFY5253NearestEndMonth(n=-1, startingMonth=12,
|
||||
weekday=WeekDay.SUN),
|
||||
list(reversed(JNJ))),
|
||||
(makeFY5253NearestEndMonth(n=1, startingMonth=12,
|
||||
weekday=WeekDay.SUN),
|
||||
[datetime(2005, 1, 2), datetime(2006, 1, 1)]),
|
||||
(makeFY5253NearestEndMonth(n=1, startingMonth=12,
|
||||
weekday=WeekDay.SUN),
|
||||
[datetime(2006, 1, 2), datetime(2006, 12, 31)]),
|
||||
(DEC_SAT, [datetime(2013, 1, 15), datetime(2012, 12, 29)])
|
||||
]
|
||||
for test in tests:
|
||||
offset, data = test
|
||||
current = data[0]
|
||||
for datum in data[1:]:
|
||||
current = current + offset
|
||||
assert current == datum
|
||||
|
||||
|
||||
class TestFY5253LastOfMonthQuarter(Base):
|
||||
|
||||
def test_isAnchored(self):
|
||||
assert makeFY5253LastOfMonthQuarter(
|
||||
startingMonth=1, weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=4).isAnchored()
|
||||
assert makeFY5253LastOfMonthQuarter(
|
||||
weekday=WeekDay.SAT, startingMonth=3,
|
||||
qtr_with_extra_week=4).isAnchored()
|
||||
assert not makeFY5253LastOfMonthQuarter(
|
||||
2, startingMonth=1, weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=4).isAnchored()
|
||||
|
||||
def test_equality(self):
|
||||
assert (makeFY5253LastOfMonthQuarter(
|
||||
startingMonth=1, weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=4) == makeFY5253LastOfMonthQuarter(
|
||||
startingMonth=1, weekday=WeekDay.SAT, qtr_with_extra_week=4))
|
||||
assert (makeFY5253LastOfMonthQuarter(
|
||||
startingMonth=1, weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=4) != makeFY5253LastOfMonthQuarter(
|
||||
startingMonth=1, weekday=WeekDay.SUN, qtr_with_extra_week=4))
|
||||
assert (makeFY5253LastOfMonthQuarter(
|
||||
startingMonth=1, weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=4) != makeFY5253LastOfMonthQuarter(
|
||||
startingMonth=2, weekday=WeekDay.SAT, qtr_with_extra_week=4))
|
||||
|
||||
def test_offset(self):
|
||||
offset = makeFY5253LastOfMonthQuarter(1, startingMonth=9,
|
||||
weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=4)
|
||||
offset2 = makeFY5253LastOfMonthQuarter(2, startingMonth=9,
|
||||
weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=4)
|
||||
offset4 = makeFY5253LastOfMonthQuarter(4, startingMonth=9,
|
||||
weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=4)
|
||||
|
||||
offset_neg1 = makeFY5253LastOfMonthQuarter(-1, startingMonth=9,
|
||||
weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=4)
|
||||
offset_neg2 = makeFY5253LastOfMonthQuarter(-2, startingMonth=9,
|
||||
weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=4)
|
||||
|
||||
GMCR = [datetime(2010, 3, 27), datetime(2010, 6, 26),
|
||||
datetime(2010, 9, 25), datetime(2010, 12, 25),
|
||||
datetime(2011, 3, 26), datetime(2011, 6, 25),
|
||||
datetime(2011, 9, 24), datetime(2011, 12, 24),
|
||||
datetime(2012, 3, 24), datetime(2012, 6, 23),
|
||||
datetime(2012, 9, 29), datetime(2012, 12, 29),
|
||||
datetime(2013, 3, 30), datetime(2013, 6, 29)]
|
||||
|
||||
assert_offset_equal(offset, base=GMCR[0], expected=GMCR[1])
|
||||
assert_offset_equal(offset, base=GMCR[0] + relativedelta(days=-1),
|
||||
expected=GMCR[0])
|
||||
assert_offset_equal(offset, base=GMCR[1], expected=GMCR[2])
|
||||
|
||||
assert_offset_equal(offset2, base=GMCR[0], expected=GMCR[2])
|
||||
assert_offset_equal(offset4, base=GMCR[0], expected=GMCR[4])
|
||||
|
||||
assert_offset_equal(offset_neg1, base=GMCR[-1], expected=GMCR[-2])
|
||||
assert_offset_equal(offset_neg1,
|
||||
base=GMCR[-1] + relativedelta(days=+1),
|
||||
expected=GMCR[-1])
|
||||
assert_offset_equal(offset_neg2, base=GMCR[-1], expected=GMCR[-3])
|
||||
|
||||
date = GMCR[0] + relativedelta(days=-1)
|
||||
for expected in GMCR:
|
||||
assert_offset_equal(offset, date, expected)
|
||||
date = date + offset
|
||||
|
||||
date = GMCR[-1] + relativedelta(days=+1)
|
||||
for expected in reversed(GMCR):
|
||||
assert_offset_equal(offset_neg1, date, expected)
|
||||
date = date + offset_neg1
|
||||
|
||||
lomq_aug_sat_4 = makeFY5253LastOfMonthQuarter(1, startingMonth=8,
|
||||
weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=4)
|
||||
lomq_sep_sat_4 = makeFY5253LastOfMonthQuarter(1, startingMonth=9,
|
||||
weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=4)
|
||||
|
||||
on_offset_cases = [
|
||||
# From Wikipedia
|
||||
(lomq_aug_sat_4, datetime(2006, 8, 26), True),
|
||||
(lomq_aug_sat_4, datetime(2007, 8, 25), True),
|
||||
(lomq_aug_sat_4, datetime(2008, 8, 30), True),
|
||||
(lomq_aug_sat_4, datetime(2009, 8, 29), True),
|
||||
(lomq_aug_sat_4, datetime(2010, 8, 28), True),
|
||||
(lomq_aug_sat_4, datetime(2011, 8, 27), True),
|
||||
(lomq_aug_sat_4, datetime(2019, 8, 31), True),
|
||||
|
||||
(lomq_aug_sat_4, datetime(2006, 8, 27), False),
|
||||
(lomq_aug_sat_4, datetime(2007, 8, 28), False),
|
||||
(lomq_aug_sat_4, datetime(2008, 8, 31), False),
|
||||
(lomq_aug_sat_4, datetime(2009, 8, 30), False),
|
||||
(lomq_aug_sat_4, datetime(2010, 8, 29), False),
|
||||
(lomq_aug_sat_4, datetime(2011, 8, 28), False),
|
||||
|
||||
(lomq_aug_sat_4, datetime(2006, 8, 25), False),
|
||||
(lomq_aug_sat_4, datetime(2007, 8, 24), False),
|
||||
(lomq_aug_sat_4, datetime(2008, 8, 29), False),
|
||||
(lomq_aug_sat_4, datetime(2009, 8, 28), False),
|
||||
(lomq_aug_sat_4, datetime(2010, 8, 27), False),
|
||||
(lomq_aug_sat_4, datetime(2011, 8, 26), False),
|
||||
(lomq_aug_sat_4, datetime(2019, 8, 30), False),
|
||||
|
||||
# From GMCR
|
||||
(lomq_sep_sat_4, datetime(2010, 9, 25), True),
|
||||
(lomq_sep_sat_4, datetime(2011, 9, 24), True),
|
||||
(lomq_sep_sat_4, datetime(2012, 9, 29), True),
|
||||
|
||||
(lomq_sep_sat_4, datetime(2013, 6, 29), True),
|
||||
(lomq_sep_sat_4, datetime(2012, 6, 23), True),
|
||||
(lomq_sep_sat_4, datetime(2012, 6, 30), False),
|
||||
|
||||
(lomq_sep_sat_4, datetime(2013, 3, 30), True),
|
||||
(lomq_sep_sat_4, datetime(2012, 3, 24), True),
|
||||
|
||||
(lomq_sep_sat_4, datetime(2012, 12, 29), True),
|
||||
(lomq_sep_sat_4, datetime(2011, 12, 24), True),
|
||||
|
||||
# INTC (extra week in Q1)
|
||||
# See: http://www.intc.com/releasedetail.cfm?ReleaseID=542844
|
||||
(makeFY5253LastOfMonthQuarter(1, startingMonth=12,
|
||||
weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=1),
|
||||
datetime(2011, 4, 2), True),
|
||||
|
||||
# see: http://google.brand.edgar-online.com/?sym=INTC&formtypeID=7
|
||||
(makeFY5253LastOfMonthQuarter(1, startingMonth=12,
|
||||
weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=1),
|
||||
datetime(2012, 12, 29), True),
|
||||
(makeFY5253LastOfMonthQuarter(1, startingMonth=12,
|
||||
weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=1),
|
||||
datetime(2011, 12, 31), True),
|
||||
(makeFY5253LastOfMonthQuarter(1, startingMonth=12,
|
||||
weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=1),
|
||||
datetime(2010, 12, 25), True)]
|
||||
|
||||
@pytest.mark.parametrize('case', on_offset_cases)
|
||||
def test_onOffset(self, case):
|
||||
offset, dt, expected = case
|
||||
assert_onOffset(offset, dt, expected)
|
||||
|
||||
def test_year_has_extra_week(self):
|
||||
# End of long Q1
|
||||
assert makeFY5253LastOfMonthQuarter(
|
||||
1, startingMonth=12, weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=1).year_has_extra_week(datetime(2011, 4, 2))
|
||||
|
||||
# Start of long Q1
|
||||
assert makeFY5253LastOfMonthQuarter(
|
||||
1, startingMonth=12, weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=1).year_has_extra_week(datetime(2010, 12, 26))
|
||||
|
||||
# End of year before year with long Q1
|
||||
assert not makeFY5253LastOfMonthQuarter(
|
||||
1, startingMonth=12, weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=1).year_has_extra_week(datetime(2010, 12, 25))
|
||||
|
||||
for year in [x
|
||||
for x in range(1994, 2011 + 1)
|
||||
if x not in [2011, 2005, 2000, 1994]]:
|
||||
assert not makeFY5253LastOfMonthQuarter(
|
||||
1, startingMonth=12, weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=1).year_has_extra_week(
|
||||
datetime(year, 4, 2))
|
||||
|
||||
# Other long years
|
||||
assert makeFY5253LastOfMonthQuarter(
|
||||
1, startingMonth=12, weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=1).year_has_extra_week(datetime(2005, 4, 2))
|
||||
|
||||
assert makeFY5253LastOfMonthQuarter(
|
||||
1, startingMonth=12, weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=1).year_has_extra_week(datetime(2000, 4, 2))
|
||||
|
||||
assert makeFY5253LastOfMonthQuarter(
|
||||
1, startingMonth=12, weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=1).year_has_extra_week(datetime(1994, 4, 2))
|
||||
|
||||
def test_get_weeks(self):
|
||||
sat_dec_1 = makeFY5253LastOfMonthQuarter(1, startingMonth=12,
|
||||
weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=1)
|
||||
sat_dec_4 = makeFY5253LastOfMonthQuarter(1, startingMonth=12,
|
||||
weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=4)
|
||||
|
||||
assert sat_dec_1.get_weeks(datetime(2011, 4, 2)) == [14, 13, 13, 13]
|
||||
assert sat_dec_4.get_weeks(datetime(2011, 4, 2)) == [13, 13, 13, 14]
|
||||
assert sat_dec_1.get_weeks(datetime(2010, 12, 25)) == [13, 13, 13, 13]
|
||||
|
||||
|
||||
class TestFY5253NearestEndMonthQuarter(Base):
|
||||
|
||||
offset_nem_sat_aug_4 = makeFY5253NearestEndMonthQuarter(
|
||||
1, startingMonth=8, weekday=WeekDay.SAT,
|
||||
qtr_with_extra_week=4)
|
||||
offset_nem_thu_aug_4 = makeFY5253NearestEndMonthQuarter(
|
||||
1, startingMonth=8, weekday=WeekDay.THU,
|
||||
qtr_with_extra_week=4)
|
||||
offset_n = FY5253(weekday=WeekDay.TUE, startingMonth=12,
|
||||
variation="nearest")
|
||||
|
||||
on_offset_cases = [
|
||||
# From Wikipedia
|
||||
(offset_nem_sat_aug_4, datetime(2006, 9, 2), True),
|
||||
(offset_nem_sat_aug_4, datetime(2007, 9, 1), True),
|
||||
(offset_nem_sat_aug_4, datetime(2008, 8, 30), True),
|
||||
(offset_nem_sat_aug_4, datetime(2009, 8, 29), True),
|
||||
(offset_nem_sat_aug_4, datetime(2010, 8, 28), True),
|
||||
(offset_nem_sat_aug_4, datetime(2011, 9, 3), True),
|
||||
|
||||
(offset_nem_sat_aug_4, datetime(2016, 9, 3), True),
|
||||
(offset_nem_sat_aug_4, datetime(2017, 9, 2), True),
|
||||
(offset_nem_sat_aug_4, datetime(2018, 9, 1), True),
|
||||
(offset_nem_sat_aug_4, datetime(2019, 8, 31), True),
|
||||
|
||||
(offset_nem_sat_aug_4, datetime(2006, 8, 27), False),
|
||||
(offset_nem_sat_aug_4, datetime(2007, 8, 28), False),
|
||||
(offset_nem_sat_aug_4, datetime(2008, 8, 31), False),
|
||||
(offset_nem_sat_aug_4, datetime(2009, 8, 30), False),
|
||||
(offset_nem_sat_aug_4, datetime(2010, 8, 29), False),
|
||||
(offset_nem_sat_aug_4, datetime(2011, 8, 28), False),
|
||||
|
||||
(offset_nem_sat_aug_4, datetime(2006, 8, 25), False),
|
||||
(offset_nem_sat_aug_4, datetime(2007, 8, 24), False),
|
||||
(offset_nem_sat_aug_4, datetime(2008, 8, 29), False),
|
||||
(offset_nem_sat_aug_4, datetime(2009, 8, 28), False),
|
||||
(offset_nem_sat_aug_4, datetime(2010, 8, 27), False),
|
||||
(offset_nem_sat_aug_4, datetime(2011, 8, 26), False),
|
||||
(offset_nem_sat_aug_4, datetime(2019, 8, 30), False),
|
||||
|
||||
# From Micron, see:
|
||||
# http://google.brand.edgar-online.com/?sym=MU&formtypeID=7
|
||||
(offset_nem_thu_aug_4, datetime(2012, 8, 30), True),
|
||||
(offset_nem_thu_aug_4, datetime(2011, 9, 1), True),
|
||||
|
||||
# See: http://google.brand.edgar-online.com/?sym=MU&formtypeID=13
|
||||
(offset_nem_thu_aug_4, datetime(2013, 5, 30), True),
|
||||
(offset_nem_thu_aug_4, datetime(2013, 2, 28), True),
|
||||
(offset_nem_thu_aug_4, datetime(2012, 11, 29), True),
|
||||
(offset_nem_thu_aug_4, datetime(2012, 5, 31), True),
|
||||
(offset_nem_thu_aug_4, datetime(2007, 3, 1), True),
|
||||
(offset_nem_thu_aug_4, datetime(1994, 3, 3), True),
|
||||
|
||||
(offset_n, datetime(2012, 12, 31), False),
|
||||
(offset_n, datetime(2013, 1, 1), True),
|
||||
(offset_n, datetime(2013, 1, 2), False)]
|
||||
|
||||
@pytest.mark.parametrize('case', on_offset_cases)
|
||||
def test_onOffset(self, case):
|
||||
offset, dt, expected = case
|
||||
assert_onOffset(offset, dt, expected)
|
||||
|
||||
def test_offset(self):
|
||||
offset = makeFY5253NearestEndMonthQuarter(1, startingMonth=8,
|
||||
weekday=WeekDay.THU,
|
||||
qtr_with_extra_week=4)
|
||||
|
||||
MU = [datetime(2012, 5, 31),
|
||||
datetime(2012, 8, 30), datetime(2012, 11, 29),
|
||||
datetime(2013, 2, 28), datetime(2013, 5, 30)]
|
||||
|
||||
date = MU[0] + relativedelta(days=-1)
|
||||
for expected in MU:
|
||||
assert_offset_equal(offset, date, expected)
|
||||
date = date + offset
|
||||
|
||||
assert_offset_equal(offset,
|
||||
datetime(2012, 5, 31),
|
||||
datetime(2012, 8, 30))
|
||||
assert_offset_equal(offset,
|
||||
datetime(2012, 5, 30),
|
||||
datetime(2012, 5, 31))
|
||||
|
||||
offset2 = FY5253Quarter(weekday=5, startingMonth=12, variation="last",
|
||||
qtr_with_extra_week=4)
|
||||
|
||||
assert_offset_equal(offset2,
|
||||
datetime(2013, 1, 15),
|
||||
datetime(2013, 3, 30))
|
||||
|
||||
|
||||
def test_bunched_yearends():
|
||||
# GH#14774 cases with two fiscal year-ends in the same calendar-year
|
||||
fy = FY5253(n=1, weekday=5, startingMonth=12, variation='nearest')
|
||||
dt = Timestamp('2004-01-01')
|
||||
assert fy.rollback(dt) == Timestamp('2002-12-28')
|
||||
assert (-fy).apply(dt) == Timestamp('2002-12-28')
|
||||
assert dt - fy == Timestamp('2002-12-28')
|
||||
|
||||
assert fy.rollforward(dt) == Timestamp('2004-01-03')
|
||||
assert fy.apply(dt) == Timestamp('2004-01-03')
|
||||
assert fy + dt == Timestamp('2004-01-03')
|
||||
assert dt + fy == Timestamp('2004-01-03')
|
||||
|
||||
# Same thing, but starting from a Timestamp in the previous year.
|
||||
dt = Timestamp('2003-12-31')
|
||||
assert fy.rollback(dt) == Timestamp('2002-12-28')
|
||||
assert (-fy).apply(dt) == Timestamp('2002-12-28')
|
||||
assert dt - fy == Timestamp('2002-12-28')
|
||||
|
||||
|
||||
def test_fy5253_last_onoffset():
|
||||
# GH#18877 dates on the year-end but not normalized to midnight
|
||||
offset = FY5253(n=-5, startingMonth=5, variation="last", weekday=0)
|
||||
ts = Timestamp('1984-05-28 06:29:43.955911354+0200',
|
||||
tz='Europe/San_Marino')
|
||||
fast = offset.onOffset(ts)
|
||||
slow = (ts + offset) - offset == ts
|
||||
assert fast == slow
|
||||
|
||||
|
||||
def test_fy5253_nearest_onoffset():
|
||||
# GH#18877 dates on the year-end but not normalized to midnight
|
||||
offset = FY5253(n=3, startingMonth=7, variation="nearest", weekday=2)
|
||||
ts = Timestamp('2032-07-28 00:12:59.035729419+0000', tz='Africa/Dakar')
|
||||
fast = offset.onOffset(ts)
|
||||
slow = (ts + offset) - offset == ts
|
||||
assert fast == slow
|
||||
|
||||
|
||||
def test_fy5253qtr_onoffset_nearest():
|
||||
# GH#19036
|
||||
ts = Timestamp('1985-09-02 23:57:46.232550356-0300',
|
||||
tz='Atlantic/Bermuda')
|
||||
offset = FY5253Quarter(n=3, qtr_with_extra_week=1, startingMonth=2,
|
||||
variation="nearest", weekday=0)
|
||||
fast = offset.onOffset(ts)
|
||||
slow = (ts + offset) - offset == ts
|
||||
assert fast == slow
|
||||
|
||||
|
||||
def test_fy5253qtr_onoffset_last():
|
||||
# GH#19036
|
||||
offset = FY5253Quarter(n=-2, qtr_with_extra_week=1,
|
||||
startingMonth=7, variation="last", weekday=2)
|
||||
ts = Timestamp('2011-01-26 19:03:40.331096129+0200',
|
||||
tz='Africa/Windhoek')
|
||||
slow = (ts + offset) - offset == ts
|
||||
fast = offset.onOffset(ts)
|
||||
assert fast == slow
|
||||
File diff suppressed because it is too large
Load Diff
+108
@@ -0,0 +1,108 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Behavioral based tests for offsets and date_range.
|
||||
|
||||
This file is adapted from https://github.com/pandas-dev/pandas/pull/18761 -
|
||||
which was more ambitious but less idiomatic in its use of Hypothesis.
|
||||
|
||||
You may wish to consult the previous version for inspiration on further
|
||||
tests, or when trying to pin down the bugs exposed by the tests below.
|
||||
"""
|
||||
import warnings
|
||||
|
||||
from hypothesis import assume, given, strategies as st
|
||||
from hypothesis.extra.dateutil import timezones as dateutil_timezones
|
||||
from hypothesis.extra.pytz import timezones as pytz_timezones
|
||||
import pytest
|
||||
|
||||
import pandas as pd
|
||||
|
||||
from pandas.tseries.offsets import (
|
||||
BMonthBegin, BMonthEnd, BQuarterBegin, BQuarterEnd, BYearBegin, BYearEnd,
|
||||
MonthBegin, MonthEnd, QuarterBegin, QuarterEnd, YearBegin, YearEnd)
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# Helpers for generating random data
|
||||
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('ignore')
|
||||
min_dt = pd.Timestamp(1900, 1, 1).to_pydatetime(),
|
||||
max_dt = pd.Timestamp(1900, 1, 1).to_pydatetime(),
|
||||
|
||||
gen_date_range = st.builds(
|
||||
pd.date_range,
|
||||
start=st.datetimes(
|
||||
# TODO: Choose the min/max values more systematically
|
||||
min_value=pd.Timestamp(1900, 1, 1).to_pydatetime(),
|
||||
max_value=pd.Timestamp(2100, 1, 1).to_pydatetime()
|
||||
),
|
||||
periods=st.integers(min_value=2, max_value=100),
|
||||
freq=st.sampled_from('Y Q M D H T s ms us ns'.split()),
|
||||
tz=st.one_of(st.none(), dateutil_timezones(), pytz_timezones()),
|
||||
)
|
||||
|
||||
gen_random_datetime = st.datetimes(
|
||||
min_value=min_dt,
|
||||
max_value=max_dt,
|
||||
timezones=st.one_of(st.none(), dateutil_timezones(), pytz_timezones())
|
||||
)
|
||||
|
||||
# The strategy for each type is registered in conftest.py, as they don't carry
|
||||
# enough runtime information (e.g. type hints) to infer how to build them.
|
||||
gen_yqm_offset = st.one_of(*map(st.from_type, [
|
||||
MonthBegin, MonthEnd, BMonthBegin, BMonthEnd,
|
||||
QuarterBegin, QuarterEnd, BQuarterBegin, BQuarterEnd,
|
||||
YearBegin, YearEnd, BYearBegin, BYearEnd
|
||||
]))
|
||||
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# Offset-specific behaviour tests
|
||||
|
||||
|
||||
# Based on CI runs: Always passes on OSX, fails on Linux, sometimes on Windows
|
||||
@pytest.mark.xfail(strict=False, reason='inconsistent between OSs, Pythons')
|
||||
@given(gen_random_datetime, gen_yqm_offset)
|
||||
def test_on_offset_implementations(dt, offset):
|
||||
assume(not offset.normalize)
|
||||
# check that the class-specific implementations of onOffset match
|
||||
# the general case definition:
|
||||
# (dt + offset) - offset == dt
|
||||
compare = (dt + offset) - offset
|
||||
assert offset.onOffset(dt) == (compare == dt)
|
||||
|
||||
|
||||
@pytest.mark.xfail
|
||||
@given(gen_yqm_offset, gen_date_range)
|
||||
def test_apply_index_implementations(offset, rng):
|
||||
# offset.apply_index(dti)[i] should match dti[i] + offset
|
||||
assume(offset.n != 0) # TODO: test for that case separately
|
||||
|
||||
# rng = pd.date_range(start='1/1/2000', periods=100000, freq='T')
|
||||
ser = pd.Series(rng)
|
||||
|
||||
res = rng + offset
|
||||
res_v2 = offset.apply_index(rng)
|
||||
assert (res == res_v2).all()
|
||||
|
||||
assert res[0] == rng[0] + offset
|
||||
assert res[-1] == rng[-1] + offset
|
||||
res2 = ser + offset
|
||||
# apply_index is only for indexes, not series, so no res2_v2
|
||||
assert res2.iloc[0] == ser.iloc[0] + offset
|
||||
assert res2.iloc[-1] == ser.iloc[-1] + offset
|
||||
# TODO: Check randomly assorted entries, not just first/last
|
||||
|
||||
|
||||
@pytest.mark.xfail
|
||||
@given(gen_yqm_offset)
|
||||
def test_shift_across_dst(offset):
|
||||
# GH#18319 check that 1) timezone is correctly normalized and
|
||||
# 2) that hour is not incorrectly changed by this normalization
|
||||
# Note that dti includes a transition across DST boundary
|
||||
dti = pd.date_range(start='2017-10-30 12:00:00', end='2017-11-06',
|
||||
freq='D', tz='US/Eastern')
|
||||
assert (dti.hour == 12).all() # we haven't screwed up yet
|
||||
|
||||
res = dti + offset
|
||||
assert (res.hour == 12).all()
|
||||
@@ -0,0 +1,320 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Tests for offsets.Tick and subclasses
|
||||
"""
|
||||
from __future__ import division
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from hypothesis import assume, example, given, settings, strategies as st
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from pandas import Timedelta, Timestamp
|
||||
|
||||
from pandas.tseries import offsets
|
||||
from pandas.tseries.offsets import Hour, Micro, Milli, Minute, Nano, Second
|
||||
|
||||
from .common import assert_offset_equal
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
# Test Helpers
|
||||
|
||||
tick_classes = [Hour, Minute, Second, Milli, Micro, Nano]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_apply_ticks():
|
||||
result = offsets.Hour(3).apply(offsets.Hour(4))
|
||||
exp = offsets.Hour(7)
|
||||
assert (result == exp)
|
||||
|
||||
|
||||
def test_delta_to_tick():
|
||||
delta = timedelta(3)
|
||||
|
||||
tick = offsets._delta_to_tick(delta)
|
||||
assert (tick == offsets.Day(3))
|
||||
|
||||
td = Timedelta(nanoseconds=5)
|
||||
tick = offsets._delta_to_tick(td)
|
||||
assert tick == Nano(5)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('cls', tick_classes)
|
||||
@settings(deadline=None) # GH 24641
|
||||
@example(n=2, m=3)
|
||||
@example(n=800, m=300)
|
||||
@example(n=1000, m=5)
|
||||
@given(n=st.integers(-999, 999), m=st.integers(-999, 999))
|
||||
def test_tick_add_sub(cls, n, m):
|
||||
# For all Tick subclasses and all integers n, m, we should have
|
||||
# tick(n) + tick(m) == tick(n+m)
|
||||
# tick(n) - tick(m) == tick(n-m)
|
||||
left = cls(n)
|
||||
right = cls(m)
|
||||
expected = cls(n + m)
|
||||
|
||||
assert left + right == expected
|
||||
assert left.apply(right) == expected
|
||||
|
||||
expected = cls(n - m)
|
||||
assert left - right == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('cls', tick_classes)
|
||||
@settings(deadline=None)
|
||||
@example(n=2, m=3)
|
||||
@given(n=st.integers(-999, 999), m=st.integers(-999, 999))
|
||||
def test_tick_equality(cls, n, m):
|
||||
assume(m != n)
|
||||
# tick == tock iff tick.n == tock.n
|
||||
left = cls(n)
|
||||
right = cls(m)
|
||||
assert left != right
|
||||
assert not (left == right)
|
||||
|
||||
right = cls(n)
|
||||
assert left == right
|
||||
assert not (left != right)
|
||||
|
||||
if n != 0:
|
||||
assert cls(n) != cls(-n)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_Hour():
|
||||
assert_offset_equal(Hour(),
|
||||
datetime(2010, 1, 1), datetime(2010, 1, 1, 1))
|
||||
assert_offset_equal(Hour(-1),
|
||||
datetime(2010, 1, 1, 1), datetime(2010, 1, 1))
|
||||
assert_offset_equal(2 * Hour(),
|
||||
datetime(2010, 1, 1), datetime(2010, 1, 1, 2))
|
||||
assert_offset_equal(-1 * Hour(),
|
||||
datetime(2010, 1, 1, 1), datetime(2010, 1, 1))
|
||||
|
||||
assert Hour(3) + Hour(2) == Hour(5)
|
||||
assert Hour(3) - Hour(2) == Hour()
|
||||
|
||||
assert Hour(4) != Hour(1)
|
||||
|
||||
|
||||
def test_Minute():
|
||||
assert_offset_equal(Minute(),
|
||||
datetime(2010, 1, 1), datetime(2010, 1, 1, 0, 1))
|
||||
assert_offset_equal(Minute(-1),
|
||||
datetime(2010, 1, 1, 0, 1), datetime(2010, 1, 1))
|
||||
assert_offset_equal(2 * Minute(),
|
||||
datetime(2010, 1, 1), datetime(2010, 1, 1, 0, 2))
|
||||
assert_offset_equal(-1 * Minute(),
|
||||
datetime(2010, 1, 1, 0, 1), datetime(2010, 1, 1))
|
||||
|
||||
assert Minute(3) + Minute(2) == Minute(5)
|
||||
assert Minute(3) - Minute(2) == Minute()
|
||||
assert Minute(5) != Minute()
|
||||
|
||||
|
||||
def test_Second():
|
||||
assert_offset_equal(Second(),
|
||||
datetime(2010, 1, 1),
|
||||
datetime(2010, 1, 1, 0, 0, 1))
|
||||
assert_offset_equal(Second(-1),
|
||||
datetime(2010, 1, 1, 0, 0, 1),
|
||||
datetime(2010, 1, 1))
|
||||
assert_offset_equal(2 * Second(),
|
||||
datetime(2010, 1, 1),
|
||||
datetime(2010, 1, 1, 0, 0, 2))
|
||||
assert_offset_equal(-1 * Second(),
|
||||
datetime(2010, 1, 1, 0, 0, 1),
|
||||
datetime(2010, 1, 1))
|
||||
|
||||
assert Second(3) + Second(2) == Second(5)
|
||||
assert Second(3) - Second(2) == Second()
|
||||
|
||||
|
||||
def test_Millisecond():
|
||||
assert_offset_equal(Milli(),
|
||||
datetime(2010, 1, 1),
|
||||
datetime(2010, 1, 1, 0, 0, 0, 1000))
|
||||
assert_offset_equal(Milli(-1),
|
||||
datetime(2010, 1, 1, 0, 0, 0, 1000),
|
||||
datetime(2010, 1, 1))
|
||||
assert_offset_equal(Milli(2),
|
||||
datetime(2010, 1, 1),
|
||||
datetime(2010, 1, 1, 0, 0, 0, 2000))
|
||||
assert_offset_equal(2 * Milli(),
|
||||
datetime(2010, 1, 1),
|
||||
datetime(2010, 1, 1, 0, 0, 0, 2000))
|
||||
assert_offset_equal(-1 * Milli(),
|
||||
datetime(2010, 1, 1, 0, 0, 0, 1000),
|
||||
datetime(2010, 1, 1))
|
||||
|
||||
assert Milli(3) + Milli(2) == Milli(5)
|
||||
assert Milli(3) - Milli(2) == Milli()
|
||||
|
||||
|
||||
def test_MillisecondTimestampArithmetic():
|
||||
assert_offset_equal(Milli(),
|
||||
Timestamp('2010-01-01'),
|
||||
Timestamp('2010-01-01 00:00:00.001'))
|
||||
assert_offset_equal(Milli(-1),
|
||||
Timestamp('2010-01-01 00:00:00.001'),
|
||||
Timestamp('2010-01-01'))
|
||||
|
||||
|
||||
def test_Microsecond():
|
||||
assert_offset_equal(Micro(),
|
||||
datetime(2010, 1, 1),
|
||||
datetime(2010, 1, 1, 0, 0, 0, 1))
|
||||
assert_offset_equal(Micro(-1),
|
||||
datetime(2010, 1, 1, 0, 0, 0, 1),
|
||||
datetime(2010, 1, 1))
|
||||
|
||||
assert_offset_equal(2 * Micro(),
|
||||
datetime(2010, 1, 1),
|
||||
datetime(2010, 1, 1, 0, 0, 0, 2))
|
||||
assert_offset_equal(-1 * Micro(),
|
||||
datetime(2010, 1, 1, 0, 0, 0, 1),
|
||||
datetime(2010, 1, 1))
|
||||
|
||||
assert Micro(3) + Micro(2) == Micro(5)
|
||||
assert Micro(3) - Micro(2) == Micro()
|
||||
|
||||
|
||||
def test_NanosecondGeneric():
|
||||
timestamp = Timestamp(datetime(2010, 1, 1))
|
||||
assert timestamp.nanosecond == 0
|
||||
|
||||
result = timestamp + Nano(10)
|
||||
assert result.nanosecond == 10
|
||||
|
||||
reverse_result = Nano(10) + timestamp
|
||||
assert reverse_result.nanosecond == 10
|
||||
|
||||
|
||||
def test_Nanosecond():
|
||||
timestamp = Timestamp(datetime(2010, 1, 1))
|
||||
assert_offset_equal(Nano(),
|
||||
timestamp,
|
||||
timestamp + np.timedelta64(1, 'ns'))
|
||||
assert_offset_equal(Nano(-1),
|
||||
timestamp + np.timedelta64(1, 'ns'),
|
||||
timestamp)
|
||||
assert_offset_equal(2 * Nano(),
|
||||
timestamp,
|
||||
timestamp + np.timedelta64(2, 'ns'))
|
||||
assert_offset_equal(-1 * Nano(),
|
||||
timestamp + np.timedelta64(1, 'ns'),
|
||||
timestamp)
|
||||
|
||||
assert Nano(3) + Nano(2) == Nano(5)
|
||||
assert Nano(3) - Nano(2) == Nano()
|
||||
|
||||
# GH9284
|
||||
assert Nano(1) + Nano(10) == Nano(11)
|
||||
assert Nano(5) + Micro(1) == Nano(1005)
|
||||
assert Micro(5) + Nano(1) == Nano(5001)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('kls, expected',
|
||||
[(Hour, Timedelta(hours=5)),
|
||||
(Minute, Timedelta(hours=2, minutes=3)),
|
||||
(Second, Timedelta(hours=2, seconds=3)),
|
||||
(Milli, Timedelta(hours=2, milliseconds=3)),
|
||||
(Micro, Timedelta(hours=2, microseconds=3)),
|
||||
(Nano, Timedelta(hours=2, nanoseconds=3))])
|
||||
def test_tick_addition(kls, expected):
|
||||
offset = kls(3)
|
||||
result = offset + Timedelta(hours=2)
|
||||
assert isinstance(result, Timedelta)
|
||||
assert result == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('cls', tick_classes)
|
||||
def test_tick_division(cls):
|
||||
off = cls(10)
|
||||
|
||||
assert off / cls(5) == 2
|
||||
assert off / 2 == cls(5)
|
||||
assert off / 2.0 == cls(5)
|
||||
|
||||
assert off / off.delta == 1
|
||||
assert off / off.delta.to_timedelta64() == 1
|
||||
|
||||
assert off / Nano(1) == off.delta / Nano(1).delta
|
||||
|
||||
if cls is not Nano:
|
||||
# A case where we end up with a smaller class
|
||||
result = off / 1000
|
||||
assert isinstance(result, offsets.Tick)
|
||||
assert not isinstance(result, cls)
|
||||
assert result.delta == off.delta / 1000
|
||||
|
||||
if cls._inc < Timedelta(seconds=1):
|
||||
# Case where we end up with a bigger class
|
||||
result = off / .001
|
||||
assert isinstance(result, offsets.Tick)
|
||||
assert not isinstance(result, cls)
|
||||
assert result.delta == off.delta / .001
|
||||
|
||||
|
||||
@pytest.mark.parametrize('cls1', tick_classes)
|
||||
@pytest.mark.parametrize('cls2', tick_classes)
|
||||
def test_tick_zero(cls1, cls2):
|
||||
assert cls1(0) == cls2(0)
|
||||
assert cls1(0) + cls2(0) == cls1(0)
|
||||
|
||||
if cls1 is not Nano:
|
||||
assert cls1(2) + cls2(0) == cls1(2)
|
||||
|
||||
if cls1 is Nano:
|
||||
assert cls1(2) + Nano(0) == cls1(2)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('cls', tick_classes)
|
||||
def test_tick_equalities(cls):
|
||||
assert cls() == cls(1)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('cls', tick_classes)
|
||||
def test_tick_offset(cls):
|
||||
assert not cls().isAnchored()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('cls', tick_classes)
|
||||
def test_compare_ticks(cls):
|
||||
three = cls(3)
|
||||
four = cls(4)
|
||||
|
||||
assert three < cls(4)
|
||||
assert cls(3) < four
|
||||
assert four > cls(3)
|
||||
assert cls(4) > three
|
||||
assert cls(3) == cls(3)
|
||||
assert cls(3) != cls(4)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('cls', tick_classes)
|
||||
def test_compare_ticks_to_strs(cls):
|
||||
# GH#23524
|
||||
off = cls(19)
|
||||
|
||||
# These tests should work with any strings, but we particularly are
|
||||
# interested in "infer" as that comparison is convenient to make in
|
||||
# Datetime/Timedelta Array/Index constructors
|
||||
assert not off == "infer"
|
||||
assert not "foo" == off
|
||||
|
||||
for left, right in [("infer", off), (off, "infer")]:
|
||||
with pytest.raises(TypeError):
|
||||
left < right
|
||||
with pytest.raises(TypeError):
|
||||
left <= right
|
||||
with pytest.raises(TypeError):
|
||||
left > right
|
||||
with pytest.raises(TypeError):
|
||||
left >= right
|
||||
+1027
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user