Skip to content

Pandas different between Python 2.7 and Python 3.5 #16380

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

Closed
marabout2015 opened this issue May 17, 2017 · 5 comments · Fixed by #44245
Closed

Pandas different between Python 2.7 and Python 3.5 #16380

marabout2015 opened this issue May 17, 2017 · 5 comments · Fixed by #44245
Labels
Compat pandas objects compatability with Numpy or Python functions good first issue Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@marabout2015
Copy link

Code Sample, a copy-pastable example if possible

import pandas as pd
X = pd.DataFrame({'x': range(3)}, pd.Index(range(3), name='x'))
Y = X.x.apply(lambda x: pd.Series(range(x + 1), pd.Index(range(x + 1), name='y')))
Y.columns.names

Problem description

On Python 2.7, the above code results in:

FrozenList([u'y'])

while on Python 3.5, the same code results in:

FrozenList([None])

I see this difference between Python 2 and 3 for pandas 0.18.0, 0.19.2, and 0.20.1.

Expected Output

FrozenList([u'y'])

Or at least consistency.

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.5.2.final.0 python-bits: 64 OS: Windows OS-release: 8.1 machine: AMD64 processor: Intel64 Family 6 Model 45 Stepping 7, GenuineIntel byteorder: little LC_ALL: None LANG: None

pandas: 0.18.0
nose: 1.3.7
pip: 8.1.2
setuptools: 27.2.0
Cython: 0.24.1
numpy: 1.10.4
scipy: 0.18.1
statsmodels: 0.6.1
xarray: None
IPython: 5.1.0
sphinx: 1.4.6
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.6.1
blosc: None
bottleneck: 1.1.0
tables: 3.2.2
numexpr: 2.6.1
matplotlib: 1.5.3
openpyxl: 2.3.2
xlrd: 1.0.0
xlwt: 1.1.2
xlsxwriter: 0.9.3
lxml: 3.6.4
bs4: 4.5.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.13
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.42.0

@jreback
Copy link
Contributor

jreback commented May 17, 2017

on py3

In [9]: X = pd.DataFrame({'x': range(3)}, pd.Index(range(3), name='x'))
   ...: Y = X.x.apply(lambda x: pd.Series(range(x + 1), pd.Index(range(x + 1), name='y')))
   ...: Y.columns.names
   ...: 
Out[9]: FrozenList([None])

In [11]: X = pd.DataFrame({'x': range(3)}, pd.Index(range(3), name='x'))
    ...: Y = X.x.apply(lambda x: pd.Series(list(range(x + 1)), pd.Index(list(range(x + 1)), name='y')))
    ...: Y.columns.names
    ...: 
Out[11]: FrozenList(['y'])

These construct differently in py2 and py3 as range is a list in py2 (and an actual range object).

In [13]: Index(range(3))
Out[13]: RangeIndex(start=0, stop=3, step=1)

In [14]: Index(list(range(3)))
Out[14]: Int64Index([0, 1, 2], dtype='int64')

@jreback
Copy link
Contributor

jreback commented May 17, 2017

that said this path is only lightly tested. so prob not propagating the name properly. a pull-request to fix welcomed!

@jreback jreback added 2/3 Compat Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels May 17, 2017
@jreback jreback added this to the Next Major Release milestone May 17, 2017
@lkirk
Copy link
Contributor

lkirk commented May 22, 2017

Going to work on this issue at the pycon sprint

@topper-123
Copy link
Contributor

Removing the 2/3 Compat label, as we've dropped Python2. The issue is now only that the name should probably be ['y'], though I think the use case is quite esoteric.

A PR still welcome though.

@mroeschke
Copy link
Member

This looks to work on master. Could use a test

In [12]: import pandas as pd
    ...: X = pd.DataFrame({'x': range(3)}, pd.Index(range(3), name='x'))
    ...: Y = X.x.apply(lambda x: pd.Series(range(x + 1), pd.Index(range(x + 1), name='y')))
    ...: Y.columns.names
Out[12]: FrozenList(['y'])

In [13]: pd.__version__
Out[13]: '1.3.0.dev0+1889.gf949788596'

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed 2/3 Compat Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Jun 12, 2021
@mroeschke mroeschke mentioned this issue Oct 31, 2021
9 tasks
@jreback jreback modified the milestones: Contributions Welcome, 1.4 Oct 31, 2021
@jreback jreback added the Compat pandas objects compatability with Numpy or Python functions label Oct 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compat pandas objects compatability with Numpy or Python functions good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants