from .test_utils import PySparkTestCase
from sparkts.datetimeindex import *
import pandas as pd

class DateTimeIndexTestCase(PySparkTestCase):
    def test_frequencies(self):
        bd = BusinessDayFrequency(1, 1, self.sc)
        self.assertEqual(bd.days(), 1)
        
        hf = HourFrequency(4, self.sc)
        self.assertEqual(hf.hours(), 4)
    
    def test_uniform(self):
        freq = DayFrequency(3, self.sc)
        self.assertEqual(freq.days(), 3)
        start = '2015-04-10'
        index = uniform(start, periods=5, freq=freq, sc=self.sc)
        index2 = uniform(start, end='2015-04-22', freq=freq, sc=self.sc)
        self.assertEqual(index, index2)
        
        self.assertEqual(len(index), 5)
        self.assertEqual(index.first(), pd.to_datetime('2015-04-10'))
        self.assertEqual(index.last(), pd.to_datetime('2015-04-22'))
        subbydate = index[pd.to_datetime('2015-04-13'):pd.to_datetime('2015-04-19')]
        subbyloc = index.islice(1, 4)
        self.assertEqual(subbydate, subbyloc)
        self.assertEqual(subbydate.first(), pd.to_datetime('2015-04-13'))
        self.assertEqual(subbydate.last(), pd.to_datetime('2015-04-19'))
        self.assertEqual(subbydate.datetime_at_loc(0), pd.to_datetime('2015-04-13'))
        self.assertEqual(subbydate[pd.to_datetime('2015-04-13')], 0)

    def test_irregular(self):
        pd_index = pd.date_range('2015-04-10', periods=5, freq='3D')
        dt_index = irregular(pd_index, self.sc)

        self.assertEqual(len(dt_index), 5)
        self.assertEqual(dt_index.first(), pd.to_datetime('2015-04-10'))
        self.assertEqual(dt_index.last(), pd.to_datetime('2015-04-22'))
        subbydate = dt_index[pd.to_datetime('2015-04-13'):pd.to_datetime('2015-04-19')]
        subbyloc = dt_index.islice(1, 4)
        self.assertEqual(subbydate, subbyloc)
        self.assertEqual(subbydate.first(), pd.to_datetime('2015-04-13'))
        self.assertEqual(subbydate.last(), pd.to_datetime('2015-04-19'))
        self.assertEqual(subbydate.datetime_at_loc(0), pd.to_datetime('2015-04-13'))
        self.assertEqual(subbydate[pd.to_datetime('2015-04-13')], 0)

        pd_index2 = dt_index.to_pandas_index()
        self.assertTrue(pd_index.equals(pd_index2), str(pd_index) + "!=" + str(pd_index2))


[((1380, 1429), 'pandas.date_range', 'pd.date_range', (['"""2015-04-10"""'], {'periods': '(5)', 'freq': '"""3D"""'}), "('2015-04-10', periods=5, freq='3D')\n", (1393, 1429), True, 'import pandas as pd\n'), ((761, 789), 'pandas.to_datetime', 'pd.to_datetime', (['"""2015-04-10"""'], {}), "('2015-04-10')\n", (775, 789), True, 'import pandas as pd\n'), ((830, 858), 'pandas.to_datetime', 'pd.to_datetime', (['"""2015-04-22"""'], {}), "('2015-04-22')\n", (844, 858), True, 'import pandas as pd\n'), ((1073, 1101), 'pandas.to_datetime', 'pd.to_datetime', (['"""2015-04-13"""'], {}), "('2015-04-13')\n", (1087, 1101), True, 'import pandas as pd\n'), ((1146, 1174), 'pandas.to_datetime', 'pd.to_datetime', (['"""2015-04-19"""'], {}), "('2015-04-19')\n", (1160, 1174), True, 'import pandas as pd\n'), ((1231, 1259), 'pandas.to_datetime', 'pd.to_datetime', (['"""2015-04-13"""'], {}), "('2015-04-13')\n", (1245, 1259), True, 'import pandas as pd\n'), ((1565, 1593), 'pandas.to_datetime', 'pd.to_datetime', (['"""2015-04-10"""'], {}), "('2015-04-10')\n", (1579, 1593), True, 'import pandas as pd\n'), ((1637, 1665), 'pandas.to_datetime', 'pd.to_datetime', (['"""2015-04-22"""'], {}), "('2015-04-22')\n", (1651, 1665), True, 'import pandas as pd\n'), ((1886, 1914), 'pandas.to_datetime', 'pd.to_datetime', (['"""2015-04-13"""'], {}), "('2015-04-13')\n", (1900, 1914), True, 'import pandas as pd\n'), ((1959, 1987), 'pandas.to_datetime', 'pd.to_datetime', (['"""2015-04-19"""'], {}), "('2015-04-19')\n", (1973, 1987), True, 'import pandas as pd\n'), ((2044, 2072), 'pandas.to_datetime', 'pd.to_datetime', (['"""2015-04-13"""'], {}), "('2015-04-13')\n", (2058, 2072), True, 'import pandas as pd\n'), ((886, 914), 'pandas.to_datetime', 'pd.to_datetime', (['"""2015-04-13"""'], {}), "('2015-04-13')\n", (900, 914), True, 'import pandas as pd\n'), ((915, 943), 'pandas.to_datetime', 'pd.to_datetime', (['"""2015-04-19"""'], {}), "('2015-04-19')\n", (929, 943), True, 'import pandas as pd\n'), ((1696, 1724), 'pandas.to_datetime', 'pd.to_datetime', (['"""2015-04-13"""'], {}), "('2015-04-13')\n", (1710, 1724), True, 'import pandas as pd\n'), ((1725, 1753), 'pandas.to_datetime', 'pd.to_datetime', (['"""2015-04-19"""'], {}), "('2015-04-19')\n", (1739, 1753), True, 'import pandas as pd\n'), ((1296, 1324), 'pandas.to_datetime', 'pd.to_datetime', (['"""2015-04-13"""'], {}), "('2015-04-13')\n", (1310, 1324), True, 'import pandas as pd\n'), ((2109, 2137), 'pandas.to_datetime', 'pd.to_datetime', (['"""2015-04-13"""'], {}), "('2015-04-13')\n", (2123, 2137), True, 'import pandas as pd\n')]