1

I need to replace a date column by formatted strings. I can do it like this:

df = pd.DataFrame(pd.date_range('2016-01-01', '2016-01-02'), columns=['date'])
df['date'] = df['date'].dt.strftime('%Y-%m-%d')

However, I want to use the column index instead of the column name (the dataframe is passed to me, and I can't guarantee that column names are unique). I can do it like this:

df.iloc[:, 0] = df.iloc[:, 0].dt.strftime('%Y-%m-%d')

This works fine, but it fails for a dataframe of length one:

df = pd.DataFrame([pd.to_datetime('2016-01-01')], columns=['date'])
df.iloc[:, 0] = df.iloc[:, 0].dt.strftime('%Y-%m-%d')

pandas/core/internals.py in _try_coerce_args(self, values, other)

-> 2265 other = other.astype('i8', copy=False).view('i8')

ValueError: invalid literal for int() with base 10: '2016-01-01'

Note that the assignment by column name still works:

df['date'] = df.iloc[:, 0].dt.strftime('%Y-%m-%d')

I would like to understand why the assignment by column index fails for a dataframe of length 1, and what I can do to make it work. I use pandas 0.19.1 with python 3.5.

IanS
  • 15,771
  • 9
  • 60
  • 84

1 Answers1

1

It is a bit hack, but works - select column by position with []:

df = pd.DataFrame([pd.to_datetime('2016-01-01')], columns=['date'])

print (df.columns[0])
date

df[df.columns[0]] = df.iloc[:, 0].dt.strftime('%Y-%m-%d')
print (df)
         date
0  2016-01-01
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Wait, it doesn't solve my problem in the case column names are not unique :( – IanS Feb 16 '17 at 09:44
  • Maybe I should just raise an error when column names are not unique. I'd still be curious to know why the assignment fails, though. – IanS Feb 16 '17 at 09:45
  • 1
    Yes, duplicated columns are problem. But if use last version of pandas `read_csv` solve duplicated columns - add `.1` ... – jezrael Feb 16 '17 at 09:46
  • I found it - [how rename duplicated column names](http://stackoverflow.com/a/24686572/2901002), I hope it help you. – jezrael Feb 16 '17 at 09:57
  • Thanks, that's useful! I will accept your answer tomorrow, just in case someone can explain why the assignment fails in the meantime. – IanS Feb 16 '17 at 10:02
  • Sure, no problem. ;) Nice day! – jezrael Feb 16 '17 at 10:03