Python 熊猫将一天添加到列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20480897/
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
Pandas add one day to column
提问by dartdog
I need to add 1 day to each date I want to get the begining date of the following month eg 2014-01-2014 for the 1st item in the dataframe. Tried:
我需要在每个日期添加 1 天以获取下个月的开始日期,例如 2014-01-2014 为数据框中的第一个项目。尝试:
montdist['date'] + pd.DateOffset(1)
Which gives me:
这给了我:
TypeError: cannot use a non-absolute DateOffset in datetime/timedelta operations [<DateOffset>]
Have a Dataframe:
有一个数据框:
Units mondist date
1 6491 0.057785 2013-12-31 00:00:00
2 7377 0.065672 2014-01-31 00:00:00
3 9990 0.088934 2014-02-28 00:00:00
4 10362 0.092245 2014-03-31 00:00:00
5 11271 0.100337 2014-04-30 00:00:00
6 11637 0.103596 2014-05-31 00:00:00
7 10199 0.090794 2014-06-30 00:00:00
8 10486 0.093349 2014-07-31 00:00:00
9 9282 0.082631 2014-08-31 00:00:00
10 8632 0.076844 2014-09-30 00:00:00
11 8204 0.073034 2013-10-31 00:00:00
12 8400 0.074779 2013-11-30 00:00:00
采纳答案by Andy Hayden
Make it a DatetimeIndex first:
首先将其设为 DatetimeIndex:
pd.DatetimeIndex(montdist['date']) + pd.DateOffset(1)
Note: I think there is a feature request that this could work with date columns...
注意:我认为有一个功能要求可以与日期列一起使用...
In action:
在行动:
In [11]: df = pd.DataFrame([[1, 2], [3, 4]], columns=['A', 'B'])
In [12]: df['date'] = pd.to_datetime(['21-11-2013', '22-11-2013'])
In [13]: pd.DatetimeIndex(df.date) + pd.DateOffset(1)
Out[13]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-11-22 00:00:00, 2013-11-23 00:00:00]
Length: 2, Freq: None, Timezone: None
In [14]: pd.DatetimeIndex(df.date) + pd.offsets.Hour(1)
Out[14]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-11-21 01:00:00, 2013-11-22 01:00:00]
Length: 2, Freq: None, Timezone: Non
回答by szu
Try to use timedelta():
尝试使用 timedelta():
mondist['shifted_date']=mondist.date + datetime.timedelta(days=1)
回答by fantabolous
As far as I can tell tshiftis a bit faster than doing math such as + pd.DateOffsetetc. Of course it only applies to Series or Dataframe indices, not columns.. but you could do:
据我所知tshift比做数学等要快一点+ pd.DateOffset。当然它只适用于系列或数据框索引,而不适用于列..但你可以这样做:
df['newdate'] = pd.Series(index=df.index).tshift(periods=1, freq='D').index
If your df is large, this may shave off half the time - at least it did for me, which is why I'm using it.
如果你的 df 很大,这可能会减少一半的时间 - 至少对我来说是这样,这就是我使用它的原因。
回答by merry
No need to turn into an index. Just using .apply()works:
不需要变成索引。只是使用.apply()作品:
df['newdate'] = pd.to_datetime(df['date']).apply(pd.DateOffset(1))
回答by Lucas H
I think that the cleanest way to do this is a variant of szu's answer. Pandas has nearly full support datetime built into its functionality, so there is no need to load datetime; instead, if you are already using pandas, create the new column like this:
我认为最干净的方法是 szu 答案的变体。Pandas 在其功能中几乎完全支持 datetime,因此无需加载 datetime;相反,如果您已经在使用 Pandas,请像这样创建新列:
mondist['shifted_date'] = mondist.date + pd.Timedelta(days=1)
回答by Ryan Bowns
One quick mention. if you are using data-frames and your datatype is datetime64[ns]non indexed, Then I would go as below:
Assuming the date column name is 'Date to Change by 1' and you want to change all dates by 1 day.
一个快速提及。如果您使用的是数据框并且您的数据类型datetime64[ns]未编入索引,那么我将如下所示:假设日期列名称是“更改日期为 1”,并且您希望将所有日期更改为 1 天。
import time
from datetime import datetime, timedelta, date, time
before
['Date to Change by 1'] = 1/31/2020
df['Date to Change by 1'] = (pd.to_datetime(df['Date to Change by 1']) +
timedelta(1)
After
['Date to Change by 1'] = 2/01/2020

