如何使用假设库创建日期时间索引 pandas DataFrame?
How to create a datetime indexed pandas DataFrame with hypothesis library?
我正在尝试使用 hypothesis
库创建一个 pandas DataFrame
用于代码测试目的,代码如下:
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
类型为 [=17] =] 例如。即使是这种情况(我不确定),我仍然不确定如何使用 hypothesis
库来实现我的目标。
谁能告诉我代码有什么问题以及解决方案是什么?
出现上述错误的原因是,data_frames
requires an index containing a strategy elements inside such as indexes
用于 index=
输入。相反,上面的 datetime64_dtypes
只提供了一个 strategy 元素,但没有索引格式。
为了解决这个问题,我们首先提供索引,然后在索引中提供策略元素,如下所示:
from hypothesis import given, strategies
@given(data_frames(index=indexes(strategies.datetimes()),
columns=columns("A B C".split(), dtype=int)))
请注意,为了获得 datetime
,我们使用 datetimes()
。
我正在尝试使用 hypothesis
库创建一个 pandas DataFrame
用于代码测试目的,代码如下:
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
类型为 [=17] =] 例如。即使是这种情况(我不确定),我仍然不确定如何使用 hypothesis
库来实现我的目标。
谁能告诉我代码有什么问题以及解决方案是什么?
出现上述错误的原因是,data_frames
requires an index containing a strategy elements inside such as indexes
用于 index=
输入。相反,上面的 datetime64_dtypes
只提供了一个 strategy 元素,但没有索引格式。
为了解决这个问题,我们首先提供索引,然后在索引中提供策略元素,如下所示:
from hypothesis import given, strategies
@given(data_frames(index=indexes(strategies.datetimes()),
columns=columns("A B C".split(), dtype=int)))
请注意,为了获得 datetime
,我们使用 datetimes()
。