def test_formatters(unittest): formatters = utils.JSONFormatter() formatters.add_string(0, name='str') formatters.add_int(1, name='int') formatters.add_float(2, name='float') formatters.add_date(3, name='date') formatters.add_timestamp(4, name='timestamp') formatters.add_json(5, name='json') date = pd.Timestamp('20180430').tz_localize('US/Eastern') data = [['hello', 1, 1.6666666, date, date, {'a': 1}]] unittest.assertEqual(formatters.format_dicts(data), [{ 'int': 1, 'date': '2018-04-30', 'float': 1.666667, 'str': 'hello', 'timestamp': 1525060800000, 'json': { 'a': 1 } }]) bad_data = [[None, np.nan, np.nan, np.nan, np.nan, np.nan]] unittest.assertEqual(formatters.format_dicts(bad_data), [{ 'int': '', 'date': '', 'float': '', 'str': '', 'timestamp': '', 'json': None }]) bad_data = [['hello', 'hello', 'hello', 'hello', 'hello', 'hello']] unittest.assertEqual(formatters.format_dicts(bad_data), [{ 'int': '', 'date': '', 'float': '', 'str': 'hello', 'timestamp': '', 'json': 'hello' }])
def test_formatters(unittest): formatters = utils.JSONFormatter() formatters.add_string(0, name='str') formatters.add_int(1, name='int') formatters.add_float(2, name='float') formatters.add_date(3, name='date') formatters.add_timestamp(4, name='timestamp') formatters.add_json(5, name='json') formatters.add_date(6, name='ts_date') date = pd.Timestamp('20180430').tz_localize('US/Eastern') timestamp = pd.Timestamp('20180430 12:36:44').tz_localize('US/Eastern') data = [['hello', 1, 1.6666666, date, date, {'a': 1}, timestamp]] unittest.assertEqual( formatters.format_dicts(data), [{'int': 1, 'date': '2018-04-30', 'float': 1.666667, 'str': 'hello', 'timestamp': 1525060800000, 'json': {'a': 1}, 'ts_date': '2018-04-30 12:36:44'}] ) bad_data = [[None, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]] unittest.assertEqual( formatters.format_dicts(bad_data), [{'int': '', 'date': '', 'float': '', 'str': '', 'timestamp': '', 'json': None, 'ts_date': ''}] ) bad_data = [['hello', 'hello', 'hello', 'hello', 'hello', 'hello', 'hello']] unittest.assertEqual( formatters.format_dicts(bad_data), [{'int': '', 'date': '', 'float': '', 'str': 'hello', 'timestamp': '', 'json': 'hello', 'ts_date': ''}] ) unittest.assertEqual( formatters.format_lists( pd.DataFrame(data, columns=['str', 'int', 'float', 'date', 'timestamp', 'json', 'ts_date']) ), { 'int': [1], 'timestamp': [1525075200000], 'float': [1.666667], 'ts_date': ['2018-04-30 16:36:44'], 'json': [{'a': 1}], 'str': ['hello'], 'date': ['2018-04-30 04:00:00'] } )
def test_formatters(unittest): formatters = utils.JSONFormatter() formatters.add_string(0, name="str") formatters.add_int(1, name="int") formatters.add_float(2, name="float") formatters.add_date(3, name="date") formatters.add_timestamp(4, name="timestamp") formatters.add_json(5, name="json") formatters.add_date(6, name="ts_date") date = pd.Timestamp("20180430").tz_localize("US/Eastern") timestamp = pd.Timestamp("20180430 12:36:44").tz_localize("US/Eastern") data = [["hello", 1, 1.6666666, date, date, {"a": 1}, timestamp]] unittest.assertEqual( formatters.format_dicts(data), [{ "int": 1, "date": "2018-04-30", "float": 1.666667, "str": "hello", "timestamp": 1525060800000, "json": { "a": 1 }, "ts_date": "2018-04-30 12:36:44", }], ) bad_data = [[None, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]] unittest.assertEqual( formatters.format_dicts(bad_data), [{ "int": "", "date": "", "float": "", "str": "", "timestamp": "", "json": None, "ts_date": "", }], ) bad_data = [[ "hello", "hello", "hello", "hello", "hello", "hello", "hello" ]] unittest.assertEqual( formatters.format_dicts(bad_data), [{ "int": "", "date": "", "float": "", "str": "hello", "timestamp": "", "json": "hello", "ts_date": "", }], ) unittest.assertEqual( formatters.format_lists( pd.DataFrame( data, columns=[ "str", "int", "float", "date", "timestamp", "json", "ts_date" ], )), { "int": [1], "timestamp": [1525075200000], "float": [1.666667], "ts_date": ["2018-04-30 16:36:44"], "json": [{ "a": 1 }], "str": ["hello"], "date": ["2018-04-30 04:00:00"], }, )