如何在时间戳中删除freq ='W-FRI'部分

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

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

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')

非常感谢。这种微小的差异不允许我使用日期对数据框编制索引。

csh279029248 回答:如何在时间戳中删除freq ='W-FRI'部分

您有一个带有freq的DatetimeIndex。将其设置为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)
,
(pd.to_datetime(str(pd.to_datetime(df2.index[0])),infer_datetime_format=True))

这可以做到,我尝试使用上面的代码删除freq ='S'。

本文链接:https://www.f2er.com/3135479.html

大家都在问