question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[PROPOSAL] Make Series.update() accept a dictionary

See original GitHub issue

Code 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:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dsaxtoncommented, Apr 1, 2020

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.

0reactions
derickecommented, Apr 23, 2020

@dsaxton I submitted a PR to close this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Updating a pandas dataframe or csv with dictionary
You can create dataframe from it: df = pd.DataFrame.from_dict(data, orient='index'). And to update it, you can revert to dict:
Read more >
Python Dictionary – Create, Append, Update, Remove
Check out what is a dictionary in Python, how to create, append, update, and delete elements. Also, learn to use comprehension with examples....
Read more >
PEP 584 – Add Union Operators To dict
This PEP proposes adding merge ( | ) and update ( |= ) operators to the built-in dict class. Note. After this PEP...
Read more >
French Translation of “proposal” - Collins Dictionary
French Translation of “proposal” | The official Collins English-French Dictionary online. Over 100000 French translations of English words and phrases.
Read more >
Append list of dictionary and series to a existing Pandas ...
For this purpose append() function of pandas, the module is ... Import module; Create data frame or series; Create a list with dictionaries...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found