Issue with converting a pandas column from int64 to datetime64

Question:

I’m trying to convert a column of Year values from int64 to datetime64 in pandas. The column currently looks like

         Year

         2003
         2003
         2003
         2003
         2003
         ... 
         2021
         2021
         2021
         2021
         2021

However the data type listed when I use dataset['Year'].dtypes is int64.

That’s after I used pd.to_datetime(dataset.Year, format='%Y') to convert the column from int64 to datetime64. How do I get around this?

Asked By: EcoHealthGuy

||

Answers:

You should be able to convert from an integer:

df = pd.DataFrame({'Year': [2003, 2022]})

df['datetime'] = pd.to_datetime(df['Year'], format='%Y')

print(df)

Output:

   Year   datetime
0  2003 2003-01-01
1  2022 2022-01-01
Answered By: mozway

You have to assign pd.to_datetime(df['Year'], format="%Y") to df['date']. Once you have done that you should be able to see convert from integer.

df = pd.DataFrame({'Year': [2000,2000,2000,2000,2000,2000]})

df['date'] = pd.to_datetime(df['Year'], format="%Y")

df

The output should be:

    Year    date
0   2000    2000-01-01
1   2000    2000-01-01
2   2000    2000-01-01
3   2000    2000-01-01
4   2000    2000-01-01
5   2000    2000-01-01

So essentially all you are missing is df['date'] = pd.to_datetime(df['Year'], format="%Y") from your code and it should be working fine with respect to converting.

The pd.to_datetime() will not just return the Year (as far as I understood from your question you wanted the year), if you want more information on what .to_date_time() returns, you can see the documentation.

I hope this helps.

Answered By: Nameer1811
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.