0

I have a large dataset with a datetime variable "CHECKIN_DATE_TIME" and would like to create a new variable that is just the date sans the time. The "CHECKIN_DATE_TIME" is formatted as such 2020-02-01 11:13:17.000. I want the new variable to be formatted like 2020-02-01.

I'm referencing the following for help https://www.programiz.com/python-programming/datetime/strptime but when I write my code, I'm getting attribute errors: "Traceback(most recent call last)" and "DataFrame' object has no attribute 'strptime'"

import datetime
NOTES_TAT=NOTES_TAT.strptime(CHECKIN_DATE_TIME,"%d %B, %Y")
1
  • What is the type of NOTES_TAT? Seems a pandas dataframe? give an example of this dataframe (say NOTES_TAT.head()) Commented Apr 2, 2020 at 21:07

2 Answers 2

2

You are using pandas dataframe. Try,

NOTES_TAT['CHECKIN_DATE_TIME'].dt.strftime('%d %B, %Y')
Sign up to request clarification or add additional context in comments.

Comments

1

You can access the datetime wrapper via the .dt DataFrame accessor. To get just the date, use the .date property at the end.

Example:

import pandas as pd

# Build a sample DataFrame
df = pd.DataFrame({'checkin': '2020-02-01 11:13:17.000'}, index=[0])
df['checkin'] = pd.to_datetime(df['checkin'])

# Create the date column using the `date` property.
df['date'] = df['checkin'].dt.date
# For a formatted date:
df['date'] = df['checkin'].dt.strftime('%d %B, %Y')

Output 1:

              checkin        date
0 2020-02-01 11:13:17  2020-02-01

Output 2:

              checkin               date
0 2020-02-01 11:13:17  01 February, 2020

2 Comments

Thanks @S3DEV So I'm still getting the same error when I run: df['date'] = df['checkin'].dt.strftime('%d %B, %Y'). The datatype for Checkin_date_time is 'object'. Do I need to change the datatype for Checkin_date_time?
Disregard, i reformatted the Checkin_date_time variable to datetime using this code:NOTES_TAT_v1['Date'] = pd.to_datetime(NOTES_TAT_v1['CHECKIN_DATE_TIME'], infer_datetime_format=True)

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.