1

I am working with several CSV's that first N columns are information and then the next Ms (M is big) columns are information regarding a date.

This is the dataframe picture This is the dataframe picture

I need to set just the columns between N+1 to N+M - 1 columns name to date format.

I tried this, in this case N+1 = 5, no matter M, I suppose that I can use -1 to not affect the last column name.

ContDiarios.columns[5:-1] = pd.to_datetime(ContDiarios.columns[5:-1])

but I get the following error:

TypeError: Index does not support mutable operations

bad_coder
  • 11,289
  • 20
  • 44
  • 72
nabetse.cl
  • 11
  • 1

1 Answers1

1

The way you are doing is not feasable. Please try this way

def convert(x):
    try:
        return pd.to_datetime(x)
    except:
        return x
x.columns = map(convert,x.columns)

Or you can also use df.rename property to convert it.

zabop
  • 6,750
  • 3
  • 39
  • 84
subhanshu kumar
  • 372
  • 3
  • 10