[PROPOSAL] Make Series.update() accept a dictionary
See original GitHub issueCode Sample, a copy-pastable example if possible
I propose to re-write the Series.update()
method to accept a dictionary object. This would be useful for example when you are iterating over a DataFrame using DataFrame.iterrows()
, and you want to update the row
object returned by iterrows()
in order to build another DataFrame starting from the DataFrame you are iterating over
Here is an example that doesn’t work:
import pandas as pd
s = pd.Series({'city':'Rome'})
s.update({'country':'Italy'})
Problem description
Traceback (most recent call last): File “<stdin>”, line 1, in <module> File “C:\InstalledPrograms\Anaconda3\lib\site-packages\pandas\core\series.py”, line 2807, in update other = other.reindex_like(self) AttributeError: ‘dict’ object has no attribute ‘reindex_like’
Simple use case
In this use case, an explicit conversion to dict() is needed
import random
import pandas as pd
def very_long_dictionary():
d = dict()
for c in range(2,10):
d[c] = random.randint(1,101)
return d
df = pd.DataFrame([[1,2],[3,4],[5,6]])
print(df)
outdl = list()
for index,row in df.iterrows():
#Explicit conversion to dict is needed
row = dict(row)
row.update(very_long_dictionary())
outdl.append(row)
newdf = pd.DataFrame(outdl)
print(newdf)
Expected Output
The series would update to the new value
Output of pd.show_versions()
pd.show_versions()
INSTALLED VERSIONS
commit : None python : 3.7.7.final.0 python-bits : 64 OS : Windows OS-release : 10 machine : AMD64 processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : None.None
pandas : 1.0.3 numpy : 1.18.1 pytz : 2019.3 dateutil : 2.8.1 pip : 20.0.2 setuptools : 46.1.3.post20200330 Cython : 0.29.15 pytest : 5.4.1 hypothesis : 5.5.4 sphinx : 2.4.4 blosc : None feather : None xlsxwriter : 1.2.8 lxml.etree : 4.5.0 html5lib : 1.0.1 pymysql : None psycopg2 : None jinja2 : 2.11.1 IPython : 7.13.0 pandas_datareader: None bs4 : 4.8.2 bottleneck : 1.3.2 fastparquet : None gcsfs : None lxml.etree : 4.5.0 matplotlib : 3.1.3 numexpr : 2.7.1 odfpy : None openpyxl : 3.0.3 pandas_gbq : None pyarrow : None pytables : None pytest : 5.4.1 pyxlsb : None s3fs : None scipy : 1.4.1 sqlalchemy : 1.3.15 tables : 3.6.1 tabulate : None xarray : None xlrd : 1.2.0 xlwt : 1.3.0 xlsxwriter : 1.2.8 numba : 0.48.0
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
How about wrapping your dict in a Series before passing to update? (Also I’m not sure if your example s.update({‘country’:‘Italy’}) will do what you want since ‘country’ isn’t in the index of s.)
I do think though that this could probably use a better error message.
@dsaxton I submitted a PR to close this.