Can someone help me understand how I can identify differences between the actual and expected output when performing a doctest? In the below, the expected and actual output looks identical eye.
I'm pretty sure there is some white space error since when I add optionflags= doctest.NORMALIZE_WHITESPACE in the doctest, the test is successful. Is there another flag that can help a user triangulate on the location of such a whitespace error?
import datetime
import numpy as np
import pandas as pd
import yfinance as yf
def id_maribozu(df, tol = 0.0025):
'''
>>> df = yf.download('aapl', start = '2021-05-17', end = '2021-06-09', progress=False)
>>> id_maribozu(df)
Open High Low Close Adj Close Volume mbzu mbzu_stop
Date
2021-05-17 126.82 126.93 125.17 126.27 125.90 74244600 0 0.00
2021-05-18 126.56 126.99 124.78 124.85 124.48 63342900 0 0.00
2021-05-19 123.16 124.92 122.86 124.69 124.32 92612000 1 122.86
2021-05-20 125.23 127.72 125.10 127.31 126.93 76857100 0 0.00
2021-05-21 127.82 128.00 125.21 125.43 125.06 79295400 -1 128.00
2021-05-24 126.01 127.94 125.94 127.10 126.72 63092900 0 0.00
2021-05-25 127.82 128.32 126.32 126.90 126.53 72009500 0 0.00
2021-05-26 126.96 127.39 126.42 126.85 126.48 56575900 0 0.00
2021-05-27 126.44 127.64 125.08 125.28 124.91 94625600 0 0.00
2021-05-28 125.57 125.80 124.55 124.61 124.24 71311100 -1 125.80
2021-06-01 125.08 125.35 123.94 124.28 123.91 67637100 0 0.00
2021-06-02 124.28 125.24 124.05 125.06 124.69 59278900 1 124.05
2021-06-03 124.68 124.85 123.13 123.54 123.18 76229200 0 0.00
2021-06-04 124.07 126.16 123.85 125.89 125.52 75169300 1 123.85
2021-06-07 126.17 126.32 124.83 125.90 125.53 71057600 0 0.00
2021-06-08 126.60 128.46 126.21 126.74 126.37 74403800 0 0.00
'''
bull_mbzu = (abs(df['Close'] / df['High'] - 1) < 0.0025) & (abs(df['Open'] / df['Low'] - 1) < tol)
bear_mbzu = (abs(df['Open'] / df['High'] - 1) < 0.0025) & (abs(df['Close'] / df['Low'] - 1) < tol)
df['mbzu'] = np.where(bull_mbzu, 1, np.where(bear_mbzu, -1, 0))
df.loc[:,'mbzu_stop'] = np.where(bull_mbzu, df['Low'], np.where(bear_mbzu, df['High'], 0))
print(df.round(2))
if __name__ == "__main__":
import doctest
doctest.testmod()
>>> %run id_maribozu.py
**********************************************************************
File "/Volumes/Data/TechnicalAnalysis/Candlesticks/Maribozu/id_maribozu.py", line 10, in __main__.id_maribozu
Failed example:
id_maribozu(df)
Expected:
Open High Low Close Adj Close Volume mbzu mbzu_stop
Date
2021-05-17 126.82 126.93 125.17 126.27 125.90 74244600 0 0.00
2021-05-18 126.56 126.99 124.78 124.85 124.48 63342900 0 0.00
2021-05-19 123.16 124.92 122.86 124.69 124.32 92612000 1 122.86
2021-05-20 125.23 127.72 125.10 127.31 126.93 76857100 0 0.00
2021-05-21 127.82 128.00 125.21 125.43 125.06 79295400 -1 128.00
2021-05-24 126.01 127.94 125.94 127.10 126.72 63092900 0 0.00
2021-05-25 127.82 128.32 126.32 126.90 126.53 72009500 0 0.00
2021-05-26 126.96 127.39 126.42 126.85 126.48 56575900 0 0.00
2021-05-27 126.44 127.64 125.08 125.28 124.91 94625600 0 0.00
2021-05-28 125.57 125.80 124.55 124.61 124.24 71311100 -1 125.80
2021-06-01 125.08 125.35 123.94 124.28 123.91 67637100 0 0.00
2021-06-02 124.28 125.24 124.05 125.06 124.69 59278900 1 124.05
2021-06-03 124.68 124.85 123.13 123.54 123.18 76229200 0 0.00
2021-06-04 124.07 126.16 123.85 125.89 125.52 75169300 1 123.85
2021-06-07 126.17 126.32 124.83 125.90 125.53 71057600 0 0.00
2021-06-08 126.60 128.46 126.21 126.74 126.37 74403800 0 0.00
Got:
Open High Low Close Adj Close Volume mbzu mbzu_stop
Date
2021-05-17 126.82 126.93 125.17 126.27 125.90 74244600 0 0.00
2021-05-18 126.56 126.99 124.78 124.85 124.48 63342900 0 0.00
2021-05-19 123.16 124.92 122.86 124.69 124.32 92612000 1 122.86
2021-05-20 125.23 127.72 125.10 127.31 126.93 76857100 0 0.00
2021-05-21 127.82 128.00 125.21 125.43 125.06 79295400 -1 128.00
2021-05-24 126.01 127.94 125.94 127.10 126.72 63092900 0 0.00
2021-05-25 127.82 128.32 126.32 126.90 126.53 72009500 0 0.00
2021-05-26 126.96 127.39 126.42 126.85 126.48 56575900 0 0.00
2021-05-27 126.44 127.64 125.08 125.28 124.91 94625600 0 0.00
2021-05-28 125.57 125.80 124.55 124.61 124.24 71311100 -1 125.80
2021-06-01 125.08 125.35 123.94 124.28 123.91 67637100 0 0.00
2021-06-02 124.28 125.24 124.05 125.06 124.69 59278900 1 124.05
2021-06-03 124.68 124.85 123.13 123.54 123.18 76229200 0 0.00
2021-06-04 124.07 126.16 123.85 125.89 125.52 75169300 1 123.85
2021-06-07 126.17 126.32 124.83 125.90 125.53 71057600 0 0.00
2021-06-08 126.60 128.46 126.21 126.74 126.37 74403800 0 0.00
**********************************************************************
1 items had failures:
1 of 2 in __main__.id_maribozu
***Test Failed*** 1 failures.
CodePudding user response:
Try using the flag REPORT_NDIFF. The docs say, "This is the only method that marks differences within lines as well as across lines", which will help identify any whitespace problems, for example:
"""
>>> print('Hello goodbye')
Hello goodbye\r
"""
Failed example:
print('Hello goodbye')
Differences (ndiff with -expected actual):
- Hello goodbye
? -
Hello goodbye
Note that the carriage return doesn't actually appear here.
