Description
Code below outlines the issue, if I have a DataFrame with a date column and want to transfer the datetime to string using df.loc[:,'date'] = df['date'].dt.strftime('%Y-%m-%d'), it works fine as long as there is more than 1 row in the Data Frame. If there is only one row then you get a Type Error (see below for full error). If, on the other hand, I use df['date']=df['date'].dt.strftime('%Y-%m-%d') then it works fine for one or more rows. This was observed on both 0.17.1 and 0.19.2.
----- Error Generated -----
Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/pandas/core/internals.py", line 2265, in _try_coerce_args
other = other.astype('i8', copy=False).view('i8')
ValueError: invalid literal for int() with base 10: '2017-02-23'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./testpd.py", line 35, in
do_stuff(d1)
File "./testpd.py", line 17, in do_stuff
df.loc[:,'date'] = df['date'].dt.strftime('%Y-%m-%d')
File "/usr/local/lib/python3.4/site-packages/pandas/core/indexing.py", line 141, in setitem
self._setitem_with_indexer(indexer, value)
File "/usr/local/lib/python3.4/site-packages/pandas/core/indexing.py", line 533, in _setitem_with_indexer
setter(labels[0], value)
File "/usr/local/lib/python3.4/site-packages/pandas/core/indexing.py", line 473, in setter
s._data = s._data.setitem(indexer=pi, value=v)
File "/usr/local/lib/python3.4/site-packages/pandas/core/internals.py", line 3168, in setitem
return self.apply('setitem', **kwargs)
File "/usr/local/lib/python3.4/site-packages/pandas/core/internals.py", line 3056, in apply
applied = getattr(b, f)(**kwargs)
File "/usr/local/lib/python3.4/site-packages/pandas/core/internals.py", line 668, in setitem
values, _, value, _ = self._try_coerce_args(self.values, value)
File "/usr/local/lib/python3.4/site-packages/pandas/core/internals.py", line 2270, in _try_coerce_args
raise TypeError
TypeError
----- Code to Reproduce Issue -----
#!/usr/bin/env python3
""" Demonstrate Pandas issue """
import datetime as dt
import pandas as pd
def do_stuff(d):
df = pd.DataFrame(d)
df['date'] = df['date'].dt.strftime('%Y-%m-%d')
df['id'] = df['id'].astype(str)
df['fl'] = df['fl'].astype(str)
print("df[df]: " + str(df))
df = pd.DataFrame(d)
df.loc[:,'id'] = df['id'].astype(str)
df.loc[:,'fl'] = df['fl'].astype(str)
df.loc[:,'date'] = df['date'].dt.strftime('%Y-%m-%d')
print("df.loc[df]: " + str(df))
""" With 2 rows in Data Frame, works fine """
d2 = { 'date': pd.Series([dt.datetime.today(), dt.datetime.today()]),
'bsym': pd.Series(['blah', 'blah2']),
'id': pd.Series([1, 2]),
'fl': pd.Series([1.5, 3]) }
print("D2...")
do_stuff(d2)
""" With 1 row in Data Frame, exception occurs on date column """
d1 = { 'date': pd.Series([dt.datetime.today()]),
'bsym': pd.Series(['blah']),
'id': pd.Series([1]),
'fl': pd.Series([1.5]) }
print("D1...")
do_stuff(d1)