【问题标题】:Orange Python Script create custom timestamp (Orange Data Mining Windows 10)Orange Python 脚本创建自定义时间戳(Orange Data Mining Windows 10)
【发布时间】:2022-10-17 05:38:30
【问题描述】:

我正在尝试实现一个脚本,它将创建一个橙色数据表,其中只有一个包含自定义时间戳的列。

用例:我需要一个完整的时间戳,以便稍后合并其他一些 csv 文件。我在Orange GUI BTW 中工作,而不是在实际的 python shell 或任何其他 IDE 中工作(以防此信息产生任何影响)。

到目前为止,这是我想出的:

From Orange.data import Domain, Table, TimeVariable
import numpy as np

domain = Domain([TimeVariable("Timestamp")])

# Timestamp from 22-03-08 to 2022-03-08 in minute steps
arr = np.arange("2022-03-08", "2022-03-15", dtype="datetime64[m]")

# Obviously necessary to achieve a correct format for the matrix
arr = arr.reshape(-1,1)

out_data = Table.from_numpy(domain, arr)

但是结果不匹配:

>>> print(arr)
[['2022-03-08T00:00']
 ['2022-03-08T00:01']
 ['2022-03-08T00:02']
 ...
 ['2022-03-14T23:57']
 ['2022-03-14T23:58']
 ['2022-03-14T23:59']]

>>> print(out_data)
[[27444960.0],
 [27444961.0],
 [27444962.0],
 ...
 [27455037.0],
 [27455038.0],
 [27455039.0]]

显然,从 numpy 移交数据时我遗漏了一些东西,但我很难理解documentation

我还发现 this post 似乎解决了类似的问题,但我还没有弄清楚如何将解决方案应用于我的问题。

如果有人可以在这里帮助我,我会非常高兴。请尽量使用简单的术语和概念。

【问题讨论】:

    标签: python numpy datetime orange


    【解决方案1】:

    感谢您提出问题,并对TimeVariable 的薄弱文档表示歉意。

    在您的代码中,您必须更改两件事才能工作。 首先,需要设置TimeVariable 是否包含时间和/或日期数据:

    • TimeVariable("Timestamp", have_date=True) 只存储日期信息——类似于datetime.date
    • TimeVariable("Timestamp", have_time=True) 只存储时间信息(没有日期)——类似于datetime.time
    • TimeVariable("Timestamp", have_time=True, have_date=True) 存储日期和时间——类似于datetime.datetime

    您没有在示例中设置该信息,因此默认情况下两者都是False。对于您的情况,您必须将两者都设置为True,因为您的属性将保存日期时间值。

    另一个问题是 Orange 的表将日期时间值存储为 UNIX 纪元(从 1970-01-01 开始的秒数),Table.from_numpy 也期望这种格式的值。当前arr 数组中的值改为以分钟为单位。我刚刚将下面代码中的dtype 转换为秒。

    这是工作代码:

    from Orange.data import Domain, Table, TimeVariable
    import numpy as np
    
    # Important: set whether TimeVariable contains time and/or date
    domain = Domain([TimeVariable("Timestamp", have_time=True, have_date=True)])
    
    # Timestamp from 22-03-08 to 2022-03-08 in minute steps
    arr = np.arange("2022-03-08", "2022-03-15", dtype="datetime64[m]").astype("datetime64[s]")
    
    # necessary to achieve a correct format for the matrix
    arr = arr.reshape(-1,1)
    out_data = Table.from_numpy(domain, arr)
    

    【讨论】:

    • 非常感谢您的快速响应 Primoz。你是我今天的英雄——感谢你的支持,我将能够节省很多时间!亲切的问候
    猜你喜欢
    • 2017-02-01
    • 2021-10-15
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 2011-06-28
    相关资源
    最近更新 更多