我想写一个doctest来测试这段代码,但是还没有在网上找到任何解决方案?有什么建议吗?
def CheckSampleTime(data):
#
# Write to log file Sample Time outside of work hours if outside work ours
#
StartTime = datetime.time(06, 00, 0)
EndTime = datetime.time(17, 00, 0)
data['SampleTime'] = pd.to_datetime(data.SampleTime)
for index, row in data.iterrows():
if (row['SampleTime'].time() < StartTime) or (row['SampleTime'].time() > EndTime):
data.set_value(index,'LOG',"Sample Time outside of work hours")
logging.warning("Sample Time outside of work hours (6am-5pm)")
答案 0 :(得分:0)
更多的pandasic方式
data=pd.read_csv(StringIO('''SampleTime
2016-05-23 05:45:00
2016-05-23 06:15:00
2016-05-23 07:45:00
2016-05-23 16:15:00
2016-05-23 18:15:00'''), parse_dates=[0])
StartTime = datetime.time(06, 00, 0)
EndTime = datetime.time(17, 00, 0)
data.loc[(data['SampleTime'].dt.time < StartTime) | (data['SampleTime'].dt.time > EndTime), 'LOG'] = 'Sample Time outside of work hours'
if data['LOG'].isnull().any():
logging.warning("Sample Time outside of work hours (6am-5pm)")
答案 1 :(得分:0)
doctest模块可与Pandas配合使用。例如,在脚本中运行以下代码将成功运行doctest.testmod
:
df = pd.DataFrame(columns=['first_name', 'last_name'])
df = df.append(pd.Series({'first_name':'Rick','last_name':'Sanchez'}), ignore_index=True)
df = df.append(pd.Series({'first_name':'Stanford', 'last_name':'Pines'}), ignore_index=True)
def find_name(df, name):
""" Find a name in a Pandas DataFrame.
>>> df = pd.DataFrame(columns=['first_name', 'last_name'])
>>> df = df.append(pd.Series({'first_name':'Rick','last_name':'Sanchez'}), ignore_index=True)
>>> df = df.append(pd.Series({'first_name':'Stanford', 'last_name':'Pines'}), ignore_index=True)
>>> find_name(df, 'Stanford')
first_name last_name
1 Stanford Pines
"""
mask = np.column_stack([df[col].str.contains(f"{name}", na=False) for col in df])
print(df.loc[mask.any(axis=1)])
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
Trying:
df_test = pd.DataFrame(columns=['first_name', 'last_name'])
Expecting nothing
ok
Trying:
df_test = df_test.append(pd.Series({'first_name':'Rick','last_name':'Sanchez'}), ignore_index=True)
Expecting nothing
ok
Trying:
df_test = df_test.append(pd.Series({'first_name':'Stanford', 'last_name':'Pines'}), ignore_index=True)
Expecting nothing
ok
Trying:
find_name(df_test, 'Stanford')
Expecting:
first_name last_name
1 Stanford Pines
ok
1 items had no tests:
__main__
1 items passed all tests:
4 tests in __main__.find_name
4 tests in 2 items.
4 passed and 0 failed.
Test passed.