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

时间:2018-10-30 20:10:26

标签: python pandas pytest python-hypothesis

我正在尝试使用$ flask run --help Usage: flask run [OPTIONS] Runs a local development server for the Flask application. This local server is recommended for development purposes only but it can also be used for simple intranet deployments. By default it will not support any sort of concurrency at all to simplify debugging. This can be changed with the --with-threads option which will enable basic multithreading. The reloader and debugger are by default enabled if the debug flag of Flask is enabled and disabled otherwise. Options: -h, --host TEXT The interface to bind to. -p, --port INTEGER The port to bind to. --reload / --no-reload Enable or disable the reloader. By default the reloader is active if debug is enabled. --debugger / --no-debugger Enable or disable the debugger. By default the debugger is active if debug is enabled. --eager-loading / --lazy-loader Enable or disable eager loading. By default eager loading is enabled if the reloader is disabled. --with-threads / --without-threads Enable or disable multithreading. --help Show this message and exit. 库创建一个pandas DataFrame库,以使用以下代码对代码进行测试:

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=columns("A B C".split(), dtype=int)))

我怀疑这是因为当我为E TypeError: 'numpy.dtype' object is not iterable 构造DataFrame时,我只传递了index=元素,而不传递了datetime类型均为ps.Series的所有元素例如。即使是这种情况(我不确定),我仍然不确定如何使用datetime库来实现我的目标。

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

1 个答案:

答案 0 :(得分:1)

发生上述错误的原因是,data_frames要求索引中包含一个 strategy 元素,例如indexes作为index=输入的索引。相反,上面的datetime64_dtypes仅提供了 strategy 元素,而没有提供索引格式。

要解决此问题,我们首先提供索引,然后再提供索引内的strategy元素,如下所示:

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

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