我认为to_timedelta
function在做什么。
time = pd.datetime.now() + pd.to_timedelta(5,unit='W')
更新
to_timedelta
函数不再支持月份,季度和年份。时间偏移对象有两种:
- 尊重日历时间的DateOffset:
- TimeOffset,它是绝对时间(与夏令时天数有关)。
使用documentation about it获得更多详细信息。 to_timedelta
函数无法知道您要使用哪个。
,
也许此功能会有所帮助?
def offset(freq:str,n:int = 0):
"""
Pandas DateOffset wrapper
import pandas as pd
"""
# =============================================================================
# offsets_df = pd.read_html('https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html')[2]
# offsets_df.drop(columns = ['Description'],inplace = True)
# offsets_df.replace('None',np.NaN,inplace = True)
# offsets_df.dropna(subset = ['Frequency String'],inplace = True)
# offsets_df['Frequency String'] = offsets_df['Frequency String'].str.replace("\'",'')
# for x in ['Date Offset','Frequency String']:
# offsets_df[x] = offsets_df[x].str.split(' or ')
# offsets_df['Date Offset'] = offsets_df['Date Offset'].map(lambda x: x[0])
# offsets_df = explode_rows(offsets_df,'Frequency String',fill_value = '')
# offsets_df.drop_duplicates(subset = ['Frequency String'],inplace = True)
# offsets_d = dict(zip(offsets_df['Frequency String'],offsets_df['Date Offset']))
# =============================================================================
offsets_d = {'B': 'BDay','C': 'CDay','W': 'Week','WOM': 'WeekOfMonth','LWOM': 'LastWeekOfMonth','M': 'MonthEnd','MS': 'MonthBegin','BM': 'BMonthEnd','BMS': 'BMonthBegin','CBM': 'CBMonthEnd','CBMS': 'CBMonthBegin','SM': 'SemiMonthEnd','SMS': 'SemiMonthBegin','Q': 'QuarterEnd','QS': 'QuarterBegin','BQ': 'BQuarterEnd','BQS': 'BQuarterBegin','REQ': 'FY5253Quarter','A': 'YearEnd','AS': 'YearBegin','BYS': 'YearBegin','BA': 'BYearEnd','BAS': 'BYearBegin','RE': 'FY5253','BH': 'BusinessHour','CBH': 'CustomBusinessHour','D': 'Day','H': 'Hour','T': 'Minute','min': 'Minute','S': 'Second','L': 'Milli','ms': 'Milli','U': 'Micro','us': 'Micro','N': 'Nano'}
return eval(f'pd.offsets.{offsets_d[freq]}({n})')
本文链接:https://www.f2er.com/1514207.html