在 Pandas 中将整数系列转换为 timedelta

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34519536/
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-14 00:26:44  来源:igfitidea点击:

Convert integer series to timedelta in pandas

pythonpandas

提问by abdouglass

I have a data frame in pandas which includes number of days since an event occurred. I want to create a new column that calculates the date of the event by subtracting the number of days from the current date. Every time I attempt to apply pd.offsets.Dayor pd.TimedeltaI get an error stating that Series are an unsupported type. This also occurs when I use apply. When I use mapI receive a runtime error saying "maximum recursion depth exceeded while calling a Python object".

我在Pandas中有一个数据框,其中包括事件发生后的天数。我想创建一个新列,通过从当前日期中减去天数来计算事件的日期。每次我尝试申请pd.offsets.Daypd.Timedelta收到错误消息,指出 Series 是不受支持的类型。当我使用apply. 当我使用时,map我收到一个运行时错误,提示“调用 Python 对象时超出了最大递归深度”。

For example, assume my data frame looked like this:

例如,假设我的数据框如下所示:

index    days_since_event
0        5
1        7
2        3
3        6
4        0

I want to create a new column with the date of the event, so my expected outcome (using today's date of 12/29/2015)

我想用事件的日期创建一个新列,所以我的预期结果(使用今天的日期 12/29/2015)

index    days_since_event    event_date
0        5                   2015-12-24
1        7                   2015-12-22
2        3                   2015-12-26
3        6                   2015-12-23
4        0                   2015-12-29

I have attempted multiple ways to do this, but have received errors for each.

我尝试了多种方法来做到这一点,但每种方法都收到了错误。

One method I tried was:

我尝试过的一种方法是:

now = pd.datetime.date(pd.datetime.now())
df['event_date'] = now - df.days_since_event.apply(pd.offsets.Day)

With this I received an error saying that Series are an unsupported type.

有了这个,我收到一个错误,说 Series 是不受支持的类型。

I tried the above with .mapinstead of .apply, and received the error that "maximum recursion depth exceeded while calling a Python object".

我尝试了上面的.map而不是.apply,并收到了“调用 Python 对象时超出了最大递归深度”的错误。

I also attempted to convert the days into timedelta, such as:

我还尝试将天数转换为 timedelta,例如:

df.days_since_event = (dt.timedelta(days = df.days_since_event)).apply

This also received an error referencing the series being an unsupported type.

这也收到了一个错误,指出该系列是不受支持的类型。

回答by joris

First, to convert the column with integers to a timedelta, you can use to_timedelta:

首先,要将带有整数的列转换为 timedelta,您可以使用to_timedelta

In [60]: pd.to_timedelta(df['days_since_event'], unit='D')
Out[60]:
0   5 days
1   7 days
2   3 days
3   6 days
4   0 days
Name: days_since_event, dtype: timedelta64[ns]

Then you can create a new column with the current date and substract those timedelta's:

然后,您可以使用当前日期创建一个新列并减去这些 timedelta:

In [62]: df['event_date'] = pd.Timestamp('2015-12-29')

In [63]: df['event_date'] = df['event_date'] -  pd.to_timedelta(df['days_since_event'], unit='D')

In [64]: df['event_date']
Out[64]:
0   2015-12-24
1   2015-12-22
2   2015-12-26
3   2015-12-23
4   2015-12-29
dtype: datetime64[ns]