Andrey Risukhin
Andrey Risukhin

Reputation: 13

DataFrame: datetime column to int type

I am working with DataFrames, and created a new datetime column ('time_diff') from two other columns. I now need to convert the new 'time_diff' column into an int column, counting days.

I've tried .astype(int), this was unsuccessful.

This is my code:

# 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   

Here's a screenshot of code and dataframe below:

Screenshot of my code and DataFrame

Thank you!

Upvotes: 0

Views: 48

Answers (2)

Andrey Risukhin
Andrey Risukhin

Reputation: 13

Answered. Simple solution:

# 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

Upvotes: 0

YOLO
YOLO

Reputation: 21709

I think you do:

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

Upvotes: 1

Related Questions