<commit_before>"""Tests suite for Period handling.

Parts derived from scikits.timeseries code, original authors:
- <NAME> & <NAME>
- pierregm_at_uga_dot_edu - mattknow_ca_at_hotmail_dot_com

"""

from unittest import TestCase
from datetime import datetime, timedelta
import unittest

from numpy.ma.testutils import assert_equal

from pandas.tseries.frequencies import MONTHS, DAYS
from pandas.tseries.period import Period, PeriodIndex, period_range
from pandas.tseries.index import DatetimeIndex, date_range
from pandas.tseries.tools import to_datetime

import pandas.core.datetools as datetools
import numpy as np
randn = np.random.randn

from pandas import Series, TimeSeries, DataFrame
from pandas.util.testing import assert_series_equal, assert_almost_equal
import pandas.util.testing as tm

class TestPeriodProperties(TestCase):
    "Test properties such as year, month, weekday, etc...."
    #
    def __init__(self, *args, **kwds):
        TestCase.__init__(self, *args, **kwds)

    def test_period_cons_quarterly(self):
        # bugs in scikits.timeseries
        for month in MONTHS:
            freq = 'Q-%s' % month
            exp = Period('1989Q3', freq=freq)
            self.assert_('1989Q3' in str(exp))
            stamp = exp.to_timestamp('D', how='end')
            p = Period(stamp, freq=freq)
            self.assertEquals(p, exp)

    def test_period_cons_annual(self):
        # bugs in scikits.timeseries
        for month in MONTHS:
            freq = 'A-%s' % month
            exp = Period('1989', freq=freq)
            stamp = exp.to_timestamp('D', how='end') + 30
            p = Period(stamp, freq=freq)
            self.assertEquals(p, exp + 1)

    def test_period_cons_weekly(self):
        for num in range(10, 17):
            daystr = '2011-02-%d' % num
            for day in DAYS:
                freq = 'W-%s' % day

                result = Period(daystr, freq=freq)
                expected = Period(daystr, freq='D').asfreq(freq)
                self.assertEquals(result, expected)

    def test_period_constructor(self):
        i1 = Period('1/1/2005', freq='M')
        i2 = Period('Jan 2005')

        self.assertEquals(i1, i2)

        i1 = Period('2005', freq='A')
        i2 = Period('2005')
        i3 = Period('2005', freq='a')

        self.assertEquals(i1, i2)
        self.assertEquals(i1, i3)

        i4 = Period('2005', freq='M')
        i5 = Period('2005', freq='m')

        self.assert_(i1 != i4)
        self.assertEquals(i4, i5)

        i1 = Period.now('Q')
        i2 = Period(datetime.now(), freq='Q')
        i3 = Period.now('q')

        self.assertEquals(i1, i2)
        self.assertEquals(i1, i3)

        # Biz day construction, roll forward if non-weekday
        i1 = Period('3/10/12', freq='B')
        i2 = Period('3/12/12', freq='D')
        self.assertEquals(i1, i2.asfreq('B'))

        i3 = Period('3/10/12', freq='b')
        self.assertEquals(i1, i3)

        i1 = Period(year=2005, quarter=1, freq='Q')
        i2 = Period('1/1/2005', freq='Q')
        self.assertEquals(i1, i2)

        i1 = Period(year=2005, quarter=3, freq='Q')
        i2 = Period('9/1/2005', freq='Q')
        self.assertEquals(i1, i2)

        i1 = Period(year=2005, month=3, day=1, freq='D')
        i2 = Period('3/1/2005', freq='D')
        self.assertEquals(i1, i2)

        i3 = Period(year=2005, month=3, day=1, freq='d')
        self.assertEquals(i1, i3)

        i1 = Period(year=2012, month=3, day=10, freq='B')
        i2 = Period('3/12/12', freq='B')
        self.assertEquals(i1, i2)

        i1 = Period('2005Q1')
        i2 = Period(year=2005, quarter=1, freq='Q')
        i3 = Period('2005q1')
        self.assertEquals(i1, i2)
        self.assertEquals(i1, i3)

        i1 = Period('05Q1')
        self.assertEquals(i1, i2)
        lower = Period('05q1')
        self.assertEquals(i1, lower)

        i1 = Period('1Q2005')
        self.assertEquals(i1, i2)
        lower = Period('1q2005')
        self.assertEquals(i1, lower)

        i1 = Period('1Q05')
        self.assertEquals(i1, i2)
        lower = Period('1q05')
        self.assertEquals(i1, lower)

        i1 = Period('4Q1984')
        self.assertEquals(i1.year, 1984)
        lower = Period('4q1984')
        self.assertEquals(i1, lower)

        i1 = Period('1982', freq='min')
        i2 = Period('1982', freq='MIN')
        self.assertEquals(i1, i2)
        i2 = Period('1982', freq=('Min', 1))
        self.assertEquals(i1, i2)

        expected = Period('2007-01', freq='M')
        i1 = Period('200701', freq='M')
        self.assertEqual(i1, expected)

        i1 = Period('200701', freq='M')
        self.assertEqual(i1, expected)

        i1 = Period(200701, freq='M')
        self.assertEqual(i1, expected)

        i1 = Period(ordinal=200701, freq='M')
        self.assertEqual(i1.year, 18695)

        self.assertRaises(ValueError, Period, ordinal=200701)

    def test_freq_str(self):
        i1 = Period('1982', freq='Min')
        self.assert_(i1.freq[0] != '1')

    def test_repr(self):
        p = Period('Jan-2000')
        self.assert_('Jan-2000' in repr(p))

    def test_to_timestamp(self):
        p = Period('1982', freq='A')
        start_ts = p.to_timestamp(how='S')
        aliases = ['s', 'StarT', 'BEGIn']
        for a in aliases:
            self.assertEquals(start_ts, p.to_timestamp(how=a))

        end_ts = p.to_timestamp(how='E')
        aliases = ['e', 'end', 'FINIsH']
        for a in aliases:
            self.assertEquals(end_ts, p.to_timestamp(how=a))

        from_lst = ['A', 'Q', 'M', 'W', 'B',
                    'D', 'H', 'Min', 'S']
        for i, fcode in enumerate(from_lst):
            p = Period('1982', freq=fcode)<commit_msg>TST: test Period.strftime<commit_after>"""Tests suite for Period handling.

Parts derived from scikits.timeseries code, original authors:
- <NAME> & <NAME>
- pierregm_at_uga_dot_edu - mattknow_ca_at_hotmail_dot_com

"""

from unittest import TestCase
from datetime import datetime, timedelta
import unittest

from numpy.ma.testutils import assert_equal

from pandas.tseries.frequencies import MONTHS, DAYS
from pandas.tseries.period import Period, PeriodIndex, period_range
from pandas.tseries.index import DatetimeIndex, date_range
from pandas.tseries.tools import to_datetime

import pandas.core.datetools as datetools
import numpy as np
randn = np.random.randn

from pandas import Series, TimeSeries, DataFrame
from pandas.util.testing import assert_series_equal, assert_almost_equal
import pandas.util.testing as tm

class TestPeriodProperties(TestCase):
    "Test properties such as year, month, weekday, etc...."
    #
    def __init__(self, *args, **kwds):
        TestCase.__init__(self, *args, **kwds)

    def test_period_cons_quarterly(self):
        # bugs in scikits.timeseries
        for month in MONTHS:
            freq = 'Q-%s' % month
            exp = Period('1989Q3', freq=freq)
            self.assert_('1989Q3' in str(exp))
            stamp = exp.to_timestamp('D', how='end')
            p = Period(stamp, freq=freq)
            self.assertEquals(p, exp)

    def test_period_cons_annual(self):
        # bugs in scikits.timeseries
        for month in MONTHS:
            freq = 'A-%s' % month
            exp = Period('1989', freq=freq)
            stamp = exp.to_timestamp('D', how='end') + 30
            p = Period(stamp, freq=freq)
            self.assertEquals(p, exp + 1)

    def test_period_cons_weekly(self):
        for num in range(10, 17):
            daystr = '2011-02-%d' % num
            for day in DAYS:
                freq = 'W-%s' % day

                result = Period(daystr, freq=freq)
                expected = Period(daystr, freq='D').asfreq(freq)
                self.assertEquals(result, expected)

    def test_period_constructor(self):
        i1 = Period('1/1/2005', freq='M')
        i2 = Period('Jan 2005')

        self.assertEquals(i1, i2)

        i1 = Period('2005', freq='A')
        i2 = Period('2005')
        i3 = Period('2005', freq='a')

        self.assertEquals(i1, i2)
        self.assertEquals(i1, i3)

        i4 = Period('2005', freq='M')
        i5 = Period('2005', freq='m')

        self.assert_(i1 != i4)
        self.assertEquals(i4, i5)

        i1 = Period.now('Q')
        i2 = Period(datetime.now(), freq='Q')
        i3 = Period.now('q')

        self.assertEquals(i1, i2)
        self.assertEquals(i1, i3)

        # Biz day construction, roll forward if non-weekday
        i1 = Period('3/10/12', freq='B')
        i2 = Period('3/12/12', freq='D')
        self.assertEquals(i1, i2.asfreq('B'))

        i3 = Period('3/10/12', freq='b')
        self.assertEquals(i1, i3)

        i1 = Period(year=2005, quarter=1, freq='Q')
        i2 = Period('1/1/2005', freq='Q')
        self.assertEquals(i1, i2)

        i1 = Period(year=2005, quarter=3, freq='Q')
        i2 = Period('9/1/2005', freq='Q')
        self.assertEquals(i1, i2)

        i1 = Period(year=2005, month=3, day=1, freq='D')
        i2 = Period('3/1/2005', freq='D')
        self.assertEquals(i1, i2)

        i3 = Period(year=2005, month=3, day=1, freq='d')
        self.assertEquals(i1, i3)

        i1 = Period(year=2012, month=3, day=10, freq='B')
        i2 = Period('3/12/12', freq='B')
        self.assertEquals(i1, i2)

        i1 = Period('2005Q1')
        i2 = Period(year=2005, quarter=1, freq='Q')
        i3 = Period('2005q1')
        self.assertEquals(i1, i2)
        self.assertEquals(i1, i3)

        i1 = Period('05Q1')
        self.assertEquals(i1, i2)
        lower = Period('05q1')
        self.assertEquals(i1, lower)

        i1 = Period('1Q2005')
        self.assertEquals(i1, i2)
        lower = Period('1q2005')
        self.assertEquals(i1, lower)

        i1 = Period('1Q05')
        self.assertEquals(i1, i2)
        lower = Period('1q05')
        self.assertEquals(i1, lower)

        i1 = Period('4Q1984')
        self.assertEquals(i1.year, 1984)
        lower = Period('4q1984')
        self.assertEquals(i1, lower)

        i1 = Period('1982', freq='min')
        i2 = Period('1982', freq='MIN')
        self.assertEquals(i1, i2)
        i2 = Period('1982', freq=('Min', 1))
        self.assertEquals(i1, i2)

        expected = Period('2007-01', freq='M')
        i1 = Period('200701', freq='M')
        self.assertEqual(i1, expected)

        i1 = Period('200701', freq='M')
        self.assertEqual(i1, expected)

        i1 = Period(200701, freq='M')
        self.assertEqual(i1, expected)

        i1 = Period(ordinal=200701, freq='M')
        self.assertEqual(i1.year, 18695)

        self.assertRaises(ValueError, Period, ordinal=200701)

    def test_freq_str(self):
        i1 = Period('1982', freq='Min')
        self.assert_(i1.freq[0] != '1')

    def test_repr(self):
        p = Period('Jan-2000')
        self.assert_('Jan-2000' in repr(p))

    def test_strftime(self):
        p = Period('2000-1-1 12:34:12', freq='S')
        self.assert_(p.strftime('%Y-%m-%d %H:%M:%S') ==
                     '2000-01-01 12:34:12')

    def test_to_timestamp(self):
        p = Period('1982', freq='A')
        start_ts = p.to_timestamp(how='S')
        aliases = ['s', 'StarT', 'BEGIn']
        for a in aliases:
            self.assertEquals(start_ts, p.to_timestamp(how=a))

        end_ts = p.to_timestamp(how='E')
        aliases = ['e', 'end', 'FINIsH']
        for a in aliases:
            self.assertEquals(end_ts, p.to_timestamp(how=a))

        from_lst = ['A', 'Q', 'M', 'W', 'B',
                    'D', 'H', 'Min', 'S']
        for i, fcode in enumerate(from_lst):
            p = Period('1982', freq=fcode)
{'fraction_non_alphanumeric': 0.14698795180722893, 'fraction_numerical': 0.06075731497418244, 'mean_word_length': 2.345135290731146, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1768045', 'n_tokens_mistral': 4376, 'n_tokens_neox': 3925, 'n_words': 855}