Python 如何使用假设库创建日期时间索引的数据帧?
我正在尝试使用Python 如何使用假设库创建日期时间索引的数据帧?,python,pandas,pytest,python-hypothesis,Python,Pandas,Pytest,Python Hypothesis,我正在尝试使用假设库创建一个熊猫数据框,用于以下代码的代码测试目的: 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=c
假设
库创建一个熊猫数据框
,用于以下代码的代码测试目的:
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)))
我收到的错误如下:
E TypeError: 'numpy.dtype' object is not iterable
我怀疑这是因为当我为index=
构造DataFrame
时,我只传递了datetime
元素,而不是ps.Series
所有类型的datetime
。即使是这样(我不确定),我仍然不确定如何使用假设库来实现我的目标
有谁能告诉我代码出了什么问题以及解决方案是什么吗?上述错误的原因是,需要一个包含策略元素的索引,例如索引=
输入。相反,上面的datetime64\u数据类型
只提供了一个策略元素,而不是索引格式
为了解决这个问题,我们首先提供索引,然后在索引中提供策略元素,如下所示:
from hypothesis import given, strategies
@given(data_frames(index=indexes(strategies.datetimes()),
columns=columns("A B C".split(), dtype=int)))
请注意,为了获得datetime
,我们使用