mark
mark

Reputation: 95

Parsing date and time in python

I am trying to create a column that grabs just the date (ie 2004-03-18) from a column in the same dataframe. The datetime expression starts with the date (year-month-day), the letter 'T' and then the time expression. For example, "2004-03-18T07:00:00", and I am just wanting "2004-03-18" portion of the datetime.

dt = datetime.now()
UAT['Date'] = pd.to_datetime(UAT['Date']).dt.date
UAT['Date'] = pd.to_datetime(UAT['Date'], format='%Y-%m-%d')

The above code gets the following error: 'tuple' object has no attribute 'lower'

What am I doing wrong?

Upvotes: 0

Views: 37

Answers (1)

Andreas
Andreas

Reputation: 2521

You might need to convert the column type to 'datetime64[ns] with astype() function, then you can retrieve just the date part of the datetimes string using date() function

UAT['Date'] = UAT['Date'].astype('datetime64[ns]')
UAT['Date'] = pd.to_datetime(UAT['Date']).dt.date
UAT['Date'] = pd.to_datetime(UAT['Date'], format='%Y-%m-%d')

Upvotes: 0

Related Questions