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.

Unexpected result when setting a row by a dict

See original GitHub issue

Code Sample, a copy-pastable example if possible

import numpy as np
import pandas as pd


persons = [
    {
        'name': ''.join([
            np.random.choice([chr(x) for x in range(97, 97 + 26)])
            for i in range(5)
        ]),
        'age': np.random.randint(0, 100),
        'sex': np.random.choice(['male', 'female']),
        'job': np.random.choice(['staff', 'cook', 'student']),
        'birthday': np.random.choice(pd.date_range('1990-01-01', '2010-01-01')),
        'hobby': np.random.choice(['cs', 'war3', 'dota'])
    }
    for i in range(10)
]

df = pd.DataFrame(persons)
df.set_index('birthday', inplace=True)
print(df)
df.iloc[0] = {
    'name': 'john',
    'age': int(10),
    'sex': 'male',
    'hobby': 'nohobby',
    'job': 'haha'
}
print(df)

Problem description

             age hobby      job   name     sex
birthday
2007-12-31  name   age      sex  hobby     job
2004-07-31    20  dota  student  uwxhn  female
2001-10-22    34  war3     cook  udknv  female
2002-10-13    91  dota     cook  bofcv  female
1992-05-25    54  war3     cook  tcqew    male
2009-09-02    95  war3    staff  jcolr  female
1998-12-15    61  war3  student  dibkw  female
2004-07-03     4  war3  student  mntqh    male
2000-06-08    88  war3    staff  jknxm  female
2006-10-19    82    cs  student  asrpz    male

Have no idea why the keys are set to the rows.

Expected Output

             age hobby      job   name     sex
birthday  
2007-12-31    10 nohobby  haha john male
2004-07-31    20  dota  student  uwxhn  female
2001-10-22    34  war3     cook  udknv  female
2002-10-13    91  dota     cook  bofcv  female
1992-05-25    54  war3     cook  tcqew    male
2009-09-02    95  war3    staff  jcolr  female
1998-12-15    61  war3  student  dibkw  female
2004-07-03     4  war3  student  mntqh    male
2000-06-08    88  war3    staff  jknxm  female
2006-10-19    82    cs  student  asrpz    male

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None python: 3.6.1.final.0 python-bits: 64 OS: Darwin OS-release: 16.6.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: en_US.UTF-8 LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

pandas: 0.20.1 pytest: 3.0.7 pip: 9.0.1 setuptools: 35.0.2 Cython: 0.25.2 numpy: 1.12.1 scipy: 0.19.0 xarray: 0.9.5 IPython: 6.0.0 sphinx: 1.5.5 patsy: 0.4.1 dateutil: 2.6.0 pytz: 2017.2 blosc: None bottleneck: 1.2.0 tables: 3.4.2 numexpr: 2.6.2 feather: None matplotlib: 2.0.2 openpyxl: 2.4.7 xlrd: 1.0.0 xlwt: None xlsxwriter: None lxml: 3.7.3 bs4: 4.6.0 html5lib: 0.999999999 sqlalchemy: 1.1.9 pymysql: None psycopg2: None jinja2: 2.9.6 s3fs: None pandas_gbq: None pandas_datareader: None

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
TomAugspurgercommented, Jun 19, 2017

Your example probably should work then, my mistake. It seems like we don’t probably set when there are multiple dtypes?

# two dtypes
In [44]: x = pd.DataFrame({'x': [1, 2, 3], 'y': ['3', '4', '5']})

In [46]: x.iloc[1] = {'x': 9, 'y': '99'}

In [47]: x  # set incorrectly with the keys
Out[47]:
   x  y
0  1  3
1  x  y
2  3  5

# single dtype
In [48]: x = pd.DataFrame({'x': [1, 2, 3], 'y': [3, 4, 5]})

In [49]: x.iloc[1] = {'x': 9, 'y': 99}

In [50]: x  # sets correctly
Out[50]:
   x   y
0  1   3
1  9  99
2  3   5
0reactions
mroeschkecommented, Oct 31, 2021

Looks like there’s a test for this issue: test_iloc_setitem_dictionary_value. Closing

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unexpected results when checking all values in a Python ...
I am looking to separate instances where all values in the dictionary are None, from everything else. (i.e. the first two should pass, ......
Read more >
Adding a new DataFrame row using dict() gives unexpected ...
Problem description. Considering that a dataframe can be created using a dictionary, it seems odd that adding to a dataframe using a dictionary...
Read more >
Result format - Great Expectations docs
The result_format parameter may be either a string or a dictionary which specifies the fields to return in result.
Read more >
Data types in Power BI Desktop - Microsoft Learn
To avoid unexpected results, you can change the column data type from Decimal Number to Fixed Decimal Number or Whole Number. Date/time types....
Read more >
pandas.read_table — pandas 1.5.2 documentation
Dict of functions for converting values in certain columns. Keys can either be integers or column labels. true_valueslist, optional. Values to consider as...
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