将 pandas datetimeindex 延长 1 个句点
extend a pandas datetimeindex by 1 period
考虑 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())
但是
- 未推广到
dates
的使用频率
- 我收到性能警告
PerformanceWarning: Non-vectorized DateOffset being applied to Series or 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()
:
In [211]: dates.append(pd.DatetimeIndex([dates[-1] + pd.offsets.BusinessMonthEnd()]))
Out[211]: DatetimeIndex(['2016-01-29', '2016-02-29', '2016-03-31', '2016-04-29', '2016-05-31'], dtype='datetime64[ns]', freq=None)
您 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 <-
pandas==1.1.1 对 +1 的回答
为了跟进,对于 pandas==1.1.1
,我发现这是最好的解决方案:
dates.union(pd.date_range(dates[-1] + dates.freq, periods=1, freq=dates.freq))
使用 n
的一般答案
n=3
dates.union(pd.date_range(dates[-1] + dates.freq, periods=n, freq=dates.freq))
学分
结合@alberto-garcia-raboso 的回答和@ballpointben 的评论。
什么没用
- 以下刚刚格式化为
Index
,而不是 DateTimeIndex
:
dates.union([dates[-1] + dates.freq])
- 同时
dates[-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')
考虑 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())
但是
- 未推广到
dates
的使用频率
- 我收到性能警告
PerformanceWarning: Non-vectorized DateOffset being applied to Series or 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()
:
In [211]: dates.append(pd.DatetimeIndex([dates[-1] + pd.offsets.BusinessMonthEnd()]))
Out[211]: DatetimeIndex(['2016-01-29', '2016-02-29', '2016-03-31', '2016-04-29', '2016-05-31'], dtype='datetime64[ns]', freq=None)
您 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 <-
pandas==1.1.1 对 +1 的回答
为了跟进,对于 pandas==1.1.1
,我发现这是最好的解决方案:
dates.union(pd.date_range(dates[-1] + dates.freq, periods=1, freq=dates.freq))
使用 n
的一般答案n=3
dates.union(pd.date_range(dates[-1] + dates.freq, periods=n, freq=dates.freq))
学分
结合@alberto-garcia-raboso 的回答和@ballpointben 的评论。
什么没用
- 以下刚刚格式化为
Index
,而不是DateTimeIndex
:dates.union([dates[-1] + dates.freq])
- 同时
dates[-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')