3

enter image description here

I have the checkout column in Dataframe of type 'object' in '2017-08-04T23:31:19.000+02:00' format. But i want it in the format as shown in the image. Can anyone help me please.

Thank you:)

2 Answers 2

7

You should be able to convert the object column to a date time column, then use the built in date and time functions.

# create an intermediate column that we won't store on the DataFrame
checkout_as_datetime = pd.to_datetime(df['checkout'])

# Add the desired columns to the dataframe
df['checkout_date'] = checkout_as_datetime.dt.date
df['checkout_time'] = checkout_as_datetime.dt.time

Though, if you're goal isn't to write these specific new columns out somewhere, but to use them for other calculations, it may be simpler to just overwrite your original column and use the datetime methods from there.

df['checkout'] = pd.to_datetime(df['checkout'])
df['checkout'].dt.date  # to access the date
Sign up to request clarification or add additional context in comments.

Comments

2

I haven't tested this, but something along the lines of:

 df['CheckOut_date'] = pd.to_datetime(df["CheckOut_date"].dt.strftime('%Y-%m-%d'))
 df['CheckOut_time'] = pd.to_datetime(df["CheckOut_time"].dt.strftime('%H:%m:%s'))

1 Comment

It looks like you're missing the day in the strftime call for CheckOut_date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.