Python 将datetimeindex延长1个句点
考虑Python 将datetimeindex延长1个句点,python,pandas,datetimeindex,Python,Pandas,Datetimeindex,考虑DateTimeIndexdates dates = pd.date_range('2016-01-29', periods=4, freq='BM') dates DatetimeIndex(['2016-01-29', '2016-02-29', '2016-03-31', '2016-04-29'], dtype='datetime64[ns]', freq='BM') 我想以附加到对象的频率将索引延长一个周期 我想 pd.date_range('20
DateTimeIndex
dates
dates = pd.date_range('2016-01-29', periods=4, freq='BM')
dates
DatetimeIndex(['2016-01-29', '2016-02-29', '2016-03-31', '2016-04-29'],
dtype='datetime64[ns]', freq='BM')
我想以附加到对象的频率将索引延长一个周期
我想
pd.date_range('2016-01-29', periods=5, freq='BM')
DatetimeIndex(['2016-01-29', '2016-02-29', '2016-03-31', '2016-04-29',
'2016-05-31'],
dtype='datetime64[ns]', freq='BM')
我试过了
dates.append(dates[[-1]] + pd.offsets.BusinessMonthEnd())
然而
- 不通用于使用
日期的频率
- 我收到性能警告 PerformanceWarning:应用于Series或DatetimeIndex的非矢量化日期偏移量
In [207]: dates = dates.append(pd.DatetimeIndex(pd.Series(dates[-1] + pd.offsets.BusinessMonthEnd())))
In [208]: dates
Out[208]: DatetimeIndex(['2016-01-29', '2016-02-29', '2016-03-31', '2016-04-29', '2016-05-31'], dtype='datetime64[ns]', freq=None)
或者使用list
([…]
)而不是pd.Series()
:
DatetimeIndex
中的时间戳已经知道它们描述的是营业月末,因此您只需添加1:
import pandas as pd
dates = pd.date_range('2016-01-29', periods=4, freq='BM')
print(repr(dates[-1]))
# => Timestamp('2016-04-29 00:00:00', offset='BM')
print(repr(dates[-1] + 1))
# => Timestamp('2016-05-31 00:00:00', offset='BM')
您可以使用.union
将后者添加到索引中:
dates = dates.union([dates[-1] + 1])
print(dates)
# => DatetimeIndex(['2016-01-29', '2016-02-29', '2016-03-31', '2016-04-29',
# '2016-05-31'],
# dtype='datetime64[ns]', freq='BM')
与
.append
相比,这保留了有关偏移的知识。我将使用.tshift
功能,然后相应地使用:
dr = pd.date_range(start='1/1/2020', periods=5, freq='D')
df = pd.DataFrame(data=[1,2,3,4,5],
index=dr,
columns=['A'])
df.head()
A
2020-01-01 1
2020-01-02 2
2020-01-03 3
2020-01-04 4
2020-01-05 5 <-
df.tshift()
A
2020-01-02 1
2020-01-03 2
2020-01-04 3
2020-01-05 4
2020-01-06 5 <-
other = pd.DataFrame([6], columns=['A'], index=[df.tshift().index[-1]])
other.head()
A
2020-01-06 6
df.append(other)
A
2020-01-01 1
2020-01-02 2
2020-01-03 3
2020-01-04 4
2020-01-05 5
2020-01-06 6 <-
dr=pd.date\u范围(起始日期为2020年1月1日,周期为5,频率为D)
df=pd.DataFrame(数据=[1,2,3,4,5],
索引=dr,
列=['A'])
df.head()
A.
2020-01-01 1
2020-01-02 2
2020-01-03 3
2020-01-04 4
2020-01-05 5熊猫==1.1.1对+1的回答
接下来,对于pandas==1.1.1
,我发现这是最好的解决方案:
dates.union(pd.date\u范围(dates[-1]+dates.freq,periods=1,freq=dates.freq))
使用n的广义答案
信用
结合@alberto garcia raboso的回答和@ballpointben的评论
什么不起作用
- 以下内容刚被格式化为
索引
,而不是日期时间索引
:
dates.union([dates[-1]+dates.freq])
- 另外,不推荐使用日期[-1]+1
- 最佳解决方案是:
import pandas as pd
dates = pd.date_range('2016-01-29', periods=4, freq='BM')
extended = dates.union(dates.shift(n)[-n:])
其中n是要添加的时段数。使用n=4
,您将获得如下扩展日期范围:
DatetimeIndex(['2016-01-29', '2016-02-29', '2016-03-31', '2016-04-29',
'2016-05-31', '2016-06-30', '2016-07-29', '2016-08-31'],
dtype='datetime64[ns]', freq='BM')
非常好和整洁的解决方案!如果您希望将索引扩展到
n
periods,可以使用dates.union(pd.date\u范围(dates[-1]+1,periods=n,freq=dates.freq))
现在不推荐使用这种方法。不推荐将整数和整数数组加减到时间戳,将在将来的版本中删除。使用n*self.freq
而不是添加/减去n
。自版本1.1.0以来,tshift似乎已被弃用。我建议使用fccoelho的答案。
import pandas as pd
dates = pd.date_range('2016-01-29', periods=4, freq='BM')
extended = dates.union(dates.shift(n)[-n:])
DatetimeIndex(['2016-01-29', '2016-02-29', '2016-03-31', '2016-04-29',
'2016-05-31', '2016-06-30', '2016-07-29', '2016-08-31'],
dtype='datetime64[ns]', freq='BM')