【问题标题】:Far dates get converted into datetime.datetime object instead of datetime64[ns]远日期被转换为 datetime.datetime 对象而不是 datetime64[ns]
【发布时间】:2019-07-23 04:14:42
【问题描述】:

我写了以下代码:

date_format = lambda x: datetime.strptime(x, "%d-%b-%Y") if x!=0 else np.nan

input_file['Business date']    = input_file['Business date'].fillna(0).apply(date_format)
input_file['Date of the Flow'] = input_file['Date of the Flow'].fillna(0).apply(date_format)

当我采用唯一的“营业日期”列时,它显示输出为:

input_file["Business date"].unique()
Out[61]: array(['2019-02-15T00:00:00.000000000', '2019-02-18T00:00:00.000000000'], dtype='datetime64[ns]')

对于“流程日期”:

input_file["Date of the Flow"].unique()
Out[64]: 
array([nan, datetime.datetime(2019, 9, 18, 0, 0),
       datetime.datetime(2023, 3, 24, 0, 0), ...,
       datetime.datetime(2028, 7, 15, 0, 0),
       datetime.datetime(2122, 12, 19, 0, 0),
       datetime.datetime(2026, 11, 28, 0, 0)], dtype=object)

为什么会这样?另请注意,“流程日期”列的日期为 2505 年。如果我删除这些日期,它会给出与“业务日期”相同的输出,即 dtype='datetime64[ns]'

【问题讨论】:

  • 您好,您能否在应用date_format 函数之前向我们提供一些您的原始输入数据示例,即来自input_file["Business date"]input_file['Date of the Flow'] 的项目?

标签: python python-3.x datetime spyder


【解决方案1】:

我发现了问题。 Python中的时间序列似乎有限制/范围。因此,我在 2505 年的日期以日期时间格式出现,而不是时间戳。在以下链接中找到它:https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html

由于 pandas 以纳秒分辨率表示时间戳,因此可以使用 64 位整数表示的时间跨度被限制为大约 584 年:

In [90]: pd.Timestamp.min
Out[90]: Timestamp('1677-09-21 00:12:43.145225')

In [91]: pd.Timestamp.max
Out[91]: Timestamp('2262-04-11 23:47:16.854775807')

【讨论】:

    猜你喜欢
    • 2021-10-01
    • 2022-08-15
    • 2023-03-09
    • 1970-01-01
    • 2020-11-05
    • 2018-09-26
    • 2019-12-19
    • 2017-01-05
    • 2020-04-28
    相关资源
    最近更新 更多