Python:在数据框中将timedelta转换为int

问题:Python:在数据框中将timedelta转换为int

我想在pandas数据框中创建一个列,该列表示timedelta列中天数的整数。是否可以使用“ datetime.days”,还是我需要做更多手动操作?

timedelta列

7天23:29:00

天整数列

7

I would like to create a column in a pandas data frame that is an integer representation of the number of days in a timedelta column. Is it possible to use ‘datetime.days’ or do I need to do something more manual?

timedelta column

7 days, 23:29:00

day integer column

7


回答 0

使用dt.days属性。通过以下方式访问此属性:

timedelta_series.dt.days

您也可以通过相同的方式获取secondsmicroseconds属性。

Use the dt.days attribute. Access this attribute via:

timedelta_series.dt.days

You can also get the seconds and microseconds attributes in the same way.


回答 1

您可以这样做,td您的一系列时间增量在哪里。该除法将纳秒增量转换为天增量,并且转换为int的时间减少为整天。

import numpy as np

(td / np.timedelta64(1, 'D')).astype(int)

You could do this, where td is your series of timedeltas. The division converts the nanosecond deltas into day deltas, and the conversion to int drops to whole days.

import numpy as np

(td / np.timedelta64(1, 'D')).astype(int)

回答 2

Timedelta对象具有只读实例属性.days.seconds.microseconds

Timedelta objects have read-only instance attributes .days, .seconds, and .microseconds.


回答 3

如果问题不仅仅在于“如何访问timedelta的整数形式?” 但是“如何将数据帧中的timedelta列转换为int?” 答案可能有所不同。除了访问.dt.days器,您还需要df.astype或者pd.to_numeric

这些选项中的任何一个都可以帮助:

df['tdColumn'] = pd.to_numeric(df['tdColumn'].dt.days, downcast='integer')

要么

df['tdColumn'] = df['tdColumn'].dt.days.astype('int16')

If the question isn’t just “how to access an integer form of the timedelta?” but “how to convert the timedelta column in the dataframe to an int?” the answer might be a little different. In addition to the .dt.days accessor you need either df.astype or pd.to_numeric

Either of these options should help:

df['tdColumn'] = pd.to_numeric(df['tdColumn'].dt.days, downcast='integer')

or

df['tdColumn'] = df['tdColumn'].dt.days.astype('int16')