0

I have a data-frame df where the head looks like:

        DATE    YEAR  MONTH  DAY
0 2014-03-04    2014      3    4
1 2014-04-04    2014      4    4
2 2014-04-07    2014      4    7
3 2014-04-08    2014      4    8
4 2014-04-09    2014      4    9

The columns YEAR, MONTH, DAY where taken from the DATE column using:

df['YEAR'] = df.DATE.dt.year
df['MONTH'] = df.DATE.dt.month
df['DAY'] = df.DATE.dt.day

Currently their types are int64. I am trying to chage the type of YEAR, MONTH, DAY columns to string. I have tried to using:

df['YEAR'] = df.DATE.dt.year + ""
df['MONTH'] = df.DATE.dt.month + ""
df['DAY'] = df.DATE.dt.day + ""

and

df['YEAR'].apply(str)
df['MONTH'].apply(str)
df['DAY'].apply(str)

I keep getting the type error:

TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U21') dtype('<U21') dtype('<U21') 
halfer
  • 19,824
  • 17
  • 99
  • 186
Stacey
  • 4,825
  • 17
  • 58
  • 99

1 Answers1

1

use .astype(str)

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

type(df.YEAR[0])
Out[11]: str
nimrodz
  • 1,504
  • 1
  • 13
  • 18