Code Sample, a copy-pastable example if possible

import pandas as pd
import datetime as dt

# only one row
s = pd.DataFrame(columns=['a', 'b', 'c'], index=[1])
s.loc[:, 'a'] = dt.datetime.now()
s.loc[:, 'b'] = 1
s.loc[:, 'c'] = dt.datetime.now()

s.loc[:, 'b'] = s.loc[:, 'b'].apply(lambda x: x + 1)

# no problem
s.loc[:, 'c'] = s.loc[:, 'c'].apply(lambda x: x.date().strftime("%Y-%m-%d"))

# raise TypeError 
s.loc[:, 'a'] = s.loc[:, 'a'].apply(lambda x: x.date().strftime("%Y-%m-%d"))

# raise TypeError 
temp = s.loc[:, 'a'].apply(lambda x: x.date().strftime("%Y-%m-%d"))
s.loc[:, 'a'] = temp



# no problems when DataFrame has more than 2 rows
s = pd.DataFrame(columns=['a', 'b', 'c'], index=[1, 2])
s.loc[:, 'a'] = dt.datetime.now()
s.loc[:, 'b'] = 1
s.loc[:, 'c'] = dt.datetime.now()

s.loc[:, 'b'] = s.loc[:, 'b'].apply(lambda x: x + 1)
temp = s.loc[:, 'a'].apply(lambda x: x.date().strftime("%Y-%m-%d"))
s.loc[:, 'a'] = temp
# s.loc[:, 'a'] = s.loc[:, 'a'].apply(lambda x: x.date().strftime("%Y-%m-%d"))
s.loc[:, 'c'] = s.loc[:, 'c'].apply(lambda x: x.date().strftime("%Y-%m-%d"))

Problem description

In my situation, I need to get user's login history from the database, then trans the type of the login time(timestamp in MySQL db, datetime64 in pandas) to string("%Y-%m-%d"). But when a user only has one login record, I have this problem occurred. Really got confused

Expected Output


In[3]: s
Out[3]: 
            a  b           c
1  2017-10-30  2  2017-10-30


Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.5.3.final.0 python-bits: 64 OS: Windows OS-release: 10 machine: AMD64 processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: None.None pandas: 0.19.2 nose: None pip: 9.0.1 setuptools: 36.2.7 Cython: None numpy: 1.12.1 scipy: 0.19.0 statsmodels: None xarray: None IPython: 5.3.0 sphinx: None patsy: None dateutil: 2.6.0 pytz: 2016.10 blosc: None bottleneck: None tables: None numexpr: None matplotlib: 2.0.2 openpyxl: 2.4.8 xlrd: 1.0.0 xlwt: None xlsxwriter: None lxml: 3.7.3 bs4: 4.6.0 html5lib: 0.9999999 httplib2: None apiclient: None sqlalchemy: 1.1.6 pymysql: 0.7.10.None psycopg2: None jinja2: 2.9.5 boto: None pandas_datareader: 0.4.0

Comment From: gfyoung

@zjhjz : Thanks for the report! Could you do us a quick favor and post the error message that you get?

Comment From: jreback

The problem is not .apply, which FYI your usage is quite non-idiomatic.

Rather the inference on indexing is not coercing in the n=1 case. this is a duplicate of #6942