【问题标题】:How to remove freq='W-FRI' portion in Timestamp如何删除时间戳中的 freq='W-FRI' 部分
【发布时间】:2020-03-05 10:51:34
【问题描述】:

好的,所以我有两个数据框都以日期时间作为索引。

当我调用 df1 index 时,我得到:

df1.index[0]
Timestamp('2016-03-04 00:00:00')

当我调用 df2 索引时,我得到:

df2.index[0]
Timestamp('2016-03-11 00:00:00', freq='W-FRI')

我正在尝试删除 freq='W-FRI' 部分。我希望我的第二个数据框 (df2) 在我调用第一个索引时返回以下内容:

df2.index[0]
Timestamp('2016-03-03 00:00:00')

非常感谢。这个小差异不允许我用日期索引我的数据框。

【问题讨论】:

  • 所以除了删除 freq 部分之外,您还需要将日期向前移动 8 天,或者这是一个错字?

标签: pandas datetime time timestamp finance


【解决方案1】:

您有一个带有freq 的日期时间索引。设置为None

import pandas as pd

idx = pd.date_range('2010-01-01', freq='W-FRI', periods=10)
idx[0]
#Timestamp('2010-01-01 00:00:00', freq='W-FRI')

idx.freq=None
idx[0]
#Timestamp('2010-01-01 00:00:00')

所有DatetimeIndex对象都有一个频率属性,默认是None

pd.DatetimeIndex(['2010-01-01', '2011-01-02'])
#DatetimeIndex(['2010-01-01', '2011-01-02'], dtype='datetime64[ns]', freq=None)

【讨论】:

    猜你喜欢
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2014-07-25
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    • 2018-01-31
    相关资源
    最近更新 更多