DataFrame:日期时间列转int型

DataFrame: datetime column to int type

我正在使用 DataFrame,并从另外两个列创建了一个新的日期时间列 ('time_diff')。我现在需要将新的 'time_diff' 列转换为一个 int 列,计算天数。

我试过 .astype(int),但没有成功。

这是我的代码:

# Preprocessing start and end dates
from datetime import datetime

train_plus = train_full.copy()

# Replace all last_event_DI 'unk' with corresponding start_time_DI
for i in range(len(train_plus)): # Makes a range from 0 to #-1, this is needed to loop over the length
    if (train_plus['last_event_DI'][i] == 'unk'):
        train_plus['last_event_DI'][i] = train_plus['start_time_DI'][i]

# Last interaction - start date = length of interaction (time_diff)
train_plus['time_diff'] = train_plus['last_event_DI'].apply(pd.to_datetime) - train_plus['start_time_DI'].apply(pd.to_datetime)

# I need the 'time_diff' column to become an int column
# CONVERSION CODE HERE

train_plus   

下面是代码和数据框的屏幕截图:

Screenshot of my code and DataFrame

谢谢!

我想你会:

train_plus['time_diff'] = train_plus['time_diff'].days

已回答。简单的解决方案:

# Replace all time_diff time_deltas with int
for i in range(len(train_plus)): # Makes a range from 0 to #-1, this is needed to loop over the length
    train_plus['time_diff'][i] = train_plus['time_diff'][i].days