Home > Mobile >  How to convert double values from df to year values/strings?
How to convert double values from df to year values/strings?

Time:02-04

I have a dataframe with year values like: 2014.0, 2013.0... as float values. I would need to convert these values to year format, like 2014, 2013... I tried converting them to string, but I get "2014.0", "2013.0"... How could I convert them to year, not double or strings? This is what I tried for string conversion:

df['year']=df['year'].astype(str)

And then:

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

But this gave me results like: 1640995200000 and so on. How could I convert the doubles to reasonable year format?

CodePudding user response:

First problem should be old pandas version, if possible try upgrade.


Converting to strings is not necessary here:

df = pd.DataFrame({'year':[2000.0, 2007.0]})

df['year'] = pd.to_datetime(df['year'], format='%Y')
print (df)
        year
0 2000-01-01
1 2007-01-01

If values are strings, not floats for me working add .0 to format:

df['year'] = pd.to_datetime(df['year'].astype(str), format='%Y.0')
print (df)
        year
0 2000-01-01
1 2007-01-01

CodePudding user response:

simply do this !!works!!

df['year']=df['year'].astype(int)

output:

2004
2005
20017

only thing is they are in int datatype not datetime objects

  •  Tags:  
  • Related