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.

BUG: Assigning dictionary to series using .loc produces random results.

See original GitHub issue

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

df = pd.DataFrame({'A': [3, 9, 3, 4], 'B': [7, 1, 6, 0], 'C': [9, 0, 3, 4], 'D': [1, 8, 0, 0]})

   A  B  C  D
0  3  7  9  1
1  9  1  0  8
2  3  6  3  0
3  4  0  4  0

d = {0:10,1:20,2:30,3:40}

df.loc[:,'A'] = d

Output:

   A  B  C  D
0  0  7  9  1
1  1  1  0  8
2  2  6  3  0
3  3  0  4  0

Issue Description

The values that are assigned to columns A are the keys of the dictionary instead of the values.

If however, instead of assigning the dictionary to an existing column, if we create a new column, we will get the same result the first time we run it, but running the same code again will get the expected result. We then are able to select any column and it will return the expected output.

First time running df.loc[:,'E'] = {0:10,1:20,2:30,3:40}

Output:

   A  B  C  D  E
0  0  7  9  1  0
1  1  1  0  8  1
2  2  6  3  0  2
3  3  0  4  0  3

Second time running df.loc[:,'E'] = {0:10,1:20,2:30,3:40}

   A  B  C  D   E
0  0  7  9  1  10
1  1  1  0  8  20
2  2  6  3  0  30
3  3  0  4  0  40

Then if we run the same code as we did at first, we get a different result:

df.loc[:,'A'] = {0:10,1:20,2:30,3:40}

Output:

    A  B  C  D   E
0  10  7  9  1  10
1  20  1  0  8  20
2  30  6  3  0  30
3  40  0  4  0  40

Expected Behavior

d = {0:10,1:20,2:30,3:40}

df.loc[:,'A'] = d

Output:

     A  B  C  D
 0  10  7  9  1
 1  20  1  0  8
 2  30  6  3  0
 3  40  0  4  0

Installed Versions

1.4.2

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
phoflcommented, Jun 15, 2022

Had a chance to take a look. This is a bug, because aligning the dict for the column case was simply forgotten, additionally, the multi-block case already works:

df = pd.DataFrame({"a": [1, 2], "b": [3, 4], "d": "a"})
df.loc[:, "a"] = {1: 100, 0: 200}
df.loc[:, "c"] = {0: 5, 1: 6}

returns

     a  b  d  c
0  200  3  a  0
1  100  4  a  1
0reactions
phoflcommented, Jun 6, 2022

The point with assigning dicts to rows is that the keys are matched with the column names. I am not sure if this makes much sense for columns, because you could simply use a Series

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pandas DataFrame Assignment Bug using Dictionaries of ...
If the index loc exists it will not convert to a series: it simply zips the column locs with the iterable. In the...
Read more >
SettingWithCopyWarning in Pandas: Views vs Copies
In this tutorial, you'll learn about views and copies in NumPy and Pandas. You'll see why the SettingWithCopyWarning occurs in Pandas and how...
Read more >
Indexing and selecting data — pandas 1.5.2 documentation
.loc is primarily label based, but may also be used with a boolean array. .loc will raise KeyError ... You can also assign...
Read more >
Python | Pandas Extracting rows using .loc[] - GeeksforGeeks
In this example, Name column is made as the index column and then two single rows are extracted one by one in the...
Read more >
Hierarchical Indexing | Python Data Science Handbook
This produces the desired result, but is not as clean (or as efficient for ... If we re-index our series with this MultiIndex...
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