如何在 pd.Timestamp.min 和 pd.Timestamp.max 值之外使用熊猫?

社会演员多 python 226

原文标题How to use pandas beyond the pd.Timestamp.min and pd.Timestamp.max value?

pd.Timestamp.min
pd.Timestamp.max

Timestamp('1677-09-21 00:12:43.145224193')
Timestamp('2262-04-11 23:47:16.854775807')

我发现 pandas 有一个最小和最大日期值。如果我需要超出这些值的日期,这可能吗?

是否无法移动最小/最大值,例如世纪窗口?那么没有熊猫的任何替代品吗?

非常感谢。

原文链接:https://stackoverflow.com//questions/71600168/how-to-use-pandas-beyond-the-pd-timestamp-min-and-pd-timestamp-max-value

回复

我来回复
  • mozway的头像
    mozway 评论

    由于时间戳的纳秒精度,这是一个已知的限制。

    时间戳限制

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

    文档建议使用pandas.period_range

    表示越界范围

    如果您有超出时间戳范围的数据,请参阅时间戳限制,那么您可以使用 PeriodIndex 和/或 Series ofPeriods 进行计算。

    pd.period_range("1215-01-01", "1381-01-01", freq="D")
    
    PeriodIndex(['1215-01-01', '1215-01-02', '1215-01-03', '1215-01-04',
                 '1215-01-05', '1215-01-06', '1215-01-07', '1215-01-08',
                 '1215-01-09', '1215-01-10',
                 ...
                 '1380-12-23', '1380-12-24', '1380-12-25', '1380-12-26',
                 '1380-12-27', '1380-12-28', '1380-12-29', '1380-12-30',
                 '1380-12-31', '1381-01-01'],
                dtype='period[D]', length=60632)
    

    转换系列

    没有直接的方法(如to_period)来转换现有的Series,你需要通过aPeriodIndex

    df = pd.DataFrame({'str': ['1900-01-01', '2500-01-01']})
    df['period'] = pd.PeriodIndex(df['str'], freq='D').values
    

    输出:

    print(df)
              str      period
    0  1900-01-01  1900-01-01
    1  2500-01-01  2500-01-01
    
    print(df.dtypes)
    str          object
    period    period[D]
    dtype: object
    
    2年前 0条评论