IanS
IanS

Reputation: 16271

Assign new series to existing column by column index

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.

Upvotes: 1

Views: 422

Answers (1)

jezrael
jezrael

Reputation: 863801

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

Upvotes: 1

Related Questions