def m_offset(self): if self._prefix.endswith("S"): # MonthBegin moff = MonthBegin(n=1, normalize=False) else: # MonthEnd moff = MonthEnd(n=1, normalize=False) return moff
def test_day_of_month(self): dt = datetime(2007, 1, 1) offset = MonthEnd() result = dt + offset assert result == Timestamp(2007, 1, 31) result = result + offset assert result == Timestamp(2007, 2, 28)
import pandas as pd from pandas._libs.tslibs.offsets import MonthEnd columns = ['year', 'month', 'day', 'conc'] data = [[2014, 1, 1, 2.0], [2014, 1, 1, 4.0], [2014, 1, 2, 6.0], [2014, 1, 2, 8.0], [2014, 2, 1, 2.0], [2014, 2, 1, 6.0], [2014, 2, 2, 10.0], [2014, 2, 2, 14.0]] df = pd.DataFrame(data, columns=columns) d_avg = df.groupby(['year', 'month', 'day'], as_index=False)['conc'].mean() m_avg = d_avg.groupby(['year', 'month'], as_index=False)['conc'].mean() m_avg['datetime'] = pd.to_datetime(m_avg.year.astype(str) + m_avg.month.astype(str), format='%Y%m') + MonthEnd(1) # m_std = d_avg.groupby(['year', 'month'], as_index=False)['conc'].agg(np.std) m_std = d_avg.groupby(['year', 'month'], as_index=False)['conc'].std() print(f'Concentrations:\n{df}\n') print(f'Daily Average:\n{d_avg}\n') print(f'Monthly Average:\n{m_avg}\n') print(f'Standard Deviation:\n{m_std}\n')
def test_error_with_zero_monthends(self): msg = r"Offset <0 \* MonthEnds> did not increment date" with pytest.raises(ValueError, match=msg): date_range("1/1/2000", "1/1/2001", freq=MonthEnd(0))
def test_normalize(self): dt = datetime(2007, 1, 1, 3) result = dt + MonthEnd(normalize=True) expected = dt.replace(hour=0) + MonthEnd() assert result == expected
class TestMonthEnd(Base): _offset: type[MonthOffset] = MonthEnd def test_day_of_month(self): dt = datetime(2007, 1, 1) offset = MonthEnd() result = dt + offset assert result == Timestamp(2007, 1, 31) result = result + offset assert result == Timestamp(2007, 2, 28) def test_normalize(self): dt = datetime(2007, 1, 1, 3) result = dt + MonthEnd(normalize=True) expected = dt.replace(hour=0) + MonthEnd() assert result == expected offset_cases = [] offset_cases.append( ( MonthEnd(), { datetime(2008, 1, 1): datetime(2008, 1, 31), datetime(2008, 1, 31): datetime(2008, 2, 29), datetime(2006, 12, 29): datetime(2006, 12, 31), datetime(2006, 12, 31): datetime(2007, 1, 31), datetime(2007, 1, 1): datetime(2007, 1, 31), datetime(2006, 12, 1): datetime(2006, 12, 31), }, ) ) offset_cases.append( ( MonthEnd(0), { datetime(2008, 1, 1): datetime(2008, 1, 31), datetime(2008, 1, 31): datetime(2008, 1, 31), datetime(2006, 12, 29): datetime(2006, 12, 31), datetime(2006, 12, 31): datetime(2006, 12, 31), datetime(2007, 1, 1): datetime(2007, 1, 31), }, ) ) offset_cases.append( ( MonthEnd(2), { datetime(2008, 1, 1): datetime(2008, 2, 29), datetime(2008, 1, 31): datetime(2008, 3, 31), datetime(2006, 12, 29): datetime(2007, 1, 31), datetime(2006, 12, 31): datetime(2007, 2, 28), datetime(2007, 1, 1): datetime(2007, 2, 28), datetime(2006, 11, 1): datetime(2006, 12, 31), }, ) ) offset_cases.append( ( MonthEnd(-1), { datetime(2007, 1, 1): datetime(2006, 12, 31), datetime(2008, 6, 30): datetime(2008, 5, 31), datetime(2008, 12, 31): datetime(2008, 11, 30), datetime(2006, 12, 29): datetime(2006, 11, 30), datetime(2006, 12, 30): datetime(2006, 11, 30), datetime(2007, 1, 1): datetime(2006, 12, 31), }, ) ) @pytest.mark.parametrize("case", offset_cases) def test_offset(self, case): offset, cases = case for base, expected in cases.items(): assert_offset_equal(offset, base, expected) on_offset_cases = [ (MonthEnd(), datetime(2007, 12, 31), True), (MonthEnd(), datetime(2008, 1, 1), False), ] @pytest.mark.parametrize("case", on_offset_cases) def test_is_on_offset(self, case): offset, dt, expected = case assert_is_on_offset(offset, dt, expected)
@pytest.mark.xfail(strict=False, reason="'Africa/Kinshasa' test case fails under pytz=2017.3" ) @pytest.mark.parametrize( "original_dt, target_dt, offset, tz", [ ( Timestamp("1900-01-01"), Timestamp("1905-07-01"), MonthBegin(66), "Africa/Kinshasa", ), # GH41906 ( Timestamp("2021-10-01 01:15"), Timestamp("2021-10-31 01:15"), MonthEnd(1), "Europe/London", ), ( Timestamp("2010-12-05 02:59"), Timestamp("2010-10-31 02:59"), SemiMonthEnd(-3), "Europe/Paris", ), ( Timestamp("2021-10-31 01:20"), Timestamp("2021-11-07 01:20"), CustomBusinessDay(2, weekmask="Sun Mon"), "US/Eastern", ), (