-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
inconsistant result shape from dataframe.apply depending on if a date column is present #14370
Comments
this is irrespective of other dtypes. You are returning a 2 element list so naturally pandas will try to coerce to the original shape as its compatible. Returning a list is not labeled so that is the only thing pandas can do. You can do this if you want. I also suppose more/better documentation is possible.
|
Jeff, are you saying it's by design that r1 and r2 come out different, (By Design), or are you saying they should be the same (Won't Fix)? |
In my case, f is a function that takes a string and returns a list of strings and it needs to be applied to each row. i.e. I want result cells to have lists in them, I simplified this in the example, but here is a better repro. If what I was doing is under specified to Pandas, I want it to error all the time, not just when another column happens to be a date. It cost a me a lot of time trying to figure out why this only worked some of the time.: import pandas as pd
df1 = pd.DataFrame.from_items([('A', [1,2,3]), ('B', ["ABCD", "EFGH", "IJKL"])])
df2 = pd.DataFrame.from_items([('A', [pd.datetime(1970,1,1), pd.datetime(1970,1,1), pd.datetime(1970,1,1)]), ('B', ["ABCD", "EFGH", "IJKL"])])
f = lambda row: [char for char in row.B]
r1 = df1.apply(f, axis=1) # works
r2 = df2.apply(f, axis=1) # fails
# bug: apply when date present crashes.
# expect: r1 and r2 to be the same values and shape. r1 has the expected value. The problem is that I now have code that crashes only when a date column is present. Your workaround to use a series: f = lambda row: pd.Series([[char for char in row.B]], ["result"])
r1 = df1.apply(f, axis=1)
r2 = df2.apply(f, axis=1) |
A small, complete example of the issue
Expected Output
Output of
pd.show_versions()
commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 58 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
pandas: 0.18.1
nose: None
pip: 8.1.2
setuptools: 20.3
Cython: None
numpy: 1.11.1
scipy: None
statsmodels: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.5.3
pytz: 2016.6.1
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
boto: None
pandas_datareader: None
The text was updated successfully, but these errors were encountered: