Skip to content

DataFrame indices can't map through dictionaries or series #12756

@azevedoph-check24

Description

@azevedoph-check24

A DataFrame index has the map function, which expects a mapper. However, unlike mapping a Series, it only accepts callables, and won't work when mapping through dictionaries or series.

Code Sample:

df = pd.DataFrame({'col1': range(5)})
d = {0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four'}
df.col1.map(d) # perfectly valid
df.index.map(d) # TypeError: 'dict' object is not callable
s = pd.Series(d)
df.col1.map(s) # perfectly valid
df.index.map(s) # TypeError: 'Series' object is not callable

Expected Output:

I would expect the same output for df.col1.map(s) and df.index.map(s), or at least that it returns an nparray with the mapped values, as it does when we do pass a callable, but not that it breaks like it does.

output of pd.show_versions():

INSTALLED VERSIONS

commit: None
python: 2.7.6.final.0
python-bits: 64
OS: Linux
OS-release: 3.16.0-67-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_DE.UTF-8

pandas: 0.18.0
nose: 1.3.7
pip: 8.1.1
setuptools: 20.6.6
Cython: 0.23.5
numpy: 1.11.0
scipy: 0.17.0
statsmodels: 0.6.1
xarray: None
IPython: 4.1.2
sphinx: 1.4
patsy: 0.4.1
dateutil: 2.5.2
pytz: 2016.3
blosc: None
bottleneck: None
tables: None
numexpr: 2.5.1
matplotlib: 1.5.1
openpyxl: 2.3.4
xlrd: 0.9.4
xlwt: None
xlsxwriter: None
lxml: 3.6.0
bs4: None
html5lib: 0.9999999
httplib2: 0.9.2
apiclient: None
sqlalchemy: 1.0.12
pymysql: None
psycopg2: 2.6.1 (dt dec pq3 ext lo64)
jinja2: 2.8
boto: 2.39.0

Activity

jreback

jreback commented on Mar 31, 2016

@jreback
Contributor

@sinhrks and I had this exact discussion very recently: #12532 (comment)

so yes, these should be unified, but are a bit non-trivial to do so. welcome to have pull-requests!

added this to the Next Major Release milestone on Mar 31, 2016
sinhrks

sinhrks commented on Mar 31, 2016

@sinhrks
Member

Yes, currently I think Index.map should be fixed prior to #12532. Will take a look.

jreback

jreback commented on Apr 1, 2016

@jreback
Contributor

xref #12766

modified the milestones: Next Major Release, 0.22.0 on Nov 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    API DesignCleanInternalsRelated to non-user accessible pandas implementationReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Participants

      @jreback@sinhrks@azevedoph-check24

      Issue actions

        DataFrame indices can't map through dictionaries or series · Issue #12756 · pandas-dev/pandas