Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply with .loc[:, col] method failed when the DataFrame only has one row and the column's type is datetime64 #18027

Closed
ppiiyyaa4149 opened this issue Oct 30, 2017 · 2 comments
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Duplicate Report Duplicate issue or pull request Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@ppiiyyaa4149
Copy link

ppiiyyaa4149 commented Oct 30, 2017

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

@ppiiyyaa4149 ppiiyyaa4149 changed the title apply method failed when the DataFrame only has one row and the column's type is datetime64 apply with .loc[:, col] method failed when the DataFrame only has one row and the column's type is datetime64 Oct 30, 2017
@gfyoung gfyoung added the Indexing Related to indexing on series/frames, not to indexes themselves label Oct 30, 2017
@gfyoung
Copy link
Member

gfyoung commented Oct 30, 2017

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

@jreback
Copy link
Contributor

jreback commented Oct 30, 2017

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

@jreback jreback closed this as completed Oct 30, 2017
@jreback jreback added Bug Dtype Conversions Unexpected or buggy dtype conversions Duplicate Report Duplicate issue or pull request labels Oct 30, 2017
@jreback jreback added this to the No action milestone Oct 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Duplicate Report Duplicate issue or pull request Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

3 participants