python test_pandas.py
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python test_pandas.py相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from datetime import datetime
import itertools
import pandas as pd
import pprint
import pytz
import unittest
class TestPandas(unittest.TestCase):
def setUp(self):
self.f = 'cpu.csv'
self.t = '24'
self.df = pd.read_csv(self.f, header=0)
def tearDown(self):
pass
def test_len_df(self):
expected = 7445
self.assertEqual(expected, len(self.df))
def test_add_date_column(self):
expected = 9
self.df['date'] = self.df['time'].str.replace(' ..:..:..$', '')
rows = self.df['date'].value_counts()
self.assertEqual(expected, rows['2016/07/25'])
def test_less_than_24(self):
expected_indexes = ['2016/12/13', '2016/07/25']
expected_values = [20, 9]
self.df['date'] = self.df['time'].str.replace(' ..:..:..$', '')
rows = self.df['date'].value_counts()
self.assertEqual(expected_indexes,
rows.where(rows < 24).dropna().index.tolist())
self.assertEqual(expected_values,
rows.where(rows < 24).dropna().tolist())
def test_unixtime(self):
expected = 1469426400
time_jst = '2016/7/25 15:00:00'
jst_dt_naive = datetime.strptime(time_jst,
'%Y/%m/%d %H:%M:%S')
jst = pytz.timezone('Asia/Tokyo')
jst_dt = jst.localize(jst_dt_naive)
self.assertEqual(expected, int(jst_dt.timestamp()))
def test_add_rows(self):
expected = []
column_names = ['unixtime', 'time', 'date']
row1 = ('1469426400', '2016/12/13 23:59', '2016/12/13')
row2 = ('1469426400', '2016/07/25 23:59', '2016/07/25')
self.df['date'] = self.df['time'].str.replace(' ..:..:..$', '')
df_append = pd.DataFrame([row1]*4 + [row2]*15, columns=column_names)
self.df = self.df.append(df_append, ignore_index=True)
rows = self.df['date'].value_counts()
self.assertEqual(expected,
rows.where(rows < 24).dropna().index.tolist())
def test_grouper(self):
column1_names = [1, 2, 3, 4, 5, 6, 7, 8, 9]
column2_names = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
expected_1 = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
expected_2 = [(1, 2, 3), (4, 5, 6), (7, 8, 9), (10, None, None)]
num = 3
self.assertEqual(expected_1, list(self.grouper(column1_names, num)))
self.assertEqual(expected_2, list(self.grouper(column2_names, num)))
def grouper(self, seq, n, fillvalue=None):
args = [iter(seq)] * n
return itertools.zip_longest(*args, fillvalue=fillvalue)
if __name__ == '__main__':
unittest.main()
以上是关于python test_pandas.py的主要内容,如果未能解决你的问题,请参考以下文章
代写python,代写python编程,python代写,python编程代写,留学生python代写