Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a datetime indexed pandas DataFrame with hypothesis library?

Tags:

I am trying to create a pandas DataFrame with the hypothesis library for code testing purporses with the following code:

from hypothesis.extra.pandas import columns, data_frames
from hypothesis.extra.numpy import datetime64_dtypes

@given(data_frames(index=datetime64_dtypes(max_period='Y', min_period='s'),
                   columns=columns("A B C".split(), dtype=int)))

The error I receive is the following:

E           TypeError: 'numpy.dtype' object is not iterable

I suspect that this is because when I construct the DataFrame for index= I only pass a datetime element and not a ps.Series all with type datetime for example. Even if this is the case (I am not sure), still I am not sure how to work with the hypothesis library in order to achieve my goal.

Can anyone tell me what's wrong with the code and what the solution would be?

like image 575
Newskooler Avatar asked Oct 30 '18 20:10

Newskooler


1 Answers

The reason for the above error was because, data_frames requires an index containing a strategy elements inside such as indexes for an index= input. Instead, the above datetime64_dtypes only provides a strategy element, but not in an index format.

To fix this we provide the index first and then the strategy element inside the index like so:

from hypothesis import given, strategies 
@given(data_frames(index=indexes(strategies.datetimes()),
                   columns=columns("A B C".split(), dtype=int)))

Note that in order to get datetime we use datetimes().

like image 54
Newskooler Avatar answered Nov 15 '22 06:11

Newskooler