Skip to content

[Feature Request] Add replace method to Index objects #19495

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

Open
jolespin opened this issue Feb 1, 2018 · 10 comments
Open

[Feature Request] Add replace method to Index objects #19495

jolespin opened this issue Feb 1, 2018 · 10 comments
Assignees
Labels

Comments

@jolespin
Copy link

jolespin commented Feb 1, 2018

I usually have to change the value of an element in pd.Index but it's difficult without the replace method that is present in pd.Series. I usually have a bunch of Jupyter cells depending on a particular pd.DataFrame or pd.Series with complicated transformations and a graph at some point. In most cases, I have to change the labels for a few to clean them up for publication quality figures. Currently, I have to use an if else statement with map method but it would nice to be able to just do replace.

print(pd.__version__)
print("_________________________")
idx = pd.Index([0,1,2])
Se = pd.Series(list("abc"), index=idx)
# Can do it to pd.Series
print(Se)
print(Se.replace({"b":"replaced_b"}))
# but not pd.Index
print("_________________________")
print(idx)
d = {1:100}
print(idx.map(lambda x: d[x] if x in d else x))
print(idx.replace({1:100}))

# 0.21.1
# _________________________
# 0    a
# 1    b
# 2    c
# dtype: object
# 0             a
# 1    replaced_b
# 2             c
# dtype: object
# _________________________
# Int64Index([0, 1, 2], dtype='int64')
# Int64Index([0, 100, 2], dtype='int64')
# ---------------------------------------------------------------------------
# AttributeError                            Traceback (most recent call last)
# <ipython-input-383-0b8eacd67c54> in <module>()
#      11 d = {1:100}
#      12 print(idx.map(lambda x: d[x] if x in d else x))
# ---> 13 print(idx.replace({1:100}))
#      14 

# AttributeError: 'Int64Index' object has no attribute 'replace'
@ZhuBaohe
Copy link
Contributor

ZhuBaohe commented Feb 2, 2018

You can do a type conversion.

>>>idx = pd.Index([0,1,2])
...d = {1:100}
...idx = pd.DataFrame(idx).replace(d).set_index(0).index
...print(idx)

Int64Index([0, 100, 2], dtype='int64', name=0)

@jolespin
Copy link
Author

jolespin commented Feb 2, 2018

Thanks @ZhuBaohe , there are a couple of work arounds but I think it would make sense to have a general method just like .to_csv is in both pd.Series and pd.DataFrame.

@jolespin
Copy link
Author

jolespin commented Feb 2, 2018

I've been using the following:

replace = lambda x,d: d[x] if x in d else x
df.index.map(lambda x:replace(x, d_cpd_newcpdname))

Not sure if this is helpful at all.

@jorisvandenbossche
Copy link
Member

@jolespin is Index.rename what you want?

@jolespin
Copy link
Author

jolespin commented Feb 3, 2018

@jorisvandenbossche I don't think it works the same way with pd.Index objects:

>>> import pandas as pd
>>> idx = pd.Index([1,2,3])
>>> idx.rename({1:"a"})
Int64Index([1, 2, 3], dtype='int64', name={1: 'a'})

@jorisvandenbossche
Copy link
Member

Sorry, I meant DataFrame.rename(index={1: 'a'})

@jolespin
Copy link
Author

jolespin commented Feb 3, 2018 via email

@jorisvandenbossche
Copy link
Member

Hmm, we could consider adding replace to Index to make it consistent with Series ?

@oguzhanogreden
Copy link
Contributor

oguzhanogreden commented Feb 10, 2020

No objections for @jorisvandenbossche's suggestion?

I "suffered" from this myself. Less dramatically, I expected that I could of course use a replace method on an index, so I'd like to give this a go if a PR is welcome.

@oguzhanogreden
Copy link
Contributor

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants