# Path: Month.py
# class Month(object):
#     def __init__(self, value1, value2=None):
#         '''Constructors
#            Month('200703')
#            Month(200703)
#            Month(2007, 3)
#            Month(datetime.date)
#         '''
# 
#         if value2 is None:
#             if isinstance(value1, str):
#                 self.year = int(value1[:4])
#                 self.month = int(value1[4:])
#             elif isinstance(value1, int):
#                 self.month = value1 % 100
#                 self.year = (value1 - self.month) / 100
#             elif isinstance(value1, Month):
#                 self.month = value1.month
#                 self.year = value1.year
#             elif isinstance(value1, datetime.date):
#                 self.month = value1.month
#                 self.year = value1.year
#             else:
#                 print 'construction error: value1 is of type %s' % type(value1), value1
#         else:
#             self.year = int(value1)
#             self.month = int(value2)
# 
#         # enforce invariant (other methods depend on this)
#         assert self.year > 0, self
#         assert 1 <= self.month <= 12, self
# 
#     def __repr__(self):
#         return 'Month(year=%d, month=%d)' % (self.year, self.month)
# 
#     def increment(self, by=1):
#         'return new Month one month after self'
#         assert by >= 0, by
#         month = self.month + by
#         if month > 12:
#             delta_years = month // 12
#             month = month - 12 * delta_years
#             year = self.year + delta_years
#             return Month(year, month)
#         else:
#             return Month(self.year, month)
# 
#     def decrement(self, by=1):
#         'return new Month one month before self'
#         assert by >= 0, by
#         month = self.month - by
#         year = self.year
#         while month <= 0:
#             month += 12
#             year -= 1
#         return Month(year, month)
# 
#     def as_str(self):
#         return '%04d%02d' % (self.year, self.month)
# 
#     def as_int(self):
#         return self.year * 100 + self.month
# 
#     def equal(self, other):
#         return self.year == other.year and self.month == other.month
# 
#     def __eq__(self, other):
#         result = self.year == other.year and self.month == other.month
#         return result
# 
#     def __hash__(self):
#         return hash((self.year, self.month))  # hash of tuple of properties


import numpy as np
import pandas as pd
import pdb
import unittest
from pprint import pprint
from Month import Month
    test_month = test_time_period.as_int()

    test_mask = trade_month == test_month
    test_df = samples[test_mask]

    first_train_month = Month(test_time_period).decrement(train_n_months_back)
    train_mask = (first_train_month.as_int() <= trade_month) & (trade_month < test_month)
    train_df = samples[train_mask]

    assert len(test_df) > 0
    assert len(train_df) > 0

    return test_df, train_df


class Test_make_test_train(unittest.TestCase):
    def test_1(self):
        def vp(x):
            if False:
                pprint(x)
        yyyymm = 'trade_month'
        x = 'x'
        samples = pd.DataFrame([
            {yyyymm: 200702, x: 0},
            {yyyymm: 200701, x: 1},
            {yyyymm: 200612, x: 2},
            {yyyymm: 200611, x: 3},
            {yyyymm: 200610, x: 4},
        ])
        vp(samples)

# Path: Month.py
# class Month(object):
#     def __init__(self, value1, value2=None):
#         '''Constructors
#            Month('200703')
#            Month(200703)
#            Month(2007, 3)
#            Month(datetime.date)
#         '''
# 
#         if value2 is None:
#             if isinstance(value1, str):
#                 self.year = int(value1[:4])
#                 self.month = int(value1[4:])
#             elif isinstance(value1, int):
#                 self.month = value1 % 100
#                 self.year = (value1 - self.month) / 100
#             elif isinstance(value1, Month):
#                 self.month = value1.month
#                 self.year = value1.year
#             elif isinstance(value1, datetime.date):
#                 self.month = value1.month
#                 self.year = value1.year
#             else:
#                 print 'construction error: value1 is of type %s' % type(value1), value1
#         else:
#             self.year = int(value1)
#             self.month = int(value2)
# 
#         # enforce invariant (other methods depend on this)
#         assert self.year > 0, self
#         assert 1 <= self.month <= 12, self
# 
#     def __repr__(self):
#         return 'Month(year=%d, month=%d)' % (self.year, self.month)
# 
#     def increment(self, by=1):
#         'return new Month one month after self'
#         assert by >= 0, by
#         month = self.month + by
#         if month > 12:
#             delta_years = month // 12
#             month = month - 12 * delta_years
#             year = self.year + delta_years
#             return Month(year, month)
#         else:
#             return Month(self.year, month)
# 
#     def decrement(self, by=1):
#         'return new Month one month before self'
#         assert by >= 0, by
#         month = self.month - by
#         year = self.year
#         while month <= 0:
#             month += 12
#             year -= 1
#         return Month(year, month)
# 
#     def as_str(self):
#         return '%04d%02d' % (self.year, self.month)
# 
#     def as_int(self):
#         return self.year * 100 + self.month
# 
#     def equal(self, other):
#         return self.year == other.year and self.month == other.month
# 
#     def __eq__(self, other):
#         result = self.year == other.year and self.month == other.month
#         return result
# 
#     def __hash__(self):
#         return hash((self.year, self.month))  # hash of tuple of properties


# Path: make_test_train.py
import numpy as np
import pandas as pd
import pdb
import unittest
from pprint import pprint
from Month import Month

    test_month = test_time_period.as_int()

    test_mask = trade_month == test_month
    test_df = samples[test_mask]

    first_train_month = Month(test_time_period).decrement(train_n_months_back)
    train_mask = (first_train_month.as_int() <= trade_month) & (trade_month < test_month)
    train_df = samples[train_mask]

    assert len(test_df) > 0
    assert len(train_df) > 0

    return test_df, train_df


class Test_make_test_train(unittest.TestCase):
    def test_1(self):
        def vp(x):
            if False:
                pprint(x)
        yyyymm = 'trade_month'
        x = 'x'
        samples = pd.DataFrame([
            {yyyymm: 200702, x: 0},
            {yyyymm: 200701, x: 1},
            {yyyymm: 200612, x: 2},
            {yyyymm: 200611, x: 3},
            {yyyymm: 200610, x: 4},
        ])
        vp(samples)

        test, train = make_test_train(Month(200702), 1, yyyymm, samples)