Vipul Dass
Vipul Dass

Reputation: 21

I'm having trouble extracting the year from the date column of this particular dataset

#Adjust Date Info

TSLA['date'] = TSLA['date'].astype(str)

TSLA['date'] = pd.to_datetime(TSLA['date'])

The datatype of both columns is object.

Ive tried using .astype(str) on the date column thenn using a lambda function to extract the YYYY-MM-DD but the datatype doesn't change. It doesn't throw up an error either when applying the .astype(str)

.to_datetime doesn't work either.

There are no missing values in either column. I'd appreciate any opinions as to what i'm doing incorrectly?

Since i am unable to add images for now, the date column has the following values: YYYY-MM-DD HH-MM-SS-HH-MM-SS

Upvotes: 0

Views: 80

Answers (2)

Vipul Dass
Vipul Dass

Reputation: 21

Alright, it seems that TSLA['date'] = pd.to_datetime(TSLA['date'],utc = True)

followed by:

TSLA['date'] = TSLA['date'].dt.date

got me the values i wanted i.e. YYYY-MM-DD.

Upvotes: 1

Anurag Dabas
Anurag Dabas

Reputation: 24314

well I don't know that what you are trying to do but as you mentioned in the heading of your question that you want to extract the year,so for doing this:-

TSLA['date'] = pd.to_datetime(TSLA['date'])
TSLA['year']=TSLA['date'].dt.year

Upvotes: 1

Related Questions