怎么把pandas offset转换成Python date?

从日期对象中减去熊猫时,将获得一个熊猫时间戳对象。您可以将此对象转换为String格式的date或Date对象(标准Python日期)。或者,您可以使用datetime库中的timedelta对象。 

示例

from pandas.tseries.frequencies import to_offset
import pandas as pd
dt = pd.to_datetime('2018-01-04') - to_offset("5D")
print(type(dt))

输出结果

这将给出输出-

<class 'pandas._libs.tslib.Timestamp'>

要将其转换为给定格式的字符串,可以使用strftime函数。要将其转换为Date对象,可以date()在此对象上使用函数。 

示例

from pandas.tseries.frequencies import to_offset
import pandas as pd
dt = pd.to_datetime('2018-01-04') - to_offset("5D")
print(dt.strftime('%Y-%m-%d'))
print(dt.date())
print(type(dt.date()))

输出结果

这将给出输出-

2017-12-30
2017-12-30
<class 'datetime.date'>