如何使用假设库创建日期时间索引的 Pandas DataFrame?

New*_*ler 5 python pytest pandas python-hypothesis

我正在尝试使用以下代码创建一个用于代码测试目的pandas DataFramehypothesis库:

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)))
Run Code Online (Sandbox Code Playgroud)

我收到的错误如下:

E           TypeError: 'numpy.dtype' object is not iterable
Run Code Online (Sandbox Code Playgroud)

我怀疑这是因为当我构造DataFramefor时,index=我只传递一个datetime元素,而不是一个ps.Series带有类型的all datetime。即使是这种情况(我不确定),我仍然不确定如何使用hypothesis库来实现我的目标。

谁能告诉我代码有什么问题以及解决方案是什么?

New*_*ler 6

上述错误的原因是,data_frames需要一个包含策略元素的索引,例如indexes输入index=。相反,上面datetime64_dtypes只提供了策略元素,但没有提供索引格式。

为了解决这个问题,我们首先提供索引,然后提供索引内的策略元素,如下所示:

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

请注意,为了获得datetime我们使用datetimes().