Home > front end >  Convert pandas.core.series.Series to Date (Year)
Convert pandas.core.series.Series to Date (Year)

Time:04-17

I'm trying to convert a Series to a Date Format. The column I'm trying to convert is Year so I can filter my daframe per year and then group by country, etc.

Country Year
USA 2018
USA 2019
CAD 2018
ARG 2018
ARG 2017

I've tried multiple options (which I'll list below) and it always returns a "Series" type. I know this is a basic question but nothing I've tried has worked so far and I feel like it's right in front of me and I'm just not seeing it.

Option 1:

World['Year']=pd.to_datetime(World['Year'])
type((World['Year']))

Output= pandas.core.series.Series

Option 2

World['Year']=pd.to_datetime(World['Year'], format='%Y')
#Didn't work either

Option 3

World['Year']=pd.to_datetime(World['Year'], format='%Y').dt.date

I also thought about converting it to numeric so I can filter the data but I'm not sure that's the best idea. Any suggestions are greatly appreciated

CodePudding user response:

You cannot convert Series to Date format but what you can do is convert the datatype of Series to Date format. To check the datatype of a Series use World['Year'].dtype

CodePudding user response:

I think you mean how to convert it to datetime64

based on your option2, we can use := operator

import pandas as pd
df = pd.DataFrame({
    'Country' : "USA,USA,CAD,ARG,ARG".split(","),
    'Year' : ["2018","2019","2018","2018","2017"]
})
print(df)
df["Year"] = (temp:=pd.to_datetime(df['Year'],format="%Y"))
print(df.dtypes)

this way we can got the datetime64[ns]

  • Related