pandas 大熊猫数据帧转移日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20278674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:23:54  来源:igfitidea点击:

pandas dataframe shift dates

pandasdataframe

提问by user1802143

I have a dataframe that is indexed by dates. I'd like to shift just the dates, one business day forward (Monday-Friday), without changing the size or anything else. Is there a simple way to do this?

我有一个按日期索引的数据框。我只想将日期向前移动一个工作日(周一至周五),而不更改大小或其他任何内容。有没有一种简单的方法可以做到这一点?

回答by Andy Hayden

You can shift with 'B' (I think this requires numpy >= 1.7):

您可以使用 'B' 转换(我认为这需要 numpy >= 1.7):

In [11]: rng = pd.to_datetime(['21-11-2013', '22-11-2013'])

In [12]: rng.shift(1, freq='B')  # 1 business day
Out[12]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-11-22 00:00:00, 2013-11-25 00:00:00]
Length: 2, Freq: None, Timezone: None

On the Series(same on a DataFrame):

系列上(在DataFrame上相同):

In [21]: s = pd.Series([1, 2], index=rng)

In [22]: s
Out[22]: 
2013-11-21    1
2013-11-22    2
dtype: int64

In [23]: s.shift(1, freq='B')
Out[23]: 
2013-11-22    1
2013-11-25    2
dtype: int64