简体   繁体   中英

How do you use Doctest with pandas?

I would like to write a doctest to test this code, but have not found any solutions on the net? Any suggestions?

def CheckSampleTime(data):
    """Write to log file Sample Time outside of work hours if outside work ours

    >>> # example doctest here
    """

    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)")

The doctest module works with Pandas just fine. For example, running the following code in a script will successfully run 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.

More pandasic way

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)")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM