6

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?

Newskooler
  • 3,973
  • 7
  • 46
  • 84

1 Answers1

6

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().

Newskooler
  • 3,973
  • 7
  • 46
  • 84
  • I am guessing you are using `hypothesis.extra.pandas.indexes`? I am getting this error: "TypeError: indexes() takes 0 positional arguments but 1 was given", I guess the API changed so you need to use `indexes(elements=...)` – phaebz Jan 18 '22 at 15:40