gpt4 book ai didi

python - Pandas 中的日期到持续时间

转载 作者:太空狗 更新时间:2023-10-29 22:12:54 30 4
gpt4 key购买 nike

我觉得这应该很容易完成,但我不知道怎么做。我有一个 pandas DataFramedate:

0    2012-08-21
1 2013-02-17
2 2013-02-18
3 2013-03-03
4 2013-03-04
Name: date, dtype: datetime64[ns]

我想要一列持续时间,例如:

0    0
1 80 days
2 1 day
3 15 days
4 1 day
Name: date, dtype: datetime64[ns]

我的尝试产生了一堆 0 天和 NaT:

>>> df.date[1:] - df.date[:-1]
0 NaT
1 0 days
2 0 days
...

有什么想法吗?

最佳答案

Timedeltas 在这里很有用:(see docs)

Starting in v0.15.0, we introduce a new scalar type Timedelta, which is a subclass of datetime.timedelta, and behaves in a similar manner, but allows compatibility with np.timedelta64 types as well as a host of custom representation, parsing, and attributes.

Timedeltas are differences in times, expressed in difference units, e.g. days, hours, minutes, seconds. They can be both positive and negative.

df

0
0 2012-08-21
1 2013-02-17
2 2013-02-18
3 2013-03-03
4 2013-03-04

你可以:

pd.to_timedelta(df)

TimedeltaIndex(['0 days'], dtype='timedelta64[ns]', freq=None)
0 0
1 180
2 1
3 13
4 1
Name: 0, dtype: int64

或者,您可以使用 .shift()(或 .diff(),如 @Andy Hayden 所示)计算时间点之间的差异:

res = df-df.shift()

得到:

res.fillna(0)

0
0 0 days
1 180 days
2 1 days
3 13 days
4 1 days

您可以使用以下方法将这些从 timedelta64 dtype 转换为 integer:

res.fillna(0).squeeze().dt.days

0 0
1 180
2 1
3 13
4 1

关于python - Pandas 中的日期到持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34446158/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com