def test_construction_month(self): expected = Period('2007-01', freq='M') i1 = Period('200701', freq='M') assert i1 == expected i1 = Period('200701', freq='M') assert i1 == expected i1 = Period(200701, freq='M') assert i1 == expected i1 = Period(ordinal=200701, freq='M') assert i1.year == 18695 i1 = Period(datetime(2007, 1, 1), freq='M') i2 = Period('200701', freq='M') assert i1 == i2 i1 = Period(date(2007, 1, 1), freq='M') i2 = Period(datetime(2007, 1, 1), freq='M') i3 = Period(np.datetime64('2007-01-01'), freq='M') i4 = Period(np_datetime64_compat('2007-01-01 00:00:00Z'), freq='M') i5 = Period(np_datetime64_compat('2007-01-01 00:00:00.000Z'), freq='M') assert i1 == i2 assert i1 == i3 assert i1 == i4 assert i1 == i5
def test_conversion(self): rs = self.pc.convert(['2012-1-1'], None, self.axis)[0] xp = Period('2012-1-1').ordinal assert rs == xp rs = self.pc.convert('2012-1-1', None, self.axis) assert rs == xp rs = self.pc.convert([date(2012, 1, 1)], None, self.axis)[0] assert rs == xp rs = self.pc.convert(date(2012, 1, 1), None, self.axis) assert rs == xp rs = self.pc.convert([Timestamp('2012-1-1')], None, self.axis)[0] assert rs == xp rs = self.pc.convert(Timestamp('2012-1-1'), None, self.axis) assert rs == xp rs = self.pc.convert( np_datetime64_compat('2012-01-01'), None, self.axis) assert rs == xp rs = self.pc.convert( np_datetime64_compat('2012-01-01 00:00:00+0000'), None, self.axis) assert rs == xp rs = self.pc.convert(np.array([ np_datetime64_compat('2012-01-01 00:00:00+0000'), np_datetime64_compat('2012-01-02 00:00:00+0000')]), None, self.axis) assert rs[0] == xp
def test_conversion(self): rs = self.dtc.convert(['2012-1-1'], None, None)[0] xp = datetime(2012, 1, 1).toordinal() self.assertEqual(rs, xp) rs = self.dtc.convert('2012-1-1', None, None) self.assertEqual(rs, xp) rs = self.dtc.convert(date(2012, 1, 1), None, None) self.assertEqual(rs, xp) rs = self.dtc.convert(datetime(2012, 1, 1).toordinal(), None, None) self.assertEqual(rs, xp) rs = self.dtc.convert('2012-1-1', None, None) self.assertEqual(rs, xp) rs = self.dtc.convert(Timestamp('2012-1-1'), None, None) self.assertEqual(rs, xp) # also testing datetime64 dtype (GH8614) rs = self.dtc.convert(np_datetime64_compat('2012-01-01'), None, None) self.assertEqual(rs, xp) rs = self.dtc.convert(np_datetime64_compat( '2012-01-01 00:00:00+0000'), None, None) self.assertEqual(rs, xp) rs = self.dtc.convert(np.array([ np_datetime64_compat('2012-01-01 00:00:00+0000'), np_datetime64_compat('2012-01-02 00:00:00+0000')]), None, None) self.assertEqual(rs[0], xp)
def test_nanosecond_timestamp(self): # GH 7610 expected = 1293840000000000005 t = Timestamp('2011-01-01') + offsets.Nano(5) assert repr(t) == "Timestamp('2011-01-01 00:00:00.000000005')" assert t.value == expected assert t.nanosecond == 5 t = Timestamp(t) assert repr(t) == "Timestamp('2011-01-01 00:00:00.000000005')" assert t.value == expected assert t.nanosecond == 5 t = Timestamp(np_datetime64_compat('2011-01-01 00:00:00.000000005Z')) assert repr(t) == "Timestamp('2011-01-01 00:00:00.000000005')" assert t.value == expected assert t.nanosecond == 5 expected = 1293840000000000010 t = t + offsets.Nano(5) assert repr(t) == "Timestamp('2011-01-01 00:00:00.000000010')" assert t.value == expected assert t.nanosecond == 10 t = Timestamp(t) assert repr(t) == "Timestamp('2011-01-01 00:00:00.000000010')" assert t.value == expected assert t.nanosecond == 10 t = Timestamp(np_datetime64_compat('2011-01-01 00:00:00.000000010Z')) assert repr(t) == "Timestamp('2011-01-01 00:00:00.000000010')" assert t.value == expected assert t.nanosecond == 10
def test_conversion(self): rs = self.dtc.convert(['2012-1-1'], None, None)[0] xp = datetime(2012, 1, 1).toordinal() self.assertEqual(rs, xp) rs = self.dtc.convert('2012-1-1', None, None) self.assertEqual(rs, xp) rs = self.dtc.convert(date(2012, 1, 1), None, None) self.assertEqual(rs, xp) rs = self.dtc.convert(datetime(2012, 1, 1).toordinal(), None, None) self.assertEqual(rs, xp) rs = self.dtc.convert('2012-1-1', None, None) self.assertEqual(rs, xp) rs = self.dtc.convert(Timestamp('2012-1-1'), None, None) self.assertEqual(rs, xp) # also testing datetime64 dtype (GH8614) rs = self.dtc.convert(np_datetime64_compat('2012-01-01'), None, None) self.assertEqual(rs, xp) rs = self.dtc.convert(np_datetime64_compat('2012-01-01 00:00:00+0000'), None, None) self.assertEqual(rs, xp) rs = self.dtc.convert( np.array([ np_datetime64_compat('2012-01-01 00:00:00+0000'), np_datetime64_compat('2012-01-02 00:00:00+0000') ]), None, None) self.assertEqual(rs[0], xp)
def test_construction(self): i1 = Period('1/1/2005', freq='M') i2 = Period('Jan 2005') assert i1 == i2 i1 = Period('2005', freq='A') i2 = Period('2005') i3 = Period('2005', freq='a') assert i1 == i2 assert i1 == i3 i4 = Period('2005', freq='M') i5 = Period('2005', freq='m') pytest.raises(ValueError, i1.__ne__, i4) assert i4 == i5 i1 = Period.now('Q') i2 = Period(datetime.now(), freq='Q') i3 = Period.now('q') assert i1 == i2 assert i1 == i3 i1 = Period('1982', freq='min') i2 = Period('1982', freq='MIN') assert i1 == i2 i2 = Period('1982', freq=('Min', 1)) assert i1 == i2 i1 = Period(year=2005, month=3, day=1, freq='D') i2 = Period('3/1/2005', freq='D') assert i1 == i2 i3 = Period(year=2005, month=3, day=1, freq='d') assert i1 == i3 i1 = Period('2007-01-01 09:00:00.001') expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1000), freq='L') assert i1 == expected expected = Period(np_datetime64_compat( '2007-01-01 09:00:00.001Z'), freq='L') assert i1 == expected i1 = Period('2007-01-01 09:00:00.00101') expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1010), freq='U') assert i1 == expected expected = Period(np_datetime64_compat('2007-01-01 09:00:00.00101Z'), freq='U') assert i1 == expected pytest.raises(ValueError, Period, ordinal=200701) pytest.raises(ValueError, Period, '2007-1-1', freq='X')
def test_conversion(self): rs = self.dtc.convert(["2012-1-1"], None, None)[0] xp = datetime(2012, 1, 1).toordinal() assert rs == xp rs = self.dtc.convert("2012-1-1", None, None) assert rs == xp rs = self.dtc.convert(date(2012, 1, 1), None, None) assert rs == xp rs = self.dtc.convert(datetime(2012, 1, 1).toordinal(), None, None) assert rs == xp rs = self.dtc.convert("2012-1-1", None, None) assert rs == xp rs = self.dtc.convert(Timestamp("2012-1-1"), None, None) assert rs == xp # also testing datetime64 dtype (GH8614) rs = self.dtc.convert(np_datetime64_compat("2012-01-01"), None, None) assert rs == xp rs = self.dtc.convert( np_datetime64_compat("2012-01-01 00:00:00+0000"), None, None ) assert rs == xp rs = self.dtc.convert( np.array( [ np_datetime64_compat("2012-01-01 00:00:00+0000"), np_datetime64_compat("2012-01-02 00:00:00+0000"), ] ), None, None, ) assert rs[0] == xp # we have a tz-aware date (constructed to that when we turn to utc it # is the same as our sample) ts = Timestamp("2012-01-01").tz_localize("UTC").tz_convert("US/Eastern") rs = self.dtc.convert(ts, None, None) assert rs == xp rs = self.dtc.convert(ts.to_pydatetime(), None, None) assert rs == xp rs = self.dtc.convert(Index([ts - Day(1), ts]), None, None) assert rs[1] == xp rs = self.dtc.convert(Index([ts - Day(1), ts]).to_pydatetime(), None, None) assert rs[1] == xp
def test_conversion(self): rs = self.dtc.convert(['2012-1-1'], None, None)[0] xp = datetime(2012, 1, 1).toordinal() assert rs == xp rs = self.dtc.convert('2012-1-1', None, None) assert rs == xp rs = self.dtc.convert(date(2012, 1, 1), None, None) assert rs == xp rs = self.dtc.convert(datetime(2012, 1, 1).toordinal(), None, None) assert rs == xp rs = self.dtc.convert('2012-1-1', None, None) assert rs == xp rs = self.dtc.convert(Timestamp('2012-1-1'), None, None) assert rs == xp # also testing datetime64 dtype (GH8614) rs = self.dtc.convert(np_datetime64_compat('2012-01-01'), None, None) assert rs == xp rs = self.dtc.convert(np_datetime64_compat( '2012-01-01 00:00:00+0000'), None, None) assert rs == xp rs = self.dtc.convert(np.array([ np_datetime64_compat('2012-01-01 00:00:00+0000'), np_datetime64_compat('2012-01-02 00:00:00+0000')]), None, None) assert rs[0] == xp # we have a tz-aware date (constructed to that when we turn to utc it # is the same as our sample) ts = (Timestamp('2012-01-01') .tz_localize('UTC') .tz_convert('US/Eastern') ) rs = self.dtc.convert(ts, None, None) assert rs == xp rs = self.dtc.convert(ts.to_pydatetime(), None, None) assert rs == xp rs = self.dtc.convert(Index([ts - Day(1), ts]), None, None) assert rs[1] == xp rs = self.dtc.convert(Index([ts - Day(1), ts]).to_pydatetime(), None, None) assert rs[1] == xp
def test_conversion(self): rs = self.dtc.convert(['2012-1-1'], None, None)[0] xp = datetime(2012, 1, 1).toordinal() self.assertEqual(rs, xp) rs = self.dtc.convert('2012-1-1', None, None) self.assertEqual(rs, xp) rs = self.dtc.convert(date(2012, 1, 1), None, None) self.assertEqual(rs, xp) rs = self.dtc.convert(datetime(2012, 1, 1).toordinal(), None, None) self.assertEqual(rs, xp) rs = self.dtc.convert('2012-1-1', None, None) self.assertEqual(rs, xp) rs = self.dtc.convert(Timestamp('2012-1-1'), None, None) self.assertEqual(rs, xp) # also testing datetime64 dtype (GH8614) rs = self.dtc.convert(np_datetime64_compat('2012-01-01'), None, None) self.assertEqual(rs, xp) rs = self.dtc.convert(np_datetime64_compat('2012-01-01 00:00:00+0000'), None, None) self.assertEqual(rs, xp) rs = self.dtc.convert( np.array([ np_datetime64_compat('2012-01-01 00:00:00+0000'), np_datetime64_compat('2012-01-02 00:00:00+0000') ]), None, None) self.assertEqual(rs[0], xp) # we have a tz-aware date (constructed to that when we turn to utc it # is the same as our sample) ts = (Timestamp('2012-01-01').tz_localize('UTC').tz_convert( 'US/Eastern')) rs = self.dtc.convert(ts, None, None) self.assertEqual(rs, xp) rs = self.dtc.convert(ts.to_pydatetime(), None, None) self.assertEqual(rs, xp) rs = self.dtc.convert(Index([ts - Day(1), ts]), None, None) self.assertEqual(rs[1], xp) rs = self.dtc.convert( Index([ts - Day(1), ts]).to_pydatetime(), None, None) self.assertEqual(rs[1], xp)
def setup_method(self, method): self.d = datetime(2008, 1, 1) self.nd = np_datetime64_compat("2008-01-01 00:00:00Z") self.offset = CDay() self.offset1 = self.offset self.offset2 = CDay(2)
def test_rollforward(self, offset_types): expecteds = self.expecteds.copy() # result will not be changed if the target is on the offset no_changes = [ "Day", "MonthBegin", "SemiMonthBegin", "YearBegin", "Week", "Hour", "Minute", "Second", "Milli", "Micro", "Nano", "DateOffset", ] for n in no_changes: expecteds[n] = Timestamp("2011/01/01 09:00") expecteds["BusinessHour"] = Timestamp("2011-01-03 09:00:00") expecteds["CustomBusinessHour"] = Timestamp("2011-01-03 09:00:00") # but be changed when normalize=True norm_expected = expecteds.copy() for k in norm_expected: norm_expected[k] = Timestamp(norm_expected[k].date()) normalized = { "Day": Timestamp("2011-01-02 00:00:00"), "DateOffset": Timestamp("2011-01-02 00:00:00"), "MonthBegin": Timestamp("2011-02-01 00:00:00"), "SemiMonthBegin": Timestamp("2011-01-15 00:00:00"), "YearBegin": Timestamp("2012-01-01 00:00:00"), "Week": Timestamp("2011-01-08 00:00:00"), "Hour": Timestamp("2011-01-01 00:00:00"), "Minute": Timestamp("2011-01-01 00:00:00"), "Second": Timestamp("2011-01-01 00:00:00"), "Milli": Timestamp("2011-01-01 00:00:00"), "Micro": Timestamp("2011-01-01 00:00:00"), } norm_expected.update(normalized) sdt = datetime(2011, 1, 1, 9, 0) ndt = np_datetime64_compat("2011-01-01 09:00Z") for dt in [sdt, ndt]: expected = expecteds[offset_types.__name__] self._check_offsetfunc_works(offset_types, "rollforward", dt, expected) expected = norm_expected[offset_types.__name__] self._check_offsetfunc_works( offset_types, "rollforward", dt, expected, normalize=True )
def test_apply(self, offset_types): sdt = datetime(2011, 1, 1, 9, 0) ndt = np_datetime64_compat("2011-01-01 09:00Z") for dt in [sdt, ndt]: expected = self.expecteds[offset_types.__name__] self._check_offsetfunc_works(offset_types, "apply", dt, expected) expected = Timestamp(expected.date()) self._check_offsetfunc_works( offset_types, "apply", dt, expected, normalize=True )
def test_conversion(self): rs = self.pc.convert(["2012-1-1"], None, self.axis)[0] xp = Period("2012-1-1").ordinal assert rs == xp rs = self.pc.convert("2012-1-1", None, self.axis) assert rs == xp rs = self.pc.convert([date(2012, 1, 1)], None, self.axis)[0] assert rs == xp rs = self.pc.convert(date(2012, 1, 1), None, self.axis) assert rs == xp rs = self.pc.convert([Timestamp("2012-1-1")], None, self.axis)[0] assert rs == xp rs = self.pc.convert(Timestamp("2012-1-1"), None, self.axis) assert rs == xp rs = self.pc.convert(np_datetime64_compat("2012-01-01"), None, self.axis) assert rs == xp rs = self.pc.convert( np_datetime64_compat("2012-01-01 00:00:00+0000"), None, self.axis ) assert rs == xp rs = self.pc.convert( np.array( [ np_datetime64_compat("2012-01-01 00:00:00+0000"), np_datetime64_compat("2012-01-02 00:00:00+0000"), ] ), None, self.axis, ) assert rs[0] == xp
def test_period_constructor_offsets(self): assert (Period('1/1/2005', freq=offsets.MonthEnd()) == Period('1/1/2005', freq='M')) assert (Period('2005', freq=offsets.YearEnd()) == Period('2005', freq='A')) assert (Period('2005', freq=offsets.MonthEnd()) == Period('2005', freq='M')) assert (Period('3/10/12', freq=offsets.BusinessDay()) == Period('3/10/12', freq='B')) assert (Period('3/10/12', freq=offsets.Day()) == Period('3/10/12', freq='D')) assert (Period(year=2005, quarter=1, freq=offsets.QuarterEnd(startingMonth=12)) == Period(year=2005, quarter=1, freq='Q')) assert (Period(year=2005, quarter=2, freq=offsets.QuarterEnd(startingMonth=12)) == Period(year=2005, quarter=2, freq='Q')) assert (Period(year=2005, month=3, day=1, freq=offsets.Day()) == Period(year=2005, month=3, day=1, freq='D')) assert (Period(year=2012, month=3, day=10, freq=offsets.BDay()) == Period(year=2012, month=3, day=10, freq='B')) expected = Period('2005-03-01', freq='3D') assert (Period(year=2005, month=3, day=1, freq=offsets.Day(3)) == expected) assert Period(year=2005, month=3, day=1, freq='3D') == expected assert (Period(year=2012, month=3, day=10, freq=offsets.BDay(3)) == Period(year=2012, month=3, day=10, freq='3B')) assert (Period(200701, freq=offsets.MonthEnd()) == Period(200701, freq='M')) i1 = Period(ordinal=200701, freq=offsets.MonthEnd()) i2 = Period(ordinal=200701, freq='M') assert i1 == i2 assert i1.year == 18695 assert i2.year == 18695 i1 = Period(datetime(2007, 1, 1), freq='M') i2 = Period('200701', freq='M') assert i1 == i2 i1 = Period(date(2007, 1, 1), freq='M') i2 = Period(datetime(2007, 1, 1), freq='M') i3 = Period(np.datetime64('2007-01-01'), freq='M') i4 = Period(np_datetime64_compat('2007-01-01 00:00:00Z'), freq='M') i5 = Period(np_datetime64_compat('2007-01-01 00:00:00.000Z'), freq='M') assert i1 == i2 assert i1 == i3 assert i1 == i4 assert i1 == i5 i1 = Period('2007-01-01 09:00:00.001') expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1000), freq='L') assert i1 == expected expected = Period(np_datetime64_compat( '2007-01-01 09:00:00.001Z'), freq='L') assert i1 == expected i1 = Period('2007-01-01 09:00:00.00101') expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1010), freq='U') assert i1 == expected expected = Period(np_datetime64_compat('2007-01-01 09:00:00.00101Z'), freq='U') assert i1 == expected pytest.raises(ValueError, Period, ordinal=200701) pytest.raises(ValueError, Period, '2007-1-1', freq='X')
def test_comparisons_nat(self): fidx1 = pd.Index([1.0, np.nan, 3.0, np.nan, 5.0, 7.0]) fidx2 = pd.Index([2.0, 3.0, np.nan, np.nan, 6.0, 7.0]) didx1 = pd.DatetimeIndex(['2014-01-01', pd.NaT, '2014-03-01', pd.NaT, '2014-05-01', '2014-07-01']) didx2 = pd.DatetimeIndex(['2014-02-01', '2014-03-01', pd.NaT, pd.NaT, '2014-06-01', '2014-07-01']) darr = np.array([np_datetime64_compat('2014-02-01 00:00Z'), np_datetime64_compat('2014-03-01 00:00Z'), np_datetime64_compat('nat'), np.datetime64('nat'), np_datetime64_compat('2014-06-01 00:00Z'), np_datetime64_compat('2014-07-01 00:00Z')]) if _np_version_under1p8: # cannot test array because np.datetime('nat') returns today's date cases = [(fidx1, fidx2), (didx1, didx2)] else: cases = [(fidx1, fidx2), (didx1, didx2), (didx1, darr)] # Check pd.NaT is handles as the same as np.nan with tm.assert_produces_warning(None): for idx1, idx2 in cases: result = idx1 < idx2 expected = np.array([True, False, False, False, True, False]) tm.assert_numpy_array_equal(result, expected) result = idx2 > idx1 expected = np.array([True, False, False, False, True, False]) tm.assert_numpy_array_equal(result, expected) result = idx1 <= idx2 expected = np.array([True, False, False, False, True, True]) tm.assert_numpy_array_equal(result, expected) result = idx2 >= idx1 expected = np.array([True, False, False, False, True, True]) tm.assert_numpy_array_equal(result, expected) result = idx1 == idx2 expected = np.array([False, False, False, False, False, True]) tm.assert_numpy_array_equal(result, expected) result = idx1 != idx2 expected = np.array([True, True, True, True, True, False]) tm.assert_numpy_array_equal(result, expected) with tm.assert_produces_warning(None): for idx1, val in [(fidx1, np.nan), (didx1, pd.NaT)]: result = idx1 < val expected = np.array([False, False, False, False, False, False]) tm.assert_numpy_array_equal(result, expected) result = idx1 > val tm.assert_numpy_array_equal(result, expected) result = idx1 <= val tm.assert_numpy_array_equal(result, expected) result = idx1 >= val tm.assert_numpy_array_equal(result, expected) result = idx1 == val tm.assert_numpy_array_equal(result, expected) result = idx1 != val expected = np.array([True, True, True, True, True, True]) tm.assert_numpy_array_equal(result, expected) # Check pd.NaT is handles as the same as np.nan with tm.assert_produces_warning(None): for idx1, val in [(fidx1, 3), (didx1, datetime(2014, 3, 1))]: result = idx1 < val expected = np.array([True, False, False, False, False, False]) tm.assert_numpy_array_equal(result, expected) result = idx1 > val expected = np.array([False, False, False, False, True, True]) tm.assert_numpy_array_equal(result, expected) result = idx1 <= val expected = np.array([True, False, True, False, False, False]) tm.assert_numpy_array_equal(result, expected) result = idx1 >= val expected = np.array([False, False, True, False, True, True]) tm.assert_numpy_array_equal(result, expected) result = idx1 == val expected = np.array([False, False, True, False, False, False]) tm.assert_numpy_array_equal(result, expected) result = idx1 != val expected = np.array([True, True, False, True, True, True]) tm.assert_numpy_array_equal(result, expected)
def test_period_constructor_offsets(self): assert Period("1/1/2005", freq=offsets.MonthEnd()) == Period( "1/1/2005", freq="M" ) assert Period("2005", freq=offsets.YearEnd()) == Period("2005", freq="A") assert Period("2005", freq=offsets.MonthEnd()) == Period("2005", freq="M") assert Period("3/10/12", freq=offsets.BusinessDay()) == Period( "3/10/12", freq="B" ) assert Period("3/10/12", freq=offsets.Day()) == Period("3/10/12", freq="D") assert Period( year=2005, quarter=1, freq=offsets.QuarterEnd(startingMonth=12) ) == Period(year=2005, quarter=1, freq="Q") assert Period( year=2005, quarter=2, freq=offsets.QuarterEnd(startingMonth=12) ) == Period(year=2005, quarter=2, freq="Q") assert Period(year=2005, month=3, day=1, freq=offsets.Day()) == Period( year=2005, month=3, day=1, freq="D" ) assert Period(year=2012, month=3, day=10, freq=offsets.BDay()) == Period( year=2012, month=3, day=10, freq="B" ) expected = Period("2005-03-01", freq="3D") assert Period(year=2005, month=3, day=1, freq=offsets.Day(3)) == expected assert Period(year=2005, month=3, day=1, freq="3D") == expected assert Period(year=2012, month=3, day=10, freq=offsets.BDay(3)) == Period( year=2012, month=3, day=10, freq="3B" ) assert Period(200701, freq=offsets.MonthEnd()) == Period(200701, freq="M") i1 = Period(ordinal=200701, freq=offsets.MonthEnd()) i2 = Period(ordinal=200701, freq="M") assert i1 == i2 assert i1.year == 18695 assert i2.year == 18695 i1 = Period(datetime(2007, 1, 1), freq="M") i2 = Period("200701", freq="M") assert i1 == i2 i1 = Period(date(2007, 1, 1), freq="M") i2 = Period(datetime(2007, 1, 1), freq="M") i3 = Period(np.datetime64("2007-01-01"), freq="M") i4 = Period(np_datetime64_compat("2007-01-01 00:00:00Z"), freq="M") i5 = Period(np_datetime64_compat("2007-01-01 00:00:00.000Z"), freq="M") assert i1 == i2 assert i1 == i3 assert i1 == i4 assert i1 == i5 i1 = Period("2007-01-01 09:00:00.001") expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1000), freq="L") assert i1 == expected expected = Period(np_datetime64_compat("2007-01-01 09:00:00.001Z"), freq="L") assert i1 == expected i1 = Period("2007-01-01 09:00:00.00101") expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1010), freq="U") assert i1 == expected expected = Period(np_datetime64_compat("2007-01-01 09:00:00.00101Z"), freq="U") assert i1 == expected
def test_construction(self): i1 = Period("1/1/2005", freq="M") i2 = Period("Jan 2005") assert i1 == i2 i1 = Period("2005", freq="A") i2 = Period("2005") i3 = Period("2005", freq="a") assert i1 == i2 assert i1 == i3 i4 = Period("2005", freq="M") i5 = Period("2005", freq="m") msg = r"Input has different freq=M from Period\(freq=A-DEC\)" with pytest.raises(IncompatibleFrequency, match=msg): i1 != i4 assert i4 == i5 i1 = Period.now("Q") i2 = Period(datetime.now(), freq="Q") i3 = Period.now("q") assert i1 == i2 assert i1 == i3 i1 = Period("1982", freq="min") i2 = Period("1982", freq="MIN") assert i1 == i2 i2 = Period("1982", freq=("Min", 1)) assert i1 == i2 i1 = Period(year=2005, month=3, day=1, freq="D") i2 = Period("3/1/2005", freq="D") assert i1 == i2 i3 = Period(year=2005, month=3, day=1, freq="d") assert i1 == i3 i1 = Period("2007-01-01 09:00:00.001") expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1000), freq="L") assert i1 == expected expected = Period(np_datetime64_compat("2007-01-01 09:00:00.001Z"), freq="L") assert i1 == expected i1 = Period("2007-01-01 09:00:00.00101") expected = Period(datetime(2007, 1, 1, 9, 0, 0, 1010), freq="U") assert i1 == expected expected = Period(np_datetime64_compat("2007-01-01 09:00:00.00101Z"), freq="U") assert i1 == expected msg = "Must supply freq for ordinal value" with pytest.raises(ValueError, match=msg): Period(ordinal=200701) msg = "Invalid frequency: X" with pytest.raises(ValueError, match=msg): Period("2007-1-1", freq="X")
def test_dti_cmp_nat_behaves_like_float_cmp_nan(self): fidx1 = pd.Index([1.0, np.nan, 3.0, np.nan, 5.0, 7.0]) fidx2 = pd.Index([2.0, 3.0, np.nan, np.nan, 6.0, 7.0]) didx1 = pd.DatetimeIndex([ '2014-01-01', pd.NaT, '2014-03-01', pd.NaT, '2014-05-01', '2014-07-01' ]) didx2 = pd.DatetimeIndex([ '2014-02-01', '2014-03-01', pd.NaT, pd.NaT, '2014-06-01', '2014-07-01' ]) darr = np.array([ np_datetime64_compat('2014-02-01 00:00Z'), np_datetime64_compat('2014-03-01 00:00Z'), np_datetime64_compat('nat'), np.datetime64('nat'), np_datetime64_compat('2014-06-01 00:00Z'), np_datetime64_compat('2014-07-01 00:00Z') ]) cases = [(fidx1, fidx2), (didx1, didx2), (didx1, darr)] # Check pd.NaT is handles as the same as np.nan with tm.assert_produces_warning(None): for idx1, idx2 in cases: result = idx1 < idx2 expected = np.array([True, False, False, False, True, False]) tm.assert_numpy_array_equal(result, expected) result = idx2 > idx1 expected = np.array([True, False, False, False, True, False]) tm.assert_numpy_array_equal(result, expected) result = idx1 <= idx2 expected = np.array([True, False, False, False, True, True]) tm.assert_numpy_array_equal(result, expected) result = idx2 >= idx1 expected = np.array([True, False, False, False, True, True]) tm.assert_numpy_array_equal(result, expected) result = idx1 == idx2 expected = np.array([False, False, False, False, False, True]) tm.assert_numpy_array_equal(result, expected) result = idx1 != idx2 expected = np.array([True, True, True, True, True, False]) tm.assert_numpy_array_equal(result, expected) with tm.assert_produces_warning(None): for idx1, val in [(fidx1, np.nan), (didx1, pd.NaT)]: result = idx1 < val expected = np.array([False, False, False, False, False, False]) tm.assert_numpy_array_equal(result, expected) result = idx1 > val tm.assert_numpy_array_equal(result, expected) result = idx1 <= val tm.assert_numpy_array_equal(result, expected) result = idx1 >= val tm.assert_numpy_array_equal(result, expected) result = idx1 == val tm.assert_numpy_array_equal(result, expected) result = idx1 != val expected = np.array([True, True, True, True, True, True]) tm.assert_numpy_array_equal(result, expected) # Check pd.NaT is handles as the same as np.nan with tm.assert_produces_warning(None): for idx1, val in [(fidx1, 3), (didx1, datetime(2014, 3, 1))]: result = idx1 < val expected = np.array([True, False, False, False, False, False]) tm.assert_numpy_array_equal(result, expected) result = idx1 > val expected = np.array([False, False, False, False, True, True]) tm.assert_numpy_array_equal(result, expected) result = idx1 <= val expected = np.array([True, False, True, False, False, False]) tm.assert_numpy_array_equal(result, expected) result = idx1 >= val expected = np.array([False, False, True, False, True, True]) tm.assert_numpy_array_equal(result, expected) result = idx1 == val expected = np.array([False, False, True, False, False, False]) tm.assert_numpy_array_equal(result, expected) result = idx1 != val expected = np.array([True, True, False, True, True, True]) tm.assert_numpy_array_equal(result, expected)
def test_should_return_credentials_path_set_by_env_var(): env = {'PANDAS_GBQ_CREDENTIALS_FILE': '/tmp/dummy.dat'} with mock.patch.dict('os.environ', env): assert gbq._get_credentials_file() == '/tmp/dummy.dat' @pytest.mark.parametrize( ('input', 'type_', 'expected'), [ (1, 'INTEGER', int(1)), (1, 'FLOAT', float(1)), pytest.param('false', 'BOOLEAN', False, marks=pytest.mark.xfail), pytest.param( '0e9', 'TIMESTAMP', np_datetime64_compat('1970-01-01T00:00:00Z'), marks=pytest.mark.xfail), ('STRING', 'STRING', 'STRING'), ]) def test_should_return_bigquery_correctly_typed( input, type_, expected): result = gbq._parse_data( dict(fields=[dict(name='x', type=type_, mode='NULLABLE')]), rows=[[input]]).iloc[0, 0] assert result == expected def test_to_gbq_should_fail_if_invalid_table_name_passed(): with pytest.raises(gbq.NotFoundException): gbq.to_gbq(DataFrame(), 'invalid_table_name', project_id="1234")
def test_should_return_credentials_path_set_by_env_var(): env = {"PANDAS_GBQ_CREDENTIALS_FILE": "/tmp/dummy.dat"} with mock.patch.dict("os.environ", env): assert gbq._get_credentials_file() == "/tmp/dummy.dat" @pytest.mark.parametrize( ("input", "type_", "expected"), [ (1, "INTEGER", int(1)), (1, "FLOAT", float(1)), pytest.param("false", "BOOLEAN", False, marks=pytest.mark.xfail), pytest.param( "0e9", "TIMESTAMP", np_datetime64_compat("1970-01-01T00:00:00Z"), marks=pytest.mark.xfail, ), ("STRING", "STRING", "STRING"), ], ) def test_should_return_bigquery_correctly_typed(input, type_, expected): result = gbq._parse_data( dict(fields=[dict(name="x", type=type_, mode="NULLABLE")]), rows=[[input]], ).iloc[0, 0] assert result == expected def test_to_gbq_should_fail_if_invalid_table_name_passed(): with pytest.raises(gbq.NotFoundException):
def test_should_return_bigquery_timestamps_as_numpy_datetime(self): result = gbq._parse_entry('0e9', 'TIMESTAMP') tm.assert_equal(result, np_datetime64_compat('1970-01-01T00:00:00Z'))
def test_dti_cmp_nat_behaves_like_float_cmp_nan(self): fidx1 = pd.Index([1.0, np.nan, 3.0, np.nan, 5.0, 7.0]) fidx2 = pd.Index([2.0, 3.0, np.nan, np.nan, 6.0, 7.0]) didx1 = pd.DatetimeIndex(['2014-01-01', pd.NaT, '2014-03-01', pd.NaT, '2014-05-01', '2014-07-01']) didx2 = pd.DatetimeIndex(['2014-02-01', '2014-03-01', pd.NaT, pd.NaT, '2014-06-01', '2014-07-01']) darr = np.array([np_datetime64_compat('2014-02-01 00:00Z'), np_datetime64_compat('2014-03-01 00:00Z'), np_datetime64_compat('nat'), np.datetime64('nat'), np_datetime64_compat('2014-06-01 00:00Z'), np_datetime64_compat('2014-07-01 00:00Z')]) cases = [(fidx1, fidx2), (didx1, didx2), (didx1, darr)] # Check pd.NaT is handles as the same as np.nan with tm.assert_produces_warning(None): for idx1, idx2 in cases: result = idx1 < idx2 expected = np.array([True, False, False, False, True, False]) tm.assert_numpy_array_equal(result, expected) result = idx2 > idx1 expected = np.array([True, False, False, False, True, False]) tm.assert_numpy_array_equal(result, expected) result = idx1 <= idx2 expected = np.array([True, False, False, False, True, True]) tm.assert_numpy_array_equal(result, expected) result = idx2 >= idx1 expected = np.array([True, False, False, False, True, True]) tm.assert_numpy_array_equal(result, expected) result = idx1 == idx2 expected = np.array([False, False, False, False, False, True]) tm.assert_numpy_array_equal(result, expected) result = idx1 != idx2 expected = np.array([True, True, True, True, True, False]) tm.assert_numpy_array_equal(result, expected) with tm.assert_produces_warning(None): for idx1, val in [(fidx1, np.nan), (didx1, pd.NaT)]: result = idx1 < val expected = np.array([False, False, False, False, False, False]) tm.assert_numpy_array_equal(result, expected) result = idx1 > val tm.assert_numpy_array_equal(result, expected) result = idx1 <= val tm.assert_numpy_array_equal(result, expected) result = idx1 >= val tm.assert_numpy_array_equal(result, expected) result = idx1 == val tm.assert_numpy_array_equal(result, expected) result = idx1 != val expected = np.array([True, True, True, True, True, True]) tm.assert_numpy_array_equal(result, expected) # Check pd.NaT is handles as the same as np.nan with tm.assert_produces_warning(None): for idx1, val in [(fidx1, 3), (didx1, datetime(2014, 3, 1))]: result = idx1 < val expected = np.array([True, False, False, False, False, False]) tm.assert_numpy_array_equal(result, expected) result = idx1 > val expected = np.array([False, False, False, False, True, True]) tm.assert_numpy_array_equal(result, expected) result = idx1 <= val expected = np.array([True, False, True, False, False, False]) tm.assert_numpy_array_equal(result, expected) result = idx1 >= val expected = np.array([False, False, True, False, True, True]) tm.assert_numpy_array_equal(result, expected) result = idx1 == val expected = np.array([False, False, True, False, False, False]) tm.assert_numpy_array_equal(result, expected) result = idx1 != val expected = np.array([True, True, False, True, True, True]) tm.assert_numpy_array_equal(result, expected)
class TestCommon(Base): # exected value created by Base._get_offset # are applied to 2011/01/01 09:00 (Saturday) # used for .apply and .rollforward expecteds = { "Day": Timestamp("2011-01-02 09:00:00"), "DateOffset": Timestamp("2011-01-02 09:00:00"), "BusinessDay": Timestamp("2011-01-03 09:00:00"), "CustomBusinessDay": Timestamp("2011-01-03 09:00:00"), "CustomBusinessMonthEnd": Timestamp("2011-01-31 09:00:00"), "CustomBusinessMonthBegin": Timestamp("2011-01-03 09:00:00"), "MonthBegin": Timestamp("2011-02-01 09:00:00"), "BusinessMonthBegin": Timestamp("2011-01-03 09:00:00"), "MonthEnd": Timestamp("2011-01-31 09:00:00"), "SemiMonthEnd": Timestamp("2011-01-15 09:00:00"), "SemiMonthBegin": Timestamp("2011-01-15 09:00:00"), "BusinessMonthEnd": Timestamp("2011-01-31 09:00:00"), "YearBegin": Timestamp("2012-01-01 09:00:00"), "BYearBegin": Timestamp("2011-01-03 09:00:00"), "YearEnd": Timestamp("2011-12-31 09:00:00"), "BYearEnd": Timestamp("2011-12-30 09:00:00"), "QuarterBegin": Timestamp("2011-03-01 09:00:00"), "BQuarterBegin": Timestamp("2011-03-01 09:00:00"), "QuarterEnd": Timestamp("2011-03-31 09:00:00"), "BQuarterEnd": Timestamp("2011-03-31 09:00:00"), "BusinessHour": Timestamp("2011-01-03 10:00:00"), "CustomBusinessHour": Timestamp("2011-01-03 10:00:00"), "WeekOfMonth": Timestamp("2011-01-08 09:00:00"), "LastWeekOfMonth": Timestamp("2011-01-29 09:00:00"), "FY5253Quarter": Timestamp("2011-01-25 09:00:00"), "FY5253": Timestamp("2011-01-25 09:00:00"), "Week": Timestamp("2011-01-08 09:00:00"), "Easter": Timestamp("2011-04-24 09:00:00"), "Hour": Timestamp("2011-01-01 10:00:00"), "Minute": Timestamp("2011-01-01 09:01:00"), "Second": Timestamp("2011-01-01 09:00:01"), "Milli": Timestamp("2011-01-01 09:00:00.001000"), "Micro": Timestamp("2011-01-01 09:00:00.000001"), "Nano": Timestamp(np_datetime64_compat("2011-01-01T09:00:00.000000001Z")), } def test_immutable(self, offset_types): # GH#21341 check that __setattr__ raises offset = self._get_offset(offset_types) msg = "objects is not writable|DateOffset objects are immutable" with pytest.raises(AttributeError, match=msg): offset.normalize = True with pytest.raises(AttributeError, match=msg): offset.n = 91 def test_return_type(self, offset_types): offset = self._get_offset(offset_types) # make sure that we are returning a Timestamp result = Timestamp("20080101") + offset assert isinstance(result, Timestamp) # make sure that we are returning NaT assert NaT + offset is NaT assert offset + NaT is NaT assert NaT - offset is NaT assert (-offset).apply(NaT) is NaT def test_offset_n(self, offset_types): offset = self._get_offset(offset_types) assert offset.n == 1 neg_offset = offset * -1 assert neg_offset.n == -1 mul_offset = offset * 3 assert mul_offset.n == 3 def test_offset_timedelta64_arg(self, offset_types): # check that offset._validate_n raises TypeError on a timedelt64 # object off = self._get_offset(offset_types) td64 = np.timedelta64(4567, "s") with pytest.raises(TypeError, match="argument must be an integer"): type(off)(n=td64, **off.kwds) def test_offset_mul_ndarray(self, offset_types): off = self._get_offset(offset_types) expected = np.array([[off, off * 2], [off * 3, off * 4]]) result = np.array([[1, 2], [3, 4]]) * off tm.assert_numpy_array_equal(result, expected) result = off * np.array([[1, 2], [3, 4]]) tm.assert_numpy_array_equal(result, expected) def test_offset_freqstr(self, offset_types): offset = self._get_offset(offset_types) freqstr = offset.freqstr if freqstr not in ("<Easter>", "<DateOffset: days=1>", "LWOM-SAT"): code = _get_offset(freqstr) assert offset.rule_code == code def _check_offsetfunc_works(self, offset, funcname, dt, expected, normalize=False): if normalize and issubclass(offset, Tick): # normalize=True disallowed for Tick subclasses GH#21427 return offset_s = self._get_offset(offset, normalize=normalize) func = getattr(offset_s, funcname) result = func(dt) assert isinstance(result, Timestamp) assert result == expected result = func(Timestamp(dt)) assert isinstance(result, Timestamp) assert result == expected # see gh-14101 exp_warning = None ts = Timestamp(dt) + Nano(5) if ( type(offset_s).__name__ == "DateOffset" and (funcname == "apply" or normalize) and ts.nanosecond > 0 ): exp_warning = UserWarning # test nanosecond is preserved with tm.assert_produces_warning(exp_warning, check_stacklevel=False): result = func(ts) assert isinstance(result, Timestamp) if normalize is False: assert result == expected + Nano(5) else: assert result == expected if isinstance(dt, np.datetime64): # test tz when input is datetime or Timestamp return for tz in self.timezones: expected_localize = expected.tz_localize(tz) tz_obj = timezones.maybe_get_tz(tz) dt_tz = conversion.localize_pydatetime(dt, tz_obj) result = func(dt_tz) assert isinstance(result, Timestamp) assert result == expected_localize result = func(Timestamp(dt, tz=tz)) assert isinstance(result, Timestamp) assert result == expected_localize # see gh-14101 exp_warning = None ts = Timestamp(dt, tz=tz) + Nano(5) if ( type(offset_s).__name__ == "DateOffset" and (funcname == "apply" or normalize) and ts.nanosecond > 0 ): exp_warning = UserWarning # test nanosecond is preserved with tm.assert_produces_warning(exp_warning, check_stacklevel=False): result = func(ts) assert isinstance(result, Timestamp) if normalize is False: assert result == expected_localize + Nano(5) else: assert result == expected_localize def test_apply(self, offset_types): sdt = datetime(2011, 1, 1, 9, 0) ndt = np_datetime64_compat("2011-01-01 09:00Z") for dt in [sdt, ndt]: expected = self.expecteds[offset_types.__name__] self._check_offsetfunc_works(offset_types, "apply", dt, expected) expected = Timestamp(expected.date()) self._check_offsetfunc_works( offset_types, "apply", dt, expected, normalize=True ) def test_rollforward(self, offset_types): expecteds = self.expecteds.copy() # result will not be changed if the target is on the offset no_changes = [ "Day", "MonthBegin", "SemiMonthBegin", "YearBegin", "Week", "Hour", "Minute", "Second", "Milli", "Micro", "Nano", "DateOffset", ] for n in no_changes: expecteds[n] = Timestamp("2011/01/01 09:00") expecteds["BusinessHour"] = Timestamp("2011-01-03 09:00:00") expecteds["CustomBusinessHour"] = Timestamp("2011-01-03 09:00:00") # but be changed when normalize=True norm_expected = expecteds.copy() for k in norm_expected: norm_expected[k] = Timestamp(norm_expected[k].date()) normalized = { "Day": Timestamp("2011-01-02 00:00:00"), "DateOffset": Timestamp("2011-01-02 00:00:00"), "MonthBegin": Timestamp("2011-02-01 00:00:00"), "SemiMonthBegin": Timestamp("2011-01-15 00:00:00"), "YearBegin": Timestamp("2012-01-01 00:00:00"), "Week": Timestamp("2011-01-08 00:00:00"), "Hour": Timestamp("2011-01-01 00:00:00"), "Minute": Timestamp("2011-01-01 00:00:00"), "Second": Timestamp("2011-01-01 00:00:00"), "Milli": Timestamp("2011-01-01 00:00:00"), "Micro": Timestamp("2011-01-01 00:00:00"), } norm_expected.update(normalized) sdt = datetime(2011, 1, 1, 9, 0) ndt = np_datetime64_compat("2011-01-01 09:00Z") for dt in [sdt, ndt]: expected = expecteds[offset_types.__name__] self._check_offsetfunc_works(offset_types, "rollforward", dt, expected) expected = norm_expected[offset_types.__name__] self._check_offsetfunc_works( offset_types, "rollforward", dt, expected, normalize=True ) def test_rollback(self, offset_types): expecteds = { "BusinessDay": Timestamp("2010-12-31 09:00:00"), "CustomBusinessDay": Timestamp("2010-12-31 09:00:00"), "CustomBusinessMonthEnd": Timestamp("2010-12-31 09:00:00"), "CustomBusinessMonthBegin": Timestamp("2010-12-01 09:00:00"), "BusinessMonthBegin": Timestamp("2010-12-01 09:00:00"), "MonthEnd": Timestamp("2010-12-31 09:00:00"), "SemiMonthEnd": Timestamp("2010-12-31 09:00:00"), "BusinessMonthEnd": Timestamp("2010-12-31 09:00:00"), "BYearBegin": Timestamp("2010-01-01 09:00:00"), "YearEnd": Timestamp("2010-12-31 09:00:00"), "BYearEnd": Timestamp("2010-12-31 09:00:00"), "QuarterBegin": Timestamp("2010-12-01 09:00:00"), "BQuarterBegin": Timestamp("2010-12-01 09:00:00"), "QuarterEnd": Timestamp("2010-12-31 09:00:00"), "BQuarterEnd": Timestamp("2010-12-31 09:00:00"), "BusinessHour": Timestamp("2010-12-31 17:00:00"), "CustomBusinessHour": Timestamp("2010-12-31 17:00:00"), "WeekOfMonth": Timestamp("2010-12-11 09:00:00"), "LastWeekOfMonth": Timestamp("2010-12-25 09:00:00"), "FY5253Quarter": Timestamp("2010-10-26 09:00:00"), "FY5253": Timestamp("2010-01-26 09:00:00"), "Easter": Timestamp("2010-04-04 09:00:00"), } # result will not be changed if the target is on the offset for n in [ "Day", "MonthBegin", "SemiMonthBegin", "YearBegin", "Week", "Hour", "Minute", "Second", "Milli", "Micro", "Nano", "DateOffset", ]: expecteds[n] = Timestamp("2011/01/01 09:00") # but be changed when normalize=True norm_expected = expecteds.copy() for k in norm_expected: norm_expected[k] = Timestamp(norm_expected[k].date()) normalized = { "Day": Timestamp("2010-12-31 00:00:00"), "DateOffset": Timestamp("2010-12-31 00:00:00"), "MonthBegin": Timestamp("2010-12-01 00:00:00"), "SemiMonthBegin": Timestamp("2010-12-15 00:00:00"), "YearBegin": Timestamp("2010-01-01 00:00:00"), "Week": Timestamp("2010-12-25 00:00:00"), "Hour": Timestamp("2011-01-01 00:00:00"), "Minute": Timestamp("2011-01-01 00:00:00"), "Second": Timestamp("2011-01-01 00:00:00"), "Milli": Timestamp("2011-01-01 00:00:00"), "Micro": Timestamp("2011-01-01 00:00:00"), } norm_expected.update(normalized) sdt = datetime(2011, 1, 1, 9, 0) ndt = np_datetime64_compat("2011-01-01 09:00Z") for dt in [sdt, ndt]: expected = expecteds[offset_types.__name__] self._check_offsetfunc_works(offset_types, "rollback", dt, expected) expected = norm_expected[offset_types.__name__] self._check_offsetfunc_works( offset_types, "rollback", dt, expected, normalize=True ) def test_is_on_offset(self, offset_types): dt = self.expecteds[offset_types.__name__] offset_s = self._get_offset(offset_types) assert offset_s.is_on_offset(dt) # when normalize=True, is_on_offset checks time is 00:00:00 if issubclass(offset_types, Tick): # normalize=True disallowed for Tick subclasses GH#21427 return offset_n = self._get_offset(offset_types, normalize=True) assert not offset_n.is_on_offset(dt) if offset_types in (BusinessHour, CustomBusinessHour): # In default BusinessHour (9:00-17:00), normalized time # cannot be in business hour range return date = datetime(dt.year, dt.month, dt.day) assert offset_n.is_on_offset(date) def test_add(self, offset_types, tz_naive_fixture): tz = tz_naive_fixture dt = datetime(2011, 1, 1, 9, 0) offset_s = self._get_offset(offset_types) expected = self.expecteds[offset_types.__name__] result_dt = dt + offset_s result_ts = Timestamp(dt) + offset_s for result in [result_dt, result_ts]: assert isinstance(result, Timestamp) assert result == expected expected_localize = expected.tz_localize(tz) result = Timestamp(dt, tz=tz) + offset_s assert isinstance(result, Timestamp) assert result == expected_localize # normalize=True, disallowed for Tick subclasses GH#21427 if issubclass(offset_types, Tick): return offset_s = self._get_offset(offset_types, normalize=True) expected = Timestamp(expected.date()) result_dt = dt + offset_s result_ts = Timestamp(dt) + offset_s for result in [result_dt, result_ts]: assert isinstance(result, Timestamp) assert result == expected expected_localize = expected.tz_localize(tz) result = Timestamp(dt, tz=tz) + offset_s assert isinstance(result, Timestamp) assert result == expected_localize def test_add_empty_datetimeindex(self, offset_types, tz_naive_fixture): # GH#12724, GH#30336 offset_s = self._get_offset(offset_types) dti = DatetimeIndex([], tz=tz_naive_fixture) warn = None if isinstance( offset_s, ( Easter, WeekOfMonth, LastWeekOfMonth, CustomBusinessDay, BusinessHour, CustomBusinessHour, CustomBusinessMonthBegin, CustomBusinessMonthEnd, FY5253, FY5253Quarter, ), ): # We don't have an optimized apply_index warn = PerformanceWarning with tm.assert_produces_warning(warn): result = dti + offset_s tm.assert_index_equal(result, dti) with tm.assert_produces_warning(warn): result = offset_s + dti tm.assert_index_equal(result, dti) dta = dti._data with tm.assert_produces_warning(warn): result = dta + offset_s tm.assert_equal(result, dta) with tm.assert_produces_warning(warn): result = offset_s + dta tm.assert_equal(result, dta) def test_pickle_roundtrip(self, offset_types): off = self._get_offset(offset_types) res = tm.round_trip_pickle(off) assert off == res if type(off) is not DateOffset: for attr in off._attributes: if attr == "calendar": # np.busdaycalendar __eq__ will return False; # we check holidays and weekmask attrs so are OK continue # Make sure nothings got lost from _params (which __eq__) is based on assert getattr(off, attr) == getattr(res, attr) def test_pickle_dateoffset_odd_inputs(self): # GH#34511 off = DateOffset(months=12) res = tm.round_trip_pickle(off) assert off == res base_dt = datetime(2020, 1, 1) assert base_dt + off == base_dt + res def test_onOffset_deprecated(self, offset_types): # GH#30340 use idiomatic naming off = self._get_offset(offset_types) ts = Timestamp.now() with tm.assert_produces_warning(FutureWarning): result = off.onOffset(ts) expected = off.is_on_offset(ts) assert result == expected def test_isAnchored_deprecated(self, offset_types): # GH#30340 use idiomatic naming off = self._get_offset(offset_types) with tm.assert_produces_warning(FutureWarning): result = off.isAnchored() expected = off.is_anchored() assert result == expected def test_offsets_hashable(self, offset_types): # GH: 37267 off = self._get_offset(offset_types) assert hash(off) is not None
def test_rollback(self, offset_types): expecteds = { "BusinessDay": Timestamp("2010-12-31 09:00:00"), "CustomBusinessDay": Timestamp("2010-12-31 09:00:00"), "CustomBusinessMonthEnd": Timestamp("2010-12-31 09:00:00"), "CustomBusinessMonthBegin": Timestamp("2010-12-01 09:00:00"), "BusinessMonthBegin": Timestamp("2010-12-01 09:00:00"), "MonthEnd": Timestamp("2010-12-31 09:00:00"), "SemiMonthEnd": Timestamp("2010-12-31 09:00:00"), "BusinessMonthEnd": Timestamp("2010-12-31 09:00:00"), "BYearBegin": Timestamp("2010-01-01 09:00:00"), "YearEnd": Timestamp("2010-12-31 09:00:00"), "BYearEnd": Timestamp("2010-12-31 09:00:00"), "QuarterBegin": Timestamp("2010-12-01 09:00:00"), "BQuarterBegin": Timestamp("2010-12-01 09:00:00"), "QuarterEnd": Timestamp("2010-12-31 09:00:00"), "BQuarterEnd": Timestamp("2010-12-31 09:00:00"), "BusinessHour": Timestamp("2010-12-31 17:00:00"), "CustomBusinessHour": Timestamp("2010-12-31 17:00:00"), "WeekOfMonth": Timestamp("2010-12-11 09:00:00"), "LastWeekOfMonth": Timestamp("2010-12-25 09:00:00"), "FY5253Quarter": Timestamp("2010-10-26 09:00:00"), "FY5253": Timestamp("2010-01-26 09:00:00"), "Easter": Timestamp("2010-04-04 09:00:00"), } # result will not be changed if the target is on the offset for n in [ "Day", "MonthBegin", "SemiMonthBegin", "YearBegin", "Week", "Hour", "Minute", "Second", "Milli", "Micro", "Nano", "DateOffset", ]: expecteds[n] = Timestamp("2011/01/01 09:00") # but be changed when normalize=True norm_expected = expecteds.copy() for k in norm_expected: norm_expected[k] = Timestamp(norm_expected[k].date()) normalized = { "Day": Timestamp("2010-12-31 00:00:00"), "DateOffset": Timestamp("2010-12-31 00:00:00"), "MonthBegin": Timestamp("2010-12-01 00:00:00"), "SemiMonthBegin": Timestamp("2010-12-15 00:00:00"), "YearBegin": Timestamp("2010-01-01 00:00:00"), "Week": Timestamp("2010-12-25 00:00:00"), "Hour": Timestamp("2011-01-01 00:00:00"), "Minute": Timestamp("2011-01-01 00:00:00"), "Second": Timestamp("2011-01-01 00:00:00"), "Milli": Timestamp("2011-01-01 00:00:00"), "Micro": Timestamp("2011-01-01 00:00:00"), } norm_expected.update(normalized) sdt = datetime(2011, 1, 1, 9, 0) ndt = np_datetime64_compat("2011-01-01 09:00Z") for dt in [sdt, ndt]: expected = expecteds[offset_types.__name__] self._check_offsetfunc_works(offset_types, "rollback", dt, expected) expected = norm_expected[offset_types.__name__] self._check_offsetfunc_works( offset_types, "rollback", dt, expected, normalize=True )